Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C/C++;:为什么本地时间的时区显示不正确?_C++_C_Timezone - Fatal编程技术网

C++ C/C++;:为什么本地时间的时区显示不正确?

C++ C/C++;:为什么本地时间的时区显示不正确?,c++,c,timezone,C++,C,Timezone,我最近在C语言上发现了一种奇怪的行为,但不知道为什么会发生这种情况 使用setenv()时,将TZ设置为GMT+1。 本地时间的输出将比UTC时间少一小时。(见输出) 实际上,当我将TZ设置为GMT-1时。 本地时间的输出将比UTC时间多出一小时 这没有道理。如果你不相信,你可以试试下面的C代码。 有人知道这种奇怪的行为吗?是虫子吗 代码: int main(int argc, char** argv) { time_t now; struct tm *local;

我最近在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;
}
Local time and date: Thu Aug  4 14:36:42 2011

UTC time and date: Thu Aug  4 15:36:42 2011
输出:

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;
}
Local time and date: Thu Aug  4 14:36:42 2011

UTC time and date: Thu Aug  4 15:36:42 2011

它确实非常混乱,但不是一个bug

指定以下内容:

如果前面有“-”,则时区应位于本初子午线以东;否则,它应该是西边(可以用可选的前面的“+”表示)


因此,它基本上与您的预期相反。

没有“setenv”(“TZ”,“GMT+1”,1);“它工作正常。如果您在*nix系统中,在运行它之前调用export TZ=“GMT+1”,结果与您的相同。尝试运行“TZ=”GMT+1“date”,您会得到相同的结果。我对此没有答案,但我也很好奇。这是一个非常奇怪的规范:(@Kit Ho:我不知道他们为什么这样指定。我同意你的说法,这非常奇怪。这是处理时间的一个大难题。