1 /* Copyright (C) 1991, 1992, 1993 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
30 #define __tzname tzname
31 #define __daylight daylight
32 #define __timezone timezone
39 long int offset; /* Seconds east of GMT. */
40 unsigned char isdst; /* Used to set tm_isdst. */
41 unsigned char idx; /* Index into `zone_names'. */
42 unsigned char isstd; /* Transition times are standard time. */
47 time_t transition; /* Time the transition takes effect. */
48 long int change; /* Seconds of correction to apply. */
51 static void compute_tzname_max __P ((size_t));
53 static size_t num_transitions;
54 static time_t *transitions = NULL;
55 static unsigned char *type_idxs = NULL;
56 static size_t num_types;
57 static struct ttinfo *types = NULL;
58 static char *zone_names = NULL;
59 static size_t num_leaps;
60 static struct leap *leaps = NULL;
62 #define uc2ul(x) _uc2ul((unsigned char *) (x))
64 ((x)[3] + ((x)[2] << CHAR_BIT) + ((x)[1] << (2 * CHAR_BIT)) + \
65 ((x)[0] << (3 * CHAR_BIT)))
68 DEFUN(__tzfile_read, (file), CONST char *file)
78 if (transitions != NULL)
79 free((PTR) transitions);
81 if (type_idxs != NULL)
82 free((PTR) type_idxs);
87 if (zone_names != NULL)
88 free((PTR) zone_names);
94 if (file == NULL || *file == '\0')
99 static CONST char tzdir[] = TZDIR;
100 register CONST unsigned int len = strlen(file) + 1;
101 char *new = (char *) __alloca(sizeof(tzdir) + len);
102 memcpy(new, tzdir, sizeof(tzdir) - 1);
103 new[sizeof(tzdir) - 1] = '/';
104 memcpy(&new[sizeof(tzdir)], file, len);
108 f = fopen(file, "r");
112 if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1)
115 num_transitions = (size_t) uc2ul(tzhead.tzh_timecnt);
116 num_types = (size_t) uc2ul(tzhead.tzh_typecnt);
117 chars = (size_t) uc2ul(tzhead.tzh_charcnt);
118 num_leaps = (size_t) uc2ul(tzhead.tzh_leapcnt);
119 num_isstd = (size_t) uc2ul(tzhead.tzh_ttisstdcnt);
121 if (num_transitions > 0)
123 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
124 if (transitions == NULL)
126 type_idxs = (unsigned char *) malloc (num_transitions);
127 if (type_idxs == NULL)
132 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
138 zone_names = (char *) malloc (chars);
139 if (zone_names == NULL)
144 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
149 if (fread((PTR) transitions, sizeof(time_t),
150 num_transitions, f) != num_transitions ||
151 fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions)
154 for (i = 0; i < num_transitions; ++i)
155 transitions[i] = uc2ul (&transitions[i]);
157 for (i = 0; i < num_types; ++i)
160 if (fread((PTR) x, 1, 4, f) != 4 ||
161 fread((PTR) &types[i].isdst, 1, 1, f) != 1 ||
162 fread((PTR) &types[i].idx, 1, 1, f) != 1)
164 types[i].offset = (long int) uc2ul(x);
167 if (fread((PTR) zone_names, 1, chars, f) != chars)
170 for (i = 0; i < num_leaps; ++i)
173 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
175 leaps[i].transition = (time_t) uc2ul(x);
176 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
178 leaps[i].change = (long int) uc2ul(x);
181 for (i = 0; i < num_isstd; ++i)
186 types[i].isstd = c != 0;
188 while (i < num_types)
189 types[i++].isstd = 0;
193 compute_tzname_max (chars);
203 DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
204 char *std AND char *dst AND
205 long int stdoff AND long int dstoff)
207 size_t stdlen, dstlen, i;
209 __tzfile_read (TZDEFRULES);
221 stdlen = strlen (std) + 1;
222 dstlen = strlen (dst) + 1;
223 zone_names = malloc (stdlen + dstlen);
224 if (zone_names == NULL)
229 memcpy (zone_names, std, stdlen);
230 memcpy (&zone_names[stdlen], dst, dstlen);
232 for (i = 0; i < num_types; ++i)
235 types[i].idx = stdlen;
237 types[i].offset = dstoff;
243 types[i].offset = stdoff;
246 compute_tzname_max (stdlen + dstlen);
250 DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
251 time_t timer AND long int *leap_correct AND int *leap_hit)
256 if (num_transitions == 0 || timer < transitions[0])
258 /* TIMER is before any transition (or there are no transitions).
259 Choose the first non-DST type
260 (or the first if they're all DST types). */
262 while (i < num_types && types[i].isdst)
269 /* Find the first transition after TIMER, and
270 then pick the type of the transition before it. */
271 for (i = 1; i < num_transitions; ++i)
272 if (timer < transitions[i])
274 i = type_idxs[i - 1];
278 __daylight = info->isdst;
279 __timezone = info->offset;
280 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
282 __tzname[types[i].isdst] = &zone_names[types[i].idx];
283 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
284 __tzname[info->isdst] = &zone_names[info->idx];
289 /* Find the last leap second correction transition time before TIMER. */
294 while (timer < leaps[i].transition);
296 /* Apply its correction. */
297 *leap_correct = leaps[i].change;
299 if (timer == leaps[i].transition && /* Exactly at the transition time. */
300 ((i == 0 && leaps[i].change > 0) ||
301 leaps[i].change > leaps[i - 1].change))
305 leaps[i].transition == leaps[i - 1].transition + 1 &&
306 leaps[i].change == leaps[i - 1].change + 1)
317 DEFUN(compute_tzname_max, (chars), size_t chars)
319 extern long int __tzname_cur_max; /* Defined in __tzset.c. */
326 const char *start = p;
329 if (p - start > __tzname_cur_max)
330 __tzname_cur_max = p - start;
331 } while (++p < &zone_names[chars]);