当 PST 切换到 PDT 时如何找到下一个日期,反之亦然

发布于 2024-11-28 17:25:13 字数 173 浏览 0 评论 0原文

我的程序在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-12-05 17:25:13

由于夏令时的开始和结束时间因国会的各种法案而发生变化,因此下一次夏令时过渡的信息并不固定。我不知道您是否需要重新启动才能应用 DST 更改,但如果需要,您可能需要更频繁地更新对下一次转换的估计。

获取此信息的本机 API 是 GetTimeZoneInformationForYear。您可以通过特定的时区和年份。该函数填充一个TIME_ZONE_INFORMATION结构;您想要的相关信息是 TIME_ZONE_INFORMATION::DaylightDateTIME_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 a TIME_ZONE_INFORMATION struct; the relevant information you want is TIME_ZONE_INFORMATION::DaylightDate and TIME_ZONE_INFORMATION::StandardDate

卷耳 2024-12-05 17:25:13

如果您使用的是 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.

你怎么这么可爱啊 2024-12-05 17:25:13

丑陋的暴力方法:

调用time(NULL)来获取当前时间作为time_t值。

使用 localtime() 将此值转换为 struct tm。 (考虑将 tm_hour 成员调整为 12,这样您每天都会检查中午。)

重复将 1 天添加到 struct tmtm_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 a time_t value.

Use localtime() to convert this value to a struct tm. (Consider adjusting the tm_hour member to 12, so you're checking noon every day.)

Repeatedly add 1 day to the tm_day member of your struct tm, then use mktime() to convert back to time_t.

Use difftime() to compare each incremented time_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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文