Convert a given date to epoch time and vice versa.

I'm developing a C++ application which is run on linux. I need to calculate epoch time for a given date and vice versa. (no of seconds elapsed since 1979-01-01) I used the following two methods.I need to convert the given date to epoch time and again convert that epoch time to the corresponding date.But the following methods doesn't give me the correct answer.

Method to calculate the epoch time:

1
2
3
4
5
6
7
time_t HistoryCache::ConvertTimeToEpoc(const char* _zTime,const char* _zFormat)
{
	struct tm tmTime;
	strptime(_zTime,_zFormat, &tmTime);		
	time_t tTime = mktime(&tmTime);
	return tTime;
}



Method to find the corresponding date for a given time in seconds:

1
2
3
4
5
6
7
8
9
const char* HistoryCache::GetTimeStamp(float epochStr) 
{
  static char timestamp[64] = "";
  time_t tt = 0;
  memset(timestamp, '\0', 64);
  tt = epochStr;
  strftime(timestamp, 64, "%Y-%m-%d:%H:%M:%S", localtime(&tt)); 
  return timestamp;
}


In my main method I'm doing something like below.

1
2
3
time_t timeFrom = ConvertTimeToEpoc("2010-01-05:000000","%Y-%m-%d:%H:%M:%S"); 
long lTime = (long)timeFrom;
const char* dateString = GetTimeStamp(1262649600);


My problem is this. Lets think that the value I get as epoch time for the 2010-01-05:000000 is 1262649600. (lTime value) Then I use the same value (1262649600) as a input to the GetTimeStamp method, I didnt get the dateString as 2010-01-05:000000. The date is changed by one day. Can some one please help me on this and it would be really helpful.

Thank you.
The Boost Date_Time library is available on most Linux distros. You may be a little easier to manage (and safer).
Topic archived. No new replies allowed.