How do I find the current system timezone?

后端 未结 12 468
攒了一身酷
攒了一身酷 2020-11-30 06:22

On Linux, I need to find the currently configured timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible.

For e

12条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 07:00

    There is no standard c or c++ function for this. However, GNU libc has an extention. its struct tm has two extra members:

    long tm_gmtoff;           /* Seconds east of UTC */
    const char *tm_zone;      /* Timezone abbreviation */
    

    This means that if you use one of the functions which populates a struct tm (such as localtime or gmtime) you can use these extra fields. This is of course only if you are using GNU libc (and a sufficiently recent version of it).

    Also many systems have a int gettimeofday(struct timeval *tv, struct timezone *tz); function (POSIX) which will fill in a struct timezone. This has the following fields:

    struct timezone {
        int tz_minuteswest;     /* minutes west of Greenwich */
        int tz_dsttime;         /* type of DST correction */
    };
    

    Not exactly what you asked for, but close...

提交回复
热议问题