]>
Commit | Line | Data |
---|---|---|
a3931cbe | 1 | /* Copyright (C) 1991-1999, 2000, 2001, 2002 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 AJ |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, write to the Free | |
16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
17 | 02111-1307 USA. */ | |
28f540f4 | 18 | |
28f540f4 | 19 | #include <ctype.h> |
9a0a462c UD |
20 | #include <errno.h> |
21 | #include <bits/libc-lock.h> | |
28f540f4 RM |
22 | #include <stddef.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <time.h> | |
27 | ||
9a0a462c | 28 | |
28f540f4 | 29 | #define NOID |
14ea22e9 | 30 | #include <timezone/tzfile.h> |
28f540f4 | 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 | |
44 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
45 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
46 | #define sign(x) ((x) < 0 ? -1 : 1) | |
47 | ||
48 | ||
49 | /* This structure contains all the information about a | |
50 | timezone given in the POSIX standard TZ envariable. */ | |
51 | typedef struct | |
52 | { | |
cd6ede75 | 53 | const char *name; |
28f540f4 RM |
54 | |
55 | /* When to change. */ | |
56 | enum { J0, J1, M } type; /* Interpretation of: */ | |
57 | unsigned short int m, n, d; /* Month, week, day. */ | |
58 | unsigned int secs; /* Time of day. */ | |
59 | ||
60 | long int offset; /* Seconds east of GMT (west if < 0). */ | |
61 | ||
62 | /* We cache the computed time of change for a | |
63 | given year so we don't have to recompute it. */ | |
64 | time_t change; /* When to change to this zone. */ | |
65 | int computed_for; /* Year above is computed for. */ | |
66 | } tz_rule; | |
67 | ||
68 | /* tz_rules[0] is standard, tz_rules[1] is daylight. */ | |
69 | static tz_rule tz_rules[2]; | |
f65fd747 UD |
70 | |
71 | ||
a3931cbe UD |
72 | static void compute_change __P ((tz_rule *rule, int year)) internal_function; |
73 | static void tz_compute __P ((const struct tm *tm)) | |
dfd2257a UD |
74 | internal_function; |
75 | static void tzset_internal __P ((int always)) internal_function; | |
28f540f4 | 76 | \f |
2698e32c UD |
77 | /* List of buffers containing time zone strings. */ |
78 | struct tzstring_l | |
cd6ede75 | 79 | { |
2698e32c UD |
80 | struct tzstring_l *next; |
81 | size_t len; /* strlen(data) - doesn't count terminating NUL! */ | |
82 | char data[0]; | |
cd6ede75 UD |
83 | }; |
84 | ||
418f1701 | 85 | static struct tzstring_l *tzstring_list; |
cd6ede75 | 86 | |
2698e32c UD |
87 | /* Allocate a permanent home for S. It will never be moved or deallocated, |
88 | but may share space with other strings. | |
89 | Don't modify the returned string. */ | |
0413b54c | 90 | char * |
2698e32c | 91 | __tzstring (const char *s) |
cd6ede75 | 92 | { |
cd6ede75 | 93 | char *p; |
2698e32c UD |
94 | struct tzstring_l *t, *u, *new; |
95 | size_t len = strlen(s); | |
96 | ||
97 | /* Walk the list and look for a match. If this string is the same | |
98 | as the end of an already-allocated string, it can share space. */ | |
99 | for (u = t = tzstring_list; t; u = t, t = t->next) | |
100 | if (len <= t->len) | |
101 | { | |
102 | p = &t->data[t->len - len]; | |
103 | if (strcmp (s, p) == 0) | |
cd6ede75 | 104 | return p; |
2698e32c UD |
105 | } |
106 | ||
107 | /* Not found; allocate a new buffer. */ | |
108 | new = malloc (sizeof (struct tzstring_l) + len + 1); | |
109 | if (!new) | |
110 | return NULL; | |
111 | ||
112 | new->next = NULL; | |
113 | new->len = len; | |
114 | strcpy (new->data, s); | |
115 | ||
116 | if (u) | |
117 | u->next = new; | |
118 | else | |
119 | tzstring_list = new; | |
120 | ||
121 | return new->data; | |
cd6ede75 UD |
122 | } |
123 | \f | |
a3931cbe UD |
124 | /* Maximum length of a timezone name. tzset_internal keeps this up to date |
125 | (never decreasing it) when ! __use_tzfile. | |
126 | tzfile.c keeps it up to date when __use_tzfile. */ | |
127 | size_t __tzname_cur_max; | |
128 | ||
129 | long int | |
130 | __tzname_max () | |
131 | { | |
132 | __libc_lock_lock (tzset_lock); | |
133 | ||
134 | tzset_internal (0); | |
135 | ||
136 | __libc_lock_unlock (tzset_lock); | |
137 | ||
138 | return __tzname_cur_max; | |
139 | } | |
140 | \f | |
c4563d2d | 141 | static char *old_tz; |
28f540f4 RM |
142 | |
143 | /* Interpret the TZ envariable. */ | |
9a0a462c | 144 | static void |
dfd2257a | 145 | internal_function |
9a0a462c | 146 | tzset_internal (always) |
df4ef2ab | 147 | int always; |
28f540f4 | 148 | { |
c4563d2d | 149 | static int is_initialized; |
f65fd747 | 150 | register const char *tz; |
28f540f4 | 151 | register size_t l; |
cd6ede75 | 152 | char *tzbuf; |
28f540f4 RM |
153 | unsigned short int hh, mm, ss; |
154 | unsigned short int whichrule; | |
155 | ||
df4ef2ab UD |
156 | if (is_initialized && !always) |
157 | return; | |
158 | is_initialized = 1; | |
159 | ||
68dbb3a6 UD |
160 | /* Examine the TZ environment variable. */ |
161 | tz = getenv ("TZ"); | |
1228ed5c UD |
162 | if (tz == NULL) |
163 | /* No user specification; use the site-wide default. */ | |
164 | tz = TZDEFAULT; | |
165 | else if (*tz == '\0') | |
166 | /* User specified the empty string; use UTC explicitly. */ | |
167 | tz = "Universal"; | |
68dbb3a6 UD |
168 | |
169 | /* A leading colon means "implementation defined syntax". | |
170 | We ignore the colon and always use the same algorithm: | |
171 | try a data file, and if none exists parse the 1003.1 syntax. */ | |
172 | if (tz && *tz == ':') | |
173 | ++tz; | |
174 | ||
175 | /* Check whether the value changes since the last run. */ | |
176 | if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0) | |
177 | /* No change, simply return. */ | |
178 | return; | |
179 | ||
5290baf0 UD |
180 | tz_rules[0].name = NULL; |
181 | tz_rules[1].name = NULL; | |
28f540f4 | 182 | |
68dbb3a6 UD |
183 | /* Save the value of `tz'. */ |
184 | if (old_tz != NULL) | |
185 | free (old_tz); | |
186 | old_tz = tz ? __strdup (tz) : NULL; | |
a3b58440 RM |
187 | |
188 | /* Try to read a data file. */ | |
c4563d2d | 189 | __tzfile_read (tz, 0, NULL); |
a3b58440 | 190 | if (__use_tzfile) |
68dbb3a6 | 191 | return; |
28f540f4 | 192 | |
a3b58440 RM |
193 | /* No data file found. Default to UTC if nothing specified. */ |
194 | ||
fb0dd050 UD |
195 | if (tz == NULL || *tz == '\0' |
196 | || (TZDEFAULT != NULL && strcmp (tz, TZDEFAULT) == 0)) | |
28f540f4 | 197 | { |
cd6ede75 | 198 | tz_rules[0].name = tz_rules[1].name = "UTC"; |
a3b58440 RM |
199 | tz_rules[0].type = tz_rules[1].type = J0; |
200 | tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0; | |
201 | tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0; | |
202 | tz_rules[0].secs = tz_rules[1].secs = 0; | |
203 | tz_rules[0].offset = tz_rules[1].offset = 0L; | |
204 | tz_rules[0].change = tz_rules[1].change = (time_t) -1; | |
205 | tz_rules[0].computed_for = tz_rules[1].computed_for = 0; | |
a3931cbe | 206 | goto out; |
28f540f4 RM |
207 | } |
208 | ||
933e73fa | 209 | /* Clear out old state and reset to unnamed UTC. */ |
28f540f4 | 210 | memset (tz_rules, 0, sizeof tz_rules); |
cd6ede75 | 211 | tz_rules[0].name = tz_rules[1].name = ""; |
28f540f4 RM |
212 | |
213 | /* Get the standard timezone name. */ | |
90865aa8 | 214 | tzbuf = strdupa (tz); |
28f540f4 | 215 | |
cd6ede75 UD |
216 | if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 || |
217 | (l = strlen (tzbuf)) < 3) | |
a3931cbe | 218 | goto out; |
28f540f4 | 219 | |
cd6ede75 | 220 | tz_rules[0].name = __tzstring (tzbuf); |
28f540f4 RM |
221 | |
222 | tz += l; | |
223 | ||
933e73fa | 224 | /* Figure out the standard offset from UTC. */ |
1228ed5c | 225 | if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz))) |
a3931cbe | 226 | goto out; |
28f540f4 RM |
227 | |
228 | if (*tz == '-' || *tz == '+') | |
229 | tz_rules[0].offset = *tz++ == '-' ? 1L : -1L; | |
230 | else | |
231 | tz_rules[0].offset = -1L; | |
232 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) | |
233 | { | |
234 | default: | |
a3931cbe | 235 | goto out; |
28f540f4 RM |
236 | case 1: |
237 | mm = 0; | |
238 | case 2: | |
239 | ss = 0; | |
240 | case 3: | |
241 | break; | |
242 | } | |
f65fd747 UD |
243 | tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) + |
244 | (min (hh, 23) * 60 * 60)); | |
28f540f4 RM |
245 | |
246 | for (l = 0; l < 3; ++l) | |
247 | { | |
248 | while (isdigit(*tz)) | |
249 | ++tz; | |
250 | if (l < 2 && *tz == ':') | |
251 | ++tz; | |
252 | } | |
253 | ||
254 | /* Get the DST timezone name (if any). */ | |
255 | if (*tz != '\0') | |
256 | { | |
cd6ede75 UD |
257 | char *n = tzbuf + strlen (tzbuf) + 1; |
258 | if (sscanf (tz, "%[^0-9,+-]", n) != 1 || | |
259 | (l = strlen (n)) < 3) | |
260 | goto done_names; /* Punt on name, set up the offsets. */ | |
261 | ||
262 | tz_rules[1].name = __tzstring (n); | |
263 | ||
264 | tz += l; | |
fd26970f UD |
265 | |
266 | /* Figure out the DST offset from GMT. */ | |
267 | if (*tz == '-' || *tz == '+') | |
268 | tz_rules[1].offset = *tz++ == '-' ? 1L : -1L; | |
269 | else | |
270 | tz_rules[1].offset = -1L; | |
271 | ||
272 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) | |
273 | { | |
274 | default: | |
275 | /* Default to one hour later than standard time. */ | |
276 | tz_rules[1].offset = tz_rules[0].offset + (60 * 60); | |
277 | break; | |
278 | ||
279 | case 1: | |
280 | mm = 0; | |
281 | case 2: | |
282 | ss = 0; | |
283 | case 3: | |
284 | tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) + | |
285 | (min (hh, 23) * (60 * 60))); | |
286 | break; | |
287 | } | |
288 | for (l = 0; l < 3; ++l) | |
289 | { | |
290 | while (isdigit (*tz)) | |
291 | ++tz; | |
292 | if (l < 2 && *tz == ':') | |
293 | ++tz; | |
294 | } | |
0413b54c UD |
295 | if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0')) |
296 | { | |
297 | /* There is no rule. See if there is a default rule file. */ | |
298 | __tzfile_default (tz_rules[0].name, tz_rules[1].name, | |
299 | tz_rules[0].offset, tz_rules[1].offset); | |
300 | if (__use_tzfile) | |
301 | { | |
302 | free (old_tz); | |
303 | old_tz = NULL; | |
304 | return; | |
305 | } | |
306 | } | |
28f540f4 | 307 | } |
5290baf0 | 308 | else |
40a55d20 UD |
309 | { |
310 | /* There is no DST. */ | |
311 | tz_rules[1].name = tz_rules[0].name; | |
ff152e3f | 312 | tz_rules[1].offset = tz_rules[0].offset; |
90865aa8 | 313 | goto out; |
40a55d20 | 314 | } |
28f540f4 | 315 | |
28f540f4 | 316 | done_names: |
28f540f4 RM |
317 | /* Figure out the standard <-> DST rules. */ |
318 | for (whichrule = 0; whichrule < 2; ++whichrule) | |
319 | { | |
320 | register tz_rule *tzr = &tz_rules[whichrule]; | |
1cbca0d9 | 321 | |
ca34d7a7 UD |
322 | /* Ignore comma to support string following the incorrect |
323 | specification in early POSIX.1 printings. */ | |
324 | tz += *tz == ','; | |
1cbca0d9 | 325 | |
28f540f4 RM |
326 | /* Get the date of the change. */ |
327 | if (*tz == 'J' || isdigit (*tz)) | |
328 | { | |
329 | char *end; | |
330 | tzr->type = *tz == 'J' ? J1 : J0; | |
331 | if (tzr->type == J1 && !isdigit (*++tz)) | |
90865aa8 | 332 | goto out; |
28f540f4 RM |
333 | tzr->d = (unsigned short int) strtoul (tz, &end, 10); |
334 | if (end == tz || tzr->d > 365) | |
90865aa8 | 335 | goto out; |
28f540f4 | 336 | else if (tzr->type == J1 && tzr->d == 0) |
90865aa8 | 337 | goto out; |
28f540f4 RM |
338 | tz = end; |
339 | } | |
340 | else if (*tz == 'M') | |
341 | { | |
342 | int n; | |
343 | tzr->type = M; | |
344 | if (sscanf (tz, "M%hu.%hu.%hu%n", | |
345 | &tzr->m, &tzr->n, &tzr->d, &n) != 3 || | |
346 | tzr->m < 1 || tzr->m > 12 || | |
347 | tzr->n < 1 || tzr->n > 5 || tzr->d > 6) | |
90865aa8 | 348 | goto out; |
28f540f4 RM |
349 | tz += n; |
350 | } | |
351 | else if (*tz == '\0') | |
352 | { | |
353 | /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */ | |
354 | tzr->type = M; | |
355 | if (tzr == &tz_rules[0]) | |
356 | { | |
357 | tzr->m = 4; | |
358 | tzr->n = 1; | |
359 | tzr->d = 0; | |
360 | } | |
361 | else | |
362 | { | |
363 | tzr->m = 10; | |
364 | tzr->n = 5; | |
365 | tzr->d = 0; | |
366 | } | |
367 | } | |
368 | else | |
90865aa8 | 369 | goto out; |
1cbca0d9 | 370 | |
28f540f4 | 371 | if (*tz != '\0' && *tz != '/' && *tz != ',') |
90865aa8 | 372 | goto out; |
28f540f4 RM |
373 | else if (*tz == '/') |
374 | { | |
375 | /* Get the time of day of the change. */ | |
376 | ++tz; | |
377 | if (*tz == '\0') | |
90865aa8 | 378 | goto out; |
28f540f4 RM |
379 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) |
380 | { | |
381 | default: | |
382 | hh = 2; /* Default to 2:00 AM. */ | |
383 | case 1: | |
384 | mm = 0; | |
385 | case 2: | |
386 | ss = 0; | |
387 | case 3: | |
388 | break; | |
389 | } | |
390 | for (l = 0; l < 3; ++l) | |
391 | { | |
f65fd747 | 392 | while (isdigit (*tz)) |
28f540f4 RM |
393 | ++tz; |
394 | if (l < 2 && *tz == ':') | |
395 | ++tz; | |
396 | } | |
397 | tzr->secs = (hh * 60 * 60) + (mm * 60) + ss; | |
398 | } | |
399 | else | |
400 | /* Default to 2:00 AM. */ | |
401 | tzr->secs = 2 * 60 * 60; | |
402 | ||
403 | tzr->computed_for = -1; | |
404 | } | |
90865aa8 UD |
405 | |
406 | out: | |
a3931cbe | 407 | __daylight = tz_rules[0].offset != tz_rules[1].offset; |
90865aa8 | 408 | __timezone = -tz_rules[0].offset; |
a3931cbe UD |
409 | __tzname[0] = (char *) tz_rules[0].name; |
410 | __tzname[1] = (char *) tz_rules[1].name; | |
28f540f4 | 411 | |
a3931cbe UD |
412 | { |
413 | /* Keep __tzname_cur_max up to date. */ | |
414 | size_t len0 = strlen (__tzname[0]); | |
415 | size_t len1 = strlen (__tzname[1]); | |
416 | if (len0 > __tzname_cur_max) | |
417 | __tzname_cur_max = len0; | |
418 | if (len1 > __tzname_cur_max) | |
419 | __tzname_cur_max = len1; | |
420 | } | |
28f540f4 RM |
421 | } |
422 | \f | |
423 | /* Figure out the exact time (as a time_t) in YEAR | |
424 | when the change described by RULE will occur and | |
a3931cbe UD |
425 | put it in RULE->change, saving YEAR in RULE->computed_for. */ |
426 | static void | |
dfd2257a | 427 | internal_function |
f65fd747 UD |
428 | compute_change (rule, year) |
429 | tz_rule *rule; | |
430 | int year; | |
28f540f4 RM |
431 | { |
432 | register time_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 UD |
516 | `__timezone', and `__daylight' accordingly. */ |
517 | static void | |
dfd2257a | 518 | internal_function |
e3e35cfc | 519 | tz_compute (tm) |
f65fd747 | 520 | const struct tm *tm; |
28f540f4 | 521 | { |
a3931cbe UD |
522 | compute_change (&tz_rules[0], 1900 + tm->tm_year); |
523 | compute_change (&tz_rules[1], 1900 + tm->tm_year); | |
28f540f4 | 524 | } |
b24be05f | 525 | \f |
b24be05f | 526 | /* Reinterpret the TZ environment variable and set `tzname'. */ |
4311b2a6 | 527 | #undef tzset |
28f540f4 | 528 | |
b24be05f | 529 | void |
10dc2a90 | 530 | __tzset (void) |
b24be05f | 531 | { |
9a0a462c | 532 | __libc_lock_lock (tzset_lock); |
68dbb3a6 | 533 | |
9a0a462c | 534 | tzset_internal (1); |
68dbb3a6 | 535 | |
860d3729 UD |
536 | if (!__use_tzfile) |
537 | { | |
538 | /* Set `tzname'. */ | |
539 | __tzname[0] = (char *) tz_rules[0].name; | |
540 | __tzname[1] = (char *) tz_rules[1].name; | |
541 | } | |
68dbb3a6 | 542 | |
9a0a462c | 543 | __libc_lock_unlock (tzset_lock); |
b24be05f | 544 | } |
10dc2a90 | 545 | weak_alias (__tzset, tzset) |
9a0a462c UD |
546 | \f |
547 | /* Return the `struct tm' representation of *TIMER in the local timezone. | |
548 | Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */ | |
549 | struct tm * | |
550 | __tz_convert (const time_t *timer, int use_localtime, struct tm *tp) | |
551 | { | |
552 | long int leap_correction; | |
553 | int leap_extra_secs; | |
554 | ||
555 | if (timer == NULL) | |
556 | { | |
557 | __set_errno (EINVAL); | |
558 | return NULL; | |
559 | } | |
560 | ||
561 | __libc_lock_lock (tzset_lock); | |
562 | ||
563 | /* Update internal database according to current TZ setting. | |
564 | POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. | |
565 | This is a good idea since this allows at least a bit more parallelism. | |
566 | By analogy we apply the same rule to gmtime_r. */ | |
567 | tzset_internal (tp == &_tmbuf); | |
568 | ||
569 | if (__use_tzfile) | |
570 | { | |
571 | if (! __tzfile_compute (*timer, use_localtime, | |
a709dd43 | 572 | &leap_correction, &leap_extra_secs, tp)) |
9a0a462c UD |
573 | tp = NULL; |
574 | } | |
575 | else | |
576 | { | |
a3931cbe | 577 | if (! __offtime (timer, 0, tp)) |
9a0a462c | 578 | tp = NULL; |
a3931cbe UD |
579 | else |
580 | tz_compute (tp); | |
9a0a462c UD |
581 | leap_correction = 0L; |
582 | leap_extra_secs = 0; | |
583 | } | |
584 | ||
585 | if (tp) | |
586 | { | |
587 | if (use_localtime) | |
588 | { | |
a709dd43 UD |
589 | if (!__use_tzfile) |
590 | { | |
a3931cbe UD |
591 | int isdst; |
592 | ||
593 | /* We have to distinguish between northern and southern | |
594 | hemisphere. For the latter the daylight saving time | |
595 | ends in the next year. */ | |
596 | if (__builtin_expect (tz_rules[0].change | |
597 | > tz_rules[1].change, 0)) | |
598 | isdst = (*timer < tz_rules[1].change | |
599 | || *timer >= tz_rules[0].change); | |
600 | else | |
601 | isdst = (*timer >= tz_rules[0].change | |
602 | && *timer < tz_rules[1].change); | |
a709dd43 UD |
603 | tp->tm_isdst = isdst; |
604 | tp->tm_zone = __tzname[isdst]; | |
605 | tp->tm_gmtoff = tz_rules[isdst].offset; | |
606 | } | |
9a0a462c UD |
607 | } |
608 | else | |
609 | { | |
610 | tp->tm_isdst = 0; | |
611 | tp->tm_zone = "GMT"; | |
612 | tp->tm_gmtoff = 0L; | |
613 | } | |
614 | ||
fe0ec73e UD |
615 | if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp)) |
616 | tp->tm_sec += leap_extra_secs; | |
617 | else | |
618 | tp = NULL; | |
9a0a462c UD |
619 | } |
620 | ||
621 | __libc_lock_unlock (tzset_lock); | |
622 | ||
623 | return tp; | |
624 | } | |
83628d31 UD |
625 | |
626 | ||
c877418f | 627 | libc_freeres_fn (free_mem) |
83628d31 UD |
628 | { |
629 | while (tzstring_list != NULL) | |
630 | { | |
631 | struct tzstring_l *old = tzstring_list; | |
632 | ||
633 | tzstring_list = tzstring_list->next; | |
634 | free (old); | |
635 | } | |
636 | free (old_tz); | |
637 | old_tz = NULL; | |
638 | } |