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