2

The question is simple, there are many API specific methods in other languages, but I found none that that were simple and straight forward for Cross-platform C++ usage.

If I have two dates, and they are assumed to be in the same locale, what is the easiest way to differentiate them in C++?

I have (somewhat) looked at using time.h, creating two tm objects, converting them to UTC and then differentiating them.

The current format for the dates is: YY/MM/DD HH:mm:SS (stored as individual integers)

EDIT:

Ok, based on current answers, I've done the following (for now):

time_t calculate_seconds_between(
    const uint Y1, const uint M1, const uint D1, const uint H1, const uint m1, const uint S1, // YY/MM/DD HH:mm:SS
    const uint Y2, const uint M2, const uint D2, const uint H2, const uint m2, const uint S2
)
{
    time_t raw;
    time(&raw);

    struct tm t1 = *gmtime(&raw), t2 = t1;

    t1.tm_year = Y1 - 1900;
    t1.tm_mon = M1 - 1;
    t1.tm_mday = D1;
    t1.tm_hour = H1;
    t1.tm_min = m1;
    t1.tm_sec = S1;

    t2.tm_year = Y2 - 1900;
    t2.tm_mon = M2 - 1;
    t2.tm_mday = D2;
    t2.tm_hour = H2;
    t2.tm_min = m2;
    t2.tm_sec = S2;

    time_t tt1, tt2;
    tt1 = mktime(&t1);
    tt2 = mktime(&t2);

    return (tt2 - tt1);
}

Which works great.

hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
  • 1
    Note that the difference between two times does not depend on the time zone, as long as it's consistent. I.e. the difference between 17:00:00(UTC+1) and 17:05:00(UTC+1) is the same as the difference between 16:00:00(UTC) and 16:05:00(UTC). (Daylight Savings Time does act as another timezone; be careful with that). – MSalters Nov 28 '11 at 10:57
  • @MSalters, yeah I just realised that. Sometimes just writing out the question gives the clarity needed to produce the answer... which is frustrating, because I usually use SO as my last resort (ie, many hours past). Cheers :) – hiddensunset4 Nov 28 '11 at 11:05
  • @Daniel Why are you using gmtime? Which fields do you need to update? – selalerer Nov 28 '11 at 12:31
  • @MSalters: and even within what you might naively call "the same timezone", in the absence of daylight savings, there are occasional discontinuities: http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result – Steve Jessop Nov 28 '11 at 12:53
  • Also: do you want the difference between the UTC times, or the amount of "actual time" (TAI) elapsed? The difference between the two scales is leap seconds. – Steve Jessop Nov 28 '11 at 12:57
  • @SteveJessop I'm after actual time in seconds, the overall use for this was for plugging in dates for solar positioning. – hiddensunset4 Nov 28 '11 at 21:54
  • 1
    @Daniel: in that case UTC is better than TAI "actual time", since the whole point of leap seconds is to correct for solar position. I think what you actually want for astronomy is UT1, but it's never more than about a second different from UTC, so the latter is close enough for jazz. By comparison, TAI is currently 34 seconds different from UTC (according to Wikipedia) - 34 more "actual seconds" have elapsed since 1972, than UTC admits to. – Steve Jessop Nov 29 '11 at 10:33

2 Answers2

3

To convert from YY/MM/DD HH:mm:SS to struct tm you can use strptime (or make an equivalent if you don't have it)

const char * dateInput;
// ....
struct tm tm;
time_t val;

memset(&tm, 0, sizeof(struct tm));
strptime(dateInput,"%y/%m/%d %H:%M:%S",&tm);
val = mktime(&tm);

After you have the two time_t values you can call difftime.

INS
  • 10,594
  • 7
  • 58
  • 89
0

The new C++11 has the chrono library (also available from boost, for non C++11 compilers )

With it you can do nifty things like

boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
// ...
boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
std::cout << "took " << sec.count() << " seconds\n";

or

typedef boost::chrono::duration<long, boost::ratio<60> > minutes;
minutes m1(3);                 // m1 stores 3
minutes m2(2);                 // m2 stores 2
minutes m3 = m1 + m2;          // m3 stores 5
Fabio Fracassi
  • 3,791
  • 1
  • 18
  • 17