exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / localtime.c
blobf0e91ac6477219b1d0fb3783711aae62fd7dd906
1 /* Work around platform bugs in localtime.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <time.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #if defined _WIN32 && ! defined __CYGWIN__
25 # include <wchar.h>
26 #endif
28 #undef localtime
30 struct tm *
31 rpl_localtime (const time_t *tp)
33 #if defined _WIN32 && ! defined __CYGWIN__
34 /* Rectify the value of the environment variable TZ.
35 There are four possible kinds of such values:
36 - Traditional US time zone names, e.g. "PST8PDT". Syntax: see
37 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>
38 - Time zone names based on geography, that contain one or more
39 slashes, e.g. "Europe/Moscow".
40 - Time zone names based on geography, without slashes, e.g.
41 "Singapore".
42 - Time zone names that contain explicit DST rules. Syntax: see
43 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>
44 The Microsoft CRT understands only the first kind. It produces incorrect
45 results if the value of TZ is of the other kinds.
46 But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value
47 of the second kind for most geographies, or of the first kind in a few
48 other geographies. If it is of the second kind, neutralize it. For the
49 Microsoft CRT, an absent or empty TZ means the time zone that the user
50 has set in the Windows Control Panel.
51 If the value of TZ is of the third or fourth kind -- Cygwin programs
52 understand these syntaxes as well --, it does not matter whether we
53 neutralize it or not, since these values occur only when a Cygwin user
54 has set TZ explicitly; this case is 1. rare and 2. under the user's
55 responsibility. */
56 const char *tz = getenv ("TZ");
57 if (tz != NULL && strchr (tz, '/') != NULL)
59 /* Neutralize it, in a way that is multithread-safe.
60 (If we were to use _putenv ("TZ="), it would free the memory allocated
61 for the environment variable "TZ", and thus other threads that are
62 using the previously fetched value of getenv ("TZ") could crash.) */
63 char **env = _environ;
64 wchar_t **wenv = _wenviron;
65 if (env != NULL)
66 for (char *s = env; *s != NULL; s++)
67 if (s[0] == 'T' && s[1] == 'Z' && s[2] == '=')
68 s[0] = '$';
69 if (wenv != NULL)
70 for (wchar_t *ws = wenv; *ws != NULL; ws++)
71 if (ws[0] == L'T' && ws[1] == L'Z' && ws[2] == L'=')
72 ws[0] = L'$';
74 #endif
76 return localtime (tp);