当 PST 切换到 PDT 时如何找到下一个日期,反之亦然
我的程序在 Windows 计算机上运行,其时区不是
PST/PDT。但需要根据 PST/PDT 时间规则进行操作。
对于夏季/冬季时间,程序需要知道
PDT 更改为 PST 的下一个日期,反之亦然。
我如何用 C++ 编程找到下一个夏令时<->冬令时切换?
My program runs on a Windows computer on which timezone is not
PST/PDT. But it needs to operate according to PST/PDT time rules.
Wrt to summer/winter time, the program needs to know
the next date when PDT changes to PST or vice versa.
How can I program in C++ finding the next summertime<->wintertime switch ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于夏令时的开始和结束时间因国会的各种法案而发生变化,因此下一次夏令时过渡的信息并不固定。我不知道您是否需要重新启动才能应用 DST 更改,但如果需要,您可能需要更频繁地更新对下一次转换的估计。
获取此信息的本机 API 是
GetTimeZoneInformationForYear
。您可以通过特定的时区和年份。该函数填充一个TIME_ZONE_INFORMATION
结构;您想要的相关信息是TIME_ZONE_INFORMATION::DaylightDate
和TIME_ZONE_INFORMATION::StandardDate
Since the start and end of Daylight Savings Time have changed due to various acts of Congress, the information of the next savings transition is not fixed. I don't know if you need to reboot to apply DST changes, but if you do, you might want to update your estimate of the next transition more frequently than once.
The native API to get this information is
GetTimeZoneInformationForYear
. You can pass in a specific time zone and year. That function fills out aTIME_ZONE_INFORMATION
struct; the relevant information you want isTIME_ZONE_INFORMATION::DaylightDate
andTIME_ZONE_INFORMATION::StandardDate
如果您使用的是 Windows,请使用 C# 类来执行此操作,并通过您选择的互操作将结果返回到您的 C++ 程序。否则,您可能最终会重建用 C+ 执行此操作的 .Net 代码,并且在此过程中会错过 .Net 将为您处理的所有边缘情况。
您可以使用 TimeZoneInfo.Local 然后 获取其调整规则。
If you are on Windows, use a C# class to do this and return the results to your C++ program via your interop of choice. Otherise, you'll likely wind up rebuilding the .Net code that does this in C+, and in the process miss all the edge cases that .Net will handle for you.
You can use TimeZoneInfo.Local and then get the adjustment rules for it.
丑陋的暴力方法:
调用
time(NULL)
来获取当前时间作为time_t
值。使用
localtime()
将此值转换为struct tm
。 (考虑将tm_hour
成员调整为 12,这样您每天都会检查中午。)重复将 1 天添加到
struct tm
的tm_day
成员中code>,然后使用mktime()
转换回time_t
。使用
difftime()
将每个递增的time_t
值与前一个值进行比较。当 `difftime() 给出的值不接近 86400.0(1 天内的秒数)时,您就发现了 DST 转换。如果您执行此操作 365 次仍找不到过渡,则说明有问题。如果您愿意对
time_t
的表示做出一些假设,您可能可以采取一些捷径。显然这只是一个解决方案的概要——我自己还没有尝试过。
我刚刚重新阅读了这个问题,并意识到我完全忽略了您所说的计算机未处于 PST 或 PDT 状态的部分。 (可以为节目设置时区吗?)
Ugly brute force method:
Call
time(NULL)
to get the current time as atime_t
value.Use
localtime()
to convert this value to astruct tm
. (Consider adjusting thetm_hour
member to 12, so you're checking noon every day.)Repeatedly add 1 day to the
tm_day
member of yourstruct tm
, then usemktime()
to convert back totime_t
.Use
difftime()
to compare each incrementedtime_t
value to the previous one. When `difftime() gives you a value that's not close to 86400.0 (the number of seconds in 1 day), you've found a DST transition. If you do this 365 times without finding a transition, something is wrong.You can probably take some shortcuts if you're willing to make some assumptions about the representation of
time_t
.Obviously this is only an outline of a solution -- and I haven't tried it myself.
And I've just re-read the question and realized that I've completely ignored the part where you said that the computer isn't on PST or PDT. (Can you set the timezone for the program?)