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 size_t num_transitions;
52 static time_t *transitions = NULL;
53 static unsigned char *type_idxs = NULL;
54 static size_t num_types;
55 static struct ttinfo *types = NULL;
56 static char *zone_names = NULL;
57 static size_t num_leaps;
58 static struct leap *leaps = NULL;
60 #define uc2ul(x) _uc2ul((unsigned char *) (x))
62 ((x)[3] + ((x)[2] << CHAR_BIT) + ((x)[1] << (2 * CHAR_BIT)) + \
63 ((x)[0] << (3 * CHAR_BIT)))
66 DEFUN(__tzfile_read, (file), CONST char *file)
76 if (transitions != NULL)
77 free((PTR) transitions);
79 if (type_idxs != NULL)
80 free((PTR) type_idxs);
85 if (zone_names != NULL)
86 free((PTR) zone_names);
92 if (file == NULL || *file == '\0')
97 static CONST char tzdir[] = TZDIR;
98 register CONST unsigned int len = strlen(file) + 1;
99 char *new = (char *) __alloca(sizeof(tzdir) + len);
100 memcpy(new, tzdir, sizeof(tzdir) - 1);
101 new[sizeof(tzdir) - 1] = '/';
102 memcpy(&new[sizeof(tzdir)], file, len);
106 f = fopen(file, "r");
110 if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1)
113 num_transitions = (size_t) uc2ul(tzhead.tzh_timecnt);
114 num_types = (size_t) uc2ul(tzhead.tzh_typecnt);
115 chars = (size_t) uc2ul(tzhead.tzh_charcnt);
116 num_leaps = (size_t) uc2ul(tzhead.tzh_leapcnt);
117 num_isstd = (size_t) uc2ul(tzhead.tzh_ttisstdcnt);
119 if (num_transitions > 0)
121 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
122 if (transitions == NULL)
124 type_idxs = (unsigned char *) malloc (num_transitions);
125 if (type_idxs == NULL)
130 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
136 zone_names = (char *) malloc (chars);
137 if (zone_names == NULL)
142 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
147 if (fread((PTR) transitions, sizeof(time_t),
148 num_transitions, f) != num_transitions ||
149 fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions)
152 for (i = 0; i < num_transitions; ++i)
153 transitions[i] = uc2ul (&transitions[i]);
155 for (i = 0; i < num_types; ++i)
158 if (fread((PTR) x, 1, 4, f) != 4 ||
159 fread((PTR) &types[i].isdst, 1, 1, f) != 1 ||
160 fread((PTR) &types[i].idx, 1, 1, f) != 1)
162 types[i].offset = (long int) uc2ul(x);
165 if (fread((PTR) zone_names, 1, chars, f) != chars)
168 for (i = 0; i < num_leaps; ++i)
171 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
173 leaps[i].transition = (time_t) uc2ul(x);
174 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
176 leaps[i].change = (long int) uc2ul(x);
179 for (i = 0; i < num_isstd; ++i)
184 types[i].isstd = c != 0;
186 while (i < num_types)
187 types[i++].isstd = 0;
198 DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
199 char *std AND char *dst AND
200 long int stdoff AND long int dstoff)
202 size_t stdlen, dstlen, i;
204 __tzfile_read (TZDEFRULES);
216 stdlen = strlen (std) + 1;
217 dstlen = strlen (dst) + 1;
218 zone_names = malloc (stdlen + dstlen);
219 if (zone_names == NULL)
224 memcpy (zone_names, std, stdlen);
225 memcpy (&zone_names[stdlen], dst, dstlen);
227 for (i = 0; i < num_types; ++i)
230 types[i].idx = stdlen;
232 types[i].offset = dstoff;
238 types[i].offset = stdoff;
243 DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
244 time_t timer AND long int *leap_correct AND int *leap_hit)
249 if (num_transitions == 0 || timer < transitions[0])
251 /* TIMER is before any transition (or there are no transitions).
252 Choose the first non-DST type
253 (or the first if they're all DST types). */
255 while (i < num_types && types[i].isdst)
262 /* Find the first transition after TIMER, and
263 then pick the type of the transition before it. */
264 for (i = 1; i < num_transitions; ++i)
265 if (timer < transitions[i])
267 i = type_idxs[i - 1];
271 __daylight = info->isdst;
272 __timezone = info->offset;
273 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
275 __tzname[types[i].isdst] = &zone_names[types[i].idx];
276 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
277 __tzname[info->isdst] = &zone_names[info->idx];
282 /* Find the last leap second correction transition time before TIMER. */
287 while (timer < leaps[i].transition);
289 /* Apply its correction. */
290 *leap_correct = leaps[i].change;
292 if (timer == leaps[i].transition && /* Exactly at the transition time. */
293 ((i == 0 && leaps[i].change > 0) ||
294 leaps[i].change > leaps[i - 1].change))
298 leaps[i].transition == leaps[i - 1].transition + 1 &&
299 leaps[i].change == leaps[i - 1].change + 1)