如何消除不稳定的 mktime 行为
how to eliminate erratic mktime behavior
我正在尝试将本地时间转换为自大纪元以来的秒数。但是,对于相同的输入,mktime() 似乎并不总是 returns 相同的值。
代码如下:
int TimeCreated;
// ...
printf("%d/%d/%d %d:%d:%d\n",t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_c);
TimeCreated = mktime(&t);
printf("%d\n",TimeCreated);
printf("%d/%d/%d %d:%d:%d\n",t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_c);
TimeCreated = mktime(&t);
printf("%d\n",TimeCreated);
似乎每次我 运行 这个问题,mktime() 的输出都会针对相同的输入值发生变化:
$ ./test1
115/2/28 15:29:1
1427581741
115/2/28 15:29:1
1427581741
$ test1
115/2/28 15:29:1
1427578141
115/2/28 14:29:1
1427578141
手册页说 "The functions mktime() and timegm() convert the broken-out time (in the structure pointed to by *timeptr) into a time value with the same encoding as that of the values returned by the time(3) function (that is, seconds from the Epoch, UTC). The mktime() function interprets the input structure according to the current timezone setting (see tzset(3))" 这表明问题与时区有关。但是,时区设置正确。
如果我只从它所属的较大程序中提取上面的代码,问题就会消失。我怀疑链接和调用 jhead
中的函数可能是问题的一部分,但我无法通过阅读地图页面来想象它如何扰乱 mktime() 结果。
看起来 struct tm
的 tm_isdst
字段在第一次调用 mktime()
之前没有初始化。 如果夏令时有效则值为正,否则为零,如果信息不可用则为负。
我正在尝试将本地时间转换为自大纪元以来的秒数。但是,对于相同的输入,mktime() 似乎并不总是 returns 相同的值。
代码如下:
int TimeCreated;
// ...
printf("%d/%d/%d %d:%d:%d\n",t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_c);
TimeCreated = mktime(&t);
printf("%d\n",TimeCreated);
printf("%d/%d/%d %d:%d:%d\n",t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_c);
TimeCreated = mktime(&t);
printf("%d\n",TimeCreated);
似乎每次我 运行 这个问题,mktime() 的输出都会针对相同的输入值发生变化:
$ ./test1
115/2/28 15:29:1
1427581741
115/2/28 15:29:1
1427581741
$ test1
115/2/28 15:29:1
1427578141
115/2/28 14:29:1
1427578141
手册页说 "The functions mktime() and timegm() convert the broken-out time (in the structure pointed to by *timeptr) into a time value with the same encoding as that of the values returned by the time(3) function (that is, seconds from the Epoch, UTC). The mktime() function interprets the input structure according to the current timezone setting (see tzset(3))" 这表明问题与时区有关。但是,时区设置正确。
如果我只从它所属的较大程序中提取上面的代码,问题就会消失。我怀疑链接和调用 jhead
中的函数可能是问题的一部分,但我无法通过阅读地图页面来想象它如何扰乱 mktime() 结果。
看起来 struct tm
的 tm_isdst
字段在第一次调用 mktime()
之前没有初始化。 如果夏令时有效则值为正,否则为零,如果信息不可用则为负。