platform_bionic/libc
Elliott Hughes 2bd4316bd6 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
2023-06-16 08:10:47 -07:00
..
arch-arm [NFCI] Change Android's NT_TYPE to NT_ANDROID_TYPE. 2022-02-07 13:49:20 -08:00
arch-arm64 Fix the *return* types in the arm64 dynamic function dispatch. 2023-05-22 19:28:33 +00:00
arch-common/bionic Remove #if cruft. 2023-03-01 20:26:41 +00:00
arch-riscv64 Implement rvv version mem* and str* for riscv64 2023-05-31 09:32:42 +08:00
arch-x86 Tidy up the x86 atom/silvermont stuff similar to x86-64. 2022-07-25 16:27:25 +00:00
arch-x86_64 avx2 implementation for memset. 2022-07-22 21:48:50 +00:00
async_safe Fix mte build breakage. 2023-03-21 18:11:02 +00:00
b64/include/bionic
bionic Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
dns Rewrite function definitions with identifier lists 2022-07-13 10:58:42 +08:00
execinfo/include
fts/include
include Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
kernel Update to v6.3 kernel headers. 2023-05-09 19:04:15 +00:00
malloc_debug Move M_PURGE to M_PURGE_ALL. 2023-03-22 13:47:27 -07:00
malloc_hooks Disable clang-tidy for some malloc tests. 2023-01-10 16:57:44 -08:00
platform Nullability check for modules in /platform directory. 2023-06-14 22:27:49 +00:00
private Make fork equivalent to vfork when HWASan or MTE stack tagging is enabled. 2023-05-08 15:26:00 -07:00
seccomp riscv64 syscall stub and seccomp filter generation. 2022-10-13 23:41:53 +00:00
stdio Implement C23 scanf 'wf' length modifiers 2023-06-14 17:15:58 +00:00
system_properties
tools riscv64: fix return value when errorno is 4095 2023-05-11 14:52:56 +08:00
tzcode Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
upstream-freebsd Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
upstream-netbsd Sync with upstream NetBSD. 2022-11-08 02:51:48 +00:00
upstream-openbsd Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
versioner-dependencies Tell the version script generation script about risc-v. 2022-10-04 22:48:38 +00:00
Android.bp Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
BUILD Remove Commodore 64 support. 2022-10-19 16:13:09 +00:00
fs_config_generator.py
libc.map.txt Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
libstdc++.map.txt Add riscv64 to the map files. 2022-10-04 23:52:57 +00:00
MODULE_LICENSE_BSD
NOTICE Expose tzalloc()/localtime_rz()/mktime_z()/tzfree(). 2023-06-16 08:10:47 -07:00
SECCOMP_ALLOWLIST_APP.TXT Fix a typo. 2022-11-08 02:52:29 +00:00
SECCOMP_ALLOWLIST_COMMON.TXT Revert "Remove workarounds for the llvm sanitizers." 2022-12-13 17:20:04 +00:00
SECCOMP_ALLOWLIST_SYSTEM.TXT
SECCOMP_BLOCKLIST_APP.TXT Fix setfsgid()/setfsuid() for LP32. 2023-02-03 23:10:04 +00:00
SECCOMP_BLOCKLIST_COMMON.TXT
SECCOMP_PRIORITY.TXT
SYSCALLS.TXT Fix setfsgid()/setfsuid() for LP32. 2023-02-03 23:10:04 +00:00
version_script.txt