From 43cd776d7dc8ac573262f8459edeb1c1f5f3cd09 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 7 Mar 2024 16:18:32 -0500 Subject: xtime: Call tzset() from main() instead of lazily POSIX specifies[1] that If a thread accesses tzname, daylight, or timezone directly while another thread is in a call to tzset(), or to any function that is required or allowed to set timezone information as if by calling tzset(), the behavior is undefined. So calling it lazily from arbitrary threads is risky. [1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tzset.html --- tests/main.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tests/main.c') diff --git a/tests/main.c b/tests/main.c index e69fe35..38438b2 100644 --- a/tests/main.c +++ b/tests/main.c @@ -11,9 +11,11 @@ #include "../src/config.h" #include "../src/diag.h" #include +#include #include #include #include +#include /** * Test context. @@ -90,6 +92,18 @@ static void run_test(struct test_ctx *ctx, const char *test, test_fn *fn) { } int main(int argc, char *argv[]) { + // Try to set a UTF-8 locale + if (!setlocale(LC_ALL, "C.UTF-8")) { + setlocale(LC_ALL, ""); + } + + // Run tests in UTC + if (setenv("TZ", "UTC0", true) != 0) { + perror("setenv()"); + return EXIT_FAILURE; + } + tzset(); + struct test_ctx ctx; if (test_init(&ctx, argc, argv) != 0) { goto done; -- cgit v1.2.3