我有一个跨平台的C应用程序。在这个程序中,我需要编写一个函数来确定给定的日期是否处于夏令时(DST)。
实际上,我尝试找到纯C中DST开始-DST结束日期。是否有任何简单和标准的方法来做到这一点?
我有一个跨平台的C应用程序。在这个程序中,我需要编写一个函数来确定给定的日期是否处于夏令时(DST)。
实际上,我尝试找到纯C中DST开始-DST结束日期。是否有任何简单和标准的方法来做到这一点?
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.
implementation-defined
是什么意思。但实现应该仍然给出正确的结果,对吧? - pmrtm_isdst
始终为 0,甚至不必为各种 <time.h>
函数检查它,或者假定时区为 GMT。希望结果是连贯的,但是(稍微)不正确的;也许这比政治家对时间的想法给出的实际不连贯但正确的结果更好 :) - pmg代码是:
time_t rawtime;
struct tm timeinfo; // get date and time info
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
int isdaylighttime = timeinfo.tm_isdst;
实现不允许假定tm_isdst始终为0。它必须提供正确的数据。如果实现无法提供与国家规则一致的tm_isdst,则应将tm_isdst设置为负值,如7.23.1/4 在同一标准中所指定。