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