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.