]> code.delx.au - gnu-emacs/blob - lib/time_rz.c
New optional ZONE arg for format-time-string etc.
[gnu-emacs] / lib / time_rz.c
1 /* Time zone functions such as tzalloc and localtime_rz
2
3 Copyright 2015 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Paul Eggert. */
19
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. */
24
25 #include <config.h>
26
27 #include <time.h>
28
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #if !HAVE_TZSET
36 static void tzset (void) { }
37 #endif
38
39 /* A time zone rule. */
40 struct tm_zone
41 {
42 /* More abbreviations, should they be needed. Their TZ_IS_SET
43 members are zero. */
44 timezone_t next;
45
46 /* If nonzero, the rule represents the TZ environment variable set
47 to the first "abbreviation" (this may be the empty string).
48 Otherwise, it represents an unset TZ. */
49 char tz_is_set;
50
51 /* A sequence of null-terminated strings packed next to each other.
52 The strings are followed by an extra null byte. If TZ_IS_SET,
53 there must be at least one string and the first string (which is
54 actually a TZ environment value value) may be empty. Otherwise
55 all strings must be nonempty.
56
57 Abbreviations are stored here because otherwise the values of
58 tm_zone and/or tzname would be dead after changing TZ and calling
59 tzset. Abbreviations never move once allocated, and are live
60 until tzfree is called. */
61 char abbrs[FLEXIBLE_ARRAY_MEMBER];
62 };
63
64 /* The approximate size to use for small allocation requests. This is
65 the largest "small" request for the GNU C library malloc. */
66 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
67
68 /* Minimum size of the ABBRS member of struct abbr. ABBRS is larger
69 only in the unlikely case where an abbreviation longer than this is
70 used. */
71 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
72
73 static char const TZ[] = "TZ";
74
75 /* Magic cookie timezone_t value, for local time. It differs from
76 NULL and from all other timezone_t values. Only the address
77 matters; the pointer is never dereferenced. */
78 static timezone_t const local_tz = (timezone_t) 1;
79
80 #if HAVE_TM_ZONE || HAVE_TZNAME
81
82 /* Return true if the values A and B differ according to the rules for
83 tm_isdst: A and B differ if one is zero and the other positive. */
84 static bool
85 isdst_differ (int a, int b)
86 {
87 return !a != !b && 0 <= a && 0 <= b;
88 }
89
90 /* Return true if A and B are equal. */
91 static int
92 equal_tm (const struct tm *a, const struct tm *b)
93 {
94 return ! ((a->tm_sec ^ b->tm_sec)
95 | (a->tm_min ^ b->tm_min)
96 | (a->tm_hour ^ b->tm_hour)
97 | (a->tm_mday ^ b->tm_mday)
98 | (a->tm_mon ^ b->tm_mon)
99 | (a->tm_year ^ b->tm_year)
100 | isdst_differ (a->tm_isdst, b->tm_isdst));
101 }
102
103 #endif
104
105 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
106 includes its trailing null byte). Append an extra null byte to
107 mark the end of ABBRS. */
108 static void
109 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
110 {
111 memcpy (abbrs, abbr, abbr_size);
112 abbrs[abbr_size] = '\0';
113 }
114
115 /* Return a newly allocated time zone for NAME, or NULL on failure.
116 As a special case, return a nonzero constant for wall clock time, a
117 constant that survives freeing. */
118 timezone_t
119 tzalloc (char const *name)
120 {
121 size_t name_size = name ? strlen (name) + 1 : 0;
122 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
123 timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size);
124 if (tz)
125 {
126 tz->next = NULL;
127 tz->tz_is_set = !!name;
128 extend_abbrs (tz->abbrs, name, name_size);
129 }
130 return tz;
131 }
132
133 #if HAVE_TZNAME
134 /* If TZNAME_ADDRESS is nonnull, an assignment of a saved abbreviation.
135 TZNAME_ADDRESS should be either null, or &tzname[0], or &tzname[1].
136 *TZNAME_ADDRESS = TZNAME_VALUE should be done after revert_tz
137 (indirectly) calls tzset, so that revert_tz can overwrite tzset's
138 assignment to tzname. Also, it should be done at the start of
139 the next localtime_tz or mktime_z, to undo the overwrite. */
140 static char **tzname_address;
141 static char *tzname_value;
142 #endif
143
144 /* Save into TZ any nontrivial time zone abbreviation used by TM,
145 and update *TM (or prepare to update tzname) if they use the abbreviation.
146 Return true if successful, false (setting errno) otherwise. */
147 static bool
148 save_abbr (timezone_t tz, struct tm *tm)
149 {
150 #if HAVE_TM_ZONE || HAVE_TZNAME
151 char const *zone = NULL;
152 char **tzname_zone = NULL;
153 char *zone_copy = (char *) "";
154 # if HAVE_TM_ZONE
155 zone = tm->tm_zone;
156 # endif
157 # if HAVE_TZNAME
158 if (! (zone && *zone) && 0 <= tm->tm_isdst)
159 zone = *(tzname_zone = &tzname[0 < tm->tm_isdst]);
160 # endif
161
162 /* No need to replace null zones, or zones within the struct tm. */
163 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
164 return true;
165
166 if (*zone)
167 {
168 zone_copy = tz->abbrs;
169
170 while (strcmp (zone_copy, zone) != 0)
171 {
172 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
173 {
174 size_t zone_size = strlen (zone) + 1;
175 if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
176 extend_abbrs (zone_copy, zone, zone_size);
177 else
178 {
179 tz = tz->next = tzalloc (zone);
180 if (!tz)
181 return false;
182 tz->tz_is_set = 0;
183 zone_copy = tz->abbrs;
184 }
185 break;
186 }
187
188 zone_copy += strlen (zone_copy) + 1;
189 if (!*zone_copy && tz->next)
190 {
191 tz = tz->next;
192 zone_copy = tz->abbrs;
193 }
194 }
195 }
196
197 /* Replace the zone name so that its lifetime matches that of TZ. */
198 # if HAVE_TM_ZONE
199 if (!tzname_zone)
200 tm->tm_zone = zone_copy;
201 # endif
202 # if HAVE_TZNAME
203 tzname_address = tzname_zone;
204 tzname_value = zone_copy;
205 # endif
206 #endif
207 return true;
208 }
209
210 /* Free a time zone. */
211 void
212 tzfree (timezone_t tz)
213 {
214 if (tz != local_tz)
215 while (tz)
216 {
217 timezone_t next = tz->next;
218 free (tz);
219 tz = next;
220 }
221 }
222
223 /* Get and set the TZ environment variable. These functions can be
224 overridden by programs like Emacs that manage their own environment. */
225
226 #ifndef getenv_TZ
227 static char *
228 getenv_TZ (void)
229 {
230 return getenv (TZ);
231 }
232 #endif
233
234 #ifndef setenv_TZ
235 static int
236 setenv_TZ (char const *tz)
237 {
238 return tz ? setenv (TZ, tz, 1) : unsetenv (TZ);
239 }
240 #endif
241
242 /* Change the environment to match the specified timezone_t value.
243 Return true if successful, false (setting errno) otherwise. */
244 static bool
245 change_env (timezone_t tz)
246 {
247 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
248 return false;
249 tzset ();
250 return true;
251 }
252
253 /* Temporarily set the time zone to TZ, which must not be null.
254 Return LOCAL_TZ if the time zone setting is already correct.
255 Otherwise return a newly allocated time zone representing the old
256 setting, or NULL (setting errno) on failure. */
257 static timezone_t
258 set_tz (timezone_t tz)
259 {
260 char *env_tz = getenv_TZ ();
261 if (env_tz
262 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
263 : !tz->tz_is_set)
264 return local_tz;
265 else
266 {
267 timezone_t old_tz = tzalloc (env_tz);
268 if (!old_tz)
269 return old_tz;
270 if (! change_env (tz))
271 {
272 int saved_errno = errno;
273 tzfree (old_tz);
274 errno = saved_errno;
275 return NULL;
276 }
277 return old_tz;
278 }
279 }
280
281 /* Restore an old setting returned by set_tz. It must not be null.
282 Return true (preserving errno) if successful, false (setting errno)
283 otherwise. */
284 static bool
285 revert_tz (timezone_t tz)
286 {
287 if (tz == local_tz)
288 return true;
289 else
290 {
291 int saved_errno = errno;
292 bool ok = change_env (tz);
293 if (!ok)
294 saved_errno = errno;
295 #if HAVE_TZNAME
296 if (!ok)
297 tzname_address = NULL;
298 if (tzname_address)
299 {
300 char *old_value = *tzname_address;
301 *tzname_address = tzname_value;
302 tzname_value = old_value;
303 }
304 #endif
305 tzfree (tz);
306 errno = saved_errno;
307 return ok;
308 }
309 }
310
311 /* Restore an old tzname setting that was temporarily munged by revert_tz. */
312 static void
313 restore_tzname (void)
314 {
315 #if HAVE_TZNAME
316 if (tzname_address)
317 {
318 *tzname_address = tzname_value;
319 tzname_address = NULL;
320 }
321 #endif
322 }
323
324 /* Use time zone TZ to compute localtime_r (T, TM). */
325 struct tm *
326 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
327 {
328 restore_tzname ();
329
330 if (!tz)
331 return gmtime_r (t, tm);
332 else
333 {
334 timezone_t old_tz = set_tz (tz);
335 if (old_tz)
336 {
337 tm = localtime_r (t, tm);
338 if (tm && !save_abbr (tz, tm))
339 tm = NULL;
340 if (revert_tz (old_tz))
341 return tm;
342 }
343 return NULL;
344 }
345 }
346
347 /* Use time zone TZ to compute mktime (TM). */
348 time_t
349 mktime_z (timezone_t tz, struct tm *tm)
350 {
351 restore_tzname ();
352
353 if (!tz)
354 return timegm (tm);
355 else
356 {
357 timezone_t old_tz = set_tz (tz);
358 if (old_tz)
359 {
360 time_t t = mktime (tm);
361 #if HAVE_TM_ZONE || HAVE_TZNAME
362 time_t badtime = -1;
363 struct tm tm_1;
364 if ((t != badtime
365 || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1)))
366 && !save_abbr (tz, tm))
367 t = badtime;
368 #endif
369 if (revert_tz (old_tz))
370 return t;
371 }
372 return -1;
373 }
374 }