LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Convert a given date to epoch time and vice versa. (https://www.linuxquestions.org/questions/programming-9/convert-a-given-date-to-epoch-time-and-vice-versa-854390/)

coders123 01-05-2011 11:11 AM

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.

dwhitney67 01-05-2011 12:43 PM

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;
}


coders123 01-05-2011 10:44 PM

Thanks a lot and your answer is perfect. It works well.


All times are GMT -5. The time now is 11:00 AM.