【发布时间】:2017-02-17 23:07:43
【问题描述】:
我试图了解 UNIX timelocal 和 mktime 是如何工作的。假设当您在 struct tm tm_isdst 字段中传递正确的值时,它们会处理夏令时。
我正在测试一个非常具体的时刻。根据“America/New_York”的时区数据库,夏令时在 2005 年 10 月 30 日 01:00 发生了变化。这是zdump -v America/New_York 的输出,您可以在自己的系统上确认。我只显示了 2005 年左右的数据子集(向右滚动查看 gmtoff 值):
为了测试这种转换,我设置了一个 struct tm 以包含特定日期的 01:30。如果我为tm_isdst 传递0,它应该给我一个-18000 的gmtoffset。如果我通过 1 并启用夏令时,那么 gmtoffset 应该是 -14400。
这是我用来在 Darwin/OSX 和 FreeBSD 上测试的代码:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void print_tm(struct tm* tm) {
printf("tm: sec [%d] min [%d] hour [%d] mday [%d] mon [%d] year [%d] wday [%d] yday [%d] isdst [%d] zone [%s] gmtoff [%ld]\n",
tm->tm_sec,
tm->tm_min,
tm->tm_hour,
tm->tm_mday,
tm->tm_mon + 1,
tm->tm_year,
tm->tm_wday,
tm->tm_yday + 1,
tm->tm_isdst,
tm->tm_zone,
tm->tm_gmtoff);
}
struct tm* set_tm(int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst, int gmtoff, char* zone) {
struct tm* tm;
tm = malloc(sizeof(struct tm));
memset(tm, 0, sizeof(struct tm));
tm->tm_sec = sec;
tm->tm_min = min;
tm->tm_hour = hour;
tm->tm_mday = mday;
tm->tm_mon = mon - 1;
tm->tm_year = year;
tm->tm_wday = wday;
tm->tm_yday = yday - 1;
tm->tm_isdst = isdst;
tm->tm_zone = zone;
tm->tm_gmtoff = gmtoff;
return tm;
}
void test_timelocal(struct tm* tm, int isdst) {
time_t seconds = -1;
if(!setenv("TZ", "America/New_York", 1)) {
printf("isdst is [%d]\n", isdst);
tm->tm_isdst = isdst;
tzset();
seconds = timelocal(tm);
localtime_r(&seconds, tm);
print_tm(tm);
} else {
printf("setenv failed with [%s]\n", strerror(errno));
}
printf("\n");
}
void test_mktime(struct tm* tm, int isdst) {
time_t seconds = -1;
if(!setenv("TZ", "America/New_York", 1)) {
printf("isdst is [%d]\n", isdst);
tm->tm_isdst = isdst;
tzset();
seconds = mktime(tm);
localtime_r(&seconds, tm);
print_tm(tm);
} else {
printf("setenv failed with [%s]\n", strerror(errno));
}
printf("\n");
}
int main(void) {
struct tm* tm;
printf("Test with timelocal\n");
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_timelocal(tm, 0);
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_timelocal(tm, 1);
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_timelocal(tm, -1);
printf("Test with mktime\n");
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_mktime(tm, 0);
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_mktime(tm, 1);
tm = set_tm(0, 30, 1, 30, 10, 2005, 0, 0, 0, 0, "");
test_mktime(tm, -1);
return 0;
}
在不同的操作系统上运行它会产生不同的结果。在 FreeBSD 上,此代码输出(向右滚动查看 gmtoffset 值):
使用 timelocal 进行测试 isdst 是 [0] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400] isdst 是 [1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400] isdst 是 [-1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400] 使用 mktime 进行测试 isdst 是 [0] tm: sec [0] min [30] hour [2] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400] isdst 是 [1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400] isdst 是 [-1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [1] zone [EDT] gmtoff [-14400]在 darwin/OSX 上,完全相同的代码会产生这个(向右滚动查看 gmtoffset 值):
使用 timelocal 进行测试 isdst 是 [0] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000] isdst 是 [1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000] isdst 是 [-1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000] 使用 mktime 进行测试 isdst 是 [0] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000] isdst 是 [1] tm: sec [0] min [30] hour [0] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000] isdst 是 [-1] tm: sec [0] min [30] hour [1] mday [30] mon [10] year [2005] wday [1] yday [303] isdst [0] zone [EST] gmtoff [-18000]在我看来,他们俩都弄错了。 tm_isdst 字段似乎对 tm_gmtoff 字段没有影响。使用mktime 时,tm_hour 输出发生变化,但偏移量仍然错误。
如果您将 tm_mday 更改为几天前或几天后,gmtoffset 根本不会改变,这让我感到困惑。
是我做错了什么还是我误解了这些功能的工作原理?
【问题讨论】:
-
夏令时更改在 02:00,而不是 01:00。
-
struct tm中的月份从0变为11,因此十月是9,而不是10。您打印的是 11 月 30 日的结果,而不是 10 月 30 日。 -
我解决了。
tm->tm_year字段基于 1900 年。因此,对于 2005 年,我需要在该字段中输入 (2005 - 1900) = 105。然后它工作。至于@barmar 的评论,代码已经针对 0-11 个月的计数进行了调整,因此这不是错误。set_tm函数将月份值减 1。 -
那个也是。我刚刚注意到您已经为
tm_mon加/减 1。 -
您应该将其发布为答案。您可以回答自己的问题。
标签: c datetime time freebsd darwin