我遇到了一个奇怪的问题,我的系统时钟知道它是夏令时,但glibc似乎没有。这是一个最新的Ubuntu安装,我检查了/ etc / localtime,它有正确的转换时间,用于上周切换到夏令时。
我目前正确的时区是太平洋夏令时(UTC-7)。当我问我的系统我所在的时区时,它正确告诉我:
$ date +%z
-0700
但是当我运行以下程序时:
#include <time.h>
#include <stdio.h>
int main() {
tzset();
printf("%lu\n", timezone);
return 0;
}
输出错误:
28800
对应于UTC-8或太平洋标准时间。 (不,TZ不在我的环境中设置)
我认为glibc和日期计划会从同一来源获取他们的时区信息,但显然要么他们没有,要么我误解了glibc时区的全球运作方式。
基本问题是:
答案 0 :(得分:3)
我不认为“时区”会随着日光时间而改变。尝试“日光”变量。在我的系统上:
The external variable timezone contains the difference, in seconds, between UTC and local standard time (for example, in the U.S. Eastern time zone (EST), timezone is 5*60*60). The external variable daylight is non-zero only if a summer time zone adjustment is specified in the TZ environment variable.
答案 1 :(得分:1)
执行此操作后,请查看tm.tm_isdst字段:
time_t current_time;
struct tm tm;
current_time = time(NULL);
localtime_r(¤t_time, &tm);
根据localtime_r(3)联机帮助页,这实际上表明DST是否在指定的时间生效。我想你需要假设DST给你已经使用的时区(3)变量增加了一个小时,或者对GMT进行差异化处理。
在澳大利亚AEST为我工作,希望它适合你。
答案 2 :(得分:0)
您可以使用与{timezone相同的结构tm的tm_gmtoff
内存,但它会考虑DST并且符号会反转。
http://www.gnu.org/s/libc/manual/html_node/Time-Zone-Functions.html#Time-Zone-Functions
答案 3 :(得分:0)
如果定义了 linux ,则使用tm_gmtoff执行此代码,并使用gettimofday中的timezone.tz_minuteswest(此处&#39; ltm&#39;是localtime的输出):
{
int tz_offset;
#if defined(__linux__)
tz_offset= ltm.tm_gmtoff;
#else
tz_offset= -tz.tz_minuteswest*60 + ltm.tm_isdst*3600;
#endif
printf ("LT = UTC +d sec\n", tz_offset);
}