X-Git-Url: http://git.csclub.uwaterloo.ca/?p=kopensolaris-gnu%2Fglibc.git;a=blobdiff_plain;f=time%2Ftzfile.c;h=e78a05ec75432c6f70dcfa8ac3f302c873349108;hp=4b1aff4d153290917633849b7df483454a57d225;hb=0a0d9d20f716a8b48e2a2d2c6c243997d8b15543;hpb=519c1de2d16f112efd42542c0378f06bb6bb8d7e diff --git a/time/tzfile.c b/time/tzfile.c index 4b1aff4d15..e78a05ec75 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991, 1992 Free Software Foundation, Inc. +/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -26,12 +26,6 @@ Cambridge, MA 02139, USA. */ #define NOID #include -#ifndef HAVE_GNU_LD -#define __tzname tzname -#define __daylight daylight -#define __timezone timezone -#endif - int __use_tzfile = 0; struct ttinfo @@ -39,15 +33,18 @@ struct ttinfo long int offset; /* Seconds east of GMT. */ unsigned char isdst; /* Used to set tm_isdst. */ unsigned char idx; /* Index into `zone_names'. */ - unsigned int isstd:1; /* Transition times are standard time. */ + unsigned char isstd; /* Transition times are in standard time. */ + unsigned char isgmt; /* Transition times are in GMT. */ }; struct leap { - time_t transition; - long int change; + time_t transition; /* Time the transition takes effect. */ + long int change; /* Seconds of correction to apply. */ }; +static void compute_tzname_max __P ((size_t)); + static size_t num_transitions; static time_t *transitions = NULL; static unsigned char *type_idxs = NULL; @@ -57,15 +54,32 @@ static char *zone_names = NULL; static size_t num_leaps; static struct leap *leaps = NULL; -#define uc2ul(x) _uc2ul((unsigned char *) (x)) -#define _uc2ul(x) \ - ((x)[3] + ((x)[2] << CHAR_BIT) + ((x)[1] << (2 * CHAR_BIT)) + \ - ((x)[0] << (3 * CHAR_BIT))) +#include + +/* Decode the four bytes at PTR as a signed integer in network byte order. */ +static inline int +decode (const void *ptr) +{ + if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4) + return *(const int *) ptr; + else + { + const unsigned char *p = ptr; + int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0; + + result = (result << 8) | *p++; + result = (result << 8) | *p++; + result = (result << 8) | *p++; + result = (result << 8) | *p++; + + return result; + } +} void DEFUN(__tzfile_read, (file), CONST char *file) { - size_t num_isstd; + size_t num_isstd, num_isgmt; register FILE *f; struct tzhead tzhead; size_t chars; @@ -110,11 +124,12 @@ DEFUN(__tzfile_read, (file), CONST char *file) if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1) goto lose; - num_transitions = (size_t) uc2ul(tzhead.tzh_timecnt); - num_types = (size_t) uc2ul(tzhead.tzh_typecnt); - chars = (size_t) uc2ul(tzhead.tzh_charcnt); - num_leaps = (size_t) uc2ul(tzhead.tzh_leapcnt); - num_isstd = (size_t) uc2ul(tzhead.tzh_ttisstdcnt); + num_transitions = (size_t) decode (tzhead.tzh_timecnt); + num_types = (size_t) decode (tzhead.tzh_typecnt); + chars = (size_t) decode (tzhead.tzh_charcnt); + num_leaps = (size_t) decode (tzhead.tzh_leapcnt); + num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt); + num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt); if (num_transitions > 0) { @@ -144,13 +159,23 @@ DEFUN(__tzfile_read, (file), CONST char *file) goto lose; } - if (fread((PTR) transitions, sizeof(time_t), - num_transitions, f) != num_transitions || + if (sizeof (time_t) < 4) + abort (); + + if (fread((PTR) transitions, 4, num_transitions, f) != num_transitions || fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions) goto lose; - for (i = 0; i < num_transitions; ++i) - transitions[i] = uc2ul (&transitions[i]); + if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4) + { + /* Decode the transition times, stored as 4-byte integers in + network (big-endian) byte order. We work from the end of + the array so as not to clobber the next element to be + processed when sizeof (time_t) > 4. */ + i = num_transitions; + while (i-- > 0) + transitions[i] = decode ((char *) transitions + i*4); + } for (i = 0; i < num_types; ++i) { @@ -159,7 +184,7 @@ DEFUN(__tzfile_read, (file), CONST char *file) fread((PTR) &types[i].isdst, 1, 1, f) != 1 || fread((PTR) &types[i].idx, 1, 1, f) != 1) goto lose; - types[i].offset = (long int) uc2ul(x); + types[i].offset = (long int) decode (x); } if (fread((PTR) zone_names, 1, chars, f) != chars) @@ -170,10 +195,10 @@ DEFUN(__tzfile_read, (file), CONST char *file) unsigned char x[4]; if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x)) goto lose; - leaps[i].transition = (time_t) uc2ul(x); + leaps[i].transition = (time_t) decode (x); if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x)) goto lose; - leaps[i].change = (long int) uc2ul(x); + leaps[i].change = (long int) decode (x); } for (i = 0; i < num_isstd; ++i) @@ -186,17 +211,126 @@ DEFUN(__tzfile_read, (file), CONST char *file) while (i < num_types) types[i++].isstd = 0; + for (i = 0; i < num_isgmt; ++i) + { + char c = getc(f); + if (c == EOF) + goto lose; + types[i].isgmt = c != 0; + } + while (i < num_types) + types[i++].isgmt = 0; + (void) fclose(f); + + compute_tzname_max (chars); + __use_tzfile = 1; return; lose:; (void) fclose(f); } + +/* The user specified a hand-made timezone, but not its DST rules. + We will use the names and offsets from the user, and the rules + from the TZDEFRULES file. */ +void +DEFUN(__tzfile_default, (std, dst, stdoff, dstoff), + char *std AND char *dst AND + long int stdoff AND long int dstoff) +{ + size_t stdlen, dstlen, i; + long int rule_offset, rule_stdoff, rule_dstoff; + int isdst; + + __tzfile_read (TZDEFRULES); + if (!__use_tzfile) + return; + + if (num_types < 2) + { + __use_tzfile = 0; + return; + } + /* Ignore the zone names read from the file. */ + free (zone_names); + + /* Use the names the user specified. */ + stdlen = strlen (std) + 1; + dstlen = strlen (dst) + 1; + zone_names = malloc (stdlen + dstlen); + if (zone_names == NULL) + { + __use_tzfile = 0; + return; + } + memcpy (zone_names, std, stdlen); + memcpy (&zone_names[stdlen], dst, dstlen); + + /* Find the standard and daylight time offsets used by the rule file. + We choose the offsets in the types of each flavor that are + transitioned to earliest in time. */ + rule_stdoff = rule_dstoff = 0; + for (i = 0; i < num_transitions; ++i) + { + if (!rule_stdoff && !types[type_idxs[i]].isdst) + rule_stdoff = types[type_idxs[i]].offset; + if (!rule_dstoff && types[type_idxs[i]].isdst) + rule_dstoff = types[type_idxs[i]].offset; + if (rule_stdoff && rule_dstoff) + break; + } + + /* Now correct the transition times for the user-specified standard and + daylight offsets from GMT. */ + isdst = 0; + rule_offset = rule_offset; + for (i = 0; i < num_transitions; ++i) + { + struct ttinfo *trans_type = &types[type_idxs[i]]; + + /* We will use only types 0 (standard) and 1 (daylight). + Fix up this transition to point to whichever matches + the flavor of its original type. */ + type_idxs[i] = trans_type->isdst; + + if (trans_type->isgmt) + /* The transition time is in GMT. No correction to apply. */ ; + else if (isdst && !trans_type->isstd) + /* The type says this transition is in "local wall clock time", and + wall clock time as of the previous transition was DST. Correct + for the difference between the rule's DST offset and the user's + DST offset. */ + transitions[i] += dstoff - rule_dstoff; + else + /* This transition is in "local wall clock time", and wall clock + time as of this iteration is non-DST. Correct for the + difference between the rule's standard offset and the user's + standard offset. */ + transitions[i] += stdoff - rule_stdoff; + + /* The DST state of "local wall clock time" for the next iteration is + as specified by this transition. */ + isdst = trans_type->isdst; + } + + /* Reset types 0 and 1 to describe the user's settings. */ + types[0].idx = 0; + types[0].offset = stdoff; + types[0].isdst = 0; + types[1].idx = stdlen; + types[1].offset = dstoff; + types[1].isdst = 1; + + compute_tzname_max (stdlen + dstlen); +} + int -DEFUN(__tzfile_compute, (timer, tm), time_t timer AND struct tm tm) +DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit), + time_t timer AND long int *leap_correct AND int *leap_hit) { struct ttinfo *info; register size_t i; @@ -214,14 +348,15 @@ DEFUN(__tzfile_compute, (timer, tm), time_t timer AND struct tm tm) } else { - /* Find the first transition after TIMER, and then go back one. */ - i = 1; - while (i < num_transitions && transitions[i] < timer) - ++i; - --i; + /* Find the first transition after TIMER, and + then pick the type of the transition before it. */ + for (i = 1; i < num_transitions; ++i) + if (timer < transitions[i]) + break; + i = type_idxs[i - 1]; } - info = &types[type_idxs[i]]; + info = &types[i]; __daylight = info->isdst; __timezone = info->offset; for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]); @@ -230,11 +365,50 @@ DEFUN(__tzfile_compute, (timer, tm), time_t timer AND struct tm tm) if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0])) __tzname[info->isdst] = &zone_names[info->idx]; + *leap_correct = 0L; + *leap_hit = 0; + + /* Find the last leap second correction transition time before TIMER. */ i = num_leaps; do if (i-- == 0) return 1; while (timer < leaps[i].transition); - __timezone += leaps[i].change; + + /* Apply its correction. */ + *leap_correct = leaps[i].change; + + if (timer == leaps[i].transition && /* Exactly at the transition time. */ + ((i == 0 && leaps[i].change > 0) || + leaps[i].change > leaps[i - 1].change)) + { + *leap_hit = 1; + while (i > 0 && + leaps[i].transition == leaps[i - 1].transition + 1 && + leaps[i].change == leaps[i - 1].change + 1) + { + ++*leap_hit; + --i; + } + } + return 1; } + +void +DEFUN(compute_tzname_max, (chars), size_t chars) +{ + extern long int __tzname_cur_max; /* Defined in __tzset.c. */ + + const char *p; + + p = zone_names; + do + { + const char *start = p; + while (*p != '\0') + ++p; + if (p - start > __tzname_cur_max) + __tzname_cur_max = p - start; + } while (++p < &zone_names[chars]); +}