Calculate timestamp difference in C++

ltswimmer
ltswimmer used Ask the Experts™
on
I have two times in YYYYmmddHHMMSS format.

string t1 = “20100129165950”
string t2 = “20100129170139”

I have found one solution in Experts-exchange in 2005 that used a Java method SimpleDateFormat() as shown below.

SimpleDateFormat s = new SimpleDateFormat(“yyyyMMddHHmmss”);
Date d1 = s.parse(t1);
Date d2 = s.parse(t2);
Long diff = d1.getTime() – d2.getTime();

How to get the same result in C++?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ref: //http://www.mkssoftware.com/docs/man3/strptime.3.asp

On some UNIX 98 systems you can get a struct tm value defined as shown below. But I see your zone is VC, so I've added another solution.

Create a function to convert your string to a time_t value. See Visual C++ code below. Use it for t1 and t2.

You'll need to do your own validation on t1 and t2.

/////////////////////////////////////
// Visual C++
/////////////////////////////////////
struct tm tmTime1;
string t1 = "20100129165950";
char formatStr[] = "%4d%2d%2d%2d%2d%2d";
sscanf( const_cast<char*>(t1.c_str()), formatStr,
       &tmTime1.tm_year, &tmTime1.tm_mon, &tmTime1.tm_mday,
       &tmTime1.tm_hour, &tmTime1.tm_min, &tmTime1.tm_sec);
tmTime1.tm_year -= 1900;
tmTime1.tm_mon--;
time_t time1 = mktime(&tmTime1);

// compute time2 doing above

// compute time difference:
double durationSec = difftime(time2, time1);


/////////////////////////////////////
// UNIX 98:
/////////////////////////////////////
string t1 = "20100129165950";
string t2 = "20100129170139";
struct tm tmTime;
char formatStr[] = "%Y%m%d%H%M%S";

strptime(t1.c_str(), formatStr, &tmTime);

cout << 1900+tmTime.tm_year  << " " << 1+tmTime.tm_mon << " "
     << tmTime.tm_mday << " " 
     << tmTime.tm_hour << " " << tmTime.tm_min << " " 
     << tmTime.tm_sec << endl;

Open in new window

Top Expert 2009
Commented:
It could look something like this :
#include <ctime>
#include <cstdio>
#include <string>

time_t parseDate(const std::string& dateStr) {
  struct tm dtm = { 0 };

  int res = sscanf(dateStr.c_str(), "%04d%02d%02d%02d%02d%02d",
                                    &(dtm.tm_year),
                                    &(dtm.tm_mon),
                                    &(dtm.tm_mday),
                                    &(dtm.tm_hour),
                                    &(dtm.tm_min),
                                    &(dtm.tm_sec));
  if (res != 6) {
    /* oops : invalid string format - handle this error properly */
    return time(0);
  }

  dtm.tm_year -= 1900;
  dtm.tm_mon -= 1;
  dtm.tm_isdst = -1;

  return mktime(&dtm);
}


/* and then : */

std::string t1 = "20100129165950";
std::string t2 = "20100129170139";

time_t d1 = parseDate(t1);
time_t d2 = parseDate(t2);
double diff = difftime(d1, d2);

Open in new window

evilrixSenior Software Engineer (Avast)

Commented:
You might be interested to know that the Boost Posix Time libraries has tools for parsing string format dates and times.
http://www.boost.org/doc/libs/1_41_0/doc/html/date_time/posix_time.html

Author

Commented:
Thanks a lot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial