Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-05-2011, 11:11 AM   #1
LQ Newbie
Registered: Jan 2011
Posts: 14

Rep: Reputation: 0
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:

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:

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.

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.
Old 01-05-2011, 12:43 PM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The following code, which is based on yours, works as expected.
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>

class HistoryCache
   static std::string getTimeStamp(time_t epochTime, const char* format = "%Y-%m-%d %H:%M:%S")
      char timestamp[64] = {0};
      strftime(timestamp, sizeof(timestamp), format, localtime(&epochTime));
      return timestamp;

   static time_t convertTimeToEpoch(const char* theTime, const char* format = "%Y-%m-%d %H:%M:%S")
      std::tm tmTime;
      memset(&tmTime, 0, sizeof(tmTime));
      strptime(theTime, format, &tmTime);
      return mktime(&tmTime);

int main()
   // get current epoch time
   const time_t curTime = time(0);

   // convert current time to a string
   std::string curTimeStr = HistoryCache::getTimeStamp(curTime);

   // convert string time to an epoch time
   const time_t curTime2 = HistoryCache::convertTimeToEpoch(curTimeStr.c_str());

   // display results
   std::cout << "Epoch Time: " << curTime    << "\n"
             << "As string : " << curTimeStr << "\n"
             << "Epoch Time: " << curTime2
             << std::endl;
1 members found this post helpful.
Old 01-05-2011, 10:44 PM   #3
LQ Newbie
Registered: Jan 2011
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks a lot and your answer is perfect. It works well.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert Traditional to Simplified Chinese & vice versa tcma Linux - Software 4 02-06-2014 09:21 PM
How to : Convert the epoch time to standard time with bash avklinux AIX 1 02-26-2009 07:21 PM
Tool to convert PDF files to office and vice versa linuxlover.chaitanya Linux - Software 2 05-01-2008 10:31 AM
how to convert pdf to pdb and vice versa dr_zayus69 Linux - Software 1 07-17-2005 05:40 PM
Convert Celius to fahreheit and vice versa bluewolf Linux - Newbie 2 02-07-2003 10:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration