Expose tzalloc()/localtime_rz()/mktime_z()/tzfree().
* Rationale
The question often comes up of how to use multiple time zones in C code.
If you're single-threaded, you can just use setenv() to manipulate $TZ.
toybox does this, for example. But that's not thread-safe in two
distinct ways: firstly, getenv() is not thread-safe with respect to
modifications to the environment (and between the way putenv() is
specified and the existence of environ, it's not obvious how to fully
fix that), and secondly the _caller_ needs to ensure that no other
threads are using tzset() or any function that behaves "as if" tzset()
was called (which is neither easy to determine nor easy to ensure).
This isn't a bigger problem because most of the time the right answer
is to stop pretending that libc is at all suitable for any i18n, and
switch to icu4c instead. (The NDK icu4c headers do not include ucal_*,
so this is not a realistic option for most applications.)
But what if you're somewhere in between? Like the rust chrono library,
for example? What then?
Currently their "least worst" option is to reinvent the entire wheel and
read our tzdata files. Which isn't a great solution for anyone, for
obvious maintainability reasons.
So it's probably time we broke the catch-22 here and joined NetBSD in
offering a less broken API than standard C has for the last 40 years.
Sure, any would-be caller will have to have a separate "is this
Android?" and even "is this API level >= 35?" path, but that will fix
itself sometime in the 2030s when developers can just assume "yes, it
is", whereas if we keep putting off exposing anything, this problem
never gets solved.
(No-one's bothered to try to implement the std::chrono::time_zone
functionality in libc++ yet, but they'll face a similar problem if/when
they do.)
* Implementation
The good news is that tzcode already implements these functions, so
there's relatively little here.
I've chosen not to expose `struct state` because `struct __timezone_t`
makes for clearer error messages, given that compiler diagnostics will
show the underlying type name (`struct __timezone_t*`) rather than the
typedef name (`timezone_t`) that's used in calling code.
I've moved us over to FreeBSD's wcsftime() rather than keep the OpenBSD
one building --- I've long wanted to only have one implementation here,
and FreeBSD is already doing the "convert back and forth, calling the
non-wide function in the middle" dance that I'd hoped to get round to
doing myself someday. This should mean that our strftime() and
wcsftime() behaviors can't easily diverge in future, plus macOS/iOS are
mostly FreeBSD, so any bugs will likely be interoperable with the other
major mobile operating system, so there's something nice for everyone
there!
The FreeBSD wcsftime() implementation includes a wcsftime_l()
implementation, so that's one stub we can remove. The flip side of that
is that it uses mbsrtowcs_l() and wcsrtombs_l() which we didn't
previously have. So expose those as aliases of mbsrtowcs() and
wcsrtombs().
Bug: https://github.com/chronotope/chrono/issues/499
Test: treehugger
Change-Id: Iee1b9d763ead15eef3d2c33666b3403b68940c3c
diff --git a/libc/include/time.h b/libc/include/time.h
index 6bf31bc..bd3fac1 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -39,6 +39,12 @@
__BEGIN_DECLS
+/* If we just use void* in the typedef, the compiler exposes that in error messages. */
+struct __timezone_t;
+
+/** The `timezone_t` type that represents a time zone. */
+typedef struct __timezone_t* timezone_t;
+
/** Divisor to compute seconds from the result of a call to clock(). */
#define CLOCKS_PER_SEC 1000000
@@ -139,11 +145,24 @@
* [mktime(3)](http://man7.org/linux/man-pages/man3/mktime.3p.html) converts
* broken-down time `tm` into the number of seconds since the Unix epoch.
*
+ * See tzset() for details of how the time zone is set, and mktime_rz()
+ * for an alternative.
+ *
* Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
*/
time_t mktime(struct tm* _Nonnull __tm);
/**
+ * mktime_z(3) converts broken-down time `tm` into the number of seconds
+ * since the Unix epoch, assuming the given time zone.
+ *
+ * Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
+ *
+ * Available since API level 35.
+ */
+time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
+
+/**
* [localtime(3)](http://man7.org/linux/man-pages/man3/localtime.3p.html) converts
* the number of seconds since the Unix epoch in `t` to a broken-down time, taking
* the device's timezone into account.
@@ -159,11 +178,25 @@
* the number of seconds since the Unix epoch in `t` to a broken-down time.
* That broken-down time will be written to the given struct `tm`.
*
+ * See tzset() for details of how the time zone is set, and localtime_rz()
+ * for an alternative.
+ *
* Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
*/
struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
/**
+ * localtime_rz(3) converts the number of seconds since the Unix epoch in
+ * `t` to a broken-down time, assuming the given time zone. That broken-down
+ * time will be written to the given struct `tm`.
+ *
+ * Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
+ *
+ * Available since API level 35.
+ */
+struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
+
+/**
* Inverse of localtime().
*/
time_t timelocal(struct tm* _Nonnull __tm);
@@ -246,10 +279,34 @@
/**
* [tzset(3)](http://man7.org/linux/man-pages/man3/tzset.3.html) tells
* libc that the time zone has changed.
+ *
+ * Android looks at both the system property `persist.sys.timezone` and the
+ * environment variable `TZ`. The former is the device's current time zone
+ * as shown in Settings, while the latter is usually unset but can be used
+ * to override the global setting. This is a bad idea outside of unit tests
+ * or single-threaded programs because it's inherently thread-unsafe.
+ * See tzalloc(), localtime_rz(), mktime_z(), and tzfree() for an
+ * alternative.
*/
void tzset(void);
/**
+ * tzalloc(3) allocates a time zone corresponding to the given Olson id.
+ *
+ * Returns a time zone object on success, and returns NULL and sets `errno` on failure.
+ *
+ * Available since API level 35.
+ */
+timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35);
+
+/**
+ * tzfree(3) frees a time zone object returned by tzalloc().
+ *
+ * Available since API level 35.
+ */
+void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35);
+
+/**
* [clock(3)](http://man7.org/linux/man-pages/man3/clock.3.html)
* returns an approximation of CPU time used, equivalent to
* `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)` but with more confusing