Working with time zones in C

I am currently porting some of my code from Cocoa (Apple Objective-C API) to C to make it available on other operating systems such as GNU / Linux.

Cocoa had many great things, such as creating NSTimeZone objects from their string descriptors using timeZoneWithAbbreviation:. This allowed me to use values ​​such as Europe / Paris to easily create NSTimeZone objects. Another important method for me was secondsFromGMTForDate:because it automatically takes into account the DST rules for you.

Right now, the closest to NSTimeZone I found for C is struct timezone(see here ). However, I don’t see how I can easily get the GMT “offset” in seconds for a specific time and time zone. I also don't understand how to get timezone structures if you only have a description of "Europe / Paris".

+3
source share
2 answers

The following is my understanding and may be wrong ...

, OS X , Linux C Olson, ( , ).

, tzset. , , , TZ, tzset. :

int main(int argc, char *argv[]) {
    time_t t = time(NULL);
    printf("%s\n", ctime(&t));

    setenv("TZ", "America/Chicago", 1);
    printf("%s\n", ctime(&t));

    return 0;
}

tzset timezone, GMT .

, ( , , timezone) C, , Linux, , .. , , , , Olson ICU TimeZone class ( C).

+4

, . , , . , Olson Database ( ), ICU ( Unicode).

+2

Source: https://habr.com/ru/post/1744188/


All Articles