tzset和daylight全局变量在time.h中的解释 [英] tzset and daylight global variable interpretation in time.h
问题描述
在夏令时全局变量的time.h标头中,它表示:
如果应用了夏令时规则,则此变量的值将为非零。非零值并不一定意味着夏令时现在生效;
In the time.h header for the daylight global variable it says: "This variable has a nonzero value if Daylight Saving Time rules apply. A nonzero value does not necessarily mean that Daylight Saving Time is now in effect; it means only that Daylight Saving Time is sometimes in effect."
现在,我注意到在Solaris 11.2和Linux中, daylight变量都设置为1,即使我的时区根本不使用夏令时(澳大利亚/布里斯班)。
Now I've noticed that in both Solaris 11.2 and Linux the "daylight" variable is being set to 1, even though my time-zone does not use daylight savings at all (Australia/Brisbane).
示例代码确认了这一点,如果我运行tzset并输出全局变量,我们将得到:
daylight = 1 tz [0] = [AEST] tz [1 ] = [AEDT]时区= [-36000]
Sample code confirms this, if I run tzset and output the global variables we get: daylight = 1 tz[0] = [AEST] tz[1] = [AEDT] timezone = [-36000]
但是,根据我的理解,应将夏令时设置为0,因为我的时区在任何时候都没有夏令时年。
But by my understanding, daylight should be set to 0 since my zone does not have daylight savings at any time during the year.
我还注意到,将struct tm设置为当前时间时会返回tm_isdst = 0,这是正确的。
I also noticed that the struct tm when set to current time returns a tm_isdst = 0, which is correct.
为什么将日光变量设置为1?不应将其设置为0吗?还是我误解了这个?
So why is the daylight variable set to 1? Shouldn't it be set to 0? Or am I misinterpreting this?
代码是:
#include <stdio.h>
#include <time.h>
void main()
{
time_t t;
struct tm *tms = { 0 };
tzset();
time(&t);
tms = localtime(&t);
printf("date and time : %s",ctime(&t));
printf("daylight = %d tz[0] = [%s] tz[1] = [%s] timezone = [%ld]\n", daylight, tzname[0], tzname[1], timezone);
printf("tm_isdst = %d\n",tms->tm_isdst);
}
输出为:
date and time : Mon Nov 30 16:41:01 2015
daylight = 1 tz[0] = [AEST] tz[1] = [AEDT] timezone = [-36000]
tm_isdst = 0
推荐答案
关于C标准 tm_isdst
成员。
的值如果夏令时有效,则tm_isdst
为正;如果夏令时无效,则为零;如果该信息不可用,则为负。 C11dr§7.27.14
The value of
tm_isdst
is positive if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and negative if the information is not available. C11dr §7.27.1 4
这与* nix规范有关* nix全局变量 daylight
。
日光
不属于标准C。
This slightly differs from *nix specification about the *nix global variable daylight
.
daylight
is not part of standard C.
gnu .org 报告
变量:int daylight
如果夏令时,此变量的值非零规则适用。非零值并不一定意味着夏令时已生效。这意味着只有夏令时有效。有时。
tm_isdst
是指 struct tm
时间戳。这仅表示 DST 在该时间戳上有效。
The tm_isdst
refers to the struct tm
timestamp. It only means DST is in effect for that time-stamp.
daylight!= 0
表示有时在时区的时间戳中使用了DST。
daylight != 0
implies DST is used sometimes in the timezone's timestamps.
正如澳大利亚/布里斯班曾经在1972年以前( @Jon Skeet )观察夏令时一样,夏令时为 1
是合理的,因为 daylight
表示DST在该时区的某些时间段内有效(可能从1970年开始)。
As Australia/Brisbane once observed DST prior (@Jon Skeet) to 1972, having daylight == 1
is reasonable as daylight
implies DST was in effect for some periods of time for that timezone (likely since 1970).
OP的 ...即使我的时区根本不使用夏令时,也不正确。
OP's "... even though my time-zone does not use daylight savings at all" is not correct.
以下代码显示使用了DST(至少时区DB如此认为),自1970年以来一直在澳大利亚/布里斯班。
The following code shows that DST is used (at least the timezone DB thinks so) for some years since 1970 in "Australia/Brisbane".
#include<time.h>
#include<stdlib.h>
#include<sys/time.h>
int main(void) {
setenv("TZ", "Australia/Brisbane", 1);
tzset();
time_t now;
time(&now);
struct tm tm;
int isdst = 42; // See Hitchhiker's_Guide_to_the_Galaxy
time_t t;
for (t = 0; t < now; t += 3600) {
tm = *localtime(&t);
if (tm.tm_isdst != isdst) {
printf("dst:%d %s", tm.tm_isdst, ctime(&t));
isdst = tm.tm_isdst;
}
}
printf("dst:%d %s", tm.tm_isdst, ctime(&t));
return 0;
}
输出
dst:0 Thu Jan 1 10:00:00 1970
dst:1 Sun Oct 31 03:00:00 1971
dst:0 Sun Feb 27 02:00:00 1972
dst:1 Sun Oct 29 03:00:00 1989
dst:0 Sun Mar 4 02:00:00 1990
dst:1 Sun Oct 28 03:00:00 1990
dst:0 Sun Mar 3 02:00:00 1991
dst:1 Sun Oct 27 03:00:00 1991
dst:0 Sun Mar 1 02:00:00 1992
dst:0 Tue Dec 1 16:00:00 2015
这篇关于tzset和daylight全局变量在time.h中的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!