stdalign: Fix test failures on 32-bit platforms with clang versions < 8.
[gnulib.git] / lib / time_rz.c
blobc58e6831bf400549d23b4354e6298daf8a6b99f5
1 /* Time zone functions such as tzalloc and localtime_rz
3 Copyright 2015-2020 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 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
23 needed. */
25 #include <config.h>
27 #include <time.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdbool.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "flexmember.h"
37 #include "time-internal.h"
39 #ifndef SIZE_MAX
40 # define SIZE_MAX ((size_t) -1)
41 #endif
43 /* The approximate size to use for small allocation requests. This is
44 the largest "small" request for the GNU C library malloc. */
45 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
47 /* Minimum size of the ABBRS member of struct tm_zone. ABBRS is larger
48 only in the unlikely case where an abbreviation longer than this is
49 used. */
50 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
52 /* Magic cookie timezone_t value, for local time. It differs from
53 NULL and from all other timezone_t values. Only the address
54 matters; the pointer is never dereferenced. */
55 static timezone_t const local_tz = (timezone_t) 1;
57 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
58 includes its trailing null byte). Append an extra null byte to
59 mark the end of ABBRS. */
60 static void
61 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
63 memcpy (abbrs, abbr, abbr_size);
64 abbrs[abbr_size] = '\0';
67 /* Return a newly allocated time zone for NAME, or NULL on failure.
68 A null NAME stands for wall clock time (which is like unset TZ). */
69 timezone_t
70 tzalloc (char const *name)
72 size_t name_size = name ? strlen (name) + 1 : 0;
73 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
74 timezone_t tz = malloc (FLEXSIZEOF (struct tm_zone, abbrs, abbr_size));
75 if (tz)
77 tz->next = NULL;
78 #if HAVE_TZNAME && !HAVE_TM_ZONE
79 tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
80 #endif
81 tz->tz_is_set = !!name;
82 tz->abbrs[0] = '\0';
83 if (name)
84 extend_abbrs (tz->abbrs, name, name_size);
86 return tz;
89 /* Save into TZ any nontrivial time zone abbreviation used by TM, and
90 update *TM (if HAVE_TM_ZONE) or *TZ (if !HAVE_TM_ZONE &&
91 HAVE_TZNAME) if they use the abbreviation. Return true if
92 successful, false (setting errno) otherwise. */
93 static bool
94 save_abbr (timezone_t tz, struct tm *tm)
96 #if HAVE_TM_ZONE || HAVE_TZNAME
97 char const *zone = NULL;
98 char *zone_copy = (char *) "";
100 # if HAVE_TZNAME
101 int tzname_index = -1;
102 # endif
104 # if HAVE_TM_ZONE
105 zone = tm->tm_zone;
106 # endif
108 # if HAVE_TZNAME
109 if (! (zone && *zone) && 0 <= tm->tm_isdst)
111 tzname_index = tm->tm_isdst != 0;
112 zone = tzname[tzname_index];
114 # endif
116 /* No need to replace null zones, or zones within the struct tm. */
117 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
118 return true;
120 if (*zone)
122 zone_copy = tz->abbrs;
124 while (strcmp (zone_copy, zone) != 0)
126 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
128 size_t zone_size = strlen (zone) + 1;
129 size_t zone_used = zone_copy - tz->abbrs;
130 if (SIZE_MAX - zone_used < zone_size)
132 errno = ENOMEM;
133 return false;
135 if (zone_used + zone_size < ABBR_SIZE_MIN)
136 extend_abbrs (zone_copy, zone, zone_size);
137 else
139 tz = tz->next = tzalloc (zone);
140 if (!tz)
141 return false;
142 tz->tz_is_set = 0;
143 zone_copy = tz->abbrs;
145 break;
148 zone_copy += strlen (zone_copy) + 1;
149 if (!*zone_copy && tz->next)
151 tz = tz->next;
152 zone_copy = tz->abbrs;
157 /* Replace the zone name so that its lifetime matches that of TZ. */
158 # if HAVE_TM_ZONE
159 tm->tm_zone = zone_copy;
160 # else
161 if (0 <= tzname_index)
162 tz->tzname_copy[tzname_index] = zone_copy;
163 # endif
164 #endif
166 return true;
169 /* Free a time zone. */
170 void
171 tzfree (timezone_t tz)
173 if (tz != local_tz)
174 while (tz)
176 timezone_t next = tz->next;
177 free (tz);
178 tz = next;
182 /* Get and set the TZ environment variable. These functions can be
183 overridden by programs like Emacs that manage their own environment. */
185 #ifndef getenv_TZ
186 static char *
187 getenv_TZ (void)
189 return getenv ("TZ");
191 #endif
193 #ifndef setenv_TZ
194 static int
195 setenv_TZ (char const *tz)
197 return tz ? setenv ("TZ", tz, 1) : unsetenv ("TZ");
199 #endif
201 /* Change the environment to match the specified timezone_t value.
202 Return true if successful, false (setting errno) otherwise. */
203 static bool
204 change_env (timezone_t tz)
206 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
207 return false;
208 tzset ();
209 return true;
212 /* Temporarily set the time zone to TZ, which must not be null.
213 Return LOCAL_TZ if the time zone setting is already correct.
214 Otherwise return a newly allocated time zone representing the old
215 setting, or NULL (setting errno) on failure. */
216 static timezone_t
217 set_tz (timezone_t tz)
219 char *env_tz = getenv_TZ ();
220 if (env_tz
221 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
222 : !tz->tz_is_set)
223 return local_tz;
224 else
226 timezone_t old_tz = tzalloc (env_tz);
227 if (!old_tz)
228 return old_tz;
229 if (! change_env (tz))
231 int saved_errno = errno;
232 tzfree (old_tz);
233 errno = saved_errno;
234 return NULL;
236 return old_tz;
240 /* Restore an old setting returned by set_tz. It must not be null.
241 Return true (preserving errno) if successful, false (setting errno)
242 otherwise. */
243 static bool
244 revert_tz (timezone_t tz)
246 if (tz == local_tz)
247 return true;
248 else
250 int saved_errno = errno;
251 bool ok = change_env (tz);
252 if (!ok)
253 saved_errno = errno;
254 tzfree (tz);
255 errno = saved_errno;
256 return ok;
260 /* Use time zone TZ to compute localtime_r (T, TM). */
261 struct tm *
262 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
264 #ifdef HAVE_LOCALTIME_INFLOOP_BUG
265 /* The -67768038400665599 comes from:
266 https://lists.gnu.org/r/bug-gnulib/2017-07/msg00142.html
267 On affected platforms the greatest POSIX-compatible time_t value
268 that could return nonnull is 67768036191766798 (when
269 TZ="XXX24:59:59" it resolves to the year 2**31 - 1 + 1900, on
270 12-31 at 23:59:59), so test for that too while we're in the
271 neighborhood. */
272 if (! (-67768038400665599 <= *t && *t <= 67768036191766798))
274 errno = EOVERFLOW;
275 return NULL;
277 #endif
279 if (!tz)
280 return gmtime_r (t, tm);
281 else
283 timezone_t old_tz = set_tz (tz);
284 if (old_tz)
286 bool abbr_saved = localtime_r (t, tm) && save_abbr (tz, tm);
287 if (revert_tz (old_tz) && abbr_saved)
288 return tm;
290 return NULL;
294 /* Use time zone TZ to compute mktime (TM). */
295 time_t
296 mktime_z (timezone_t tz, struct tm *tm)
298 if (!tz)
299 return timegm (tm);
300 else
302 timezone_t old_tz = set_tz (tz);
303 if (old_tz)
305 struct tm tm_1;
306 tm_1.tm_sec = tm->tm_sec;
307 tm_1.tm_min = tm->tm_min;
308 tm_1.tm_hour = tm->tm_hour;
309 tm_1.tm_mday = tm->tm_mday;
310 tm_1.tm_mon = tm->tm_mon;
311 tm_1.tm_year = tm->tm_year;
312 tm_1.tm_yday = -1;
313 tm_1.tm_isdst = tm->tm_isdst;
314 time_t t = mktime (&tm_1);
315 bool ok = 0 <= tm_1.tm_yday;
316 #if HAVE_TM_ZONE || HAVE_TZNAME
317 ok = ok && save_abbr (tz, &tm_1);
318 #endif
319 if (revert_tz (old_tz) && ok)
321 *tm = tm_1;
322 return t;
325 return -1;