]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/tzfile.c
* hurd/Makefile (routines): Add get-host, set-host.
[thirdparty/glibc.git] / time / tzfile.c
CommitLineData
98fa1d6e 1/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
28f540f4
RM
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19#include <ansidecl.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <time.h>
23#include <string.h>
24#include <limits.h>
25
26#define NOID
27#include <tzfile.h>
28
28f540f4
RM
29int __use_tzfile = 0;
30
31struct ttinfo
32 {
33 long int offset; /* Seconds east of GMT. */
34 unsigned char isdst; /* Used to set tm_isdst. */
35 unsigned char idx; /* Index into `zone_names'. */
98fa1d6e
RM
36 unsigned char isstd; /* Transition times are in standard time. */
37 unsigned char isgmt; /* Transition times are in GMT. */
28f540f4
RM
38 };
39
40struct leap
41 {
42 time_t transition; /* Time the transition takes effect. */
43 long int change; /* Seconds of correction to apply. */
44 };
45
46static void compute_tzname_max __P ((size_t));
47
48static size_t num_transitions;
49static time_t *transitions = NULL;
50static unsigned char *type_idxs = NULL;
51static size_t num_types;
52static struct ttinfo *types = NULL;
53static char *zone_names = NULL;
54static size_t num_leaps;
55static struct leap *leaps = NULL;
56
a2cafe30
RM
57#include <endian.h>
58
59/* Decode the four bytes at PTR as a signed integer in network byte order. */
60static inline int
61decode (const void *ptr)
62{
b20e47cb
RM
63 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
64 return *(const int *) ptr;
65 else
66 {
67 const unsigned char *p = ptr;
68 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
69
70 result = (result << 8) | *p++;
71 result = (result << 8) | *p++;
72 result = (result << 8) | *p++;
73 result = (result << 8) | *p++;
74
75 return result;
76 }
a2cafe30 77}
28f540f4
RM
78
79void
80DEFUN(__tzfile_read, (file), CONST char *file)
81{
98fa1d6e 82 size_t num_isstd, num_isgmt;
28f540f4
RM
83 register FILE *f;
84 struct tzhead tzhead;
85 size_t chars;
86 register size_t i;
87
88 __use_tzfile = 0;
89
90 if (transitions != NULL)
91 free((PTR) transitions);
92 transitions = NULL;
93 if (type_idxs != NULL)
94 free((PTR) type_idxs);
95 type_idxs = NULL;
96 if (types != NULL)
97 free((PTR) types);
98 types = NULL;
99 if (zone_names != NULL)
100 free((PTR) zone_names);
101 zone_names = NULL;
102 if (leaps != NULL)
103 free((PTR) leaps);
104 leaps = NULL;
105
106 if (file == NULL || *file == '\0')
107 file = TZDEFAULT;
108
109 if (*file != '/')
110 {
111 static CONST char tzdir[] = TZDIR;
112 register CONST unsigned int len = strlen(file) + 1;
113 char *new = (char *) __alloca(sizeof(tzdir) + len);
114 memcpy(new, tzdir, sizeof(tzdir) - 1);
115 new[sizeof(tzdir) - 1] = '/';
116 memcpy(&new[sizeof(tzdir)], file, len);
117 file = new;
118 }
119
120 f = fopen(file, "r");
121 if (f == NULL)
122 return;
123
124 if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1)
125 goto lose;
126
a2cafe30
RM
127 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
128 num_types = (size_t) decode (tzhead.tzh_typecnt);
129 chars = (size_t) decode (tzhead.tzh_charcnt);
130 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
131 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
132 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
28f540f4
RM
133
134 if (num_transitions > 0)
135 {
136 transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
137 if (transitions == NULL)
138 goto lose;
139 type_idxs = (unsigned char *) malloc (num_transitions);
140 if (type_idxs == NULL)
141 goto lose;
142 }
143 if (num_types > 0)
144 {
145 types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
146 if (types == NULL)
147 goto lose;
148 }
149 if (chars > 0)
150 {
151 zone_names = (char *) malloc (chars);
152 if (zone_names == NULL)
153 goto lose;
154 }
155 if (num_leaps > 0)
156 {
157 leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
158 if (leaps == NULL)
159 goto lose;
160 }
161
b20e47cb
RM
162 if (sizeof (time_t) < 4)
163 abort ();
164
a2cafe30 165 if (fread((PTR) transitions, 4, num_transitions, f) != num_transitions ||
28f540f4
RM
166 fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions)
167 goto lose;
168
a2cafe30
RM
169 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
170 {
171 /* Decode the transition times, stored as 4-byte integers in
172 network (big-endian) byte order. We work from the end of
173 the array so as not to clobber the next element to be
174 processed when sizeof (time_t) > 4. */
175 i = num_transitions;
b20e47cb 176 while (i-- > 0)
a2cafe30
RM
177 transitions[i] = decode ((char *) transitions + i*4);
178 }
28f540f4
RM
179
180 for (i = 0; i < num_types; ++i)
181 {
182 unsigned char x[4];
183 if (fread((PTR) x, 1, 4, f) != 4 ||
184 fread((PTR) &types[i].isdst, 1, 1, f) != 1 ||
185 fread((PTR) &types[i].idx, 1, 1, f) != 1)
186 goto lose;
a2cafe30 187 types[i].offset = (long int) decode (x);
28f540f4
RM
188 }
189
190 if (fread((PTR) zone_names, 1, chars, f) != chars)
191 goto lose;
192
193 for (i = 0; i < num_leaps; ++i)
194 {
195 unsigned char x[4];
196 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
197 goto lose;
a2cafe30 198 leaps[i].transition = (time_t) decode (x);
28f540f4
RM
199 if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
200 goto lose;
a2cafe30 201 leaps[i].change = (long int) decode (x);
28f540f4
RM
202 }
203
204 for (i = 0; i < num_isstd; ++i)
205 {
206 char c = getc(f);
207 if (c == EOF)
208 goto lose;
209 types[i].isstd = c != 0;
210 }
211 while (i < num_types)
212 types[i++].isstd = 0;
213
98fa1d6e
RM
214 for (i = 0; i < num_isgmt; ++i)
215 {
216 char c = getc(f);
217 if (c == EOF)
218 goto lose;
219 types[i].isgmt = c != 0;
220 }
221 while (i < num_types)
222 types[i++].isgmt = 0;
223
28f540f4
RM
224 (void) fclose(f);
225
226 compute_tzname_max (chars);
227
228 __use_tzfile = 1;
229 return;
230
231 lose:;
232 (void) fclose(f);
233}
234\f
98fa1d6e
RM
235/* The user specified a hand-made timezone, but not its DST rules.
236 We will use the names and offsets from the user, and the rules
237 from the TZDEFRULES file. */
238
28f540f4
RM
239void
240DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
241 char *std AND char *dst AND
242 long int stdoff AND long int dstoff)
243{
244 size_t stdlen, dstlen, i;
98fa1d6e
RM
245 long int rule_offset, rule_stdoff, rule_dstoff;
246 int isdst;
28f540f4
RM
247
248 __tzfile_read (TZDEFRULES);
249 if (!__use_tzfile)
250 return;
251
252 if (num_types < 2)
253 {
254 __use_tzfile = 0;
255 return;
256 }
257
98fa1d6e 258 /* Ignore the zone names read from the file. */
28f540f4
RM
259 free (zone_names);
260
98fa1d6e 261 /* Use the names the user specified. */
28f540f4
RM
262 stdlen = strlen (std) + 1;
263 dstlen = strlen (dst) + 1;
264 zone_names = malloc (stdlen + dstlen);
265 if (zone_names == NULL)
266 {
267 __use_tzfile = 0;
268 return;
269 }
270 memcpy (zone_names, std, stdlen);
271 memcpy (&zone_names[stdlen], dst, dstlen);
272
98fa1d6e
RM
273 /* Find the standard and daylight time offsets used by the rule file.
274 We choose the offsets in the types of each flavor that are
275 transitioned to earliest in time. */
f0bf9cb9 276 rule_stdoff = rule_dstoff = 0;
98fa1d6e
RM
277 for (i = 0; i < num_transitions; ++i)
278 {
279 if (!rule_stdoff && !types[type_idxs[i]].isdst)
280 rule_stdoff = types[type_idxs[i]].offset;
281 if (!rule_dstoff && types[type_idxs[i]].isdst)
282 rule_dstoff = types[type_idxs[i]].offset;
283 if (rule_stdoff && rule_dstoff)
284 break;
285 }
286
287 /* Now correct the transition times for the user-specified standard and
288 daylight offsets from GMT. */
289 isdst = 0;
290 rule_offset = rule_offset;
291 for (i = 0; i < num_transitions; ++i)
292 {
293 struct ttinfo *trans_type = &types[type_idxs[i]];
294
295 /* We will use only types 0 (standard) and 1 (daylight).
296 Fix up this transition to point to whichever matches
297 the flavor of its original type. */
298 type_idxs[i] = trans_type->isdst;
299
300 if (trans_type->isgmt)
301 /* The transition time is in GMT. No correction to apply. */ ;
302 else if (isdst && !trans_type->isstd)
303 /* The type says this transition is in "local wall clock time", and
304 wall clock time as of the previous transition was DST. Correct
305 for the difference between the rule's DST offset and the user's
306 DST offset. */
307 transitions[i] += dstoff - rule_dstoff;
308 else
309 /* This transition is in "local wall clock time", and wall clock
310 time as of this iteration is non-DST. Correct for the
311 difference between the rule's standard offset and the user's
312 standard offset. */
313 transitions[i] += stdoff - rule_stdoff;
314
315 /* The DST state of "local wall clock time" for the next iteration is
316 as specified by this transition. */
317 isdst = trans_type->isdst;
318 }
319
320 /* Reset types 0 and 1 to describe the user's settings. */
321 types[0].idx = 0;
322 types[0].offset = stdoff;
323 types[0].isdst = 0;
324 types[1].idx = stdlen;
325 types[1].offset = dstoff;
326 types[1].isdst = 1;
28f540f4
RM
327
328 compute_tzname_max (stdlen + dstlen);
329}
330\f
331int
332DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
333 time_t timer AND long int *leap_correct AND int *leap_hit)
334{
335 struct ttinfo *info;
336 register size_t i;
337
338 if (num_transitions == 0 || timer < transitions[0])
339 {
340 /* TIMER is before any transition (or there are no transitions).
341 Choose the first non-DST type
342 (or the first if they're all DST types). */
343 i = 0;
344 while (i < num_types && types[i].isdst)
345 ++i;
346 if (i == num_types)
347 i = 0;
348 }
349 else
350 {
351 /* Find the first transition after TIMER, and
352 then pick the type of the transition before it. */
353 for (i = 1; i < num_transitions; ++i)
354 if (timer < transitions[i])
355 break;
356 i = type_idxs[i - 1];
357 }
358
359 info = &types[i];
360 __daylight = info->isdst;
361 __timezone = info->offset;
362 for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
363 ++i)
364 __tzname[types[i].isdst] = &zone_names[types[i].idx];
365 if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
366 __tzname[info->isdst] = &zone_names[info->idx];
367
368 *leap_correct = 0L;
369 *leap_hit = 0;
370
371 /* Find the last leap second correction transition time before TIMER. */
372 i = num_leaps;
373 do
374 if (i-- == 0)
375 return 1;
376 while (timer < leaps[i].transition);
377
378 /* Apply its correction. */
379 *leap_correct = leaps[i].change;
380
381 if (timer == leaps[i].transition && /* Exactly at the transition time. */
382 ((i == 0 && leaps[i].change > 0) ||
383 leaps[i].change > leaps[i - 1].change))
384 {
385 *leap_hit = 1;
386 while (i > 0 &&
387 leaps[i].transition == leaps[i - 1].transition + 1 &&
388 leaps[i].change == leaps[i - 1].change + 1)
389 {
390 ++*leap_hit;
391 --i;
392 }
393 }
394
395 return 1;
396}
397\f
398void
399DEFUN(compute_tzname_max, (chars), size_t chars)
400{
401 extern long int __tzname_cur_max; /* Defined in __tzset.c. */
402
403 const char *p;
404
405 p = zone_names;
406 do
407 {
408 const char *start = p;
409 while (*p != '\0')
410 ++p;
411 if (p - start > __tzname_cur_max)
412 __tzname_cur_max = p - start;
413 } while (++p < &zone_names[chars]);
414}