The request is accept a specific time and calculate how many time elapsed since 1970/01/01 00:00:00 Need taking millisecond into account!
For example, input a date time: 2009/08/06 14:35:19.42 The result show: 124958371942
As I know, time_t is only take care how many seconds elapsed since 1970/01/01 and struct tm has no millisecond attribute to set
I try to use mktime and get the wrong result: 1249540519ll
struct tm t;
t.tm_year = 2009 - 1900;
t.tm_mon = 7;
t.tm_mday = 6;
t.tm_hour = 14;
t.tm_min = 35;
t.tm_sec = 19;
wprintf(L"%ull\n", mktime(&t));
What API should use for this case? Thanks!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.