Skip to content

Commit 2bd4316

Browse files
committed
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: chronotope/chrono#499 Test: treehugger Change-Id: Iee1b9d763ead15eef3d2c33666b3403b68940c3c
1 parent 5c6961f commit 2bd4316

File tree

13 files changed

+379
-595
lines changed

13 files changed

+379
-595
lines changed

docs/status.md

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ Current libc symbols: https://android.googlesource.com/platform/bionic/+/master/
5757

5858
New libc functions in V (API level 35):
5959
* `timespec_getres` (C23 addition).
60+
* `localtime_rz`, `mktime_z`, `tzalloc`, and `tzfree` (NetBSD
61+
extensions implemented in tzcode, and the "least non-standard"
62+
functions for avoiding $TZ if you need to use multiple time zones in
63+
multi-threaded C).
64+
* `mbsrtowcs_l` and `wcsrtombs_l` aliases for `mbsrtowcs` and `wcsrtombs`.
6065

6166
New libc functions in U (API level 34):
6267
* `close_range` and `copy_file_range` (Linux-specific GNU extensions).

libc/Android.bp

+8-4
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ cc_library_static {
252252
srcs: [
253253
"tzcode/**/*.c",
254254
"tzcode/bionic.cpp",
255-
// tzcode doesn't include strptime or wcsftime, so we use the OpenBSD
256-
// code (with some local changes in the strptime case).
255+
// tzcode doesn't include strptime, so we use a fork of the
256+
// OpenBSD code which needs this global data.
257257
"upstream-openbsd/lib/libc/locale/_def_time.c",
258-
"upstream-openbsd/lib/libc/time/wcsftime.c",
258+
// tzcode doesn't include wcsftime, so we use the FreeBSD code.
259+
"upstream-freebsd/lib/libc/locale/wcsftime.c",
259260
],
260261

261262
cflags: [
@@ -284,7 +285,10 @@ cc_library_static {
284285
"-Dlint",
285286
],
286287

287-
local_include_dirs: ["tzcode/"],
288+
local_include_dirs: [
289+
"tzcode/",
290+
"upstream-freebsd/android/include",
291+
],
288292
name: "libc_tzcode",
289293
}
290294

libc/NOTICE

+31-32
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,6 @@ is preserved.
6363

6464
-------------------------------------------------------------------
6565

66-
Based on the UCB version with the ID appearing below.
67-
This is ANSIish only when "multibyte character == plain character".
68-
69-
Copyright (c) 1989, 1993
70-
The Regents of the University of California. All rights reserved.
71-
72-
Redistribution and use in source and binary forms, with or without
73-
modification, are permitted provided that the following conditions
74-
are met:
75-
1. Redistributions of source code must retain the above copyright
76-
notice, this list of conditions and the following disclaimer.
77-
2. Redistributions in binary form must reproduce the above copyright
78-
notice, this list of conditions and the following disclaimer in the
79-
documentation and/or other materials provided with the distribution.
80-
3. Neither the name of the University nor the names of its contributors
81-
may be used to endorse or promote products derived from this software
82-
without specific prior written permission.
83-
84-
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87-
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90-
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92-
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93-
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94-
SUCH DAMAGE.
95-
96-
-------------------------------------------------------------------
97-
9866
Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9967
All rights reserved.
10068

@@ -3242,6 +3210,37 @@ POSSIBILITY OF SUCH DAMAGE.
32423210
Copyright (c) 2002 Tim J. Robbins
32433211
All rights reserved.
32443212

3213+
Copyright (c) 2011 The FreeBSD Foundation
3214+
3215+
Portions of this software were developed by David Chisnall
3216+
under sponsorship from the FreeBSD Foundation.
3217+
3218+
Redistribution and use in source and binary forms, with or without
3219+
modification, are permitted provided that the following conditions
3220+
are met:
3221+
1. Redistributions of source code must retain the above copyright
3222+
notice, this list of conditions and the following disclaimer.
3223+
2. Redistributions in binary form must reproduce the above copyright
3224+
notice, this list of conditions and the following disclaimer in the
3225+
documentation and/or other materials provided with the distribution.
3226+
3227+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3228+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3229+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3230+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3231+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3232+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3233+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3234+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3235+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3236+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3237+
SUCH DAMAGE.
3238+
3239+
-------------------------------------------------------------------
3240+
3241+
Copyright (c) 2002 Tim J. Robbins
3242+
All rights reserved.
3243+
32453244
Redistribution and use in source and binary forms, with or without
32463245
modification, are permitted provided that the following conditions
32473246
are met:

libc/bionic/wchar.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ size_t mbsnrtowcs(wchar_t* dst, const char** src, size_t nmc, size_t len, mbstat
135135
size_t mbsrtowcs(wchar_t* dst, const char** src, size_t len, mbstate_t* ps) {
136136
return mbsnrtowcs(dst, src, SIZE_MAX, len, ps);
137137
}
138+
__strong_alias(mbsrtowcs_l, mbsrtowcs);
138139

139140
size_t wcrtomb(char* s, wchar_t wc, mbstate_t* ps) {
140141
static mbstate_t __private_state;
@@ -210,3 +211,4 @@ size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstat
210211
size_t wcsrtombs(char* dst, const wchar_t** src, size_t len, mbstate_t* ps) {
211212
return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
212213
}
214+
__strong_alias(wcsrtombs_l, wcsrtombs);

libc/bionic/wchar_l.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ int wcscoll_l(const wchar_t* ws1, const wchar_t* ws2, locale_t) {
4141
return wcscoll(ws1, ws2);
4242
}
4343

44-
size_t wcsftime_l(wchar_t* buf, size_t n, const wchar_t* fmt, const struct tm* tm, locale_t) {
45-
return wcsftime(buf, n, fmt, tm);
46-
}
47-
48-
size_t wcsxfrm_l(wchar_t* dst, const wchar_t* src, size_t n, locale_t) {
49-
return wcsxfrm(dst, src, n);
50-
}
51-
5244
double wcstod_l(const wchar_t* s, wchar_t** end_ptr, locale_t) {
5345
return wcstod(s, end_ptr);
5446
}
@@ -76,3 +68,7 @@ unsigned long long wcstoull_l(const wchar_t* s, wchar_t** end_ptr, int base, loc
7668
long double wcstold_l(const wchar_t* s, wchar_t** end_ptr, locale_t) {
7769
return wcstold(s, end_ptr);
7870
}
71+
72+
size_t wcsxfrm_l(wchar_t* dst, const wchar_t* src, size_t n, locale_t) {
73+
return wcsxfrm(dst, src, n);
74+
}

libc/include/time.h

+57
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040
__BEGIN_DECLS
4141

42+
/* If we just use void* in the typedef, the compiler exposes that in error messages. */
43+
struct __timezone_t;
44+
45+
/** The `timezone_t` type that represents a time zone. */
46+
typedef struct __timezone_t* timezone_t;
47+
4248
/** Divisor to compute seconds from the result of a call to clock(). */
4349
#define CLOCKS_PER_SEC 1000000
4450

@@ -139,10 +145,23 @@ double difftime(time_t __lhs, time_t __rhs);
139145
* [mktime(3)](http://man7.org/linux/man-pages/man3/mktime.3p.html) converts
140146
* broken-down time `tm` into the number of seconds since the Unix epoch.
141147
*
148+
* See tzset() for details of how the time zone is set, and mktime_rz()
149+
* for an alternative.
150+
*
142151
* Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
143152
*/
144153
time_t mktime(struct tm* _Nonnull __tm);
145154

155+
/**
156+
* mktime_z(3) converts broken-down time `tm` into the number of seconds
157+
* since the Unix epoch, assuming the given time zone.
158+
*
159+
* Returns the time in seconds on success, and returns -1 and sets `errno` on failure.
160+
*
161+
* Available since API level 35.
162+
*/
163+
time_t mktime_z(timezone_t _Nonnull __tz, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
164+
146165
/**
147166
* [localtime(3)](http://man7.org/linux/man-pages/man3/localtime.3p.html) converts
148167
* the number of seconds since the Unix epoch in `t` to a broken-down time, taking
@@ -159,10 +178,24 @@ struct tm* _Nullable localtime(const time_t* _Nonnull __t);
159178
* the number of seconds since the Unix epoch in `t` to a broken-down time.
160179
* That broken-down time will be written to the given struct `tm`.
161180
*
181+
* See tzset() for details of how the time zone is set, and localtime_rz()
182+
* for an alternative.
183+
*
162184
* Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
163185
*/
164186
struct tm* _Nullable localtime_r(const time_t* _Nonnull __t, struct tm* _Nonnull __tm);
165187

188+
/**
189+
* localtime_rz(3) converts the number of seconds since the Unix epoch in
190+
* `t` to a broken-down time, assuming the given time zone. That broken-down
191+
* time will be written to the given struct `tm`.
192+
*
193+
* Returns a pointer to a broken-down time on success, and returns null and sets `errno` on failure.
194+
*
195+
* Available since API level 35.
196+
*/
197+
struct tm* _Nullable localtime_rz(timezone_t _Nonnull __tz, const time_t* _Nonnull __t, struct tm* _Nonnull __tm) __INTRODUCED_IN(35);
198+
166199
/**
167200
* Inverse of localtime().
168201
*/
@@ -246,9 +279,33 @@ char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf);
246279
/**
247280
* [tzset(3)](http://man7.org/linux/man-pages/man3/tzset.3.html) tells
248281
* libc that the time zone has changed.
282+
*
283+
* Android looks at both the system property `persist.sys.timezone` and the
284+
* environment variable `TZ`. The former is the device's current time zone
285+
* as shown in Settings, while the latter is usually unset but can be used
286+
* to override the global setting. This is a bad idea outside of unit tests
287+
* or single-threaded programs because it's inherently thread-unsafe.
288+
* See tzalloc(), localtime_rz(), mktime_z(), and tzfree() for an
289+
* alternative.
249290
*/
250291
void tzset(void);
251292

293+
/**
294+
* tzalloc(3) allocates a time zone corresponding to the given Olson id.
295+
*
296+
* Returns a time zone object on success, and returns NULL and sets `errno` on failure.
297+
*
298+
* Available since API level 35.
299+
*/
300+
timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35);
301+
302+
/**
303+
* tzfree(3) frees a time zone object returned by tzalloc().
304+
*
305+
* Available since API level 35.
306+
*/
307+
void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35);
308+
252309
/**
253310
* [clock(3)](http://man7.org/linux/man-pages/man3/clock.3.html)
254311
* returns an approximation of CPU time used, equivalent to

libc/include/wchar.h

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int mbsinit(const mbstate_t* _Nullable __ps);
5757
size_t mbrlen(const char* _Nullable __s, size_t __n, mbstate_t* _Nullable __ps);
5858
size_t mbrtowc(wchar_t* _Nullable __buf, const char* _Nullable __s, size_t __n, mbstate_t* _Nullable __ps);
5959
size_t mbsrtowcs(wchar_t* _Nullable __dst, const char* _Nullable * _Nonnull __src, size_t __dst_n, mbstate_t* _Nullable __ps);
60+
size_t mbsrtowcs_l(wchar_t* _Nullable __dst, const char* _Nullable * _Nonnull __src, size_t __dst_n, mbstate_t* _Nullable __ps, locale_t _Nonnull __l) __INTRODUCED_IN(35);
6061
size_t mbsnrtowcs(wchar_t* _Nullable __dst, const char* _Nullable * _Nullable __src, size_t __src_n, size_t __dst_n, mbstate_t* _Nullable __ps) __INTRODUCED_IN(21);
6162
wint_t putwc(wchar_t __wc, FILE* _Nonnull __fp);
6263
wint_t putwchar(wchar_t __wc);
@@ -92,6 +93,7 @@ size_t wcsnrtombs(char* _Nullable __dst, const wchar_t* __BIONIC_COMPLICATED_NUL
9293
wchar_t* _Nullable wcspbrk(const wchar_t* _Nonnull __s, const wchar_t* _Nonnull __accept);
9394
wchar_t* _Nullable wcsrchr(const wchar_t* _Nonnull __s, wchar_t __wc);
9495
size_t wcsrtombs(char* _Nullable __dst, const wchar_t* __BIONIC_COMPLICATED_NULLNESS * _Nullable __src, size_t __dst_n, mbstate_t* _Nullable __ps);
96+
size_t wcsrtombs_l(char* _Nullable __dst, const wchar_t* __BIONIC_COMPLICATED_NULLNESS * _Nullable __src, size_t __dst_n, mbstate_t* _Nullable __ps, locale_t _Nonnull __l) __INTRODUCED_IN(35);
9597
size_t wcsspn(const wchar_t* _Nonnull __s, const wchar_t* _Nonnull __accept);
9698
wchar_t* _Nullable wcsstr(const wchar_t* _Nonnull __haystack, const wchar_t* _Nonnull __needle);
9799
double wcstod(const wchar_t* _Nonnull __s, wchar_t* __BIONIC_COMPLICATED_NULLNESS * _Nullable __end_ptr);

libc/libc.map.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,13 @@ LIBC_U { # introduced=UpsideDownCake
15861586

15871587
LIBC_V { # introduced=VanillaIceCream
15881588
global:
1589+
localtime_rz;
1590+
mbsrtowcs_l;
1591+
mktime_z;
15891592
timespec_getres;
1593+
tzalloc;
1594+
tzfree;
1595+
wcsrtombs_l;
15901596
} LIBC_U;
15911597

15921598
LIBC_PRIVATE {

libc/tzcode/strptime.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31+
#include "private.h"
32+
3133
#include <ctype.h>
3234
#include <errno.h>
3335
#include <limits.h>
@@ -37,7 +39,6 @@
3739
#include <time.h>
3840

3941
#include "localedef.h"
40-
#include "private.h"
4142
#include "tzfile.h"
4243

4344
// Android: ignore OpenBSD's DEF_WEAK() stuff.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
* SUCH DAMAGE.
27+
*/
28+
29+
#pragma once
30+
31+
#include <locale.h>
32+
33+
#define __get_locale() LC_GLOBAL_LOCALE
34+
35+
#define FIX_LOCALE(__l) /* Nothing. */

0 commit comments

Comments
 (0)