[tz] Thread-safe localtime(3) (was Re: Reading binary files)
guy at alum.mit.edu
Wed Nov 2 19:56:02 UTC 2011
On Nov 2, 2011, at 12:34 PM, Bennett Todd wrote:
> That sounds all superb, and excellent. The one question I'd ask is, would it be practical and reasonable to lose the _z and _rz suffixes, and make the timezone arg an optional extra arg to localtime/mktine, with varargs?
How would you do that without breaking any existing code on any implementation? (That means "without relying on any implementation quirks", so you can't assume arguments are passed on the stack or in registers or....) How would localtime distinguish
tm = localtime(when);
tm = localtime(when, tz);
in a fashion that works with all C compilers on all platforms?
> It seems saddening to carry the original api forward unchanged at the expense of blessing into official api standard what look to a naive eye like placeholder function names.
That's already happened - localtime() is not thread safe, so there's localtime_r(), which takes a time_t and a pointer to a struct tm as arguments, and strftime() and strptime() are also not thread-safe if you need to make different formatting or parsing calls in different locales, so there's strftime_l() and strptime_l(), which take a locale_t in addition to the regular strftime() or strptime() arguments. These are in the current Single UNIX standard (as are other _r and _l functions).
More information about the tz