LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-05-2011, 12:11 PM   #1
coders123
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:

Code:
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:

Code:
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.

Code:
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, 01:43 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

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

class HistoryCache
{
public:
   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, 11:44 PM   #3
coders123
LQ Newbie
 
Registered: Jan 2011
Posts: 14

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


Reply


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 10:21 PM
How to : Convert the epoch time to standard time with bash avklinux AIX 1 02-26-2009 08:21 PM
Tool to convert PDF files to office and vice versa linuxlover.chaitanya Linux - Software 2 05-01-2008 11:31 AM
how to convert pdf to pdb and vice versa dr_zayus69 Linux - Software 1 07-17-2005 06:40 PM
Convert Celius to fahreheit and vice versa bluewolf Linux - Newbie 2 02-07-2003 11:49 AM


All times are GMT -5. The time now is 06:18 AM.

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