New APIs to allow conversion of times in arbitrary zones

For the benefit of programs such as world clocks and any other program that would need to convert times in an arbitrary zone specified by a TZ setting, I've added some APIs to load time zone information for a specified zone, free that time zone information, and convert between time_t and struct tm in specified zones: zoneinfo_t loadzone(const char *zonename) Attempts to allocate and fill in a structure to hold information for the time zone specified by "zonename"; returns a pointer to the time zone information on success and NULL on failure. void freezone(zoneinfo_t zoneinfo) Frees zone information returned by loadzone(). struct tm *localtime_zr(const time *clock, struct tm *result, zoneinfo_t zoneinfo) Like localtime_r(), but does the conversion in the time zone specified by zoneinfo. time_t mktime_z(struct tm *timeptr) Like mktime(), but does the conversion in the time zone specified by zoneinfo. zoneinfo_t is implemented, internally, as a pointer to a "struct state", but it's opaque to the callers. I'm not committed to any of the names, or the order of the arguments; for example, "loadzone()" doesn't necessarily load information from a file - it might parse a POSIX-style TZ string - so that might not be the right name. The new functions are declared in an "xtime.h" public header (the name was inspired by the BSD "xlocale.h" header that declares functions such as printf_l() which take a locale information pointer as an argument). See the attached patch and "xtime.h" file.

<<On Tue, 26 May 2009 11:00:03 -0700, Guy Harris <guy@alum.mit.edu> said:
zoneinfo_t is implemented, internally, as a pointer to a "struct state", but it's opaque to the callers.
There is no such thing as an "opaque typedef". Using typedefs like this creates #include spaghetti, obscures dependencies, and ultimately reduces type safety. The only type-safe opaque types C has are "struct <foo> *" and "union <foo> *". -GAWollman

I did some similar work back in 2001. See http://article.gmane.org/gmane.comp.time.tz/81 for a 2004 repost of my patches, or download the full tz mailing list history for the discussion from June of 2001. On Tuesday, May 26 2009, "Guy Harris" wrote to "tz@lecserver.nci.nih.gov" saying:
For the benefit of programs such as world clocks and any other program that would need to convert times in an arbitrary zone specified by a TZ setting, I've added some APIs to load time zone information for a specified zone, free that time zone information, and convert between time_t and struct tm in specified zones:
zoneinfo_t loadzone(const char *zonename)
Attempts to allocate and fill in a structure to hold information for the time zone specified by "zonename"; returns a pointer to the time zone information on success and NULL on failure.
void freezone(zoneinfo_t zoneinfo)
Frees zone information returned by loadzone().
struct tm *localtime_zr(const time *clock, struct tm *result, zoneinfo_t zoneinfo)
Like localtime_r(), but does the conversion in the time zone specified by zoneinfo.
time_t mktime_z(struct tm *timeptr)
Like mktime(), but does the conversion in the time zone specified by zoneinfo.
zoneinfo_t is implemented, internally, as a pointer to a "struct state", but it's opaque to the callers.
I'm not committed to any of the names, or the order of the arguments; for example, "loadzone()" doesn't necessarily load information from a file - it might parse a POSIX-style TZ string - so that might not be the right name.
The new functions are declared in an "xtime.h" public header (the name was inspired by the BSD "xlocale.h" header that declares functions such as printf_l() which take a locale information pointer as an argument).
See the attached patch and "xtime.h" file.

If you're going to have such an interface, why not make it reentrant as well? That, IMHO, is the biggest shortcoming of the current set of standards. Michael Lindner -----Original Message----- From: Guy Harris [mailto:guy@alum.mit.edu] Sent: Tue 5/26/2009 2:00 PM To: tz@lecserver.nci.nih.gov Subject: New APIs to allow conversion of times in arbitrary zones For the benefit of programs such as world clocks and any other program that would need to convert times in an arbitrary zone specified by a TZ setting, I've added some APIs to load time zone information for a specified zone, free that time zone information, and convert between time_t and struct tm in specified zones: zoneinfo_t loadzone(const char *zonename) Attempts to allocate and fill in a structure to hold information for the time zone specified by "zonename"; returns a pointer to the time zone information on success and NULL on failure. void freezone(zoneinfo_t zoneinfo) Frees zone information returned by loadzone(). struct tm *localtime_zr(const time *clock, struct tm *result, zoneinfo_t zoneinfo) Like localtime_r(), but does the conversion in the time zone specified by zoneinfo. time_t mktime_z(struct tm *timeptr) Like mktime(), but does the conversion in the time zone specified by zoneinfo. zoneinfo_t is implemented, internally, as a pointer to a "struct state", but it's opaque to the callers. I'm not committed to any of the names, or the order of the arguments; for example, "loadzone()" doesn't necessarily load information from a file - it might parse a POSIX-style TZ string - so that might not be the right name. The new functions are declared in an "xtime.h" public header (the name was inspired by the BSD "xlocale.h" header that declares functions such as printf_l() which take a locale information pointer as an argument). See the attached patch and "xtime.h" file.
participants (4)
-
Garrett Wollman
-
Guy Harris
-
lennox@cs.columbia.edu
-
Michael Lindner