C ++ localtime when changing timezone

I have a setting using localtime () to get tm with the local time represented in it. And this is all good.

However, if I change the time zone while the application is running, it does not notice that I changed the time intervals.

Is there a way to say “look again” to update the system time zone?

I know this is probably not an ordinary case, but it is a test that tests this feature, so they want it to be supported.

+3
source share
2 answers

. , localtime(), , , .

, , localtime , , .

API , .

+1

tzset ( posix). , . TZ , .

man:

tzset() tzname TZ. , . SysV- ( GMT) (0, - , , , ).

TZ , tzname , tzfile (5) - localtime (. ). ( /etc/localtime, .)

:

#include <iostream>
#include <time.h>
#include <stdlib.h>

int main()
{
        tzset();

        time_t t;
        time(&t);

        std::cout << "tz: " << tzname[0] << " - " << tzname[1] << " " << ctime(&t) << std::endl;

        setenv("TZ", "EST5EDT", 1);
        tzset();

        std::cout << "tz: " << tzname[0] << " - " << tzname[1] << " " << ctime(&t) << std::endl;

        return 0;
}

:

tz: CST - CDT Wed Jan 11 12:35:02 2012

tz: EST - EDT Wed Jan 11 13:35:02 2012

+2

All Articles