1 /* Copyright (C) 1991, 92, 93, 95, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
32 long int offset; /* Seconds east of GMT. */
33 unsigned char isdst; /* Used to set tm_isdst. */
34 unsigned char idx; /* Index into `zone_names'. */
35 unsigned char isstd; /* Transition times are in standard time. */
36 unsigned char isgmt; /* Transition times are in GMT. */
41 time_t transition; /* Time the transition takes effect. */
42 long int change; /* Seconds of correction to apply. */
45 static struct ttinfo *find_transition (time_t timer);
46 static void compute_tzname_max (size_t);
48 static size_t num_transitions;
49 static time_t *transitions = NULL;
50 static unsigned char *type_idxs = NULL;
51 static size_t num_types;
52 static struct ttinfo *types = NULL;
53 static char *zone_names = NULL;
54 static size_t num_leaps;
55 static struct leap *leaps = NULL;
59 /* Decode the four bytes at PTR as a signed integer in network byte order. */
61 decode (const void *ptr)
63 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
64 return *(const int *) ptr;
67 const unsigned char *p = ptr;
68 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
70 result = (result << 8) | *p++;
71 result = (result << 8) | *p++;
72 result = (result << 8) | *p++;
73 result = (result << 8) | *p++;
80 __tzfile_read (const char *file)
82 size_t num_isstd, num_isgmt;
91 if (transitions != NULL)
92 free ((void *) transitions);
94 if (type_idxs != NULL)
95 free ((void *) type_idxs);
98 free ((void *) types);
100 if (zone_names != NULL)
101 free ((void *) zone_names);
104 free ((void *) leaps);
108 /* No user specification; use the site-wide default. */
110 else if (*file == '\0')
111 /* User specified the empty string; use UTC explicitly. */
116 static const char default_tzdir[] = TZDIR;
118 unsigned int len, tzdir_len;
121 tzdir = __secure_getenv ("TZDIR");
122 if (tzdir == NULL || *tzdir == '\0')
124 tzdir = default_tzdir;
125 tzdir_len = sizeof (default_tzdir) - 1;
128 tzdir_len = strlen (tzdir);
129 len = strlen (file) + 1;
130 new = (char *) __alloca (tzdir_len + 1 + len);
131 memcpy (new, tzdir, tzdir_len);
132 new[tzdir_len] = '/';
133 memcpy (&new[tzdir_len + 1], file, len);
137 f = fopen (file, "r");
141 if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
144 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
145 num_types = (size_t) decode (tzhead.tzh_typecnt);
146 chars = (size_t) decode (tzhead.tzh_charcnt);
147 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
148 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
149 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
151 if (num_transitions > 0)
153 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
154 if (transitions == NULL)
156 type_idxs = (unsigned char *) malloc (num_transitions);
157 if (type_idxs == NULL)
162 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
168 zone_names = (char *) malloc (chars);
169 if (zone_names == NULL)
174 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
179 if (sizeof (time_t) < 4)
182 if (fread(transitions, 4, num_transitions, f) != num_transitions ||
183 fread(type_idxs, 1, num_transitions, f) != num_transitions)
186 /* Check for bogus indices in the data file, so we can hereafter
187 safely use type_idxs[T] as indices into `types' and never crash. */
188 for (i = 0; i < num_transitions; ++i)
189 if (type_idxs[i] >= num_types)
192 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
194 /* Decode the transition times, stored as 4-byte integers in
195 network (big-endian) byte order. We work from the end of
196 the array so as not to clobber the next element to be
197 processed when sizeof (time_t) > 4. */
200 transitions[i] = decode ((char *) transitions + i*4);
203 for (i = 0; i < num_types; ++i)
206 if (fread (x, 1, 4, f) != 4 ||
207 fread (&types[i].isdst, 1, 1, f) != 1 ||
208 fread (&types[i].idx, 1, 1, f) != 1)
210 if (types[i].idx >= chars) /* Bogus index in data file. */
212 types[i].offset = (long int) decode (x);
215 if (fread (zone_names, 1, chars, f) != chars)
218 for (i = 0; i < num_leaps; ++i)
221 if (fread (x, 1, sizeof (x), f) != sizeof (x))
223 leaps[i].transition = (time_t) decode (x);
224 if (fread (x, 1, sizeof (x), f) != sizeof (x))
226 leaps[i].change = (long int) decode (x);
229 for (i = 0; i < num_isstd; ++i)
234 types[i].isstd = c != 0;
236 while (i < num_types)
237 types[i++].isstd = 0;
239 for (i = 0; i < num_isgmt; ++i)
244 types[i].isgmt = c != 0;
246 while (i < num_types)
247 types[i++].isgmt = 0;
251 info = find_transition (0);
252 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
254 __tzname[types[i].isdst] = &zone_names[types[i].idx];
255 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
256 __tzname[info->isdst] = &zone_names[info->idx];
258 compute_tzname_max (chars);
267 /* The user specified a hand-made timezone, but not its DST rules.
268 We will use the names and offsets from the user, and the rules
269 from the TZDEFRULES file. */
272 __tzfile_default (char *std, char *dst, long int stdoff, long int dstoff)
274 size_t stdlen, dstlen, i;
275 long int rule_offset, rule_stdoff, rule_dstoff;
278 __tzfile_read (TZDEFRULES);
288 /* Ignore the zone names read from the file. */
291 /* Use the names the user specified. */
292 stdlen = strlen (std) + 1;
293 dstlen = strlen (dst) + 1;
294 zone_names = malloc (stdlen + dstlen);
295 if (zone_names == NULL)
300 memcpy (zone_names, std, stdlen);
301 memcpy (&zone_names[stdlen], dst, dstlen);
303 /* Find the standard and daylight time offsets used by the rule file.
304 We choose the offsets in the types of each flavor that are
305 transitioned to earliest in time. */
306 rule_stdoff = rule_dstoff = 0;
307 for (i = 0; i < num_transitions; ++i)
309 if (!rule_stdoff && !types[type_idxs[i]].isdst)
310 rule_stdoff = types[type_idxs[i]].offset;
311 if (!rule_dstoff && types[type_idxs[i]].isdst)
312 rule_dstoff = types[type_idxs[i]].offset;
313 if (rule_stdoff && rule_dstoff)
317 /* Now correct the transition times for the user-specified standard and
318 daylight offsets from GMT. */
320 rule_offset = rule_offset;
321 for (i = 0; i < num_transitions; ++i)
323 struct ttinfo *trans_type = &types[type_idxs[i]];
325 /* We will use only types 0 (standard) and 1 (daylight).
326 Fix up this transition to point to whichever matches
327 the flavor of its original type. */
328 type_idxs[i] = trans_type->isdst;
330 if (trans_type->isgmt)
331 /* The transition time is in GMT. No correction to apply. */ ;
332 else if (isdst && !trans_type->isstd)
333 /* The type says this transition is in "local wall clock time", and
334 wall clock time as of the previous transition was DST. Correct
335 for the difference between the rule's DST offset and the user's
337 transitions[i] += dstoff - rule_dstoff;
339 /* This transition is in "local wall clock time", and wall clock
340 time as of this iteration is non-DST. Correct for the
341 difference between the rule's standard offset and the user's
343 transitions[i] += stdoff - rule_stdoff;
345 /* The DST state of "local wall clock time" for the next iteration is
346 as specified by this transition. */
347 isdst = trans_type->isdst;
350 /* Reset types 0 and 1 to describe the user's settings. */
352 types[0].offset = stdoff;
354 types[1].idx = stdlen;
355 types[1].offset = dstoff;
358 /* Reset the zone names to point to the user's names. */
359 __tzname[0] = &zone_names[0];
360 __tzname[1] = &zone_names[stdlen];
362 compute_tzname_max (stdlen + dstlen);
365 static struct ttinfo *
366 find_transition (time_t timer)
370 if (num_transitions == 0 || timer < transitions[0])
372 /* TIMER is before any transition (or there are no transitions).
373 Choose the first non-DST type
374 (or the first if they're all DST types). */
376 while (i < num_types && types[i].isdst)
383 /* Find the first transition after TIMER, and
384 then pick the type of the transition before it. */
385 for (i = 1; i < num_transitions; ++i)
386 if (timer < transitions[i])
388 i = type_idxs[i - 1];
395 __tzfile_compute (time_t timer, long int *leap_correct, int *leap_hit)
400 info = find_transition (timer);
401 __daylight = info->isdst;
402 __timezone = info->offset;
403 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
405 __tzname[types[i].isdst] = &zone_names[types[i].idx];
406 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
407 __tzname[info->isdst] = &zone_names[info->idx];
412 /* Find the last leap second correction transition time before TIMER. */
417 while (timer < leaps[i].transition);
419 /* Apply its correction. */
420 *leap_correct = leaps[i].change;
422 if (timer == leaps[i].transition && /* Exactly at the transition time. */
423 ((i == 0 && leaps[i].change > 0) ||
424 leaps[i].change > leaps[i - 1].change))
428 leaps[i].transition == leaps[i - 1].transition + 1 &&
429 leaps[i].change == leaps[i - 1].change + 1)
440 compute_tzname_max (size_t chars)
442 extern size_t __tzname_cur_max; /* Defined in __tzset.c. */
449 const char *start = p;
452 if ((size_t) (p - start) > __tzname_cur_max)
453 __tzname_cur_max = p - start;
454 } while (++p < &zone_names[chars]);