开发者

How to determine if daylight saving time is active in C?

开发者 https://www.devze.com 2023-03-29 03:53 出处:网络
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.

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?


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.


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消