Linux上的mktime行为令人困惑? [英] Confusing behaviour of mktime on Linux?

查看:310
本文介绍了Linux上的mktime行为令人困惑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Suse 10中使用mktime(struct tm *)函数.

I am using the mktime(struct tm*) function in Suse 10.

现在,当启用夏令时时,我注意到一些奇怪的行为.假设我已启用夏令时,从9月15日18:10开始,夏令时校正为30分钟.现在,当我使用日期为9月15日18:10的tm结构调用mktime并将tm_isdst设置为0时,只有在tm_isdst设置为1的情况下,我才能在tm结构中获得相同的值.

Now, I am noticing some strange behaviour when daylight saving time is enabled. Let's say I have enabled daylight saving time to begin on Sep 15 at 18:10 and the daylight correction is for 30 minutes. Now, when I call mktime with tm structure having the date as Sep 15 18:10 and tm_isdst is set to 0, then I get back the same values in the tm structure only with the tm_isdst set to 1.

但是,如果通过tm_isdst设置为9月15日18:10的日期,那么我发现时间更改为17:40.在9月15日18:10到9月15日18:40之间经过的时间中注意到了tm结构中的此更正,但是此后没有进行任何时间更正并且teh dst标志保持启用.即使我将日期设为9月16日18:10,也不会发生任何时间校正,只有dst标志保持启用状态.

But, if the pass the date as Sep 15 18:10 with tm_isdst set to 1, then I find the time changed to 17:40. This correction in the tm structure is noticed for time passed between Sep 15 18:10 to Sep 15 18:40, but after that no correction in time happens and teh dst flag remains enabled. Even if I pass the date as Sep 16 18:10, no time correction happens only dst flag remains enabled.

我完全感到困惑.这是mktime的正确行为吗?

I am totally confused. Is this the correct behavior of mktime?

推荐答案

如果DST的本地时间更改为30分钟,那么每年一次,则有30分钟的本地时间发生两次(一次DST,一次DST)再过30分钟,从不发生(随着时间的变化,它会被跳过).

If the local time changes by 30 minutes for DST, then once per year there's 30 minutes of local time that happens twice (once with DST, and once without) and another 30 minutes that never happens (it gets skipped when the time changes).

因此,除非指定了DST是否生效,否则在时钟调回后30分钟内的本地时间是模棱两可的.它们可能对应于两个实际时间点.

So local times within 30 minutes of when the clock gets set back are ambiguous, unless whether DST is in effect is specified; there are two actual instants in time that they could correspond to.

时钟提前30分钟之内的当地时间无效;没有可能与之对应的实际时刻(尽管可以通过假设DST生效或无效来完成转换).

Local times within 30 minutes of when the clock gets set ahead are invalid; there are no actual instants in time they could correspond to (though the conversion might still be done by supposing that DST is in effect, or not in effect).

因此,在某些本地时间(忽略DST状态),可能会有多个对应的UTC时间,但是对于任何给定的UTC时间,只有一个可能的本地时间(如果正确考虑了DST调整).

So for some local times (ignoring the DST state) there could be more than one corresponding UTC time, but for any given UTC time there is only one possible local time (if DST adjustments are accounted for properly).

当您调用mktime时,它会将您提供的本地时间转换为time_t,就像DST是否生效一样,具体取决于tm_isdst的值.您获得的校正后的值基于此转换的相反结果,系统将确定您获得DST时间还是获得非DST时间,具体取决于其在转换时DST是否有效的想法.您给出的时间和返回的时间实际上代表同一时间,但是由于DST状态不同,因此与UTC的偏移量也不同.

When you call mktime, it's converting the local time you give it to a time_t as though DST is either in effect or not, depending on the value of tm_isdst. The corrected values you get back are based on the reverse of this conversion, and the system will determine whether you get a DST time or a non-DST time, depending on its idea of whether DST is in effect at the time from the conversion. The time you gave it and the time you got back actually represent the same moment in time, but with different offsets from UTC due to the different DST states.

是的,这是mktime的正确行为.应该根据结构中有关如何正确表示给定时间的想法对结构中的值进行规范化.

So yes, this is the correct behaviour of mktime. It is supposed to normalize the values in the structure, according to its idea of how to properly represent the time you gave it.

这也说明了为什么要谨慎使用本地时间来跟踪实际事件-如果未将DST状态或与UTC的偏移与时间一起保存,则某些本地时间值可能会模棱两可.

This also illustrates why one should be careful about using local time keep track of actual events -- if the DST state or offset from UTC is not saved along with the time, some local time values can be ambiguous.

这篇关于Linux上的mktime行为令人困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆