1 /* Time zone functions such as tzalloc and localtime_rz
3 Copyright 2015-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 /* Although this module is not thread-safe, any races should be fairly
21 rare and reasonably benign. For complete thread-safety, use a C
22 library with a working timezone_t type, so that this module is not
30 #if NEED_TIMEZONE_NULL_SUPPORT /* Android API level >= 35 */
33 localtime_rz (timezone_t tz
, time_t const *t
, struct tm
*tm
)
37 return gmtime_r (t
, tm
);
39 return localtime_rz (tz
, t
, tm
);
43 mktime_z (timezone_t tz
, struct tm
*tm
)
49 return mktime_z (tz
, tm
);
59 # include "flexmember.h"
61 # include "time-internal.h"
63 /* The approximate size to use for small allocation requests. This is
64 the largest "small" request for the GNU C library malloc. */
65 enum { DEFAULT_MXFAST
= 64 * sizeof (size_t) / 4 };
67 /* Minimum size of the ABBRS member of struct tm_zone. ABBRS is larger
68 only in the unlikely case where an abbreviation longer than this is
70 enum { ABBR_SIZE_MIN
= DEFAULT_MXFAST
- offsetof (struct tm_zone
, abbrs
) };
72 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
73 includes its trailing null byte). Append an extra null byte to
74 mark the end of ABBRS. */
76 extend_abbrs (char *abbrs
, char const *abbr
, size_t abbr_size
)
78 memcpy (abbrs
, abbr
, abbr_size
);
79 abbrs
[abbr_size
] = '\0';
82 /* Return a newly allocated time zone for NAME, or NULL on failure.
83 A null NAME stands for wall clock time (which is like unset TZ). */
85 tzalloc (char const *name
)
87 size_t name_size
= name
? strlen (name
) + 1 : 0;
88 size_t abbr_size
= name_size
< ABBR_SIZE_MIN
? ABBR_SIZE_MIN
: name_size
+ 1;
89 timezone_t tz
= malloc (FLEXSIZEOF (struct tm_zone
, abbrs
, abbr_size
));
93 tz
->tz_is_set
= !!name
;
96 extend_abbrs (tz
->abbrs
, name
, name_size
);
101 /* If HAVE_STRUCT_TM_TM_ZONE, save into TZ any nontrivial time zone
102 abbreviation used by TM, and update *TM to contain the saved abbreviation.
103 Return true if successful, false (setting errno) otherwise. */
105 save_abbr (timezone_t tz
, struct tm
*tm
)
107 # if HAVE_STRUCT_TM_TM_ZONE
108 char const *zone
= tm
->tm_zone
;
109 char *zone_copy
= (char *) "";
111 /* No need to replace null zones, or zones within the struct tm. */
112 if (!zone
|| ((char *) tm
<= zone
&& zone
< (char *) (tm
+ 1)))
117 zone_copy
= tz
->abbrs
;
119 while (strcmp (zone_copy
, zone
) != 0)
121 if (! (*zone_copy
|| (zone_copy
== tz
->abbrs
&& tz
->tz_is_set
)))
123 idx_t zone_size
= strlen (zone
) + 1;
124 if (zone_size
< tz
->abbrs
+ ABBR_SIZE_MIN
- zone_copy
)
125 extend_abbrs (zone_copy
, zone
, zone_size
);
128 tz
= tz
->next
= tzalloc (zone
);
132 zone_copy
= tz
->abbrs
;
137 zone_copy
+= strlen (zone_copy
) + 1;
138 if (!*zone_copy
&& tz
->next
)
141 zone_copy
= tz
->abbrs
;
146 /* Replace the zone name so that its lifetime matches that of TZ. */
147 tm
->tm_zone
= zone_copy
;
153 /* Free a time zone. */
155 tzfree (timezone_t tz
)
160 timezone_t next
= tz
->next
;
166 /* Get and set the TZ environment variable. These functions can be
167 overridden by programs like Emacs that manage their own environment. */
173 return getenv ("TZ");
179 setenv_TZ (char const *tz
)
181 return tz
? setenv ("TZ", tz
, 1) : unsetenv ("TZ");
185 /* Change the environment to match the specified timezone_t value.
186 Return true if successful, false (setting errno) otherwise. */
188 change_env (timezone_t tz
)
190 if (setenv_TZ (tz
->tz_is_set
? tz
->abbrs
: NULL
) != 0)
196 /* Temporarily set the time zone to TZ, which must not be null.
197 Return LOCAL_TZ if the time zone setting is already correct.
198 Otherwise return a newly allocated time zone representing the old
199 setting, or NULL (setting errno) on failure. */
201 set_tz (timezone_t tz
)
203 char *env_tz
= getenv_TZ ();
205 ? tz
->tz_is_set
&& strcmp (tz
->abbrs
, env_tz
) == 0
210 timezone_t old_tz
= tzalloc (env_tz
);
213 if (! change_env (tz
))
215 int saved_errno
= errno
;
224 /* Restore an old setting returned by set_tz. It must not be null.
225 Return true (preserving errno) if successful, false (setting errno)
228 revert_tz (timezone_t tz
)
234 int saved_errno
= errno
;
235 bool ok
= change_env (tz
);
244 /* Use time zone TZ to compute localtime_r (T, TM). */
246 localtime_rz (timezone_t tz
, time_t const *t
, struct tm
*tm
)
248 # ifdef HAVE_LOCALTIME_INFLOOP_BUG
249 /* The -67768038400665599 comes from:
250 https://lists.gnu.org/r/bug-gnulib/2017-07/msg00142.html
251 On affected platforms the greatest POSIX-compatible time_t value
252 that could return nonnull is 67768036191766798 (when
253 TZ="XXX24:59:59" it resolves to the year 2**31 - 1 + 1900, on
254 12-31 at 23:59:59), so test for that too while we're in the
256 if (! (-67768038400665599 <= *t
&& *t
<= 67768036191766798))
264 return gmtime_r (t
, tm
);
267 timezone_t old_tz
= set_tz (tz
);
270 bool abbr_saved
= localtime_r (t
, tm
) && save_abbr (tz
, tm
);
271 if (revert_tz (old_tz
) && abbr_saved
)
278 /* Use time zone TZ to compute mktime (TM). */
280 mktime_z (timezone_t tz
, struct tm
*tm
)
286 timezone_t old_tz
= set_tz (tz
);
290 tm_1
.tm_sec
= tm
->tm_sec
;
291 tm_1
.tm_min
= tm
->tm_min
;
292 tm_1
.tm_hour
= tm
->tm_hour
;
293 tm_1
.tm_mday
= tm
->tm_mday
;
294 tm_1
.tm_mon
= tm
->tm_mon
;
295 tm_1
.tm_year
= tm
->tm_year
;
297 tm_1
.tm_isdst
= tm
->tm_isdst
;
298 time_t t
= mktime (&tm_1
);
299 bool ok
= 0 <= tm_1
.tm_yday
;
300 ok
= ok
&& save_abbr (tz
, &tm_1
);
301 if (revert_tz (old_tz
) && ok
)