I have a setup working using localtime() to get a tm with the local times represented in it. And that is all good.
However, if I change timezone while the application is running, it does not notice that I have changed timezones.
Is there some way to tell it to 'go look again' to refresh to the system timezone?
I know this is probably not a common case, but it is what test are doing to test this feature, so they want it supported!
There's nothing in the standard library to do this. Unless your platform offers some extension to the library for updating the time zone, your program's calls to
localtime()
will probably always use the time zone that was active at program start up.You could probably work around that by putting the
localtime
stuff in a separate process that your main program can startup and shutdown at will, thus re-initializing that process's time zone.Or instead your platform may offer some other API for getting the local time that will reflect changes in the system time zone.
Take a look at
tzset
(this is posix only). This might give you what you need. If yourTZ
environment variable is unset, it should reinitialize from the OS.From the man page:
A simple test:
Gives me output: