]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/tzset.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / time / tzset.c
CommitLineData
688903eb 1/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
f65fd747 2 This file is part of the GNU C Library.
28f540f4 3
f65fd747 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
f65fd747
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
28f540f4 18#include <ctype.h>
9a0a462c 19#include <errno.h>
ec999b8e 20#include <libc-lock.h>
42261ad7 21#include <stdbool.h>
28f540f4
RM
22#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <time.h>
27
14ea22e9 28#include <timezone/tzfile.h>
28f540f4 29
92bd70fb
JM
30#define SECSPERDAY 86400
31
28f540f4
RM
32char *__tzname[2] = { (char *) "GMT", (char *) "GMT" };
33int __daylight = 0;
34long int __timezone = 0L;
35
0ac2e7d8
BK
36weak_alias (__tzname, tzname)
37weak_alias (__daylight, daylight)
38weak_alias (__timezone, timezone)
39
9a0a462c 40/* This locks all the state variables in tzfile.c and this file. */
727211c4 41__libc_lock_define_initialized (static, tzset_lock)
9a0a462c 42
28f540f4
RM
43/* This structure contains all the information about a
44 timezone given in the POSIX standard TZ envariable. */
45typedef struct
46 {
cd6ede75 47 const char *name;
28f540f4
RM
48
49 /* When to change. */
50 enum { J0, J1, M } type; /* Interpretation of: */
51 unsigned short int m, n, d; /* Month, week, day. */
0748546f 52 int secs; /* Time of day. */
28f540f4
RM
53
54 long int offset; /* Seconds east of GMT (west if < 0). */
55
56 /* We cache the computed time of change for a
57 given year so we don't have to recompute it. */
58 time_t change; /* When to change to this zone. */
59 int computed_for; /* Year above is computed for. */
60 } tz_rule;
61
62/* tz_rules[0] is standard, tz_rules[1] is daylight. */
63static tz_rule tz_rules[2];
f65fd747
UD
64
65
7e01f080 66static void compute_change (tz_rule *rule, int year) __THROW;
8492c4dd 67static void tzset_internal (int always);
28f540f4 68\f
2698e32c
UD
69/* List of buffers containing time zone strings. */
70struct tzstring_l
cd6ede75 71{
2698e32c
UD
72 struct tzstring_l *next;
73 size_t len; /* strlen(data) - doesn't count terminating NUL! */
74 char data[0];
cd6ede75
UD
75};
76
418f1701 77static struct tzstring_l *tzstring_list;
cd6ede75 78
42261ad7
FW
79/* Allocate a permanent home for the first LEN characters of S. It
80 will never be moved or deallocated, but may share space with other
81 strings. Don't modify the returned string. */
82static char *
83__tzstring_len (const char *s, size_t len)
cd6ede75 84{
cd6ede75 85 char *p;
2698e32c 86 struct tzstring_l *t, *u, *new;
2698e32c
UD
87
88 /* Walk the list and look for a match. If this string is the same
89 as the end of an already-allocated string, it can share space. */
90 for (u = t = tzstring_list; t; u = t, t = t->next)
91 if (len <= t->len)
92 {
93 p = &t->data[t->len - len];
42261ad7 94 if (memcmp (s, p, len) == 0)
cd6ede75 95 return p;
2698e32c
UD
96 }
97
98 /* Not found; allocate a new buffer. */
99 new = malloc (sizeof (struct tzstring_l) + len + 1);
100 if (!new)
101 return NULL;
102
103 new->next = NULL;
104 new->len = len;
42261ad7
FW
105 memcpy (new->data, s, len);
106 new->data[len] = '\0';
2698e32c
UD
107
108 if (u)
109 u->next = new;
110 else
111 tzstring_list = new;
112
113 return new->data;
cd6ede75 114}
42261ad7
FW
115
116/* Allocate a permanent home for S. It will never be moved or
117 deallocated, but may share space with other strings. Don't modify
118 the returned string. */
119char *
120__tzstring (const char *s)
121{
122 return __tzstring_len (s, strlen (s));
123}
a3931cbe 124
c4563d2d 125static char *old_tz;
28f540f4 126
9a0a462c 127static void
fa76dde2
UD
128update_vars (void)
129{
130 __daylight = tz_rules[0].offset != tz_rules[1].offset;
131 __timezone = -tz_rules[0].offset;
132 __tzname[0] = (char *) tz_rules[0].name;
133 __tzname[1] = (char *) tz_rules[1].name;
fa76dde2
UD
134}
135
bd82a247
UD
136
137static unsigned int
bd82a247
UD
138compute_offset (unsigned int ss, unsigned int mm, unsigned int hh)
139{
6e3b5229
FW
140 if (ss > 59)
141 ss = 59;
142 if (mm > 59)
143 mm = 59;
144 if (hh > 24)
145 hh = 24;
146 return ss + mm * 60 + hh * 60 * 60;
bd82a247
UD
147}
148
42261ad7
FW
149/* Parses the time zone name at *TZP, and writes a pointer to an
150 interned string to tz_rules[WHICHRULE].name. On success, advances
151 *TZP, and returns true. Returns false otherwise. */
152static bool
153parse_tzname (const char **tzp, int whichrule)
28f540f4 154{
42261ad7
FW
155 const char *start = *tzp;
156 const char *p = start;
157 while (('a' <= *p && *p <= 'z')
158 || ('A' <= *p && *p <= 'Z'))
159 ++p;
160 size_t len = p - start;
161 if (len < 3)
82780cbe 162 {
42261ad7
FW
163 p = *tzp;
164 if (__glibc_unlikely (*p++ != '<'))
165 return false;
166 start = p;
167 while (('a' <= *p && *p <= 'z')
168 || ('A' <= *p && *p <= 'Z')
169 || ('0' <= *p && *p <= '9')
170 || *p == '+' || *p == '-')
171 ++p;
172 len = p - start;
173 if (*p++ != '>' || len < 3)
174 return false;
82780cbe 175 }
cc8dcf96
FW
176
177 const char *name = __tzstring_len (start, len);
178 if (name == NULL)
179 return false;
180 tz_rules[whichrule].name = name;
181
42261ad7
FW
182 *tzp = p;
183 return true;
184}
28f540f4 185
42261ad7
FW
186/* Parses the time zone offset at *TZP, and writes it to
187 tz_rules[WHICHRULE].offset. Returns true if the parse was
188 successful. */
189static bool
190parse_offset (const char **tzp, int whichrule)
191{
192 const char *tz = *tzp;
193 if (whichrule == 0
194 && (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz))))
195 return false;
28f540f4 196
42261ad7 197 long sign;
28f540f4 198 if (*tz == '-' || *tz == '+')
42261ad7 199 sign = *tz++ == '-' ? 1L : -1L;
28f540f4 200 else
42261ad7
FW
201 sign = -1L;
202 *tzp = tz;
203
204 unsigned short int hh;
205 unsigned short mm = 0;
206 unsigned short ss = 0;
207 int consumed = 0;
208 if (sscanf (tz, "%hu%n:%hu%n:%hu%n",
209 &hh, &consumed, &mm, &consumed, &ss, &consumed) > 0)
210 tz_rules[whichrule].offset = sign * compute_offset (ss, mm, hh);
211 else
212 /* Nothing could be parsed. */
213 if (whichrule == 0)
214 {
215 /* Standard time defaults to offset zero. */
216 tz_rules[0].offset = 0;
217 return false;
218 }
82780cbe 219 else
42261ad7
FW
220 /* DST defaults to one hour later than standard time. */
221 tz_rules[1].offset = tz_rules[0].offset + (60 * 60);
222 *tzp = tz + consumed;
223 return true;
224}
cd6ede75 225
42261ad7
FW
226/* Parses the standard <-> DST rules at *TZP. Updates
227 tz_rule[WHICHRULE]. On success, advances *TZP and returns true.
228 Otherwise, returns false. */
229static bool
230parse_rule (const char **tzp, int whichrule)
231{
232 const char *tz = *tzp;
233 tz_rule *tzr = &tz_rules[whichrule];
fd26970f 234
42261ad7
FW
235 /* Ignore comma to support string following the incorrect
236 specification in early POSIX.1 printings. */
237 tz += *tz == ',';
fd26970f 238
42261ad7
FW
239 /* Get the date of the change. */
240 if (*tz == 'J' || isdigit (*tz))
241 {
242 char *end;
243 tzr->type = *tz == 'J' ? J1 : J0;
244 if (tzr->type == J1 && !isdigit (*++tz))
245 return false;
246 unsigned long int d = strtoul (tz, &end, 10);
247 if (end == tz || d > 365)
248 return false;
249 if (tzr->type == J1 && d == 0)
250 return false;
251 tzr->d = d;
252 tz = end;
253 }
254 else if (*tz == 'M')
255 {
256 tzr->type = M;
257 int consumed;
258 if (sscanf (tz, "M%hu.%hu.%hu%n",
259 &tzr->m, &tzr->n, &tzr->d, &consumed) != 3
260 || tzr->m < 1 || tzr->m > 12
261 || tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
262 return false;
263 tz += consumed;
264 }
265 else if (*tz == '\0')
266 {
267 /* Daylight time rules in the U.S. are defined in the U.S. Code,
268 Title 15, Chapter 6, Subchapter IX - Standard Time. These
269 dates were established by Congress in the Energy Policy Act
270 of 2005 [Pub. L. no. 109-58, 119 Stat 594 (2005)].
271 Below is the equivalent of "M3.2.0,M11.1.0" [/2 not needed
272 since 2:00AM is the default]. */
273 tzr->type = M;
274 if (tzr == &tz_rules[0])
fd26970f 275 {
42261ad7
FW
276 tzr->m = 3;
277 tzr->n = 2;
278 tzr->d = 0;
fd26970f 279 }
42261ad7 280 else
0413b54c 281 {
42261ad7
FW
282 tzr->m = 11;
283 tzr->n = 1;
284 tzr->d = 0;
0413b54c 285 }
28f540f4 286 }
5290baf0 287 else
42261ad7
FW
288 return false;
289
290 if (*tz != '\0' && *tz != '/' && *tz != ',')
291 return false;
292 else if (*tz == '/')
40a55d20 293 {
42261ad7
FW
294 /* Get the time of day of the change. */
295 int negative;
296 ++tz;
297 if (*tz == '\0')
298 return false;
299 negative = *tz == '-';
300 tz += negative;
301 /* Default to 2:00 AM. */
302 unsigned short hh = 2;
303 unsigned short mm = 0;
304 unsigned short ss = 0;
305 int consumed = 0;
306 sscanf (tz, "%hu%n:%hu%n:%hu%n",
307 &hh, &consumed, &mm, &consumed, &ss, &consumed);;
308 tz += consumed;
309 tzr->secs = (negative ? -1 : 1) * ((hh * 60 * 60) + (mm * 60) + ss);
40a55d20 310 }
42261ad7
FW
311 else
312 /* Default to 2:00 AM. */
313 tzr->secs = 2 * 60 * 60;
28f540f4 314
42261ad7
FW
315 tzr->computed_for = -1;
316 *tzp = tz;
317 return true;
318}
1cbca0d9 319
42261ad7
FW
320/* Parse the POSIX TZ-style string. */
321void
322__tzset_parse_tz (const char *tz)
323{
324 /* Clear out old state and reset to unnamed UTC. */
325 memset (tz_rules, '\0', sizeof tz_rules);
326 tz_rules[0].name = tz_rules[1].name = "";
1cbca0d9 327
42261ad7
FW
328 /* Get the standard timezone name. */
329 if (parse_tzname (&tz, 0) && parse_offset (&tz, 0))
330 {
331 /* Get the DST timezone name (if any). */
332 if (*tz != '\0')
28f540f4 333 {
42261ad7 334 if (parse_tzname (&tz, 1))
28f540f4 335 {
42261ad7
FW
336 parse_offset (&tz, 1);
337 if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
338 {
339 /* There is no rule. See if there is a default rule
340 file. */
341 __tzfile_default (tz_rules[0].name, tz_rules[1].name,
342 tz_rules[0].offset, tz_rules[1].offset);
343 if (__use_tzfile)
344 {
345 free (old_tz);
346 old_tz = NULL;
347 return;
348 }
349 }
28f540f4 350 }
42261ad7
FW
351 /* Figure out the standard <-> DST rules. */
352 if (parse_rule (&tz, 0))
353 parse_rule (&tz, 1);
28f540f4
RM
354 }
355 else
28f540f4 356 {
42261ad7
FW
357 /* There is no DST. */
358 tz_rules[1].name = tz_rules[0].name;
359 tz_rules[1].offset = tz_rules[0].offset;
28f540f4 360 }
28f540f4 361 }
90865aa8 362
fa76dde2
UD
363 update_vars ();
364}
28f540f4 365
fa76dde2
UD
366/* Interpret the TZ envariable. */
367static void
8492c4dd 368tzset_internal (int always)
fa76dde2
UD
369{
370 static int is_initialized;
2e09a79a 371 const char *tz;
fa76dde2
UD
372
373 if (is_initialized && !always)
374 return;
375 is_initialized = 1;
376
377 /* Examine the TZ environment variable. */
378 tz = getenv ("TZ");
fa76dde2
UD
379 if (tz && *tz == '\0')
380 /* User specified the empty string; use UTC explicitly. */
381 tz = "Universal";
382
383 /* A leading colon means "implementation defined syntax".
384 We ignore the colon and always use the same algorithm:
385 try a data file, and if none exists parse the 1003.1 syntax. */
386 if (tz && *tz == ':')
387 ++tz;
388
58423c7d 389 /* Check whether the value changed since the last run. */
fa76dde2
UD
390 if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0)
391 /* No change, simply return. */
392 return;
393
394 if (tz == NULL)
395 /* No user specification; use the site-wide default. */
396 tz = TZDEFAULT;
397
398 tz_rules[0].name = NULL;
399 tz_rules[1].name = NULL;
400
401 /* Save the value of `tz'. */
72e6cdfa 402 free (old_tz);
fa76dde2
UD
403 old_tz = tz ? __strdup (tz) : NULL;
404
405 /* Try to read a data file. */
406 __tzfile_read (tz, 0, NULL);
407 if (__use_tzfile)
408 return;
409
410 /* No data file found. Default to UTC if nothing specified. */
411
412 if (tz == NULL || *tz == '\0'
413 || (TZDEFAULT != NULL && strcmp (tz, TZDEFAULT) == 0))
414 {
bd82a247 415 memset (tz_rules, '\0', sizeof tz_rules);
fa76dde2 416 tz_rules[0].name = tz_rules[1].name = "UTC";
bd82a247
UD
417 if (J0 != 0)
418 tz_rules[0].type = tz_rules[1].type = J0;
fa76dde2 419 tz_rules[0].change = tz_rules[1].change = (time_t) -1;
fa76dde2
UD
420 update_vars ();
421 return;
422 }
423
424 __tzset_parse_tz (tz);
28f540f4
RM
425}
426\f
427/* Figure out the exact time (as a time_t) in YEAR
428 when the change described by RULE will occur and
a3931cbe
UD
429 put it in RULE->change, saving YEAR in RULE->computed_for. */
430static void
9d46370c 431compute_change (tz_rule *rule, int year)
28f540f4 432{
2e09a79a 433 time_t t;
28f540f4
RM
434
435 if (year != -1 && rule->computed_for == year)
7fcc87f4 436 /* Operations on times in 2 BC will be slower. Oh well. */
a3931cbe 437 return;
1cbca0d9 438
28f540f4 439 /* First set T to January 1st, 0:00:00 GMT in YEAR. */
7fcc87f4
UD
440 if (year > 1970)
441 t = ((year - 1970) * 365
442 + /* Compute the number of leapdays between 1970 and YEAR
443 (exclusive). There is a leapday every 4th year ... */
444 + ((year - 1) / 4 - 1970 / 4)
445 /* ... except every 100th year ... */
446 - ((year - 1) / 100 - 1970 / 100)
447 /* ... but still every 400th year. */
448 + ((year - 1) / 400 - 1970 / 400)) * SECSPERDAY;
449 else
450 t = 0;
28f540f4
RM
451
452 switch (rule->type)
453 {
454 case J1:
455 /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
456 In non-leap years, or if the day number is 59 or less, just
457 add SECSPERDAY times the day number-1 to the time of
458 January 1, midnight, to get the day. */
459 t += (rule->d - 1) * SECSPERDAY;
460 if (rule->d >= 60 && __isleap (year))
461 t += SECSPERDAY;
462 break;
463
464 case J0:
465 /* n - Day of year.
466 Just add SECSPERDAY times the day number to the time of Jan 1st. */
467 t += rule->d * SECSPERDAY;
468 break;
469
470 case M:
471 /* Mm.n.d - Nth "Dth day" of month M. */
472 {
9a0a462c
UD
473 unsigned int i;
474 int d, m1, yy0, yy1, yy2, dow;
475 const unsigned short int *myday =
80fd7387 476 &__mon_yday[__isleap (year)][rule->m];
28f540f4
RM
477
478 /* First add SECSPERDAY for each day in months before M. */
80fd7387 479 t += myday[-1] * SECSPERDAY;
28f540f4
RM
480
481 /* Use Zeller's Congruence to get day-of-week of first day of month. */
482 m1 = (rule->m + 9) % 12 + 1;
483 yy0 = (rule->m <= 2) ? (year - 1) : year;
484 yy1 = yy0 / 100;
485 yy2 = yy0 % 100;
486 dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
487 if (dow < 0)
488 dow += 7;
489
490 /* DOW is the day-of-week of the first day of the month. Get the
491 day-of-month (zero-origin) of the first DOW day of the month. */
492 d = rule->d - dow;
493 if (d < 0)
494 d += 7;
495 for (i = 1; i < rule->n; ++i)
496 {
9a0a462c 497 if (d + 7 >= (int) myday[0] - myday[-1])
28f540f4
RM
498 break;
499 d += 7;
500 }
501
502 /* D is the day-of-month (zero-origin) of the day we want. */
503 t += d * SECSPERDAY;
504 }
505 break;
506 }
507
508 /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
509 Just add the time of day and local offset from GMT, and we're done. */
510
68dbb3a6 511 rule->change = t - rule->offset + rule->secs;
28f540f4 512 rule->computed_for = year;
28f540f4
RM
513}
514
515
e3e35cfc 516/* Figure out the correct timezone for TM and set `__tzname',
a3931cbe 517 `__timezone', and `__daylight' accordingly. */
fa76dde2 518void
9d46370c 519__tz_compute (time_t timer, struct tm *tm, int use_localtime)
28f540f4 520{
a3931cbe
UD
521 compute_change (&tz_rules[0], 1900 + tm->tm_year);
522 compute_change (&tz_rules[1], 1900 + tm->tm_year);
fa76dde2
UD
523
524 if (use_localtime)
525 {
526 int isdst;
527
528 /* We have to distinguish between northern and southern
529 hemisphere. For the latter the daylight saving time
530 ends in the next year. */
531 if (__builtin_expect (tz_rules[0].change
532 > tz_rules[1].change, 0))
533 isdst = (timer < tz_rules[1].change
534 || timer >= tz_rules[0].change);
535 else
536 isdst = (timer >= tz_rules[0].change
537 && timer < tz_rules[1].change);
538 tm->tm_isdst = isdst;
539 tm->tm_zone = __tzname[isdst];
540 tm->tm_gmtoff = tz_rules[isdst].offset;
541 }
28f540f4 542}
b24be05f 543\f
b24be05f 544/* Reinterpret the TZ environment variable and set `tzname'. */
4311b2a6 545#undef tzset
28f540f4 546
b24be05f 547void
10dc2a90 548__tzset (void)
b24be05f 549{
9a0a462c 550 __libc_lock_lock (tzset_lock);
68dbb3a6 551
8492c4dd 552 tzset_internal (1);
68dbb3a6 553
860d3729
UD
554 if (!__use_tzfile)
555 {
556 /* Set `tzname'. */
557 __tzname[0] = (char *) tz_rules[0].name;
558 __tzname[1] = (char *) tz_rules[1].name;
559 }
68dbb3a6 560
9a0a462c 561 __libc_lock_unlock (tzset_lock);
b24be05f 562}
10dc2a90 563weak_alias (__tzset, tzset)
9a0a462c
UD
564\f
565/* Return the `struct tm' representation of *TIMER in the local timezone.
566 Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */
567struct tm *
568__tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
569{
570 long int leap_correction;
571 int leap_extra_secs;
572
573 if (timer == NULL)
574 {
575 __set_errno (EINVAL);
576 return NULL;
577 }
578
579 __libc_lock_lock (tzset_lock);
580
581 /* Update internal database according to current TZ setting.
582 POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
58423c7d 583 This is a good idea since this allows at least a bit more parallelism. */
8492c4dd 584 tzset_internal (tp == &_tmbuf && use_localtime);
9a0a462c
UD
585
586 if (__use_tzfile)
38e68573
UD
587 __tzfile_compute (*timer, use_localtime, &leap_correction,
588 &leap_extra_secs, tp);
9a0a462c
UD
589 else
590 {
a3931cbe 591 if (! __offtime (timer, 0, tp))
9a0a462c 592 tp = NULL;
a3931cbe 593 else
fa76dde2 594 __tz_compute (*timer, tp, use_localtime);
9a0a462c
UD
595 leap_correction = 0L;
596 leap_extra_secs = 0;
597 }
598
6807b1db
KE
599 __libc_lock_unlock (tzset_lock);
600
9a0a462c
UD
601 if (tp)
602 {
fa76dde2 603 if (! use_localtime)
9a0a462c
UD
604 {
605 tp->tm_isdst = 0;
606 tp->tm_zone = "GMT";
607 tp->tm_gmtoff = 0L;
608 }
609
fe0ec73e
UD
610 if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp))
611 tp->tm_sec += leap_extra_secs;
612 else
613 tp = NULL;
9a0a462c
UD
614 }
615
9a0a462c
UD
616 return tp;
617}
83628d31
UD
618
619
c877418f 620libc_freeres_fn (free_mem)
83628d31
UD
621{
622 while (tzstring_list != NULL)
623 {
624 struct tzstring_l *old = tzstring_list;
625
626 tzstring_list = tzstring_list->next;
627 free (old);
628 }
629 free (old_tz);
630 old_tz = NULL;
631}