]>
Commit | Line | Data |
---|---|---|
14ea22e9 | 1 | /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc. |
f65fd747 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
f65fd747 UD |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public License as | |
6 | published by the Free Software Foundation; either version 2 of the | |
7 | 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 | |
12 | Library General Public License for more details. | |
28f540f4 | 13 | |
f65fd747 UD |
14 | You should have received a copy of the GNU Library General Public |
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 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 | ||
28 | /* Defined in mktime.c. */ | |
f65fd747 | 29 | extern const unsigned short int __mon_yday[2][13]; |
28f540f4 | 30 | |
9a0a462c UD |
31 | /* Defined in localtime.c. */ |
32 | extern struct tm _tmbuf; | |
33 | ||
28f540f4 | 34 | #define NOID |
14ea22e9 | 35 | #include <timezone/tzfile.h> |
28f540f4 RM |
36 | |
37 | extern int __use_tzfile; | |
f65fd747 | 38 | extern void __tzfile_read __P ((const char *file)); |
9a0a462c | 39 | extern int __tzfile_compute __P ((time_t timer, int use_localtime, |
b17277cf | 40 | long int *leap_correct, int *leap_hit, |
a709dd43 | 41 | struct tm *tp)); |
cd6ede75 | 42 | extern void __tzfile_default __P ((const char *std, const char *dst, |
f65fd747 | 43 | long int stdoff, long int dstoff)); |
14ea22e9 | 44 | extern char *__tzstring __P ((const char *string)); |
28f540f4 | 45 | |
28f540f4 RM |
46 | char *__tzname[2] = { (char *) "GMT", (char *) "GMT" }; |
47 | int __daylight = 0; | |
48 | long int __timezone = 0L; | |
49 | ||
0ac2e7d8 BK |
50 | weak_alias (__tzname, tzname) |
51 | weak_alias (__daylight, daylight) | |
52 | weak_alias (__timezone, timezone) | |
53 | ||
9a0a462c UD |
54 | /* This locks all the state variables in tzfile.c and this file. */ |
55 | __libc_lock_define (static, tzset_lock) | |
56 | ||
28f540f4 RM |
57 | |
58 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
59 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
60 | #define sign(x) ((x) < 0 ? -1 : 1) | |
61 | ||
62 | ||
63 | /* This structure contains all the information about a | |
64 | timezone given in the POSIX standard TZ envariable. */ | |
65 | typedef struct | |
66 | { | |
cd6ede75 | 67 | const char *name; |
28f540f4 RM |
68 | |
69 | /* When to change. */ | |
70 | enum { J0, J1, M } type; /* Interpretation of: */ | |
71 | unsigned short int m, n, d; /* Month, week, day. */ | |
72 | unsigned int secs; /* Time of day. */ | |
73 | ||
74 | long int offset; /* Seconds east of GMT (west if < 0). */ | |
75 | ||
76 | /* We cache the computed time of change for a | |
77 | given year so we don't have to recompute it. */ | |
78 | time_t change; /* When to change to this zone. */ | |
79 | int computed_for; /* Year above is computed for. */ | |
80 | } tz_rule; | |
81 | ||
82 | /* tz_rules[0] is standard, tz_rules[1] is daylight. */ | |
83 | static tz_rule tz_rules[2]; | |
f65fd747 UD |
84 | |
85 | ||
dfd2257a UD |
86 | static int compute_change __P ((tz_rule *rule, int year)) internal_function; |
87 | static int tz_compute __P ((time_t timer, const struct tm *tm)) | |
88 | internal_function; | |
89 | static void tzset_internal __P ((int always)) internal_function; | |
28f540f4 | 90 | \f |
cd6ede75 | 91 | /* Header for a list of buffers containing time zone strings. */ |
0413b54c | 92 | struct tzstring_head |
cd6ede75 UD |
93 | { |
94 | struct tzstring_head *next; | |
95 | /* The buffer itself immediately follows the header. | |
96 | The buffer contains zero or more (possibly overlapping) strings. | |
97 | The last string is followed by 2 '\0's instead of the usual 1. */ | |
98 | }; | |
99 | ||
100 | /* First in a list of buffers containing time zone strings. | |
101 | All the buffers but the last are read-only. */ | |
102 | static struct | |
103 | { | |
104 | struct tzstring_head head; | |
105 | char data[48]; | |
106 | } tzstring_list; | |
107 | ||
108 | /* Size of the last buffer in the list, not counting its header. */ | |
109 | static size_t tzstring_last_buffer_size = sizeof tzstring_list.data; | |
110 | ||
111 | /* Allocate a time zone string with given contents. | |
112 | The string will never be moved or deallocated. | |
113 | However, its contents may be shared with other such strings. */ | |
0413b54c | 114 | char * |
cd6ede75 UD |
115 | __tzstring (string) |
116 | const char *string; | |
117 | { | |
118 | struct tzstring_head *h = &tzstring_list.head; | |
119 | size_t needed; | |
120 | char *p; | |
121 | ||
122 | /* Look through time zone string list for a duplicate of this one. */ | |
123 | for (h = &tzstring_list.head; ; h = h->next) | |
124 | { | |
0413b54c | 125 | for (p = (char *) (h + 1); p[0] | p[1]; ++p) |
cd6ede75 UD |
126 | if (strcmp (p, string) == 0) |
127 | return p; | |
128 | if (! h->next) | |
129 | break; | |
130 | } | |
131 | ||
132 | /* No duplicate was found. Copy to the end of this buffer if there's room; | |
133 | otherwise, append a large-enough new buffer to the list and use it. */ | |
0413b54c | 134 | ++p; |
cd6ede75 UD |
135 | needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string. */ |
136 | ||
137 | if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed) | |
138 | { | |
139 | size_t buffer_size = tzstring_last_buffer_size; | |
140 | while ((buffer_size *= 2) < needed) | |
141 | continue; | |
142 | if (! (h = h->next = malloc (sizeof *h + buffer_size))) | |
143 | return NULL; | |
144 | h->next = NULL; | |
145 | tzstring_last_buffer_size = buffer_size; | |
146 | p = (char *) (h + 1); | |
147 | } | |
148 | ||
0413b54c | 149 | return strncpy (p, string, needed); |
cd6ede75 UD |
150 | } |
151 | \f | |
68dbb3a6 | 152 | static char *old_tz = NULL; |
28f540f4 RM |
153 | |
154 | /* Interpret the TZ envariable. */ | |
9a0a462c | 155 | static void |
dfd2257a | 156 | internal_function |
9a0a462c | 157 | tzset_internal (always) |
df4ef2ab | 158 | int always; |
28f540f4 | 159 | { |
df4ef2ab | 160 | static int is_initialized = 0; |
f65fd747 | 161 | register const char *tz; |
28f540f4 | 162 | register size_t l; |
cd6ede75 | 163 | char *tzbuf; |
28f540f4 RM |
164 | unsigned short int hh, mm, ss; |
165 | unsigned short int whichrule; | |
166 | ||
df4ef2ab UD |
167 | if (is_initialized && !always) |
168 | return; | |
169 | is_initialized = 1; | |
170 | ||
68dbb3a6 UD |
171 | /* Examine the TZ environment variable. */ |
172 | tz = getenv ("TZ"); | |
1228ed5c UD |
173 | if (tz == NULL) |
174 | /* No user specification; use the site-wide default. */ | |
175 | tz = TZDEFAULT; | |
176 | else if (*tz == '\0') | |
177 | /* User specified the empty string; use UTC explicitly. */ | |
178 | tz = "Universal"; | |
68dbb3a6 UD |
179 | |
180 | /* A leading colon means "implementation defined syntax". | |
181 | We ignore the colon and always use the same algorithm: | |
182 | try a data file, and if none exists parse the 1003.1 syntax. */ | |
183 | if (tz && *tz == ':') | |
184 | ++tz; | |
185 | ||
186 | /* Check whether the value changes since the last run. */ | |
187 | if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0) | |
188 | /* No change, simply return. */ | |
189 | return; | |
190 | ||
5290baf0 UD |
191 | tz_rules[0].name = NULL; |
192 | tz_rules[1].name = NULL; | |
28f540f4 | 193 | |
68dbb3a6 UD |
194 | /* Save the value of `tz'. */ |
195 | if (old_tz != NULL) | |
196 | free (old_tz); | |
197 | old_tz = tz ? __strdup (tz) : NULL; | |
a3b58440 RM |
198 | |
199 | /* Try to read a data file. */ | |
200 | __tzfile_read (tz); | |
201 | if (__use_tzfile) | |
68dbb3a6 | 202 | return; |
28f540f4 | 203 | |
a3b58440 RM |
204 | /* No data file found. Default to UTC if nothing specified. */ |
205 | ||
28f540f4 RM |
206 | if (tz == NULL || *tz == '\0') |
207 | { | |
cd6ede75 | 208 | tz_rules[0].name = tz_rules[1].name = "UTC"; |
a3b58440 RM |
209 | tz_rules[0].type = tz_rules[1].type = J0; |
210 | tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0; | |
211 | tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0; | |
212 | tz_rules[0].secs = tz_rules[1].secs = 0; | |
213 | tz_rules[0].offset = tz_rules[1].offset = 0L; | |
214 | tz_rules[0].change = tz_rules[1].change = (time_t) -1; | |
215 | tz_rules[0].computed_for = tz_rules[1].computed_for = 0; | |
28f540f4 RM |
216 | return; |
217 | } | |
218 | ||
933e73fa | 219 | /* Clear out old state and reset to unnamed UTC. */ |
28f540f4 | 220 | memset (tz_rules, 0, sizeof tz_rules); |
cd6ede75 | 221 | tz_rules[0].name = tz_rules[1].name = ""; |
28f540f4 RM |
222 | |
223 | /* Get the standard timezone name. */ | |
cd6ede75 UD |
224 | tzbuf = malloc (strlen (tz) + 1); |
225 | if (! tzbuf) | |
68dbb3a6 UD |
226 | { |
227 | /* Clear the old tz name so we will try again. */ | |
228 | free (old_tz); | |
229 | old_tz = NULL; | |
230 | return; | |
231 | } | |
28f540f4 | 232 | |
cd6ede75 UD |
233 | if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 || |
234 | (l = strlen (tzbuf)) < 3) | |
28f540f4 | 235 | { |
cd6ede75 | 236 | free (tzbuf); |
28f540f4 RM |
237 | return; |
238 | } | |
239 | ||
cd6ede75 | 240 | tz_rules[0].name = __tzstring (tzbuf); |
28f540f4 RM |
241 | |
242 | tz += l; | |
243 | ||
933e73fa | 244 | /* Figure out the standard offset from UTC. */ |
1228ed5c | 245 | if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz))) |
cd6ede75 UD |
246 | { |
247 | free (tzbuf); | |
248 | return; | |
249 | } | |
28f540f4 RM |
250 | |
251 | if (*tz == '-' || *tz == '+') | |
252 | tz_rules[0].offset = *tz++ == '-' ? 1L : -1L; | |
253 | else | |
254 | tz_rules[0].offset = -1L; | |
255 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) | |
256 | { | |
257 | default: | |
cd6ede75 | 258 | free (tzbuf); |
28f540f4 RM |
259 | return; |
260 | case 1: | |
261 | mm = 0; | |
262 | case 2: | |
263 | ss = 0; | |
264 | case 3: | |
265 | break; | |
266 | } | |
f65fd747 UD |
267 | tz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) + |
268 | (min (hh, 23) * 60 * 60)); | |
28f540f4 RM |
269 | |
270 | for (l = 0; l < 3; ++l) | |
271 | { | |
272 | while (isdigit(*tz)) | |
273 | ++tz; | |
274 | if (l < 2 && *tz == ':') | |
275 | ++tz; | |
276 | } | |
277 | ||
278 | /* Get the DST timezone name (if any). */ | |
279 | if (*tz != '\0') | |
280 | { | |
cd6ede75 UD |
281 | char *n = tzbuf + strlen (tzbuf) + 1; |
282 | if (sscanf (tz, "%[^0-9,+-]", n) != 1 || | |
283 | (l = strlen (n)) < 3) | |
284 | goto done_names; /* Punt on name, set up the offsets. */ | |
285 | ||
286 | tz_rules[1].name = __tzstring (n); | |
287 | ||
288 | tz += l; | |
fd26970f UD |
289 | |
290 | /* Figure out the DST offset from GMT. */ | |
291 | if (*tz == '-' || *tz == '+') | |
292 | tz_rules[1].offset = *tz++ == '-' ? 1L : -1L; | |
293 | else | |
294 | tz_rules[1].offset = -1L; | |
295 | ||
296 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) | |
297 | { | |
298 | default: | |
299 | /* Default to one hour later than standard time. */ | |
300 | tz_rules[1].offset = tz_rules[0].offset + (60 * 60); | |
301 | break; | |
302 | ||
303 | case 1: | |
304 | mm = 0; | |
305 | case 2: | |
306 | ss = 0; | |
307 | case 3: | |
308 | tz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) + | |
309 | (min (hh, 23) * (60 * 60))); | |
310 | break; | |
311 | } | |
312 | for (l = 0; l < 3; ++l) | |
313 | { | |
314 | while (isdigit (*tz)) | |
315 | ++tz; | |
316 | if (l < 2 && *tz == ':') | |
317 | ++tz; | |
318 | } | |
0413b54c UD |
319 | if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0')) |
320 | { | |
321 | /* There is no rule. See if there is a default rule file. */ | |
322 | __tzfile_default (tz_rules[0].name, tz_rules[1].name, | |
323 | tz_rules[0].offset, tz_rules[1].offset); | |
324 | if (__use_tzfile) | |
325 | { | |
326 | free (old_tz); | |
327 | old_tz = NULL; | |
40a55d20 | 328 | free (tzbuf); |
0413b54c UD |
329 | return; |
330 | } | |
331 | } | |
28f540f4 | 332 | } |
5290baf0 | 333 | else |
40a55d20 UD |
334 | { |
335 | /* There is no DST. */ | |
336 | tz_rules[1].name = tz_rules[0].name; | |
ff152e3f | 337 | tz_rules[1].offset = tz_rules[0].offset; |
40a55d20 UD |
338 | free (tzbuf); |
339 | return; | |
340 | } | |
28f540f4 | 341 | |
28f540f4 | 342 | done_names: |
cd6ede75 | 343 | free (tzbuf); |
28f540f4 | 344 | |
28f540f4 RM |
345 | /* Figure out the standard <-> DST rules. */ |
346 | for (whichrule = 0; whichrule < 2; ++whichrule) | |
347 | { | |
348 | register tz_rule *tzr = &tz_rules[whichrule]; | |
1cbca0d9 | 349 | |
ca34d7a7 UD |
350 | /* Ignore comma to support string following the incorrect |
351 | specification in early POSIX.1 printings. */ | |
352 | tz += *tz == ','; | |
1cbca0d9 | 353 | |
28f540f4 RM |
354 | /* Get the date of the change. */ |
355 | if (*tz == 'J' || isdigit (*tz)) | |
356 | { | |
357 | char *end; | |
358 | tzr->type = *tz == 'J' ? J1 : J0; | |
359 | if (tzr->type == J1 && !isdigit (*++tz)) | |
360 | return; | |
361 | tzr->d = (unsigned short int) strtoul (tz, &end, 10); | |
362 | if (end == tz || tzr->d > 365) | |
363 | return; | |
364 | else if (tzr->type == J1 && tzr->d == 0) | |
365 | return; | |
366 | tz = end; | |
367 | } | |
368 | else if (*tz == 'M') | |
369 | { | |
370 | int n; | |
371 | tzr->type = M; | |
372 | if (sscanf (tz, "M%hu.%hu.%hu%n", | |
373 | &tzr->m, &tzr->n, &tzr->d, &n) != 3 || | |
374 | tzr->m < 1 || tzr->m > 12 || | |
375 | tzr->n < 1 || tzr->n > 5 || tzr->d > 6) | |
376 | return; | |
377 | tz += n; | |
378 | } | |
379 | else if (*tz == '\0') | |
380 | { | |
381 | /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0". */ | |
382 | tzr->type = M; | |
383 | if (tzr == &tz_rules[0]) | |
384 | { | |
385 | tzr->m = 4; | |
386 | tzr->n = 1; | |
387 | tzr->d = 0; | |
388 | } | |
389 | else | |
390 | { | |
391 | tzr->m = 10; | |
392 | tzr->n = 5; | |
393 | tzr->d = 0; | |
394 | } | |
395 | } | |
396 | else | |
397 | return; | |
1cbca0d9 | 398 | |
28f540f4 RM |
399 | if (*tz != '\0' && *tz != '/' && *tz != ',') |
400 | return; | |
401 | else if (*tz == '/') | |
402 | { | |
403 | /* Get the time of day of the change. */ | |
404 | ++tz; | |
405 | if (*tz == '\0') | |
406 | return; | |
407 | switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) | |
408 | { | |
409 | default: | |
410 | hh = 2; /* Default to 2:00 AM. */ | |
411 | case 1: | |
412 | mm = 0; | |
413 | case 2: | |
414 | ss = 0; | |
415 | case 3: | |
416 | break; | |
417 | } | |
418 | for (l = 0; l < 3; ++l) | |
419 | { | |
f65fd747 | 420 | while (isdigit (*tz)) |
28f540f4 RM |
421 | ++tz; |
422 | if (l < 2 && *tz == ':') | |
423 | ++tz; | |
424 | } | |
425 | tzr->secs = (hh * 60 * 60) + (mm * 60) + ss; | |
426 | } | |
427 | else | |
428 | /* Default to 2:00 AM. */ | |
429 | tzr->secs = 2 * 60 * 60; | |
430 | ||
431 | tzr->computed_for = -1; | |
432 | } | |
28f540f4 RM |
433 | } |
434 | \f | |
435 | /* Maximum length of a timezone name. __tz_compute keeps this up to date | |
436 | (never decreasing it) when ! __use_tzfile. | |
437 | tzfile.c keeps it up to date when __use_tzfile. */ | |
f0f1bf85 | 438 | size_t __tzname_cur_max; |
28f540f4 RM |
439 | |
440 | long int | |
f65fd747 | 441 | __tzname_max () |
28f540f4 | 442 | { |
9a0a462c UD |
443 | __libc_lock_lock (tzset_lock); |
444 | ||
445 | tzset_internal (0); | |
446 | ||
447 | __libc_lock_unlock (tzset_lock); | |
28f540f4 RM |
448 | |
449 | return __tzname_cur_max; | |
450 | } | |
451 | \f | |
452 | /* Figure out the exact time (as a time_t) in YEAR | |
453 | when the change described by RULE will occur and | |
454 | put it in RULE->change, saving YEAR in RULE->computed_for. | |
455 | Return nonzero if successful, zero on failure. */ | |
456 | static int | |
dfd2257a | 457 | internal_function |
f65fd747 UD |
458 | compute_change (rule, year) |
459 | tz_rule *rule; | |
460 | int year; | |
28f540f4 RM |
461 | { |
462 | register time_t t; | |
463 | int y; | |
464 | ||
465 | if (year != -1 && rule->computed_for == year) | |
466 | /* Operations on times in 1969 will be slower. Oh well. */ | |
467 | return 1; | |
1cbca0d9 | 468 | |
28f540f4 RM |
469 | /* First set T to January 1st, 0:00:00 GMT in YEAR. */ |
470 | t = 0; | |
471 | for (y = 1970; y < year; ++y) | |
472 | t += SECSPERDAY * (__isleap (y) ? 366 : 365); | |
473 | ||
474 | switch (rule->type) | |
475 | { | |
476 | case J1: | |
477 | /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years. | |
478 | In non-leap years, or if the day number is 59 or less, just | |
479 | add SECSPERDAY times the day number-1 to the time of | |
480 | January 1, midnight, to get the day. */ | |
481 | t += (rule->d - 1) * SECSPERDAY; | |
482 | if (rule->d >= 60 && __isleap (year)) | |
483 | t += SECSPERDAY; | |
484 | break; | |
485 | ||
486 | case J0: | |
487 | /* n - Day of year. | |
488 | Just add SECSPERDAY times the day number to the time of Jan 1st. */ | |
489 | t += rule->d * SECSPERDAY; | |
490 | break; | |
491 | ||
492 | case M: | |
493 | /* Mm.n.d - Nth "Dth day" of month M. */ | |
494 | { | |
9a0a462c UD |
495 | unsigned int i; |
496 | int d, m1, yy0, yy1, yy2, dow; | |
497 | const unsigned short int *myday = | |
80fd7387 | 498 | &__mon_yday[__isleap (year)][rule->m]; |
28f540f4 RM |
499 | |
500 | /* First add SECSPERDAY for each day in months before M. */ | |
80fd7387 | 501 | t += myday[-1] * SECSPERDAY; |
28f540f4 RM |
502 | |
503 | /* Use Zeller's Congruence to get day-of-week of first day of month. */ | |
504 | m1 = (rule->m + 9) % 12 + 1; | |
505 | yy0 = (rule->m <= 2) ? (year - 1) : year; | |
506 | yy1 = yy0 / 100; | |
507 | yy2 = yy0 % 100; | |
508 | dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; | |
509 | if (dow < 0) | |
510 | dow += 7; | |
511 | ||
512 | /* DOW is the day-of-week of the first day of the month. Get the | |
513 | day-of-month (zero-origin) of the first DOW day of the month. */ | |
514 | d = rule->d - dow; | |
515 | if (d < 0) | |
516 | d += 7; | |
517 | for (i = 1; i < rule->n; ++i) | |
518 | { | |
9a0a462c | 519 | if (d + 7 >= (int) myday[0] - myday[-1]) |
28f540f4 RM |
520 | break; |
521 | d += 7; | |
522 | } | |
523 | ||
524 | /* D is the day-of-month (zero-origin) of the day we want. */ | |
525 | t += d * SECSPERDAY; | |
526 | } | |
527 | break; | |
528 | } | |
529 | ||
530 | /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want. | |
531 | Just add the time of day and local offset from GMT, and we're done. */ | |
532 | ||
68dbb3a6 | 533 | rule->change = t - rule->offset + rule->secs; |
28f540f4 RM |
534 | rule->computed_for = year; |
535 | return 1; | |
536 | } | |
537 | ||
538 | ||
539 | /* Figure out the correct timezone for *TIMER and TM (which must be the same) | |
540 | and set `__tzname', `__timezone', and `__daylight' accordingly. | |
541 | Return nonzero on success, zero on failure. */ | |
9a0a462c | 542 | static int |
dfd2257a | 543 | internal_function |
9a0a462c | 544 | tz_compute (timer, tm) |
f65fd747 UD |
545 | time_t timer; |
546 | const struct tm *tm; | |
28f540f4 | 547 | { |
28f540f4 RM |
548 | if (! compute_change (&tz_rules[0], 1900 + tm->tm_year) || |
549 | ! compute_change (&tz_rules[1], 1900 + tm->tm_year)) | |
550 | return 0; | |
551 | ||
ff152e3f UD |
552 | __daylight = tz_rules[0].offset != tz_rules[1].offset; |
553 | __timezone = -tz_rules[0].offset; | |
28f540f4 RM |
554 | __tzname[0] = (char *) tz_rules[0].name; |
555 | __tzname[1] = (char *) tz_rules[1].name; | |
556 | ||
557 | { | |
558 | /* Keep __tzname_cur_max up to date. */ | |
559 | size_t len0 = strlen (__tzname[0]); | |
560 | size_t len1 = strlen (__tzname[1]); | |
561 | if (len0 > __tzname_cur_max) | |
562 | __tzname_cur_max = len0; | |
563 | if (len1 > __tzname_cur_max) | |
564 | __tzname_cur_max = len1; | |
565 | } | |
566 | ||
567 | return 1; | |
568 | } | |
b24be05f | 569 | \f |
b24be05f | 570 | /* Reinterpret the TZ environment variable and set `tzname'. */ |
4311b2a6 | 571 | #undef tzset |
28f540f4 | 572 | |
b24be05f | 573 | void |
10dc2a90 | 574 | __tzset (void) |
b24be05f | 575 | { |
9a0a462c | 576 | __libc_lock_lock (tzset_lock); |
68dbb3a6 | 577 | |
9a0a462c | 578 | tzset_internal (1); |
68dbb3a6 | 579 | |
860d3729 UD |
580 | if (!__use_tzfile) |
581 | { | |
582 | /* Set `tzname'. */ | |
583 | __tzname[0] = (char *) tz_rules[0].name; | |
584 | __tzname[1] = (char *) tz_rules[1].name; | |
585 | } | |
68dbb3a6 | 586 | |
9a0a462c | 587 | __libc_lock_unlock (tzset_lock); |
b24be05f | 588 | } |
10dc2a90 | 589 | weak_alias (__tzset, tzset) |
9a0a462c UD |
590 | \f |
591 | /* Return the `struct tm' representation of *TIMER in the local timezone. | |
592 | Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */ | |
593 | struct tm * | |
594 | __tz_convert (const time_t *timer, int use_localtime, struct tm *tp) | |
595 | { | |
596 | long int leap_correction; | |
597 | int leap_extra_secs; | |
598 | ||
599 | if (timer == NULL) | |
600 | { | |
601 | __set_errno (EINVAL); | |
602 | return NULL; | |
603 | } | |
604 | ||
605 | __libc_lock_lock (tzset_lock); | |
606 | ||
607 | /* Update internal database according to current TZ setting. | |
608 | POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. | |
609 | This is a good idea since this allows at least a bit more parallelism. | |
610 | By analogy we apply the same rule to gmtime_r. */ | |
611 | tzset_internal (tp == &_tmbuf); | |
612 | ||
613 | if (__use_tzfile) | |
614 | { | |
615 | if (! __tzfile_compute (*timer, use_localtime, | |
a709dd43 | 616 | &leap_correction, &leap_extra_secs, tp)) |
9a0a462c UD |
617 | tp = NULL; |
618 | } | |
619 | else | |
620 | { | |
fe0ec73e | 621 | if (! (__offtime (timer, 0, tp) && tz_compute (*timer, tp))) |
9a0a462c UD |
622 | tp = NULL; |
623 | leap_correction = 0L; | |
624 | leap_extra_secs = 0; | |
625 | } | |
626 | ||
627 | if (tp) | |
628 | { | |
629 | if (use_localtime) | |
630 | { | |
a709dd43 UD |
631 | if (!__use_tzfile) |
632 | { | |
633 | int isdst = (*timer >= tz_rules[0].change | |
634 | && *timer < tz_rules[1].change); | |
635 | tp->tm_isdst = isdst; | |
636 | tp->tm_zone = __tzname[isdst]; | |
637 | tp->tm_gmtoff = tz_rules[isdst].offset; | |
638 | } | |
9a0a462c UD |
639 | } |
640 | else | |
641 | { | |
642 | tp->tm_isdst = 0; | |
643 | tp->tm_zone = "GMT"; | |
644 | tp->tm_gmtoff = 0L; | |
645 | } | |
646 | ||
fe0ec73e UD |
647 | if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp)) |
648 | tp->tm_sec += leap_extra_secs; | |
649 | else | |
650 | tp = NULL; | |
9a0a462c UD |
651 | } |
652 | ||
653 | __libc_lock_unlock (tzset_lock); | |
654 | ||
655 | return tp; | |
656 | } |