Confusing behaviour of mktime on Linux?

旧巷老猫 提交于 2019-12-05 10:13:44

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).

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.

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).

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).

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.

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.

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.

Check out this answer and see if that helps. Also, whats the system timezone offset? Check by running:

date +%z
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!