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

Kit*_* Ho 4 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;
       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)

San*_*ker 5

这确实非常令人困惑,但不是一个错误.

POSIX指定了这个:

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

所以,它基本上与你所期望的相反.