CTime如何真正地“应用"应用程序? DST? [英] How does CTime actually "apply" DST?

查看:96
本文介绍了CTime如何真正地“应用"应用程序? DST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据MSDN,如果将DST标志设置为> 0夏令时有效".

我有一个应用程序,该应用程序将一些时间信息写入文件,然后将其读入以执行某些操作,然后再次将其全部写出.

如果我的程序在2012年8月5日的8:00运行,并且从文件"2012年1月1日15:30"读取,DST标志的作用是什么?

我将从文件中读取一个字符串,其中包含信息"2012年1月1日15:30",并将其全部转换为数字,这样我就可以了

小时:15
分钟:30
天:1
月:1(尽管可能是0 ...)
年:2012年(或有任何定义的偏移量)

然后,我调用CTime构造函数,并全力以赴以在当地时间获取CTime对象,但这是15:30,因为它是这样说的,还是14:30,因为它是在8月运行的?

==============

我很想了解CTime的工作方式才能完成工作,但是我真正需要弄清楚的是一种写到UTC的文件然后再读回UTC的方法,但是据我所知,如果我使用CTime,它将把我输入的内容转换成本地时间.

因此,目前,我有一个程序可以在每次运行时回溯时间8小时(我在日本).

请帮忙!

在此先感谢.

According to MSDN, if you set the DST flag to >0 "Daylight saving time is in effect".

I have an application that writes some time information to file, then reads it in to do some things then write it all out again.

If my program is being run at 8:00 on the 5th of August 2012, and it reads from the file "15:30 1st Jan 2012", what exactly does the DST flag do?

I will read from the file a string that holds the information "15:30 1st January 2012" and convert that all into numbers so I have

hours: 15
minutes: 30
days: 1
months: 1 (although it could be 0...)
years: 2012 (or with any defined offset)

Then I call the CTime constructor and shove that all in to get a CTime object in local time, but will that be 15:30 because it says so, or 14:30 because it''s run in August?

===============

I''d love to get my work done having understood what CTime does, but what I really need to figure out is a way of writing out to file in UTC then reading that back in UTC, but from what I can tell, if I use CTime, it will convert whatever I put into into local time.

Hence, at the moment, I''ve got a program that back tracks through time by 8 hours every time it runs (I''m in Japan).

Please help!

Thanks in advance.

推荐答案

答案是,永远不要将时间和/或日期存储为字符串.正确跟踪时间的唯一有效方法是始终将其存储为UTC值.这意味着时间戳是事件发生的真实世界时的准确表示,并且永远是正确的.当您要为用户显示值时,可以应用所需的任何时区,并自动添加DST.
The answer is, never store times and/or dates as strings. The only valid way to track times correctly is always to store them as UTC values. That means the timestamp is an accurate representation of the real universal time that your event occurred and is correct forever. When you want to display the value for a user you can apply whatever timezone you wish, and get DST added automatically.


您可以将值存储在标准化的ISO-8601 [^] 格式:
[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z.
字符串解析器的大多数日期/时间都支持此格式.为了便于阅读,日期/时间分隔符"T"通常替换为空格字符(这会违反标准,但已被广泛接受).

末尾的"Z"(祖鲁语)字符表示UTC.正如Richard所指出的那样,始终首选存储为UTC.
You may store the values in the standardized ISO-8601 [^] format:
[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z.
This format is supported by most date/time from string parsers. The date/time separator ''T'' is often replaced by a space character for human readability (this will break the standard but is widely accepted).

The ''Z'' (Zulu) character at the end indicates UTC here. As already noted by Richard, storing as UTC is always preferred.


要回答您的原始问题:然后我调用CTime构造函数,并全力以赴,以获取一个CTime对象.当地时间,但会是15:30,因为是这样,还是14:30是因为它在八月开始?

如果将nDST参数设置为负值(默认值),则CTime将确定是否在8月(和该时间)的那个日期应用DST,并将您的本地时间转换为UTC.如果将nDST设置为0,则CTime会认为此时间戳是在没有DST的时间获取的,因此将其相应地转换回UTC.如果将nDST设置为1,则CTime会理所当然地认为时间戳记是在DST生效时进行的,并进行相应的转换.

请注意,CTime会查看当前的语言环境设置,以找出您所在的时区并找出相应的更正值.如果nDST< 0,它还将查询语言环境信息,以找出适用于确定时间戳记是否在DST周期内的规则.

因此,nDst参数使您对是否应用DST校正有更多的控制,例如在过去可能已应用了不同DST规则的情况下.如果您可以从基于文本的时间戳记中确定当时的DST是否有效,那么我将始终使用该信息,而不使用默认的nDst = -1.
To answer your original question: Then I call the CTime constructor and shove that all in to get a CTime object in local time, but will that be 15:30 because it says so, or 14:30 because it''s run in August?

If you set the nDST parameter to a negative value (the dafault), CTime will figure out whether on that date in August (and that time) DST will apply and convert your local time according into UTC. It you set nDST to 0, CTime will take it for granted that this time stamp was taken at a time with no DST and hence convert it back to UTC accordingly. If you set nDST to 1, CTime will take it for granted that the timestamp was taken while DST was in effect and convert accordingly.

Note that CTime looks at the current locale settings to figure out in which time zone you are and find out the corresponding correction value. In case of nDST < 0 it will also consult the locale information to find out which rule applies for determining if a timestamp falls into the DST period.

The nDst parameter hence gives you a little more control over whether the DST correction is applied, for example in cases in the past for which different DST rules might have applied. If you can determine from your text-based representation of a timestamp if DST was in effect at that time, I would always use that information instead of using the default nDst = -1.


这篇关于CTime如何真正地“应用"应用程序? DST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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