]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/tzfile.c
Update from translation team.
[thirdparty/glibc.git] / time / tzfile.c
CommitLineData
29143408 1/* Copyright (C) 1991-1993,1995-2001,2003,2004,2006,2007,2009
11bf311e 2 Free Software Foundation, Inc.
860d3729 3 This file is part of the GNU C Library.
28f540f4 4
860d3729 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
28f540f4 9
860d3729
UD
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
28f540f4 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
28f540f4 19
a3e0e9ae
UD
20#include <assert.h>
21#include <limits.h>
28f540f4 22#include <stdio.h>
2706ee38 23#include <stdio_ext.h>
a3e0e9ae 24#include <stdlib.h>
28f540f4 25#include <string.h>
a3e0e9ae 26#include <time.h>
9d187dd4 27#include <unistd.h>
fe6cc2ae 28#include <sys/stat.h>
28f540f4
RM
29
30#define NOID
14ea22e9 31#include <timezone/tzfile.h>
28f540f4 32
c4563d2d 33int __use_tzfile;
fe6cc2ae
UD
34static dev_t tzfile_dev;
35static ino64_t tzfile_ino;
8930fcf9 36static time_t tzfile_mtime;
28f540f4
RM
37
38struct ttinfo
39 {
40 long int offset; /* Seconds east of GMT. */
41 unsigned char isdst; /* Used to set tm_isdst. */
42 unsigned char idx; /* Index into `zone_names'. */
98fa1d6e
RM
43 unsigned char isstd; /* Transition times are in standard time. */
44 unsigned char isgmt; /* Transition times are in GMT. */
28f540f4
RM
45 };
46
47struct leap
48 {
49 time_t transition; /* Time the transition takes effect. */
50 long int change; /* Seconds of correction to apply. */
51 };
52
dfd2257a 53static void compute_tzname_max (size_t) internal_function;
28f540f4
RM
54
55static size_t num_transitions;
c877418f 56libc_freeres_ptr (static time_t *transitions);
c4563d2d 57static unsigned char *type_idxs;
28f540f4 58static size_t num_types;
c4563d2d
UD
59static struct ttinfo *types;
60static char *zone_names;
ff152e3f
UD
61static long int rule_stdoff;
62static long int rule_dstoff;
28f540f4 63static size_t num_leaps;
c4563d2d 64static struct leap *leaps;
fa76dde2 65static char *tzspec;
28f540f4 66
a2cafe30 67#include <endian.h>
ba9234d9 68#include <byteswap.h>
a2cafe30
RM
69
70/* Decode the four bytes at PTR as a signed integer in network byte order. */
71static inline int
9c7ff11a 72__attribute ((always_inline))
a2cafe30
RM
73decode (const void *ptr)
74{
11bf311e 75 if (BYTE_ORDER == BIG_ENDIAN && sizeof (int) == 4)
b20e47cb 76 return *(const int *) ptr;
11bf311e 77 if (sizeof (int) == 4)
ba9234d9 78 return bswap_32 (*(const int *) ptr);
b20e47cb 79
11bf311e
UD
80 const unsigned char *p = ptr;
81 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
064737fb 82
11bf311e
UD
83 result = (result << 8) | *p++;
84 result = (result << 8) | *p++;
85 result = (result << 8) | *p++;
86 result = (result << 8) | *p++;
87
88 return result;
064737fb
UD
89}
90
11bf311e
UD
91
92static inline int64_t
93__attribute ((always_inline))
94decode64 (const void *ptr)
95{
96 if ((BYTE_ORDER == BIG_ENDIAN))
97 return *(const int64_t *) ptr;
98
99 return bswap_64 (*(const int64_t *) ptr);
100}
101
102
28f540f4 103void
c4563d2d 104__tzfile_read (const char *file, size_t extra, char **extrap)
28f540f4 105{
9d187dd4 106 static const char default_tzdir[] = TZDIR;
98fa1d6e 107 size_t num_isstd, num_isgmt;
28f540f4
RM
108 register FILE *f;
109 struct tzhead tzhead;
110 size_t chars;
111 register size_t i;
c4563d2d
UD
112 size_t total_size;
113 size_t types_idx;
114 size_t leaps_idx;
fe6cc2ae 115 int was_using_tzfile = __use_tzfile;
11bf311e 116 int trans_width = 4;
fa76dde2 117 size_t tzspec_len;
11bf311e
UD
118
119 if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
120 abort ();
28f540f4
RM
121
122 __use_tzfile = 0;
123
a3b58440
RM
124 if (file == NULL)
125 /* No user specification; use the site-wide default. */
28f540f4 126 file = TZDEFAULT;
a3b58440 127 else if (*file == '\0')
9a0a462c 128 /* User specified the empty string; use UTC with no leap seconds. */
640b76b7 129 goto ret_free_transitions;
1228ed5c
UD
130 else
131 {
132 /* We must not allow to read an arbitrary file in a setuid
133 program. So we fail for any file which is not in the
7434ccad
UD
134 directory hierachy starting at TZDIR
135 and which is not the system wide default TZDEFAULT. */
1228ed5c
UD
136 if (__libc_enable_secure
137 && ((*file == '/'
4cca6b86 138 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
1228ed5c
UD
139 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
140 || strstr (file, "../") != NULL))
7434ccad
UD
141 /* This test is certainly a bit too restrictive but it should
142 catch all critical cases. */
640b76b7 143 goto ret_free_transitions;
1228ed5c 144 }
9d187dd4 145
28f540f4
RM
146 if (*file != '/')
147 {
5290baf0
UD
148 const char *tzdir;
149 unsigned int len, tzdir_len;
86187531 150 char *new, *tmp;
5290baf0 151
74955460 152 tzdir = getenv ("TZDIR");
5290baf0
UD
153 if (tzdir == NULL || *tzdir == '\0')
154 {
155 tzdir = default_tzdir;
156 tzdir_len = sizeof (default_tzdir) - 1;
157 }
158 else
159 tzdir_len = strlen (tzdir);
160 len = strlen (file) + 1;
161 new = (char *) __alloca (tzdir_len + 1 + len);
86187531
UD
162 tmp = __mempcpy (new, tzdir, tzdir_len);
163 *tmp++ = '/';
c4563d2d 164 memcpy (tmp, file, len);
28f540f4
RM
165 file = new;
166 }
167
250ecb48
UD
168 /* If we were already using tzfile, check whether the file changed. */
169 struct stat64 st;
170 if (was_using_tzfile
171 && stat64 (file, &st) == 0
172 && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
173 && tzfile_mtime == st.st_mtime)
174 {
175 /* Nothing to do. */
176 __use_tzfile = 1;
177 return;
178 }
179
ee8449f7
UD
180 /* Note the file is opened with cancellation in the I/O functions
181 disabled. */
182 f = fopen (file, "rc");
28f540f4 183 if (f == NULL)
640b76b7 184 goto ret_free_transitions;
28f540f4 185
250ecb48 186 /* Get information about the file we are actually using. */
fe6cc2ae
UD
187 if (fstat64 (fileno (f), &st) != 0)
188 {
189 fclose (f);
640b76b7 190 goto ret_free_transitions;
fe6cc2ae 191 }
fe6cc2ae 192
640b76b7
UD
193 free ((void *) transitions);
194 transitions = NULL;
195
8930fcf9 196 /* Remember the inode and device number and modification time. */
fe6cc2ae
UD
197 tzfile_dev = st.st_dev;
198 tzfile_ino = st.st_ino;
8930fcf9 199 tzfile_mtime = st.st_mtime;
fe6cc2ae 200
2706ee38
UD
201 /* No threads reading this stream. */
202 __fsetlocking (f, FSETLOCKING_BYCALLER);
203
11bf311e 204 read_again:
9c7ff11a 205 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
11bf311e
UD
206 1, f) != 1, 0)
207 || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
28f540f4
RM
208 goto lose;
209
a2cafe30
RM
210 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
211 num_types = (size_t) decode (tzhead.tzh_typecnt);
212 chars = (size_t) decode (tzhead.tzh_charcnt);
213 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
214 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
215 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
28f540f4 216
11bf311e
UD
217 /* For platforms with 64-bit time_t we use the new format if available. */
218 if (sizeof (time_t) == 8 && trans_width == 4
219 && tzhead.tzh_version[0] != '\0')
220 {
221 /* We use the 8-byte format. */
222 trans_width = 8;
223
224 /* Position the stream before the second header. */
225 size_t to_skip = (num_transitions * (4 + 1)
226 + num_types * 6
227 + chars
228 + num_leaps * 8
229 + num_isstd
230 + num_isgmt);
231 if (fseek (f, to_skip, SEEK_CUR) != 0)
232 goto lose;
233
234 goto read_again;
235 }
236
c4563d2d
UD
237 total_size = num_transitions * (sizeof (time_t) + 1);
238 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
239 & ~(__alignof__ (struct ttinfo) - 1));
240 types_idx = total_size;
241 total_size += num_types * sizeof (struct ttinfo) + chars;
242 total_size = ((total_size + __alignof__ (struct leap) - 1)
243 & ~(__alignof__ (struct leap) - 1));
244 leaps_idx = total_size;
245 total_size += num_leaps * sizeof (struct leap);
6a649d25 246 tzspec_len = (sizeof (time_t) == 8 && trans_width == 8
fa76dde2
UD
247 ? st.st_size - (ftello (f)
248 + num_transitions * (8 + 1)
249 + num_types * 6
250 + chars
6355c997 251 + num_leaps * 12
fa76dde2
UD
252 + num_isstd
253 + num_isgmt) - 1 : 0);
c4563d2d 254
11bf311e
UD
255 /* Allocate enough memory including the extra block requested by the
256 caller. */
fa76dde2 257 transitions = (time_t *) malloc (total_size + tzspec_len + extra);
c4563d2d
UD
258 if (transitions == NULL)
259 goto lose;
260
261 type_idxs = (unsigned char *) transitions + (num_transitions
262 * sizeof (time_t));
263 types = (struct ttinfo *) ((char *) transitions + types_idx);
264 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
265 leaps = (struct leap *) ((char *) transitions + leaps_idx);
6a649d25 266 if (sizeof (time_t) == 8 && trans_width == 8)
69819d92 267 tzspec = (char *) leaps + num_leaps * sizeof (struct leap) + extra;
fa76dde2
UD
268 else
269 tzspec = NULL;
c4563d2d
UD
270 if (extra > 0)
271 *extrap = (char *) &leaps[num_leaps];
272
6a649d25 273 if (sizeof (time_t) == 4 || __builtin_expect (trans_width == 8, 1))
28f540f4 274 {
11bf311e
UD
275 if (__builtin_expect (fread_unlocked (transitions, trans_width + 1,
276 num_transitions, f)
277 != num_transitions, 0))
28f540f4
RM
278 goto lose;
279 }
c4563d2d 280 else
28f540f4 281 {
9c7ff11a
UD
282 if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
283 != num_transitions, 0)
284 || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
285 f) != num_transitions, 0))
28f540f4
RM
286 goto lose;
287 }
288
5290baf0
UD
289 /* Check for bogus indices in the data file, so we can hereafter
290 safely use type_idxs[T] as indices into `types' and never crash. */
291 for (i = 0; i < num_transitions; ++i)
9c7ff11a 292 if (__builtin_expect (type_idxs[i] >= num_types, 0))
5290baf0
UD
293 goto lose;
294
11bf311e
UD
295 if ((BYTE_ORDER != BIG_ENDIAN && (sizeof (time_t) == 4 || trans_width == 4))
296 || (BYTE_ORDER == BIG_ENDIAN && sizeof (time_t) == 8
297 && trans_width == 4))
a2cafe30
RM
298 {
299 /* Decode the transition times, stored as 4-byte integers in
300 network (big-endian) byte order. We work from the end of
301 the array so as not to clobber the next element to be
302 processed when sizeof (time_t) > 4. */
303 i = num_transitions;
b20e47cb 304 while (i-- > 0)
a3e0e9ae 305 transitions[i] = decode ((char *) transitions + i * 4);
a2cafe30 306 }
11bf311e
UD
307 else if (BYTE_ORDER != BIG_ENDIAN && sizeof (time_t) == 8)
308 {
309 /* Decode the transition times, stored as 8-byte integers in
310 network (big-endian) byte order. */
311 for (i = 0; i < num_transitions; ++i)
312 transitions[i] = decode64 ((char *) transitions + i * 8);
313 }
28f540f4
RM
314
315 for (i = 0; i < num_types; ++i)
316 {
317 unsigned char x[4];
c4563d2d 318 int c;
9c7ff11a
UD
319 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
320 0))
28f540f4 321 goto lose;
c4563d2d 322 c = getc_unlocked (f);
9c7ff11a 323 if (__builtin_expect ((unsigned int) c > 1u, 0))
8b7fb588 324 goto lose;
c4563d2d
UD
325 types[i].isdst = c;
326 c = getc_unlocked (f);
9c7ff11a
UD
327 if (__builtin_expect ((size_t) c > chars, 0))
328 /* Bogus index in data file. */
5290baf0 329 goto lose;
c4563d2d 330 types[i].idx = c;
a2cafe30 331 types[i].offset = (long int) decode (x);
28f540f4
RM
332 }
333
9c7ff11a 334 if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
28f540f4
RM
335 goto lose;
336
337 for (i = 0; i < num_leaps; ++i)
338 {
11bf311e
UD
339 unsigned char x[8];
340 if (__builtin_expect (fread_unlocked (x, 1, trans_width, f)
341 != trans_width, 0))
28f540f4 342 goto lose;
11bf311e
UD
343 if (sizeof (time_t) == 4 || trans_width == 4)
344 leaps[i].transition = (time_t) decode (x);
345 else
346 leaps[i].transition = (time_t) decode64 (x);
347
348 if (__builtin_expect (fread_unlocked (x, 1, 4, f) != 4, 0))
28f540f4 349 goto lose;
a2cafe30 350 leaps[i].change = (long int) decode (x);
28f540f4
RM
351 }
352
353 for (i = 0; i < num_isstd; ++i)
354 {
ba9234d9 355 int c = getc_unlocked (f);
9c7ff11a 356 if (__builtin_expect (c == EOF, 0))
28f540f4
RM
357 goto lose;
358 types[i].isstd = c != 0;
359 }
360 while (i < num_types)
361 types[i++].isstd = 0;
362
98fa1d6e
RM
363 for (i = 0; i < num_isgmt; ++i)
364 {
ba9234d9 365 int c = getc_unlocked (f);
9c7ff11a 366 if (__builtin_expect (c == EOF, 0))
98fa1d6e
RM
367 goto lose;
368 types[i].isgmt = c != 0;
369 }
370 while (i < num_types)
371 types[i++].isgmt = 0;
372
fa76dde2 373 /* Read the POSIX TZ-style information if possible. */
c6d381d3 374 if (sizeof (time_t) == 8 && tzspec != NULL)
fa76dde2
UD
375 {
376 /* Skip over the newline first. */
377 if (getc_unlocked (f) != '\n'
c6d381d3
UD
378 || (fread_unlocked (tzspec, 1, tzspec_len - 1, f)
379 != tzspec_len - 1))
fa76dde2
UD
380 tzspec = NULL;
381 else
382 tzspec[tzspec_len - 1] = '\0';
383 }
94a749f6 384 else if (sizeof (time_t) == 4 && tzhead.tzh_version[0] != '\0')
c6d381d3
UD
385 {
386 /* Get the TZ string. */
387 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
388 1, f) != 1, 0)
389 || (memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic))
390 != 0))
391 goto lose;
392
393 size_t num_transitions2 = (size_t) decode (tzhead.tzh_timecnt);
394 size_t num_types2 = (size_t) decode (tzhead.tzh_typecnt);
395 size_t chars2 = (size_t) decode (tzhead.tzh_charcnt);
396 size_t num_leaps2 = (size_t) decode (tzhead.tzh_leapcnt);
397 size_t num_isstd2 = (size_t) decode (tzhead.tzh_ttisstdcnt);
398 size_t num_isgmt2 = (size_t) decode (tzhead.tzh_ttisgmtcnt);
399
400 /* Position the stream before the second header. */
401 size_t to_skip = (num_transitions2 * (8 + 1)
402 + num_types2 * 6
403 + chars2
404 + num_leaps2 * 12
405 + num_isstd2
406 + num_isgmt2);
407 off_t off;
408 if (fseek (f, to_skip, SEEK_CUR) != 0
409 || (off = ftello (f)) < 0
410 || st.st_size < off + 2)
411 goto lose;
412
413 tzspec_len = st.st_size - off - 1;
414 char *tzstr = alloca (tzspec_len);
415 if (getc_unlocked (f) != '\n'
416 || (fread_unlocked (tzstr, 1, tzspec_len - 1, f) != tzspec_len - 1))
417 goto lose;
418 tzstr[tzspec_len - 1] = '\0';
419 tzspec = __tzstring (tzstr);
420 }
11bf311e 421
860d3729
UD
422 fclose (f);
423
a3e0e9ae
UD
424 /* First "register" all timezone names. */
425 for (i = 0; i < num_types; ++i)
426 (void) __tzstring (&zone_names[types[i].idx]);
427
ff152e3f
UD
428 /* Find the standard and daylight time offsets used by the rule file.
429 We choose the offsets in the types of each flavor that are
430 transitioned to earliest in time. */
a3e0e9ae 431 __tzname[0] = NULL;
ff152e3f 432 __tzname[1] = NULL;
a3e0e9ae
UD
433 for (i = num_transitions; i > 0; )
434 {
435 int type = type_idxs[--i];
436 int dst = types[type].isdst;
a3e0e9ae
UD
437
438 if (__tzname[dst] == NULL)
439 {
d3345073
UD
440 int idx = types[type].idx;
441
a3e0e9ae
UD
442 __tzname[dst] = __tzstring (&zone_names[idx]);
443
444 if (__tzname[1 - dst] != NULL)
445 break;
446 }
447 }
448 if (__tzname[0] == NULL)
449 {
450 /* This should only happen if there are no transition rules.
451 In this case there should be only one single type. */
452 assert (num_types == 1);
453 __tzname[0] = __tzstring (zone_names);
454 }
ff152e3f
UD
455 if (__tzname[1] == NULL)
456 __tzname[1] = __tzname[0];
28f540f4
RM
457
458 compute_tzname_max (chars);
a3b58440 459
90865aa8 460 if (num_transitions == 0)
0155a773 461 /* Use the first rule (which should also be the only one). */
90865aa8
UD
462 rule_stdoff = rule_dstoff = types[0].offset;
463 else
ff152e3f 464 {
d16e36e0 465 int stdoff_set = 0, dstoff_set = 0;
90865aa8 466 rule_stdoff = rule_dstoff = 0;
d3345073
UD
467 i = num_transitions - 1;
468 do
90865aa8 469 {
d16e36e0
AS
470 if (!stdoff_set && !types[type_idxs[i]].isdst)
471 {
472 stdoff_set = 1;
473 rule_stdoff = types[type_idxs[i]].offset;
474 }
d3345073 475 else if (!dstoff_set && types[type_idxs[i]].isdst)
d16e36e0
AS
476 {
477 dstoff_set = 1;
478 rule_dstoff = types[type_idxs[i]].offset;
479 }
480 if (stdoff_set && dstoff_set)
90865aa8
UD
481 break;
482 }
d3345073
UD
483 while (i-- > 0);
484
d16e36e0
AS
485 if (!dstoff_set)
486 rule_dstoff = rule_stdoff;
ff152e3f
UD
487 }
488
489 __daylight = rule_stdoff != rule_dstoff;
490 __timezone = -rule_stdoff;
491
28f540f4
RM
492 __use_tzfile = 1;
493 return;
494
ba9234d9
UD
495 lose:
496 fclose (f);
640b76b7
UD
497 ret_free_transitions:
498 free ((void *) transitions);
499 transitions = NULL;
28f540f4
RM
500}
501\f
98fa1d6e
RM
502/* The user specified a hand-made timezone, but not its DST rules.
503 We will use the names and offsets from the user, and the rules
504 from the TZDEFRULES file. */
505
28f540f4 506void
cd6ede75
UD
507__tzfile_default (const char *std, const char *dst,
508 long int stdoff, long int dstoff)
28f540f4 509{
c4563d2d
UD
510 size_t stdlen = strlen (std) + 1;
511 size_t dstlen = strlen (dst) + 1;
512 size_t i;
98fa1d6e 513 int isdst;
c4563d2d 514 char *cp;
28f540f4 515
c4563d2d 516 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
28f540f4
RM
517 if (!__use_tzfile)
518 return;
519
520 if (num_types < 2)
521 {
522 __use_tzfile = 0;
523 return;
524 }
525
c4563d2d
UD
526 /* Ignore the zone names read from the file and use the given ones
527 instead. */
528 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
529 zone_names = cp;
28f540f4 530
88455219
UD
531 /* Now there are only two zones, regardless of what the file contained. */
532 num_types = 2;
533
98fa1d6e
RM
534 /* Now correct the transition times for the user-specified standard and
535 daylight offsets from GMT. */
536 isdst = 0;
98fa1d6e
RM
537 for (i = 0; i < num_transitions; ++i)
538 {
539 struct ttinfo *trans_type = &types[type_idxs[i]];
540
541 /* We will use only types 0 (standard) and 1 (daylight).
542 Fix up this transition to point to whichever matches
543 the flavor of its original type. */
544 type_idxs[i] = trans_type->isdst;
545
546 if (trans_type->isgmt)
547 /* The transition time is in GMT. No correction to apply. */ ;
548 else if (isdst && !trans_type->isstd)
549 /* The type says this transition is in "local wall clock time", and
550 wall clock time as of the previous transition was DST. Correct
551 for the difference between the rule's DST offset and the user's
552 DST offset. */
553 transitions[i] += dstoff - rule_dstoff;
554 else
555 /* This transition is in "local wall clock time", and wall clock
556 time as of this iteration is non-DST. Correct for the
557 difference between the rule's standard offset and the user's
558 standard offset. */
559 transitions[i] += stdoff - rule_stdoff;
560
561 /* The DST state of "local wall clock time" for the next iteration is
562 as specified by this transition. */
563 isdst = trans_type->isdst;
564 }
565
4c326621
UD
566 /* Now that we adjusted the transitions to the requested offsets,
567 reset the rule_stdoff and rule_dstoff values appropriately. They
568 are used elsewhere. */
569 rule_stdoff = stdoff;
570 rule_dstoff = dstoff;
571
98fa1d6e
RM
572 /* Reset types 0 and 1 to describe the user's settings. */
573 types[0].idx = 0;
574 types[0].offset = stdoff;
575 types[0].isdst = 0;
576 types[1].idx = stdlen;
577 types[1].offset = dstoff;
578 types[1].isdst = 1;
28f540f4 579
5290baf0 580 /* Reset the zone names to point to the user's names. */
cd6ede75
UD
581 __tzname[0] = (char *) std;
582 __tzname[1] = (char *) dst;
5290baf0 583
90865aa8
UD
584 /* Set the timezone. */
585 __timezone = -types[0].offset;
586
28f540f4
RM
587 compute_tzname_max (stdlen + dstlen);
588}
589\f
fa76dde2
UD
590void
591__tzfile_compute (time_t timer, int use_localtime,
592 long int *leap_correct, int *leap_hit,
593 struct tm *tp)
28f540f4 594{
fa76dde2 595 register size_t i;
89dc9d4c 596
fa76dde2 597 if (use_localtime)
28f540f4 598 {
fa76dde2
UD
599 __tzname[0] = NULL;
600 __tzname[1] = NULL;
601
c6d381d3 602 if (__builtin_expect (num_transitions == 0 || timer < transitions[0], 0))
89dc9d4c 603 {
fa76dde2
UD
604 /* TIMER is before any transition (or there are no transitions).
605 Choose the first non-DST type
606 (or the first if they're all DST types). */
607 i = 0;
608 while (i < num_types && types[i].isdst)
609 {
610 if (__tzname[1] == NULL)
611 __tzname[1] = __tzstring (&zone_names[types[i].idx]);
89dc9d4c 612
fa76dde2
UD
613 ++i;
614 }
89dc9d4c 615
fa76dde2
UD
616 if (i == num_types)
617 i = 0;
618 __tzname[0] = __tzstring (&zone_names[types[i].idx]);
619 if (__tzname[1] == NULL)
620 {
621 size_t j = i;
622 while (j < num_types)
623 if (types[j].isdst)
624 {
625 __tzname[1] = __tzstring (&zone_names[types[j].idx]);
626 break;
627 }
628 else
629 ++j;
630 }
631 }
c6d381d3 632 else if (__builtin_expect (timer >= transitions[num_transitions - 1], 0))
89dc9d4c 633 {
c6d381d3 634 if (__builtin_expect (tzspec == NULL, 0))
fa76dde2
UD
635 {
636 use_last:
e2cceb5a 637 i = num_transitions;
fa76dde2
UD
638 goto found;
639 }
640
641 /* Parse the POSIX TZ-style string. */
642 __tzset_parse_tz (tzspec);
643
644 /* Convert to broken down structure. If this fails do not
645 use the string. */
c6d381d3 646 if (__builtin_expect (! __offtime (&timer, 0, tp), 0))
fa76dde2
UD
647 goto use_last;
648
649 /* Use the rules from the TZ string to compute the change. */
650 __tz_compute (timer, tp, 1);
651
c6d381d3
UD
652 /* If tzspec comes from posixrules loaded by __tzfile_default,
653 override the STD and DST zone names with the ones user
654 requested in TZ envvar. */
655 if (__builtin_expect (zone_names == (char *) &leaps[num_leaps], 0))
656 {
657 assert (num_types == 2);
658 __tzname[0] = __tzstring (zone_names);
659 __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]);
660 }
661
29143408 662 goto leap;
89dc9d4c 663 }
fa76dde2 664 else
13252678 665 {
fa76dde2
UD
666 /* Find the first transition after TIMER, and
667 then pick the type of the transition before it. */
668 size_t lo = 0;
669 size_t hi = num_transitions - 1;
670 /* Assume that DST is changing twice a year and guess initial
671 search spot from it.
672 Half of a gregorian year has on average 365.2425 * 86400 / 2
673 = 15778476 seconds. */
674 i = (transitions[num_transitions - 1] - timer) / 15778476;
675 if (i < num_transitions)
13252678 676 {
fa76dde2
UD
677 i = num_transitions - 1 - i;
678 if (timer < transitions[i])
13252678 679 {
fa76dde2
UD
680 if (i < 10 || timer >= transitions[i - 10])
681 {
682 /* Linear search. */
683 while (timer < transitions[i - 1])
684 --i;
685 goto found;
686 }
687 hi = i - 10;
13252678 688 }
fa76dde2 689 else
13252678 690 {
fa76dde2
UD
691 if (i + 10 >= num_transitions || timer < transitions[i + 10])
692 {
693 /* Linear search. */
694 while (timer >= transitions[i])
695 ++i;
696 goto found;
697 }
698 lo = i + 10;
13252678 699 }
13252678 700 }
13252678 701
fa76dde2
UD
702 /* Binary search. */
703 /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
704 while (lo + 1 < hi)
705 {
706 i = (lo + hi) / 2;
707 if (timer < transitions[i])
708 hi = i;
709 else
710 lo = i;
711 }
712 i = hi;
89dc9d4c 713
fa76dde2 714 found:
e2cceb5a
UD
715 /* assert (timer >= transitions[i - 1]
716 && (i == num_transitions || timer < transitions[i])); */
fa76dde2
UD
717 __tzname[types[type_idxs[i - 1]].isdst]
718 = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
719 size_t j = i;
720 while (j < num_transitions)
89dc9d4c 721 {
fa76dde2
UD
722 int type = type_idxs[j];
723 int dst = types[type].isdst;
724 int idx = types[type].idx;
89dc9d4c 725
fa76dde2
UD
726 if (__tzname[dst] == NULL)
727 {
728 __tzname[dst] = __tzstring (&zone_names[idx]);
89dc9d4c 729
fa76dde2
UD
730 if (__tzname[1 - dst] != NULL)
731 break;
732 }
89dc9d4c 733
fa76dde2
UD
734 ++j;
735 }
28f540f4 736
c6d381d3
UD
737 if (__builtin_expect (__tzname[0] == NULL, 0))
738 __tzname[0] = __tzname[1];
739
fa76dde2
UD
740 i = type_idxs[i - 1];
741 }
860d3729 742
fa76dde2 743 struct ttinfo *info = &types[i];
ff152e3f
UD
744 __daylight = rule_stdoff != rule_dstoff;
745 __timezone = -rule_stdoff;
a3e0e9ae 746
a3e0e9ae
UD
747 if (__tzname[0] == NULL)
748 {
749 /* This should only happen if there are no transition rules.
750 In this case there should be only one single type. */
751 assert (num_types == 1);
752 __tzname[0] = __tzstring (zone_names);
753 }
ff152e3f
UD
754 if (__tzname[1] == NULL)
755 /* There is no daylight saving time. */
756 __tzname[1] = __tzname[0];
a709dd43 757 tp->tm_isdst = info->isdst;
89dc9d4c
UD
758 assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
759 tp->tm_zone = __tzname[tp->tm_isdst];
a709dd43 760 tp->tm_gmtoff = info->offset;
9a0a462c 761 }
28f540f4 762
29143408 763 leap:
28f540f4
RM
764 *leap_correct = 0L;
765 *leap_hit = 0;
766
767 /* Find the last leap second correction transition time before TIMER. */
768 i = num_leaps;
769 do
770 if (i-- == 0)
38e68573 771 return;
28f540f4
RM
772 while (timer < leaps[i].transition);
773
774 /* Apply its correction. */
775 *leap_correct = leaps[i].change;
776
777 if (timer == leaps[i].transition && /* Exactly at the transition time. */
778 ((i == 0 && leaps[i].change > 0) ||
779 leaps[i].change > leaps[i - 1].change))
780 {
781 *leap_hit = 1;
ba9234d9
UD
782 while (i > 0
783 && leaps[i].transition == leaps[i - 1].transition + 1
784 && leaps[i].change == leaps[i - 1].change + 1)
28f540f4
RM
785 {
786 ++*leap_hit;
787 --i;
788 }
789 }
28f540f4
RM
790}
791\f
9a0a462c 792static void
dfd2257a 793internal_function
860d3729 794compute_tzname_max (size_t chars)
28f540f4 795{
28f540f4
RM
796 const char *p;
797
798 p = zone_names;
799 do
800 {
801 const char *start = p;
802 while (*p != '\0')
803 ++p;
5290baf0 804 if ((size_t) (p - start) > __tzname_cur_max)
28f540f4 805 __tzname_cur_max = p - start;
ba9234d9
UD
806 }
807 while (++p < &zone_names[chars]);
28f540f4 808}