我最近在C语言上发现了一个奇怪的行为,但不知道为什么会这样.
当我使用setenv()时,将TZ设置为GMT + 1.本地时间的输出将比UTC时间少一个小时.(见输出)
实际上,当我将TZ设置为GMT-1时.本地时间的输出将比UTC时间多一个小时.
这没有意义.如果您不相信,可以在C中尝试以下代码.任何人都知道这种奇怪的行为吗?这是一个错误吗?
码:
int main(int argc, char** argv)
{
time_t now;
struct tm *local;
setenv("TZ", "GMT+1", 1);
tzset();
now = time(NULL);
//Get the Local time (GMT+1)
local = localtime(&now);
printf("Local time and date: %s\n", asctime(local));
//Get the system time (GMT)
local = gmtime(&now);
printf("UTC time and date: %s\n", asctime(local));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Local time and date: Thu Aug 4 14:36:42 2011
UTC time and date: Thu Aug 4 15:36:42 2011
Run Code Online (Sandbox Code Playgroud)