如何确定 C 中是否启用夏令时?

发布于 2024-11-30 08:48:11 字数 115 浏览 2 评论 0原文

我有一个在跨平台上运行的 C 应用程序。在此程序中,我需要编写一个函数来确定给定日期是否为 DST。
实际上,我尝试在纯 C 中查找 DST 开始-DST 结束日期。有没有任何简单且标准的方法可以做到这一点?

I have a C application running on cross platforms. In this program, I need to write a function which determines if the given date is DST or not.
Actually, i try to find DST begin-DST end dates in pure C. Is there any simple and standard way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

祁梦 2024-12-07 08:48:11

time.h 提供带有 tm_isdst 标志的 tm 结构。使用 time 获取当前时间,使用 localtime 获取将时间调整为当前语言环境的 tm 结构体,并读取 tm_isdst 标志。

从联机帮助页:

tm_isdst  A flag that indicates whether daylight saving time is in effect at the
time described.  The value is positive if daylight saving time is in effect, zero 
if it is not, and negative if the information is not available.

time.h provides tm structs with a tm_isdst flag. Use time to get the current time, localtime to get a tm struct with the time adjusted to the current locale and read the tm_isdst flag.

From the manpage:

tm_isdst  A flag that indicates whether daylight saving time is in effect at the
time described.  The value is positive if daylight saving time is in effect, zero 
if it is not, and negative if the information is not available.
裂开嘴轻声笑有多痛 2024-12-07 08:48:11

代码是:

time_t rawtime;
struct tm timeinfo;  // get date and time info
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
int isdaylighttime = timeinfo.tm_isdst;

The code is:

time_t rawtime;
struct tm timeinfo;  // get date and time info
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
int isdaylighttime = timeinfo.tm_isdst;
孤檠 2024-12-07 08:48:11

实现中不允许假设 tm_isdst 始终为 0。
它必须提供正确的数据。
如果实现无法提供与国家/地区设置的规则一致的 tm_isdst,则应将 tm_isdst 设置为负值,如 7.23.1/4 同一标准

Implementation is not allowed to assume that tm_isdst is always 0.
It must provide correct data.
If implementation cannot provide tm_isdst which is consistent with rules set in a country than it should set tm_isdst to negative value as specified in 7.23.1/4 in the same Standard.

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