It appears with 124958371942, OP is counting centiseconds and not milliseconds. There also is a timezone issue. It appears the 1970 timestamp is UTC and the 2009 date may be local.
Various approaches to cope with timezone differences.
A comon approach is to assume both the 2009/08/06 14:35:19.42 and 1970/01/01 00:00:00 are universal time. Another is that they are both local time. The following assumes both local time.
#include <stdio.h>
#include <time.h>
void tt(void) {
struct tm t = { 0 }; // important to set _all_ fields
t.tm_year = 2009 - 1900;
t.tm_mon = 8 - 1;
t.tm_mday = 6;
t.tm_hour = 14;
t.tm_min = 35;
t.tm_sec = 19;
t.tm_isdst = -1; // Unless known let code determine if it is DST or not.
time_t t1 = mktime(&t);
printf("%10lld\n", (long long) t1);
t.tm_year = 1970 - 1900;
t.tm_mon = 1 - 1;
t.tm_mday = 1;
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 0;
t.tm_isdst = -1;
time_t t0 = mktime(&t);
printf("%10lld\n", (long long) t0);
printf("%lf\n", difftime(t1, t0) * 1000.0);
}
// Of course these are per my timezone value
1249587319
21600
1249565719000.000000
Some details:
struct tm()
may have a sub-seconds field. That is why it is important to zero the entire structure. Only 9 fields are specified, others may exist. Two fields, tm_wday
and tm_yday
do not need to be initialize before calling mktime()
, but at least 7 of them should be set including tm_isdst
.
difftime()
is the portable way to subtract timestamps.
When printing time_t
, the value is typically the integer count of seconds since 1970 without leap seconds. Converting to long long
is a reasonable, but not absolute portable method of printing.