Skip to content

Commit

Permalink
Expose tzalloc()/localtime_rz()/mktime_z()/tzfree().
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
enh-google committed Jun 16, 2023
1 parent 5c6961f commit 2bd4316
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 595 deletions.
5 changes: 5 additions & 0 deletions docs/status.md
Expand Up @@ -57,6 +57,11 @@ Current libc symbols: https://android.googlesource.com/platform/bionic/+/master/

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

New libc functions in U (API level 34):
* `close_range` and `copy_file_range` (Linux-specific GNU extensions).
Expand Down
12 changes: 8 additions & 4 deletions libc/Android.bp
Expand Up @@ -252,10 +252,11 @@ cc_library_static {
srcs: [
"tzcode/**/*.c",
"tzcode/bionic.cpp",
// tzcode doesn't include strptime or wcsftime, so we use the OpenBSD
// code (with some local changes in the strptime case).
// tzcode doesn't include strptime, so we use a fork of the
// OpenBSD code which needs this global data.
"upstream-openbsd/lib/libc/locale/_def_time.c",
"upstream-openbsd/lib/libc/time/wcsftime.c",
// tzcode doesn't include wcsftime, so we use the FreeBSD code.
"upstream-freebsd/lib/libc/locale/wcsftime.c",
],

cflags: [
Expand Down Expand Up @@ -284,7 +285,10 @@ cc_library_static {
"-Dlint",
],

local_include_dirs: ["tzcode/"],
local_include_dirs: [
"tzcode/",
"upstream-freebsd/android/include",
],
name: "libc_tzcode",
}

Expand Down
63 changes: 31 additions & 32 deletions libc/NOTICE
Expand Up @@ -63,38 +63,6 @@ is preserved.

-------------------------------------------------------------------

Based on the UCB version with the ID appearing below.
This is ANSIish only when "multibyte character == plain character".

Copyright (c) 1989, 1993
The Regents of the University of California. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

-------------------------------------------------------------------

Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
All rights reserved.

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

Copyright (c) 2011 The FreeBSD Foundation

Portions of this software were developed by David Chisnall
under sponsorship from the FreeBSD Foundation.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

-------------------------------------------------------------------

Copyright (c) 2002 Tim J. Robbins
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Expand Down
2 changes: 2 additions & 0 deletions libc/bionic/wchar.cpp
Expand Up @@ -135,6 +135,7 @@ size_t mbsnrtowcs(wchar_t* dst, const char** src, size_t nmc, size_t len, mbstat
size_t mbsrtowcs(wchar_t* dst, const char** src, size_t len, mbstate_t* ps) {
return mbsnrtowcs(dst, src, SIZE_MAX, len, ps);
}
__strong_alias(mbsrtowcs_l, mbsrtowcs);

size_t wcrtomb(char* s, wchar_t wc, mbstate_t* ps) {
static mbstate_t __private_state;
Expand Down Expand Up @@ -210,3 +211,4 @@ size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstat
size_t wcsrtombs(char* dst, const wchar_t** src, size_t len, mbstate_t* ps) {
return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
}
__strong_alias(wcsrtombs_l, wcsrtombs);
12 changes: 4 additions & 8 deletions libc/bionic/wchar_l.cpp
Expand Up @@ -41,14 +41,6 @@ int wcscoll_l(const wchar_t* ws1, const wchar_t* ws2, locale_t) {
return wcscoll(ws1, ws2);
}

size_t wcsftime_l(wchar_t* buf, size_t n, const wchar_t* fmt, const struct tm* tm, locale_t) {
return wcsftime(buf, n, fmt, tm);
}

size_t wcsxfrm_l(wchar_t* dst, const wchar_t* src, size_t n, locale_t) {
return wcsxfrm(dst, src, n);
}

double wcstod_l(const wchar_t* s, wchar_t** end_ptr, locale_t) {
return wcstod(s, end_ptr);
}
Expand Down Expand Up @@ -76,3 +68,7 @@ unsigned long long wcstoull_l(const wchar_t* s, wchar_t** end_ptr, int base, loc
long double wcstold_l(const wchar_t* s, wchar_t** end_ptr, locale_t) {
return wcstold(s, end_ptr);
}

size_t wcsxfrm_l(wchar_t* dst, const wchar_t* src, size_t n, locale_t) {
return wcsxfrm(dst, src, n);
}
57 changes: 57 additions & 0 deletions libc/include/time.h
Expand Up @@ -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

Expand Down Expand Up @@ -139,10 +145,23 @@ double difftime(time_t __lhs, time_t __rhs);
* [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
Expand All @@ -159,10 +178,24 @@ struct tm* _Nullable localtime(const time_t* _Nonnull __t);
* 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().
*/
Expand Down Expand Up @@ -246,9 +279,33 @@ char* _Nullable ctime_r(const time_t* _Nonnull __t, char* _Nonnull __buf);
/**
* [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
Expand Down
2 changes: 2 additions & 0 deletions libc/include/wchar.h
Expand Up @@ -57,6 +57,7 @@ int mbsinit(const mbstate_t* _Nullable __ps);
size_t mbrlen(const char* _Nullable __s, size_t __n, mbstate_t* _Nullable __ps);
size_t mbrtowc(wchar_t* _Nullable __buf, const char* _Nullable __s, size_t __n, mbstate_t* _Nullable __ps);
size_t mbsrtowcs(wchar_t* _Nullable __dst, const char* _Nullable * _Nonnull __src, size_t __dst_n, mbstate_t* _Nullable __ps);
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);
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);
wint_t putwc(wchar_t __wc, FILE* _Nonnull __fp);
wint_t putwchar(wchar_t __wc);
Expand Down Expand Up @@ -92,6 +93,7 @@ size_t wcsnrtombs(char* _Nullable __dst, const wchar_t* __BIONIC_COMPLICATED_NUL
wchar_t* _Nullable wcspbrk(const wchar_t* _Nonnull __s, const wchar_t* _Nonnull __accept);
wchar_t* _Nullable wcsrchr(const wchar_t* _Nonnull __s, wchar_t __wc);
size_t wcsrtombs(char* _Nullable __dst, const wchar_t* __BIONIC_COMPLICATED_NULLNESS * _Nullable __src, size_t __dst_n, mbstate_t* _Nullable __ps);
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);
size_t wcsspn(const wchar_t* _Nonnull __s, const wchar_t* _Nonnull __accept);
wchar_t* _Nullable wcsstr(const wchar_t* _Nonnull __haystack, const wchar_t* _Nonnull __needle);
double wcstod(const wchar_t* _Nonnull __s, wchar_t* __BIONIC_COMPLICATED_NULLNESS * _Nullable __end_ptr);
Expand Down
6 changes: 6 additions & 0 deletions libc/libc.map.txt
Expand Up @@ -1586,7 +1586,13 @@ LIBC_U { # introduced=UpsideDownCake

LIBC_V { # introduced=VanillaIceCream
global:
localtime_rz;
mbsrtowcs_l;
mktime_z;
timespec_getres;
tzalloc;
tzfree;
wcsrtombs_l;
} LIBC_U;

LIBC_PRIVATE {
Expand Down
3 changes: 2 additions & 1 deletion libc/tzcode/strptime.c
Expand Up @@ -28,6 +28,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "private.h"

#include <ctype.h>
#include <errno.h>
#include <limits.h>
Expand All @@ -37,7 +39,6 @@
#include <time.h>

#include "localedef.h"
#include "private.h"
#include "tzfile.h"

// Android: ignore OpenBSD's DEF_WEAK() stuff.
Expand Down
35 changes: 35 additions & 0 deletions libc/upstream-freebsd/android/include/xlocale_private.h
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2023 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#pragma once

#include <locale.h>

#define __get_locale() LC_GLOBAL_LOCALE

#define FIX_LOCALE(__l) /* Nothing. */

0 comments on commit 2bd4316

Please sign in to comment.