Merge branch 'release-process-lowercase'
[emacs.git] / lib / time_rz.c
bloba89b7e70f1408fdb6fd1e8b4125b3c5f395ff919
1 /* Time zone functions such as tzalloc and localtime_rz
3 Copyright 2015 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 3, 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 <http://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 <stdbool.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "time-internal.h"
37 #if !HAVE_TZSET
38 static void tzset (void) { }
39 #endif
41 /* The approximate size to use for small allocation requests. This is
42 the largest "small" request for the GNU C library malloc. */
43 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
45 /* Minimum size of the ABBRS member of struct abbr. ABBRS is larger
46 only in the unlikely case where an abbreviation longer than this is
47 used. */
48 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
50 static char const TZ[] = "TZ";
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 #if HAVE_TM_ZONE || HAVE_TZNAME
59 /* Return true if the values A and B differ according to the rules for
60 tm_isdst: A and B differ if one is zero and the other positive. */
61 static bool
62 isdst_differ (int a, int b)
64 return !a != !b && 0 <= a && 0 <= b;
67 /* Return true if A and B are equal. */
68 static int
69 equal_tm (const struct tm *a, const struct tm *b)
71 return ! ((a->tm_sec ^ b->tm_sec)
72 | (a->tm_min ^ b->tm_min)
73 | (a->tm_hour ^ b->tm_hour)
74 | (a->tm_mday ^ b->tm_mday)
75 | (a->tm_mon ^ b->tm_mon)
76 | (a->tm_year ^ b->tm_year)
77 | isdst_differ (a->tm_isdst, b->tm_isdst));
80 #endif
82 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
83 includes its trailing null byte). Append an extra null byte to
84 mark the end of ABBRS. */
85 static void
86 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
88 memcpy (abbrs, abbr, abbr_size);
89 abbrs[abbr_size] = '\0';
92 /* Return a newly allocated time zone for NAME, or NULL on failure.
93 A null NAME stands for wall clock time (which is like unset TZ). */
94 timezone_t
95 tzalloc (char const *name)
97 size_t name_size = name ? strlen (name) + 1 : 0;
98 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
99 timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size);
100 if (tz)
102 tz->next = NULL;
103 #if HAVE_TZNAME && !HAVE_TM_ZONE
104 tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
105 #endif
106 tz->tz_is_set = !!name;
107 tz->abbrs[0] = '\0';
108 if (name)
109 extend_abbrs (tz->abbrs, name, name_size);
111 return tz;
114 /* Save into TZ any nontrivial time zone abbreviation used by TM, and
115 update *TM (if HAVE_TM_ZONE) or *TZ (if !HAVE_TM_ZONE &&
116 HAVE_TZNAME) if they use the abbreviation. Return true if
117 successful, false (setting errno) otherwise. */
118 static bool
119 save_abbr (timezone_t tz, struct tm *tm)
121 #if HAVE_TM_ZONE || HAVE_TZNAME
122 char const *zone = NULL;
123 char *zone_copy = (char *) "";
125 # if HAVE_TZNAME
126 int tzname_index = -1;
127 # endif
129 # if HAVE_TM_ZONE
130 zone = tm->tm_zone;
131 # endif
133 # if HAVE_TZNAME
134 if (! (zone && *zone) && 0 <= tm->tm_isdst)
136 tzname_index = tm->tm_isdst != 0;
137 zone = tzname[tzname_index];
139 # endif
141 /* No need to replace null zones, or zones within the struct tm. */
142 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
143 return true;
145 if (*zone)
147 zone_copy = tz->abbrs;
149 while (strcmp (zone_copy, zone) != 0)
151 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
153 size_t zone_size = strlen (zone) + 1;
154 if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
155 extend_abbrs (zone_copy, zone, zone_size);
156 else
158 tz = tz->next = tzalloc (zone);
159 if (!tz)
160 return false;
161 tz->tz_is_set = 0;
162 zone_copy = tz->abbrs;
164 break;
167 zone_copy += strlen (zone_copy) + 1;
168 if (!*zone_copy && tz->next)
170 tz = tz->next;
171 zone_copy = tz->abbrs;
176 /* Replace the zone name so that its lifetime matches that of TZ. */
177 # if HAVE_TM_ZONE
178 tm->tm_zone = zone_copy;
179 # else
180 if (0 <= tzname_index)
181 tz->tzname_copy[tzname_index] = zone_copy;
182 # endif
183 #endif
185 return true;
188 /* Free a time zone. */
189 void
190 tzfree (timezone_t tz)
192 if (tz != local_tz)
193 while (tz)
195 timezone_t next = tz->next;
196 free (tz);
197 tz = next;
201 /* Get and set the TZ environment variable. These functions can be
202 overridden by programs like Emacs that manage their own environment. */
204 #ifndef getenv_TZ
205 static char *
206 getenv_TZ (void)
208 return getenv (TZ);
210 #endif
212 #ifndef setenv_TZ
213 static int
214 setenv_TZ (char const *tz)
216 return tz ? setenv (TZ, tz, 1) : unsetenv (TZ);
218 #endif
220 /* Change the environment to match the specified timezone_t value.
221 Return true if successful, false (setting errno) otherwise. */
222 static bool
223 change_env (timezone_t tz)
225 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
226 return false;
227 tzset ();
228 return true;
231 /* Temporarily set the time zone to TZ, which must not be null.
232 Return LOCAL_TZ if the time zone setting is already correct.
233 Otherwise return a newly allocated time zone representing the old
234 setting, or NULL (setting errno) on failure. */
235 static timezone_t
236 set_tz (timezone_t tz)
238 char *env_tz = getenv_TZ ();
239 if (env_tz
240 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
241 : !tz->tz_is_set)
242 return local_tz;
243 else
245 timezone_t old_tz = tzalloc (env_tz);
246 if (!old_tz)
247 return old_tz;
248 if (! change_env (tz))
250 int saved_errno = errno;
251 tzfree (old_tz);
252 errno = saved_errno;
253 return NULL;
255 return old_tz;
259 /* Restore an old setting returned by set_tz. It must not be null.
260 Return true (preserving errno) if successful, false (setting errno)
261 otherwise. */
262 static bool
263 revert_tz (timezone_t tz)
265 if (tz == local_tz)
266 return true;
267 else
269 int saved_errno = errno;
270 bool ok = change_env (tz);
271 if (!ok)
272 saved_errno = errno;
273 tzfree (tz);
274 errno = saved_errno;
275 return ok;
279 /* Use time zone TZ to compute localtime_r (T, TM). */
280 struct tm *
281 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
283 if (!tz)
284 return gmtime_r (t, tm);
285 else
287 timezone_t old_tz = set_tz (tz);
288 if (old_tz)
290 bool abbr_saved = localtime_r (t, tm) && save_abbr (tz, tm);
291 if (revert_tz (old_tz) && abbr_saved)
292 return tm;
294 return NULL;
298 /* Use time zone TZ to compute mktime (TM). */
299 time_t
300 mktime_z (timezone_t tz, struct tm *tm)
302 if (!tz)
303 return timegm (tm);
304 else
306 timezone_t old_tz = set_tz (tz);
307 if (old_tz)
309 time_t t = mktime (tm);
310 #if HAVE_TM_ZONE || HAVE_TZNAME
311 time_t badtime = -1;
312 struct tm tm_1;
313 if ((t != badtime
314 || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1)))
315 && !save_abbr (tz, tm))
316 t = badtime;
317 #endif
318 if (revert_tz (old_tz))
319 return t;
321 return -1;