[PLUG] Question on using localtime(), but not in your own timezone, to find calendar time

Daniel Hedlund daniel at digitree.org
Tue Nov 19 22:55:05 UTC 2013


On Tue, Nov 19, 2013 at 2:32 PM, website reader
<website.reader3 at gmail.com>wrote:

> setenv("TZ", const_cast<char*>(zone.c_str)),1);
>

I wouldn't expect this to be thread-safe as ENV is a global per-process.


new_calender = *localtime(current_secs);
>

This is definitely not threadsafe in glibc.  From the man page:

The four functions asctime(), ctime(), gmtime() and localtime() return a
pointer to static data and  hence  are  not thread-safe.   Thread-safe
versions asctime_r(), ctime_r(), gmtime_r() and localtime_r() are specified
by SUSv2, and available since libc 5.2.5.

The man page suggests "localtime_r", which is threadsafe.  Glibc also has
an additional fields on the tm struct, which store timezone.

    The glibc version of struct tm has additional fields

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


I haven't tried, but I would guess that you could get your unix time value
and then build a new tz struct with your expected timezone and then use one
of the *_r threadsafe functions to get what you want.



More information about the PLUG mailing list