]> code.delx.au - gnu-emacs/blob - lib/time_rz.c
Remove now-inaccurate bytecode comments
[gnu-emacs] / lib / time_rz.c
1 /* Time zone functions such as tzalloc and localtime_rz
2
3 Copyright 2015-2016 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 #include "time-internal.h"
36
37 #if !HAVE_TZSET
38 static void tzset (void) { }
39 #endif
40
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 };
44
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) };
49
50 /* Magic cookie timezone_t value, for local time. It differs from
51 NULL and from all other timezone_t values. Only the address
52 matters; the pointer is never dereferenced. */
53 static timezone_t const local_tz = (timezone_t) 1;
54
55 #if HAVE_TM_ZONE || HAVE_TZNAME
56
57 /* Return true if the values A and B differ according to the rules for
58 tm_isdst: A and B differ if one is zero and the other positive. */
59 static bool
60 isdst_differ (int a, int b)
61 {
62 return !a != !b && 0 <= a && 0 <= b;
63 }
64
65 /* Return true if A and B are equal. */
66 static int
67 equal_tm (const struct tm *a, const struct tm *b)
68 {
69 return ! ((a->tm_sec ^ b->tm_sec)
70 | (a->tm_min ^ b->tm_min)
71 | (a->tm_hour ^ b->tm_hour)
72 | (a->tm_mday ^ b->tm_mday)
73 | (a->tm_mon ^ b->tm_mon)
74 | (a->tm_year ^ b->tm_year)
75 | isdst_differ (a->tm_isdst, b->tm_isdst));
76 }
77
78 #endif
79
80 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
81 includes its trailing null byte). Append an extra null byte to
82 mark the end of ABBRS. */
83 static void
84 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
85 {
86 memcpy (abbrs, abbr, abbr_size);
87 abbrs[abbr_size] = '\0';
88 }
89
90 /* Return a newly allocated time zone for NAME, or NULL on failure.
91 A null NAME stands for wall clock time (which is like unset TZ). */
92 timezone_t
93 tzalloc (char const *name)
94 {
95 size_t name_size = name ? strlen (name) + 1 : 0;
96 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
97 timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size);
98 if (tz)
99 {
100 tz->next = NULL;
101 #if HAVE_TZNAME && !HAVE_TM_ZONE
102 tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
103 #endif
104 tz->tz_is_set = !!name;
105 tz->abbrs[0] = '\0';
106 if (name)
107 extend_abbrs (tz->abbrs, name, name_size);
108 }
109 return tz;
110 }
111
112 /* Save into TZ any nontrivial time zone abbreviation used by TM, and
113 update *TM (if HAVE_TM_ZONE) or *TZ (if !HAVE_TM_ZONE &&
114 HAVE_TZNAME) if they use the abbreviation. Return true if
115 successful, false (setting errno) otherwise. */
116 static bool
117 save_abbr (timezone_t tz, struct tm *tm)
118 {
119 #if HAVE_TM_ZONE || HAVE_TZNAME
120 char const *zone = NULL;
121 char *zone_copy = (char *) "";
122
123 # if HAVE_TZNAME
124 int tzname_index = -1;
125 # endif
126
127 # if HAVE_TM_ZONE
128 zone = tm->tm_zone;
129 # endif
130
131 # if HAVE_TZNAME
132 if (! (zone && *zone) && 0 <= tm->tm_isdst)
133 {
134 tzname_index = tm->tm_isdst != 0;
135 zone = tzname[tzname_index];
136 }
137 # endif
138
139 /* No need to replace null zones, or zones within the struct tm. */
140 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
141 return true;
142
143 if (*zone)
144 {
145 zone_copy = tz->abbrs;
146
147 while (strcmp (zone_copy, zone) != 0)
148 {
149 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
150 {
151 size_t zone_size = strlen (zone) + 1;
152 if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
153 extend_abbrs (zone_copy, zone, zone_size);
154 else
155 {
156 tz = tz->next = tzalloc (zone);
157 if (!tz)
158 return false;
159 tz->tz_is_set = 0;
160 zone_copy = tz->abbrs;
161 }
162 break;
163 }
164
165 zone_copy += strlen (zone_copy) + 1;
166 if (!*zone_copy && tz->next)
167 {
168 tz = tz->next;
169 zone_copy = tz->abbrs;
170 }
171 }
172 }
173
174 /* Replace the zone name so that its lifetime matches that of TZ. */
175 # if HAVE_TM_ZONE
176 tm->tm_zone = zone_copy;
177 # else
178 if (0 <= tzname_index)
179 tz->tzname_copy[tzname_index] = zone_copy;
180 # endif
181 #endif
182
183 return true;
184 }
185
186 /* Free a time zone. */
187 void
188 tzfree (timezone_t tz)
189 {
190 if (tz != local_tz)
191 while (tz)
192 {
193 timezone_t next = tz->next;
194 free (tz);
195 tz = next;
196 }
197 }
198
199 /* Get and set the TZ environment variable. These functions can be
200 overridden by programs like Emacs that manage their own environment. */
201
202 #ifndef getenv_TZ
203 static char *
204 getenv_TZ (void)
205 {
206 return getenv ("TZ");
207 }
208 #endif
209
210 #ifndef setenv_TZ
211 static int
212 setenv_TZ (char const *tz)
213 {
214 return tz ? setenv ("TZ", tz, 1) : unsetenv ("TZ");
215 }
216 #endif
217
218 /* Change the environment to match the specified timezone_t value.
219 Return true if successful, false (setting errno) otherwise. */
220 static bool
221 change_env (timezone_t tz)
222 {
223 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
224 return false;
225 tzset ();
226 return true;
227 }
228
229 /* Temporarily set the time zone to TZ, which must not be null.
230 Return LOCAL_TZ if the time zone setting is already correct.
231 Otherwise return a newly allocated time zone representing the old
232 setting, or NULL (setting errno) on failure. */
233 static timezone_t
234 set_tz (timezone_t tz)
235 {
236 char *env_tz = getenv_TZ ();
237 if (env_tz
238 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
239 : !tz->tz_is_set)
240 return local_tz;
241 else
242 {
243 timezone_t old_tz = tzalloc (env_tz);
244 if (!old_tz)
245 return old_tz;
246 if (! change_env (tz))
247 {
248 int saved_errno = errno;
249 tzfree (old_tz);
250 errno = saved_errno;
251 return NULL;
252 }
253 return old_tz;
254 }
255 }
256
257 /* Restore an old setting returned by set_tz. It must not be null.
258 Return true (preserving errno) if successful, false (setting errno)
259 otherwise. */
260 static bool
261 revert_tz (timezone_t tz)
262 {
263 if (tz == local_tz)
264 return true;
265 else
266 {
267 int saved_errno = errno;
268 bool ok = change_env (tz);
269 if (!ok)
270 saved_errno = errno;
271 tzfree (tz);
272 errno = saved_errno;
273 return ok;
274 }
275 }
276
277 /* Use time zone TZ to compute localtime_r (T, TM). */
278 struct tm *
279 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
280 {
281 if (!tz)
282 return gmtime_r (t, tm);
283 else
284 {
285 timezone_t old_tz = set_tz (tz);
286 if (old_tz)
287 {
288 bool abbr_saved = localtime_r (t, tm) && save_abbr (tz, tm);
289 if (revert_tz (old_tz) && abbr_saved)
290 return tm;
291 }
292 return NULL;
293 }
294 }
295
296 /* Use time zone TZ to compute mktime (TM). */
297 time_t
298 mktime_z (timezone_t tz, struct tm *tm)
299 {
300 if (!tz)
301 return timegm (tm);
302 else
303 {
304 timezone_t old_tz = set_tz (tz);
305 if (old_tz)
306 {
307 time_t t = mktime (tm);
308 #if HAVE_TM_ZONE || HAVE_TZNAME
309 time_t badtime = -1;
310 struct tm tm_1;
311 if ((t != badtime
312 || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1)))
313 && !save_abbr (tz, tm))
314 t = badtime;
315 #endif
316 if (revert_tz (old_tz))
317 return t;
318 }
319 return -1;
320 }
321 }