本文介绍了mktime和tm_isdst标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对tm结构中tm_isdst标志的使用有以下疑问。根据man页和google的结果,我的理解,其值解释如下

I have the following doubt on the usage of tm_isdst flag in the tm structure. As per man pages and googled results, I understand that its value is interpreted as follows

A。值为0表示DST在所表示的时间内无效。

A. A value of 0 indicates DST is not in effect for the represented time

B。值为1表示DST有效

B. A value of 1 indicates DST is in effect

C。值为-1会导致mktime检查DST是否生效。

C. A value of -1 causes mktime to check whether DST is in effect or not.

这是第三点,这让我很困惑。我怀疑是mktime如何确定是否应用或不准确应用DST。

It is this third point which is confusing me. My doubt is how mktime can figure out whether DST has to be applied or not accurately.

例如

My Time Zone = GMT + 3:00
DST shifting = +1 Hour at 5:00 AM in January (to keep it simple)
Current UTC time = "01/Jan/2012 00:00:00"
UTC time in seconds time_t timetUTC = X seconds
Hence my time is = "01/Jan/2012 03:00:00"

随着时间的推移,我的时间价值变化如下

As time passes, my time value changes as follows

"01/Jan/2012 04:00:00"            (X + 1 * 60 * 60)
"01/Jan/2012 05:00:00"            (X + 2 * 60 * 60)
"01/Jan/2012 05:59:59"            (X + 2 * 60 * 60 + 59)
"01/Jan/2012 05:00:00"            (X + 3 * 60 * 60)
"01/Jan/2012 06:00:00"            (X + 4 * 60 * 60)

根据我的理解

tm tmMyTime = localtime_r(X + 2 * 60 * 60) will set tmMyTime.tm_isdst to 0
tm tmMyTime = localtime_r(X + 3 * 60 * 60) will set tmMyTime.tm_isdst to 1

这样,即使tm结构的所有其他组件在这两种情况下相等,
mktime(tmMyTime)可以返回正确的UTC值,具体取决于tm_isdst值。

This way, even though all other components of tm structure are equal in both cases,mktime(tmMyTime) can return proper UTC value, depending on tm_isdst value.

现在,如果我设置tmMyTime.tm_isdst = -1,mktime返回什么值?我读了关于TZ变量,时间数据库等等。尽管如此,mktime()如何知道是否应用DST校正是否可以发生两次的tm值?

Now, if I set tmMyTime.tm_isdst = -1, what value would mktime return? I read about TZ variable, time database etc etc. In spite of all that, logically how can mktime() figure out whether to apply DST correction or not for those tm values that can occur twice?

我们的时区没有DST。因此,我不太确定我的理解
是否正确。如果我错了,请纠正我。

We do not have DST in our time zone. Hence I am not very sure whether my understandingis correct. Please correct me if I am wrong. Your help is much appreciated.

推荐答案

简而言之:它与实现相关。

In short: it is implementation dependent.

mktime通过检查语言环境来了解DST规则。

mktime knows the DST rules by checking the locale.

对于一年中较大的一部分,mktime可以计算出DST是否适用于特定当地时间。问题确实是DST向后移动时的重复小时(在示例中为05:00:00 - 05:59:59)。对于此本地时间范围,给定tm_isdst = -1,mktime不能知道DST是否生效。选择这些中的哪一个,从一个实现到另一个不同。使用mktime的GNU版本,返回移位之前的UTC。

For the bigger part of the year, mktime can figure out if DST is to be applied for a particular local time. The problem is indeed the "duplicate" hour when DST shifts backwards (in your example 05:00:00 - 05:59:59). For this local time range, given tm_isdst = -1, mktime cannot know if DST is in effect or not. Which one of these is chosen, differs from one implementation to another. With the GNU version of mktime, the UTC before the shift is returned.

这篇关于mktime和tm_isdst标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 21:59