[PLUG] Timezone conversions in linux (again)

website reader website.reader3 at gmail.com
Thu Nov 21 02:23:48 UTC 2013


My attempts using the thread safe localtime_r() command have failed, due to
the fact that the external global environment variable TZ is being set or
unset by various threads and processes on the machine and they are not
synchronized.  I had dozens and dozens of timezone conversion failures
today while running threads which do a sum total of about 56 conversions
per minute for the whole period of 10 hours despite my efforts to
setenv(TZ,timezone,1) and unsetenv(TZ) calls to insure safe conversion.

This is a known problem, see the following articles:

http://stackoverflow.com/questions/1223947/time-zone-conversion-c-api-on-linux-anyone
http://www.gossamer-threads.com/lists/rsyslog/users/10616

Apparently UNIX and Linux has grown up with dependency upon the local
timezone as a simple global variable set on a user's computer, apparently
not enough effort was made to provide a general timezone conversion
algorithm as a freestanding algorithm not dependent upon any external
global variable.

As a mathematician, I have to say that I was hoping for the following:

Unix epoch seconds + nanoseconds (timespec ) --> tm struct (extended) with
timezone and timezone offset from GMT

And the inverse function:

tm struct extended --> Unix epoch sec + nsecs

That should be a simple orthogonal function, backwards and forwards, but to
my knowledge this does not yet exist in the linux world.

I am going to have to write a C++ function which will keep the TZ as a
local variable, when doing the conversion.



More information about the PLUG mailing list