Search code examples
cdst

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


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?


Solution

  • 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.