diff --git a/libc/include/time.h b/libc/include/time.h index 08f30ef8d..31c20505b 100644 --- a/libc/include/time.h +++ b/libc/include/time.h @@ -42,7 +42,13 @@ __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 timezone. */ +/** + * The `timezone_t` type that represents a timezone. + * + * To use this with std::unique_ptr you'll want something like + * `std::unique_ptr, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};` + * to remove the pointer. + */ typedef struct __timezone_t* timezone_t; /** Divisor to compute seconds from the result of a call to clock(). */ @@ -300,6 +306,10 @@ void tzset(void); * tzalloc() is thread safe (though obviously the system timezone can * change, especially if your mobile device is actually mobile!). * + * To use this with std::unique_ptr you'll want something like + * `std::unique_ptr, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};` + * to remove the pointer. + * * Returns a timezone object on success, and returns NULL and sets `errno` on failure. * * Available since API level 35. @@ -309,6 +319,10 @@ timezone_t _Nullable tzalloc(const char* _Nullable __id) __INTRODUCED_IN(35); /** * tzfree(3) frees a timezone object returned by tzalloc(). * + * To use this with std::unique_ptr you'll want something like + * `std::unique_ptr, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), tzfree};` + * to remove the pointer. + * * Available since API level 35. */ void tzfree(timezone_t _Nullable __tz) __INTRODUCED_IN(35); diff --git a/tests/time_test.cpp b/tests/time_test.cpp index abf6ce08c..ec59aa7fe 100644 --- a/tests/time_test.cpp +++ b/tests/time_test.cpp @@ -1452,3 +1452,12 @@ TEST(time, tzalloc_nullptr) { GTEST_SKIP() << "glibc doesn't have timezone_t"; #endif } + +TEST(time, tzalloc_unique_ptr) { +#if __BIONIC__ + std::unique_ptr, decltype(&tzfree)> tz{tzalloc("Asia/Seoul"), + tzfree}; +#else + GTEST_SKIP() << "glibc doesn't have timezone_t"; +#endif +}