122084Smckusick /* 237140Sbostic * Copyright (c) 1987, 1989 Regents of the University of California. 332754Sbostic * All rights reserved. 432754Sbostic * 534952Sbostic * This code is derived from software contributed to Berkeley by 634952Sbostic * Arthur Olson. 734952Sbostic * 832754Sbostic * Redistribution and use in source and binary forms are permitted 934821Sbostic * provided that the above copyright notice and this paragraph are 1034821Sbostic * duplicated in all such forms and that any documentation, 1134821Sbostic * advertising materials, and other materials related to such 1234821Sbostic * distribution and use acknowledge that the software was developed 1334821Sbostic * by the University of California, Berkeley. The name of the 1434821Sbostic * University may not be used to endorse or promote products derived 1534821Sbostic * from this software without specific prior written permission. 1634821Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1734821Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1834821Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1922084Smckusick */ 2022084Smckusick 2130684Sbostic #if defined(LIBC_SCCS) && !defined(lint) 22*39105Sbostic static char sccsid[] = "@(#)ctime.c 5.18 (Berkeley) 09/09/89"; 2332754Sbostic #endif /* LIBC_SCCS and not lint */ 2422084Smckusick 2537140Sbostic /* 2637140Sbostic ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu). 2737140Sbostic ** POSIX-style TZ environment variable handling from Guy Harris 2837140Sbostic ** (guy@auspex.com). 2937140Sbostic */ 301959Swnj 3137140Sbostic /*LINTLIBRARY*/ 321959Swnj 3337140Sbostic #include <sys/param.h> 3437140Sbostic #include <fcntl.h> 3537140Sbostic #include <time.h> 3637140Sbostic #include <tzfile.h> 3737140Sbostic #include <string.h> 3837140Sbostic #include <ctype.h> 3937140Sbostic #include <stdio.h> 4030608Sbostic 4137140Sbostic #ifdef __STDC__ 4237140Sbostic #include <stdlib.h> 4337140Sbostic 4437140Sbostic #define P(s) s 4537140Sbostic #define alloc_size_t size_t 4637140Sbostic #define qsort_size_t size_t 4737140Sbostic #define fread_size_t size_t 4837140Sbostic #define fwrite_size_t size_t 4937140Sbostic 5037140Sbostic #else /* !defined __STDC__ */ 5137140Sbostic 5237140Sbostic #define ASTERISK * 5337140Sbostic #define P(s) (/ASTERISK s ASTERISK/) 5437140Sbostic #define const 5537140Sbostic #define volatile 5637140Sbostic 5737140Sbostic typedef char * genericptr_t; 5837140Sbostic typedef unsigned alloc_size_t; 5937140Sbostic typedef int qsort_size_t; 6037140Sbostic typedef int fread_size_t; 6137140Sbostic typedef int fwrite_size_t; 6237140Sbostic 6337140Sbostic extern char * calloc(); 6437140Sbostic extern char * malloc(); 6537140Sbostic extern char * realloc(); 6637140Sbostic extern char * getenv(); 6737140Sbostic 6837140Sbostic #endif /* !defined __STDC__ */ 6937140Sbostic 7037140Sbostic extern time_t time(); 7137140Sbostic 7237140Sbostic #define FILENAME_MAX MAXPATHLEN 7337140Sbostic #define ACCESS_MODE O_RDONLY 7437140Sbostic #define OPEN_MODE O_RDONLY 7537140Sbostic 7637140Sbostic #ifndef WILDABBR 771959Swnj /* 7837140Sbostic ** Someone might make incorrect use of a time zone abbreviation: 7937140Sbostic ** 1. They might reference tzname[0] before calling tzset (explicitly 8037140Sbostic ** or implicitly). 8137140Sbostic ** 2. They might reference tzname[1] before calling tzset (explicitly 8237140Sbostic ** or implicitly). 8337140Sbostic ** 3. They might reference tzname[1] after setting to a time zone 8437140Sbostic ** in which Daylight Saving Time is never observed. 8537140Sbostic ** 4. They might reference tzname[0] after setting to a time zone 8637140Sbostic ** in which Standard Time is never observed. 8737140Sbostic ** 5. They might reference tm.TM_ZONE after calling offtime. 8837140Sbostic ** What's best to do in the above cases is open to debate; 8937140Sbostic ** for now, we just set things up so that in any of the five cases 9037140Sbostic ** WILDABBR is used. Another possibility: initialize tzname[0] to the 9137140Sbostic ** string "tzname[0] used before set", and similarly for the other cases. 9237140Sbostic ** And another: initialize tzname[0] to "ERA", with an explanation in the 9337140Sbostic ** manual page of what this "time zone abbreviation" means (doing this so 9437140Sbostic ** that tzname[0] has the "normal" length of three characters). 9530608Sbostic */ 9637140Sbostic #define WILDABBR " " 9737140Sbostic #endif /* !defined WILDABBR */ 981959Swnj 9930608Sbostic #ifndef TRUE 10030608Sbostic #define TRUE 1 10130608Sbostic #define FALSE 0 10237140Sbostic #endif /* !defined TRUE */ 10330608Sbostic 10437140Sbostic static const char GMT[] = "GMT"; 10530608Sbostic 10630608Sbostic struct ttinfo { /* time type information */ 10730608Sbostic long tt_gmtoff; /* GMT offset in seconds */ 10830608Sbostic int tt_isdst; /* used to set tm_isdst */ 10930608Sbostic int tt_abbrind; /* abbreviation list index */ 11037140Sbostic int tt_ttisstd; /* TRUE if transition is std time */ 11113876Ssam }; 11213876Ssam 11337140Sbostic struct lsinfo { /* leap second information */ 11437140Sbostic time_t ls_trans; /* transition time */ 11537140Sbostic long ls_corr; /* correction to apply */ 11637140Sbostic }; 11737140Sbostic 11830608Sbostic struct state { 11937140Sbostic int leapcnt; 12030608Sbostic int timecnt; 12130608Sbostic int typecnt; 12230608Sbostic int charcnt; 12330608Sbostic time_t ats[TZ_MAX_TIMES]; 12430608Sbostic unsigned char types[TZ_MAX_TIMES]; 12530608Sbostic struct ttinfo ttis[TZ_MAX_TYPES]; 12637140Sbostic char chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ? 12737140Sbostic TZ_MAX_CHARS + 1 : sizeof GMT]; 12837140Sbostic struct lsinfo lsis[TZ_MAX_LEAPS]; 12923720Skre }; 13023720Skre 13137140Sbostic struct rule { 13237140Sbostic int r_type; /* type of rule--see below */ 13337140Sbostic int r_day; /* day number of rule */ 13437140Sbostic int r_week; /* week number of rule */ 13537140Sbostic int r_mon; /* month number of rule */ 13637140Sbostic long r_time; /* transition time of rule */ 13737140Sbostic }; 13830608Sbostic 13937140Sbostic #define JULIAN_DAY 0 /* Jn - Julian day */ 14037140Sbostic #define DAY_OF_YEAR 1 /* n - day of year */ 14137140Sbostic #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ 14230608Sbostic 14337140Sbostic /* 14437140Sbostic ** Prototypes for static functions. 14537140Sbostic */ 14637140Sbostic 14737140Sbostic static long detzcode P((const char * codep)); 14837140Sbostic static const char * getzname P((const char * strp)); 14937140Sbostic static const char * getnum P((const char * strp, int * nump, int min, 15037140Sbostic int max)); 15137140Sbostic static const char * getsecs P((const char * strp, long * secsp)); 15237140Sbostic static const char * getoffset P((const char * strp, long * offsetp)); 15337140Sbostic static const char * getrule P((const char * strp, struct rule * rulep)); 15437140Sbostic static void gmtload P((struct state * sp)); 15537140Sbostic static void gmtsub P((const time_t * timep, long offset, 15637140Sbostic struct tm * tmp)); 15737140Sbostic static void localsub P((const time_t * timep, long offset, 15837140Sbostic struct tm * tmp)); 15937140Sbostic static void normalize P((int * tensptr, int * unitsptr, int base)); 16037140Sbostic static void settzname P((void)); 16137140Sbostic static time_t time1 P((struct tm * tmp, void (* funcp)(), 16237140Sbostic long offset)); 16337140Sbostic static time_t time2 P((struct tm *tmp, void (* funcp)(), 16437140Sbostic long offset, int * okayp)); 16537140Sbostic static void timesub P((const time_t * timep, long offset, 16637140Sbostic const struct state * sp, struct tm * tmp)); 16737140Sbostic static int tmcomp P((const struct tm * atmp, 16837140Sbostic const struct tm * btmp)); 16937140Sbostic static time_t transtime P((time_t janfirst, int year, 17037140Sbostic const struct rule * rulep, long offset)); 17137140Sbostic static int tzload P((const char * name, struct state * sp)); 17237140Sbostic static int tzparse P((const char * name, struct state * sp, 17337140Sbostic int lastditch)); 17437140Sbostic 17537140Sbostic #ifdef ALL_STATE 17637140Sbostic static struct state * lclptr; 17737140Sbostic static struct state * gmtptr; 17837140Sbostic #endif /* defined ALL_STATE */ 17937140Sbostic 18037140Sbostic #ifndef ALL_STATE 18137140Sbostic static struct state lclmem; 18237140Sbostic static struct state gmtmem; 18337140Sbostic #define lclptr (&lclmem) 18437140Sbostic #define gmtptr (&gmtmem) 18537140Sbostic #endif /* State Farm */ 18637140Sbostic 18737140Sbostic static int lcl_is_set; 18837140Sbostic static int gmt_is_set; 18937140Sbostic 19030608Sbostic char * tzname[2] = { 19137140Sbostic WILDABBR, 19237140Sbostic WILDABBR 19312974Ssam }; 19412974Ssam 19530608Sbostic #ifdef USG_COMPAT 19630608Sbostic time_t timezone = 0; 19730608Sbostic int daylight = 0; 19837140Sbostic #endif /* defined USG_COMPAT */ 1991959Swnj 20037140Sbostic #ifdef ALTZONE 20137140Sbostic time_t altzone = 0; 20237140Sbostic #endif /* defined ALTZONE */ 20337140Sbostic 20430608Sbostic static long 20530608Sbostic detzcode(codep) 20637140Sbostic const char * const codep; 2071959Swnj { 20830608Sbostic register long result; 20930608Sbostic register int i; 21030608Sbostic 21130608Sbostic result = 0; 21230608Sbostic for (i = 0; i < 4; ++i) 21330608Sbostic result = (result << 8) | (codep[i] & 0xff); 21430608Sbostic return result; 2151959Swnj } 2161959Swnj 21737140Sbostic static void 21837140Sbostic settzname() 2191959Swnj { 22037140Sbostic register const struct state * const sp = lclptr; 22137140Sbostic register int i; 2221959Swnj 22337140Sbostic tzname[0] = WILDABBR; 22437140Sbostic tzname[1] = WILDABBR; 22537140Sbostic #ifdef USG_COMPAT 22637140Sbostic daylight = 0; 22737140Sbostic timezone = 0; 22837140Sbostic #endif /* defined USG_COMPAT */ 22937140Sbostic #ifdef ALTZONE 23037140Sbostic altzone = 0; 23137140Sbostic #endif /* defined ALTZONE */ 23237140Sbostic #ifdef ALL_STATE 23337140Sbostic if (sp == NULL) { 23437140Sbostic tzname[0] = tzname[1] = GMT; 23537140Sbostic return; 23637140Sbostic } 23737140Sbostic #endif /* defined ALL_STATE */ 23837140Sbostic for (i = 0; i < sp->typecnt; ++i) { 23937140Sbostic register const struct ttinfo * const ttisp = &sp->ttis[i]; 24037140Sbostic 24137140Sbostic tzname[ttisp->tt_isdst] = 24237140Sbostic (char *) &sp->chars[ttisp->tt_abbrind]; 24337140Sbostic #ifdef USG_COMPAT 24437140Sbostic if (ttisp->tt_isdst) 24537140Sbostic daylight = 1; 24637140Sbostic if (i == 0 || !ttisp->tt_isdst) 24737140Sbostic timezone = -(ttisp->tt_gmtoff); 24837140Sbostic #endif /* defined USG_COMPAT */ 24937140Sbostic #ifdef ALTZONE 25037140Sbostic if (i == 0 || ttisp->tt_isdst) 25137140Sbostic altzone = -(ttisp->tt_gmtoff); 25237140Sbostic #endif /* defined ALTZONE */ 25337140Sbostic } 25437140Sbostic /* 25537140Sbostic ** And to get the latest zone names into tzname. . . 25637140Sbostic */ 25737140Sbostic for (i = 0; i < sp->timecnt; ++i) { 25837140Sbostic register const struct ttinfo * const ttisp = 25937140Sbostic &sp->ttis[sp->types[i]]; 26037140Sbostic 26137140Sbostic tzname[ttisp->tt_isdst] = 26237140Sbostic (char *) &sp->chars[ttisp->tt_abbrind]; 26337140Sbostic } 26437140Sbostic } 26537140Sbostic 26637140Sbostic static int 26737140Sbostic tzload(name, sp) 26837140Sbostic register const char * name; 26937140Sbostic register struct state * const sp; 27037140Sbostic { 27137140Sbostic register const char * p; 27237140Sbostic register int i; 27337140Sbostic register int fid; 27437140Sbostic 27537140Sbostic if (name == NULL && (name = TZDEFAULT) == NULL) 27630608Sbostic return -1; 27730608Sbostic { 27837140Sbostic register int doaccess; 27937140Sbostic char fullname[FILENAME_MAX + 1]; 28030608Sbostic 28137140Sbostic if (name[0] == ':') 28237140Sbostic ++name; 28330608Sbostic doaccess = name[0] == '/'; 28430608Sbostic if (!doaccess) { 28537140Sbostic if ((p = TZDIR) == NULL) 28630608Sbostic return -1; 28730608Sbostic if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) 28830608Sbostic return -1; 28930608Sbostic (void) strcpy(fullname, p); 29030608Sbostic (void) strcat(fullname, "/"); 29130608Sbostic (void) strcat(fullname, name); 29230608Sbostic /* 29330608Sbostic ** Set doaccess if '.' (as in "../") shows up in name. 29430608Sbostic */ 29537140Sbostic if (strchr(name, '.') != NULL) 29637140Sbostic doaccess = TRUE; 29730608Sbostic name = fullname; 29830608Sbostic } 29937140Sbostic if (doaccess && access(name, ACCESS_MODE) != 0) 30030608Sbostic return -1; 30137140Sbostic if ((fid = open(name, OPEN_MODE)) == -1) 30230608Sbostic return -1; 30325662Sbloom } 30430608Sbostic { 30537140Sbostic register const struct tzhead * tzhp; 30637140Sbostic char buf[sizeof *sp + sizeof *tzhp]; 30737140Sbostic int ttisstdcnt; 30830608Sbostic 30930608Sbostic i = read(fid, buf, sizeof buf); 31030608Sbostic if (close(fid) != 0 || i < sizeof *tzhp) 31130608Sbostic return -1; 31230608Sbostic tzhp = (struct tzhead *) buf; 31337140Sbostic ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt); 31437140Sbostic sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt); 31537140Sbostic sp->timecnt = (int) detzcode(tzhp->tzh_timecnt); 31637140Sbostic sp->typecnt = (int) detzcode(tzhp->tzh_typecnt); 31737140Sbostic sp->charcnt = (int) detzcode(tzhp->tzh_charcnt); 31837140Sbostic if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 31937140Sbostic sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 32037140Sbostic sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 32137140Sbostic sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 32237140Sbostic (ttisstdcnt != sp->typecnt && ttisstdcnt != 0)) 32330608Sbostic return -1; 32430608Sbostic if (i < sizeof *tzhp + 32537140Sbostic sp->timecnt * (4 + sizeof (char)) + 32637140Sbostic sp->typecnt * (4 + 2 * sizeof (char)) + 32737140Sbostic sp->charcnt * sizeof (char) + 32837140Sbostic sp->leapcnt * 2 * 4 + 32937140Sbostic ttisstdcnt * sizeof (char)) 33030608Sbostic return -1; 33130608Sbostic p = buf + sizeof *tzhp; 33237140Sbostic for (i = 0; i < sp->timecnt; ++i) { 33337140Sbostic sp->ats[i] = detzcode(p); 33430608Sbostic p += 4; 33512974Ssam } 33637140Sbostic for (i = 0; i < sp->timecnt; ++i) { 33737140Sbostic sp->types[i] = (unsigned char) *p++; 33837140Sbostic if (sp->types[i] >= sp->typecnt) 33937140Sbostic return -1; 34037140Sbostic } 34137140Sbostic for (i = 0; i < sp->typecnt; ++i) { 34230608Sbostic register struct ttinfo * ttisp; 34330608Sbostic 34437140Sbostic ttisp = &sp->ttis[i]; 34530608Sbostic ttisp->tt_gmtoff = detzcode(p); 34630608Sbostic p += 4; 34730608Sbostic ttisp->tt_isdst = (unsigned char) *p++; 34837140Sbostic if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) 34937140Sbostic return -1; 35030608Sbostic ttisp->tt_abbrind = (unsigned char) *p++; 35137140Sbostic if (ttisp->tt_abbrind < 0 || 35237140Sbostic ttisp->tt_abbrind > sp->charcnt) 35337140Sbostic return -1; 35430608Sbostic } 35537140Sbostic for (i = 0; i < sp->charcnt; ++i) 35637140Sbostic sp->chars[i] = *p++; 35737140Sbostic sp->chars[i] = '\0'; /* ensure '\0' at end */ 35837140Sbostic for (i = 0; i < sp->leapcnt; ++i) { 35937140Sbostic register struct lsinfo * lsisp; 36037140Sbostic 36137140Sbostic lsisp = &sp->lsis[i]; 36237140Sbostic lsisp->ls_trans = detzcode(p); 36337140Sbostic p += 4; 36437140Sbostic lsisp->ls_corr = detzcode(p); 36537140Sbostic p += 4; 36637140Sbostic } 36737140Sbostic for (i = 0; i < sp->typecnt; ++i) { 36837140Sbostic register struct ttinfo * ttisp; 36937140Sbostic 37037140Sbostic ttisp = &sp->ttis[i]; 37137140Sbostic if (ttisstdcnt == 0) 37237140Sbostic ttisp->tt_ttisstd = FALSE; 37337140Sbostic else { 37437140Sbostic ttisp->tt_ttisstd = *p++; 37537140Sbostic if (ttisp->tt_ttisstd != TRUE && 37637140Sbostic ttisp->tt_ttisstd != FALSE) 37737140Sbostic return -1; 37837140Sbostic } 37937140Sbostic } 3801959Swnj } 38137140Sbostic return 0; 38237140Sbostic } 38337140Sbostic 38437140Sbostic static const int mon_lengths[2][MONSPERYEAR] = { 38537140Sbostic 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 38637140Sbostic 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 38737140Sbostic }; 38837140Sbostic 38937140Sbostic static const int year_lengths[2] = { 39037140Sbostic DAYSPERNYEAR, DAYSPERLYEAR 39137140Sbostic }; 39237140Sbostic 39337140Sbostic /* 39437140Sbostic ** Given a pointer into a time zone string, scan until a character that is not 39537140Sbostic ** a valid character in a zone name is found. Return a pointer to that 39637140Sbostic ** character. 39737140Sbostic */ 39837140Sbostic 39937140Sbostic static const char * 40037140Sbostic getzname(strp) 40137140Sbostic register const char * strp; 40237140Sbostic { 40337140Sbostic register char c; 40437140Sbostic 40537140Sbostic while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' && 40637140Sbostic c != '+') 40737140Sbostic ++strp; 40837140Sbostic return strp; 40937140Sbostic } 41037140Sbostic 41137140Sbostic /* 41237140Sbostic ** Given a pointer into a time zone string, extract a number from that string. 41337140Sbostic ** Check that the number is within a specified range; if it is not, return 41437140Sbostic ** NULL. 41537140Sbostic ** Otherwise, return a pointer to the first character not part of the number. 41637140Sbostic */ 41737140Sbostic 41837140Sbostic static const char * 41937140Sbostic getnum(strp, nump, min, max) 42037140Sbostic register const char * strp; 42137140Sbostic int * const nump; 42237140Sbostic const int min; 42337140Sbostic const int max; 42437140Sbostic { 42537140Sbostic register char c; 42637140Sbostic register int num; 42737140Sbostic 42837140Sbostic if (strp == NULL || !isdigit(*strp)) 42937140Sbostic return NULL; 43037140Sbostic num = 0; 43137140Sbostic while ((c = *strp) != '\0' && isdigit(c)) { 43237140Sbostic num = num * 10 + (c - '0'); 43337140Sbostic if (num > max) 43437140Sbostic return NULL; /* illegal value */ 43537140Sbostic ++strp; 43637140Sbostic } 43737140Sbostic if (num < min) 43837140Sbostic return NULL; /* illegal value */ 43937140Sbostic *nump = num; 44037140Sbostic return strp; 44137140Sbostic } 44237140Sbostic 44337140Sbostic /* 44437140Sbostic ** Given a pointer into a time zone string, extract a number of seconds, 44537140Sbostic ** in hh[:mm[:ss]] form, from the string. 44637140Sbostic ** If any error occurs, return NULL. 44737140Sbostic ** Otherwise, return a pointer to the first character not part of the number 44837140Sbostic ** of seconds. 44937140Sbostic */ 45037140Sbostic 45137140Sbostic static const char * 45237140Sbostic getsecs(strp, secsp) 45337140Sbostic register const char * strp; 45437140Sbostic long * const secsp; 45537140Sbostic { 45637140Sbostic int num; 45737140Sbostic 45837140Sbostic strp = getnum(strp, &num, 0, HOURSPERDAY); 45937140Sbostic if (strp == NULL) 46037140Sbostic return NULL; 46137140Sbostic *secsp = num * SECSPERHOUR; 46237140Sbostic if (*strp == ':') { 46337140Sbostic ++strp; 46437140Sbostic strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 46537140Sbostic if (strp == NULL) 46637140Sbostic return NULL; 46737140Sbostic *secsp += num * SECSPERMIN; 46837140Sbostic if (*strp == ':') { 46937140Sbostic ++strp; 47037140Sbostic strp = getnum(strp, &num, 0, SECSPERMIN - 1); 47137140Sbostic if (strp == NULL) 47237140Sbostic return NULL; 47337140Sbostic *secsp += num; 47437140Sbostic } 47537140Sbostic } 47637140Sbostic return strp; 47737140Sbostic } 47837140Sbostic 47937140Sbostic /* 48037140Sbostic ** Given a pointer into a time zone string, extract an offset, in 48137140Sbostic ** [+-]hh[:mm[:ss]] form, from the string. 48237140Sbostic ** If any error occurs, return NULL. 48337140Sbostic ** Otherwise, return a pointer to the first character not part of the time. 48437140Sbostic */ 48537140Sbostic 48637140Sbostic static const char * 48737140Sbostic getoffset(strp, offsetp) 48837140Sbostic register const char * strp; 48937140Sbostic long * const offsetp; 49037140Sbostic { 49137140Sbostic register int neg; 49237140Sbostic 49337140Sbostic if (*strp == '-') { 49437140Sbostic neg = 1; 49537140Sbostic ++strp; 49637140Sbostic } else if (isdigit(*strp) || *strp++ == '+') 49737140Sbostic neg = 0; 49837140Sbostic else return NULL; /* illegal offset */ 49937140Sbostic strp = getsecs(strp, offsetp); 50037140Sbostic if (strp == NULL) 50137140Sbostic return NULL; /* illegal time */ 50237140Sbostic if (neg) 50337140Sbostic *offsetp = -*offsetp; 50437140Sbostic return strp; 50537140Sbostic } 50637140Sbostic 50737140Sbostic /* 50837140Sbostic ** Given a pointer into a time zone string, extract a rule in the form 50937140Sbostic ** date[/time]. See POSIX section 8 for the format of "date" and "time". 51037140Sbostic ** If a valid rule is not found, return NULL. 51137140Sbostic ** Otherwise, return a pointer to the first character not part of the rule. 51237140Sbostic */ 51337140Sbostic 51437140Sbostic static const char * 51537140Sbostic getrule(strp, rulep) 51637140Sbostic const char * strp; 51737140Sbostic register struct rule * const rulep; 51837140Sbostic { 51937140Sbostic if (*strp == 'J') { 52037140Sbostic /* 52137140Sbostic ** Julian day. 52237140Sbostic */ 52337140Sbostic rulep->r_type = JULIAN_DAY; 52437140Sbostic ++strp; 52537140Sbostic strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 52637140Sbostic } else if (*strp == 'M') { 52737140Sbostic /* 52837140Sbostic ** Month, week, day. 52937140Sbostic */ 53037140Sbostic rulep->r_type = MONTH_NTH_DAY_OF_WEEK; 53137140Sbostic ++strp; 53237140Sbostic strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 53337140Sbostic if (strp == NULL) 53437140Sbostic return NULL; 53537140Sbostic if (*strp++ != '.') 53637140Sbostic return NULL; 53737140Sbostic strp = getnum(strp, &rulep->r_week, 1, 5); 53837140Sbostic if (strp == NULL) 53937140Sbostic return NULL; 54037140Sbostic if (*strp++ != '.') 54137140Sbostic return NULL; 54237140Sbostic strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 54337140Sbostic } else if (isdigit(*strp)) { 54437140Sbostic /* 54537140Sbostic ** Day of year. 54637140Sbostic */ 54737140Sbostic rulep->r_type = DAY_OF_YEAR; 54837140Sbostic strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 54937140Sbostic } else return NULL; /* invalid format */ 55037140Sbostic if (strp == NULL) 55137140Sbostic return NULL; 55237140Sbostic if (*strp == '/') { 55337140Sbostic /* 55437140Sbostic ** Time specified. 55537140Sbostic */ 55637140Sbostic ++strp; 55737140Sbostic strp = getsecs(strp, &rulep->r_time); 55837140Sbostic } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 55937140Sbostic return strp; 56037140Sbostic } 56137140Sbostic 56237140Sbostic /* 56337140Sbostic ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the 56437140Sbostic ** year, a rule, and the offset from GMT at the time that rule takes effect, 56537140Sbostic ** calculate the Epoch-relative time that rule takes effect. 56637140Sbostic */ 56737140Sbostic 56837140Sbostic static time_t 56937140Sbostic transtime(janfirst, year, rulep, offset) 57037140Sbostic const time_t janfirst; 57137140Sbostic const int year; 57237140Sbostic register const struct rule * const rulep; 57337140Sbostic const long offset; 57437140Sbostic { 57537140Sbostic register int leapyear; 57637140Sbostic register time_t value; 57737140Sbostic register int i; 57837140Sbostic int d, m1, yy0, yy1, yy2, dow; 57937140Sbostic 58037140Sbostic leapyear = isleap(year); 58137140Sbostic switch (rulep->r_type) { 58237140Sbostic 58337140Sbostic case JULIAN_DAY: 58437140Sbostic /* 58537140Sbostic ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap 58637140Sbostic ** years. 58737140Sbostic ** In non-leap years, or if the day number is 59 or less, just 58837140Sbostic ** add SECSPERDAY times the day number-1 to the time of 58937140Sbostic ** January 1, midnight, to get the day. 59037140Sbostic */ 59137140Sbostic value = janfirst + (rulep->r_day - 1) * SECSPERDAY; 59237140Sbostic if (leapyear && rulep->r_day >= 60) 59337140Sbostic value += SECSPERDAY; 59437140Sbostic break; 59537140Sbostic 59637140Sbostic case DAY_OF_YEAR: 59737140Sbostic /* 59837140Sbostic ** n - day of year. 59937140Sbostic ** Just add SECSPERDAY times the day number to the time of 60037140Sbostic ** January 1, midnight, to get the day. 60137140Sbostic */ 60237140Sbostic value = janfirst + rulep->r_day * SECSPERDAY; 60337140Sbostic break; 60437140Sbostic 60537140Sbostic case MONTH_NTH_DAY_OF_WEEK: 60637140Sbostic /* 60737140Sbostic ** Mm.n.d - nth "dth day" of month m. 60837140Sbostic */ 60937140Sbostic value = janfirst; 61037140Sbostic for (i = 0; i < rulep->r_mon - 1; ++i) 61137140Sbostic value += mon_lengths[leapyear][i] * SECSPERDAY; 61237140Sbostic 61337140Sbostic /* 61437140Sbostic ** Use Zeller's Congruence to get day-of-week of first day of 61537140Sbostic ** month. 61637140Sbostic */ 61737140Sbostic m1 = (rulep->r_mon + 9) % 12 + 1; 61837140Sbostic yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 61937140Sbostic yy1 = yy0 / 100; 62037140Sbostic yy2 = yy0 % 100; 62137140Sbostic dow = ((26 * m1 - 2) / 10 + 62237140Sbostic 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 62337140Sbostic if (dow < 0) 62437140Sbostic dow += DAYSPERWEEK; 62537140Sbostic 62637140Sbostic /* 62737140Sbostic ** "dow" is the day-of-week of the first day of the month. Get 62837140Sbostic ** the day-of-month (zero-origin) of the first "dow" day of the 62937140Sbostic ** month. 63037140Sbostic */ 63137140Sbostic d = rulep->r_day - dow; 63237140Sbostic if (d < 0) 63337140Sbostic d += DAYSPERWEEK; 63437140Sbostic for (i = 1; i < rulep->r_week; ++i) { 63537140Sbostic if (d + DAYSPERWEEK >= 63637140Sbostic mon_lengths[leapyear][rulep->r_mon - 1]) 63737140Sbostic break; 63837140Sbostic d += DAYSPERWEEK; 63937140Sbostic } 64037140Sbostic 64137140Sbostic /* 64237140Sbostic ** "d" is the day-of-month (zero-origin) of the day we want. 64337140Sbostic */ 64437140Sbostic value += d * SECSPERDAY; 64537140Sbostic break; 64637140Sbostic } 64737140Sbostic 64830608Sbostic /* 64937140Sbostic ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in 65037140Sbostic ** question. To get the Epoch-relative time of the specified local 65137140Sbostic ** time on that day, add the transition time and the current offset 65237140Sbostic ** from GMT. 65330608Sbostic */ 65437140Sbostic return value + rulep->r_time + offset; 65537140Sbostic } 65637140Sbostic 65737140Sbostic /* 65837140Sbostic ** Given a POSIX section 8-style TZ string, fill in the rule tables as 65937140Sbostic ** appropriate. 66037140Sbostic */ 66137140Sbostic 66237140Sbostic static int 66337140Sbostic tzparse(name, sp, lastditch) 66437140Sbostic const char * name; 66537140Sbostic register struct state * const sp; 66637140Sbostic const int lastditch; 66737140Sbostic { 66837140Sbostic const char * stdname; 66937140Sbostic const char * dstname; 67037140Sbostic int stdlen; 67137140Sbostic int dstlen; 67237140Sbostic long stdoffset; 67337140Sbostic long dstoffset; 67437140Sbostic register time_t * atp; 67537140Sbostic register unsigned char * typep; 67637140Sbostic register char * cp; 67737140Sbostic register int load_result; 67837140Sbostic 67937140Sbostic stdname = name; 68037140Sbostic if (lastditch) { 68137140Sbostic stdlen = strlen(name); /* length of standard zone name */ 68237140Sbostic name += stdlen; 68337140Sbostic if (stdlen >= sizeof sp->chars) 68437140Sbostic stdlen = (sizeof sp->chars) - 1; 68537140Sbostic } else { 68637140Sbostic name = getzname(name); 68737140Sbostic stdlen = name - stdname; 68837140Sbostic if (stdlen < 3) 68930608Sbostic return -1; 69037140Sbostic } 69137140Sbostic if (*name == '\0') 692*39105Sbostic return -1; 69337140Sbostic else { 69437140Sbostic name = getoffset(name, &stdoffset); 69537140Sbostic if (name == NULL) 69630608Sbostic return -1; 69737140Sbostic } 69837140Sbostic load_result = tzload(TZDEFRULES, sp); 69937140Sbostic if (load_result != 0) 70037140Sbostic sp->leapcnt = 0; /* so, we're off a little */ 70137140Sbostic if (*name != '\0') { 70237140Sbostic dstname = name; 70337140Sbostic name = getzname(name); 70437140Sbostic dstlen = name - dstname; /* length of DST zone name */ 70537140Sbostic if (dstlen < 3) 70637140Sbostic return -1; 70737140Sbostic if (*name != '\0' && *name != ',' && *name != ';') { 70837140Sbostic name = getoffset(name, &dstoffset); 70937140Sbostic if (name == NULL) 71037140Sbostic return -1; 71137140Sbostic } else dstoffset = stdoffset - SECSPERHOUR; 71237140Sbostic if (*name == ',' || *name == ';') { 71337140Sbostic struct rule start; 71437140Sbostic struct rule end; 71537140Sbostic register int year; 71637140Sbostic register time_t janfirst; 71737140Sbostic time_t starttime; 71837140Sbostic time_t endtime; 71930608Sbostic 72037140Sbostic ++name; 72137140Sbostic if ((name = getrule(name, &start)) == NULL) 72237140Sbostic return -1; 72337140Sbostic if (*name++ != ',') 72437140Sbostic return -1; 72537140Sbostic if ((name = getrule(name, &end)) == NULL) 72637140Sbostic return -1; 72737140Sbostic if (*name != '\0') 72837140Sbostic return -1; 72937140Sbostic sp->typecnt = 2; /* standard time and DST */ 73037140Sbostic /* 73137140Sbostic ** Two transitions per year, from EPOCH_YEAR to 2037. 73237140Sbostic */ 73337140Sbostic sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1); 73437140Sbostic if (sp->timecnt > TZ_MAX_TIMES) 73537140Sbostic return -1; 73637140Sbostic sp->ttis[0].tt_gmtoff = -dstoffset; 73737140Sbostic sp->ttis[0].tt_isdst = 1; 73837140Sbostic sp->ttis[0].tt_abbrind = stdlen + 1; 73937140Sbostic sp->ttis[1].tt_gmtoff = -stdoffset; 74037140Sbostic sp->ttis[1].tt_isdst = 0; 74137140Sbostic sp->ttis[1].tt_abbrind = 0; 74237140Sbostic atp = sp->ats; 74337140Sbostic typep = sp->types; 74437140Sbostic janfirst = 0; 74537140Sbostic for (year = EPOCH_YEAR; year <= 2037; ++year) { 74637140Sbostic starttime = transtime(janfirst, year, &start, 74737140Sbostic stdoffset); 74837140Sbostic endtime = transtime(janfirst, year, &end, 74937140Sbostic dstoffset); 75037140Sbostic if (starttime > endtime) { 75137140Sbostic *atp++ = endtime; 75237140Sbostic *typep++ = 1; /* DST ends */ 75337140Sbostic *atp++ = starttime; 75437140Sbostic *typep++ = 0; /* DST begins */ 75537140Sbostic } else { 75637140Sbostic *atp++ = starttime; 75737140Sbostic *typep++ = 0; /* DST begins */ 75837140Sbostic *atp++ = endtime; 75937140Sbostic *typep++ = 1; /* DST ends */ 76037140Sbostic } 76137140Sbostic janfirst += 76237140Sbostic year_lengths[isleap(year)] * SECSPERDAY; 76337140Sbostic } 76430608Sbostic } else { 76537140Sbostic int sawstd; 76637140Sbostic int sawdst; 76737140Sbostic long stdfix; 76837140Sbostic long dstfix; 76937140Sbostic long oldfix; 77037140Sbostic int isdst; 77137140Sbostic register int i; 77237140Sbostic 77337140Sbostic if (*name != '\0') 77437140Sbostic return -1; 77537140Sbostic if (load_result != 0) 77637140Sbostic return -1; 77737140Sbostic /* 77837140Sbostic ** Compute the difference between the real and 77937140Sbostic ** prototype standard and summer time offsets 78037140Sbostic ** from GMT, and put the real standard and summer 78137140Sbostic ** time offsets into the rules in place of the 78237140Sbostic ** prototype offsets. 78337140Sbostic */ 78437140Sbostic sawstd = FALSE; 78537140Sbostic sawdst = FALSE; 78637140Sbostic stdfix = 0; 78737140Sbostic dstfix = 0; 78837140Sbostic for (i = 0; i < sp->typecnt; ++i) { 78937140Sbostic if (sp->ttis[i].tt_isdst) { 79037140Sbostic oldfix = dstfix; 79137140Sbostic dstfix = 79237140Sbostic sp->ttis[i].tt_gmtoff + dstoffset; 79337140Sbostic if (sawdst && (oldfix != dstfix)) 79437140Sbostic return -1; 79537140Sbostic sp->ttis[i].tt_gmtoff = -dstoffset; 79637140Sbostic sp->ttis[i].tt_abbrind = stdlen + 1; 79737140Sbostic sawdst = TRUE; 79837140Sbostic } else { 79937140Sbostic oldfix = stdfix; 80037140Sbostic stdfix = 80137140Sbostic sp->ttis[i].tt_gmtoff + stdoffset; 80237140Sbostic if (sawstd && (oldfix != stdfix)) 80337140Sbostic return -1; 80437140Sbostic sp->ttis[i].tt_gmtoff = -stdoffset; 80537140Sbostic sp->ttis[i].tt_abbrind = 0; 80637140Sbostic sawstd = TRUE; 80737140Sbostic } 80837140Sbostic } 80937140Sbostic /* 81037140Sbostic ** Make sure we have both standard and summer time. 81137140Sbostic */ 81237140Sbostic if (!sawdst || !sawstd) 81337140Sbostic return -1; 81437140Sbostic /* 81537140Sbostic ** Now correct the transition times by shifting 81637140Sbostic ** them by the difference between the real and 81737140Sbostic ** prototype offsets. Note that this difference 81837140Sbostic ** can be different in standard and summer time; 81937140Sbostic ** the prototype probably has a 1-hour difference 82037140Sbostic ** between standard and summer time, but a different 82137140Sbostic ** difference can be specified in TZ. 82237140Sbostic */ 82337140Sbostic isdst = FALSE; /* we start in standard time */ 82437140Sbostic for (i = 0; i < sp->timecnt; ++i) { 82537140Sbostic register const struct ttinfo * ttisp; 82637140Sbostic 82737140Sbostic /* 82837140Sbostic ** If summer time is in effect, and the 82937140Sbostic ** transition time was not specified as 83037140Sbostic ** standard time, add the summer time 83137140Sbostic ** offset to the transition time; 83237140Sbostic ** otherwise, add the standard time offset 83337140Sbostic ** to the transition time. 83437140Sbostic */ 83537140Sbostic ttisp = &sp->ttis[sp->types[i]]; 83637140Sbostic sp->ats[i] += 83737140Sbostic (isdst && !ttisp->tt_ttisstd) ? 83837140Sbostic dstfix : stdfix; 83937140Sbostic isdst = ttisp->tt_isdst; 84037140Sbostic } 84130608Sbostic } 84237140Sbostic } else { 84337140Sbostic dstlen = 0; 84437140Sbostic sp->typecnt = 1; /* only standard time */ 84537140Sbostic sp->timecnt = 0; 84637140Sbostic sp->ttis[0].tt_gmtoff = -stdoffset; 84737140Sbostic sp->ttis[0].tt_isdst = 0; 84837140Sbostic sp->ttis[0].tt_abbrind = 0; 84930608Sbostic } 85037140Sbostic sp->charcnt = stdlen + 1; 85137140Sbostic if (dstlen != 0) 85237140Sbostic sp->charcnt += dstlen + 1; 85337140Sbostic if (sp->charcnt > sizeof sp->chars) 85430682Sbostic return -1; 85537140Sbostic cp = sp->chars; 85637140Sbostic (void) strncpy(cp, stdname, stdlen); 85737140Sbostic cp += stdlen; 85837140Sbostic *cp++ = '\0'; 85937140Sbostic if (dstlen != 0) { 86037140Sbostic (void) strncpy(cp, dstname, dstlen); 86137140Sbostic *(cp + dstlen) = '\0'; 86237140Sbostic } 86330682Sbostic return 0; 86430682Sbostic } 86530682Sbostic 86637140Sbostic static void 86737140Sbostic gmtload(sp) 86837140Sbostic struct state * const sp; 8691959Swnj { 87037140Sbostic if (tzload(GMT, sp) != 0) 87137140Sbostic (void) tzparse(GMT, sp, TRUE); 8721959Swnj } 8731959Swnj 87430608Sbostic void 87530608Sbostic tzset() 87630608Sbostic { 87737140Sbostic register const char * name; 87837140Sbostic void tzsetwall(); 87930608Sbostic 88030608Sbostic name = getenv("TZ"); 88137140Sbostic if (name == NULL) { 88237140Sbostic tzsetwall(); 88337140Sbostic return; 88437140Sbostic } 88537140Sbostic lcl_is_set = TRUE; 88637140Sbostic #ifdef ALL_STATE 88737140Sbostic if (lclptr == NULL) { 88837140Sbostic lclptr = (struct state *) malloc(sizeof *lclptr); 88937140Sbostic if (lclptr == NULL) { 89037140Sbostic settzname(); /* all we can do */ 89130682Sbostic return; 89237140Sbostic } 89337140Sbostic } 89437140Sbostic #endif /* defined ALL_STATE */ 89537140Sbostic if (*name == '\0') { 89637140Sbostic /* 89737140Sbostic ** User wants it fast rather than right. 89837140Sbostic */ 89937140Sbostic lclptr->leapcnt = 0; /* so, we're off a little */ 90037140Sbostic lclptr->timecnt = 0; 90137140Sbostic lclptr->ttis[0].tt_gmtoff = 0; 90237140Sbostic lclptr->ttis[0].tt_abbrind = 0; 90337140Sbostic (void) strcpy(lclptr->chars, GMT); 90437140Sbostic } else if (tzload(name, lclptr) != 0) 90537140Sbostic if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) 906*39105Sbostic (void) gmtload(lclptr); 90737140Sbostic settzname(); 90837140Sbostic } 90937140Sbostic 91037140Sbostic void 91137140Sbostic tzsetwall() 91237140Sbostic { 91337140Sbostic lcl_is_set = TRUE; 91437140Sbostic #ifdef ALL_STATE 91537140Sbostic if (lclptr == NULL) { 91637140Sbostic lclptr = (struct state *) malloc(sizeof *lclptr); 91737140Sbostic if (lclptr == NULL) { 91837140Sbostic settzname(); /* all we can do */ 91930682Sbostic return; 92037140Sbostic } 92130682Sbostic } 92237140Sbostic #endif /* defined ALL_STATE */ 92337140Sbostic if (tzload((char *) NULL, lclptr) != 0) 92437140Sbostic gmtload(lclptr); 92537140Sbostic settzname(); 92630608Sbostic } 92730608Sbostic 92837140Sbostic /* 92937140Sbostic ** The easy way to behave "as if no library function calls" localtime 93037140Sbostic ** is to not call it--so we drop its guts into "localsub", which can be 93137140Sbostic ** freely called. (And no, the PANS doesn't require the above behavior-- 93237140Sbostic ** but it *is* desirable.) 93337140Sbostic ** 93437140Sbostic ** The unused offset argument is for the benefit of mktime variants. 93537140Sbostic */ 93637140Sbostic 93737140Sbostic /*ARGSUSED*/ 93837140Sbostic static void 93937140Sbostic localsub(timep, offset, tmp) 94037140Sbostic const time_t * const timep; 94137140Sbostic const long offset; 94237140Sbostic struct tm * const tmp; 9431959Swnj { 94437140Sbostic register const struct state * sp; 94537140Sbostic register const struct ttinfo * ttisp; 94630608Sbostic register int i; 94737140Sbostic const time_t t = *timep; 9481959Swnj 94937140Sbostic if (!lcl_is_set) 95037140Sbostic tzset(); 95137140Sbostic sp = lclptr; 95237140Sbostic #ifdef ALL_STATE 95337140Sbostic if (sp == NULL) { 95437140Sbostic gmtsub(timep, offset, tmp); 95537140Sbostic return; 95637140Sbostic } 95737140Sbostic #endif /* defined ALL_STATE */ 95837140Sbostic if (sp->timecnt == 0 || t < sp->ats[0]) { 95930608Sbostic i = 0; 96037140Sbostic while (sp->ttis[i].tt_isdst) 96137140Sbostic if (++i >= sp->typecnt) { 96230608Sbostic i = 0; 96330608Sbostic break; 96430608Sbostic } 96530608Sbostic } else { 96637140Sbostic for (i = 1; i < sp->timecnt; ++i) 96737140Sbostic if (t < sp->ats[i]) 96830608Sbostic break; 96937140Sbostic i = sp->types[i - 1]; 9701959Swnj } 97137140Sbostic ttisp = &sp->ttis[i]; 9721959Swnj /* 97330608Sbostic ** To get (wrong) behavior that's compatible with System V Release 2.0 97430608Sbostic ** you'd replace the statement below with 97537140Sbostic ** t += ttisp->tt_gmtoff; 97637140Sbostic ** timesub(&t, 0L, sp, tmp); 97730608Sbostic */ 97837140Sbostic timesub(&t, ttisp->tt_gmtoff, sp, tmp); 97930608Sbostic tmp->tm_isdst = ttisp->tt_isdst; 98037140Sbostic tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind]; 98137140Sbostic tmp->tm_zone = &sp->chars[ttisp->tt_abbrind]; 98230608Sbostic } 9831959Swnj 98430608Sbostic struct tm * 98537140Sbostic localtime(timep) 98637140Sbostic const time_t * const timep; 98730608Sbostic { 98837140Sbostic static struct tm tm; 9891959Swnj 99037140Sbostic localsub(timep, 0L, &tm); 99137140Sbostic return &tm; 99230608Sbostic } 9931959Swnj 99437140Sbostic /* 99537140Sbostic ** gmtsub is to gmtime as localsub is to localtime. 99637140Sbostic */ 9971959Swnj 99837140Sbostic static void 99937140Sbostic gmtsub(timep, offset, tmp) 100037140Sbostic const time_t * const timep; 100137140Sbostic const long offset; 100237140Sbostic struct tm * const tmp; 100337140Sbostic { 100437140Sbostic if (!gmt_is_set) { 100537140Sbostic gmt_is_set = TRUE; 100637140Sbostic #ifdef ALL_STATE 100737140Sbostic gmtptr = (struct state *) malloc(sizeof *gmtptr); 100837140Sbostic if (gmtptr != NULL) 100937140Sbostic #endif /* defined ALL_STATE */ 101037140Sbostic gmtload(gmtptr); 101137140Sbostic } 101237140Sbostic timesub(timep, offset, gmtptr, tmp); 101337140Sbostic /* 101437140Sbostic ** Could get fancy here and deliver something such as 101537140Sbostic ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero, 101637140Sbostic ** but this is no time for a treasure hunt. 101737140Sbostic */ 101837140Sbostic if (offset != 0) 101937140Sbostic tmp->tm_zone = WILDABBR; 102037140Sbostic else { 102137140Sbostic #ifdef ALL_STATE 102237140Sbostic if (gmtptr == NULL) 102337140Sbostic tmp->TM_ZONE = GMT; 102437140Sbostic else tmp->TM_ZONE = gmtptr->chars; 102537140Sbostic #endif /* defined ALL_STATE */ 102637140Sbostic #ifndef ALL_STATE 102737140Sbostic tmp->tm_zone = gmtptr->chars; 102837140Sbostic #endif /* State Farm */ 102937140Sbostic } 103037140Sbostic } 10311959Swnj 103230608Sbostic struct tm * 103337140Sbostic gmtime(timep) 103437140Sbostic const time_t * const timep; 10351959Swnj { 103630608Sbostic static struct tm tm; 10371959Swnj 103837140Sbostic gmtsub(timep, 0L, &tm); 103937140Sbostic return &tm; 104037140Sbostic } 104137140Sbostic 104237140Sbostic static void 104337140Sbostic timesub(timep, offset, sp, tmp) 104437140Sbostic const time_t * const timep; 104537140Sbostic const long offset; 104637140Sbostic register const struct state * const sp; 104737140Sbostic register struct tm * const tmp; 104837140Sbostic { 104937140Sbostic register const struct lsinfo * lp; 105037140Sbostic register long days; 105137140Sbostic register long rem; 105237140Sbostic register int y; 105337140Sbostic register int yleap; 105437140Sbostic register const int * ip; 105537140Sbostic register long corr; 105637140Sbostic register int hit; 105737140Sbostic register int i; 105837140Sbostic 105937140Sbostic corr = 0; 106037140Sbostic hit = FALSE; 106137140Sbostic #ifdef ALL_STATE 106237140Sbostic i = (sp == NULL) ? 0 : sp->leapcnt; 106337140Sbostic #endif /* defined ALL_STATE */ 106437140Sbostic #ifndef ALL_STATE 106537140Sbostic i = sp->leapcnt; 106637140Sbostic #endif /* State Farm */ 106737140Sbostic while (--i >= 0) { 106837140Sbostic lp = &sp->lsis[i]; 106937140Sbostic if (*timep >= lp->ls_trans) { 107037140Sbostic if (*timep == lp->ls_trans) 107137140Sbostic hit = ((i == 0 && lp->ls_corr > 0) || 107237140Sbostic lp->ls_corr > sp->lsis[i - 1].ls_corr); 107337140Sbostic corr = lp->ls_corr; 107437140Sbostic break; 107537140Sbostic } 107637140Sbostic } 107737140Sbostic days = *timep / SECSPERDAY; 107837140Sbostic rem = *timep % SECSPERDAY; 107937140Sbostic #ifdef mc68k 108037140Sbostic if (*timep == 0x80000000) { 108137140Sbostic /* 108237140Sbostic ** A 3B1 muffs the division on the most negative number. 108337140Sbostic */ 108437140Sbostic days = -24855; 108537140Sbostic rem = -11648; 108637140Sbostic } 108737140Sbostic #endif /* mc68k */ 108837140Sbostic rem += (offset - corr); 108930608Sbostic while (rem < 0) { 109037140Sbostic rem += SECSPERDAY; 109130608Sbostic --days; 10921959Swnj } 109337140Sbostic while (rem >= SECSPERDAY) { 109437140Sbostic rem -= SECSPERDAY; 109530608Sbostic ++days; 109630608Sbostic } 109737140Sbostic tmp->tm_hour = (int) (rem / SECSPERHOUR); 109837140Sbostic rem = rem % SECSPERHOUR; 109937140Sbostic tmp->tm_min = (int) (rem / SECSPERMIN); 110037140Sbostic tmp->tm_sec = (int) (rem % SECSPERMIN); 110137140Sbostic if (hit) 110237140Sbostic /* 110337140Sbostic ** A positive leap second requires a special 110437140Sbostic ** representation. This uses "... ??:59:60". 110537140Sbostic */ 110637140Sbostic ++(tmp->tm_sec); 110737140Sbostic tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK); 110830608Sbostic if (tmp->tm_wday < 0) 110937140Sbostic tmp->tm_wday += DAYSPERWEEK; 111030608Sbostic y = EPOCH_YEAR; 111130608Sbostic if (days >= 0) 111230608Sbostic for ( ; ; ) { 111330608Sbostic yleap = isleap(y); 111430608Sbostic if (days < (long) year_lengths[yleap]) 111530608Sbostic break; 111630608Sbostic ++y; 111730608Sbostic days = days - (long) year_lengths[yleap]; 111830608Sbostic } 111930608Sbostic else do { 112030608Sbostic --y; 112130608Sbostic yleap = isleap(y); 112230608Sbostic days = days + (long) year_lengths[yleap]; 112330608Sbostic } while (days < 0); 112430608Sbostic tmp->tm_year = y - TM_YEAR_BASE; 112530608Sbostic tmp->tm_yday = (int) days; 112630608Sbostic ip = mon_lengths[yleap]; 112730608Sbostic for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon)) 112830608Sbostic days = days - (long) ip[tmp->tm_mon]; 112930608Sbostic tmp->tm_mday = (int) (days + 1); 113030608Sbostic tmp->tm_isdst = 0; 113137140Sbostic #ifdef TM_GMTOFF 113237140Sbostic tmp->TM_GMTOFF = offset; 113337140Sbostic #endif /* defined TM_GMTOFF */ 11341959Swnj } 113537140Sbostic 113637140Sbostic /* 113737140Sbostic ** A la X3J11 113837140Sbostic */ 113937140Sbostic 114037140Sbostic char * 114137140Sbostic asctime(timeptr) 114237140Sbostic register const struct tm * timeptr; 114337140Sbostic { 114437140Sbostic static const char wday_name[DAYSPERWEEK][3] = { 114537140Sbostic "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 114637140Sbostic }; 114737140Sbostic static const char mon_name[MONSPERYEAR][3] = { 114837140Sbostic "Jan", "Feb", "Mar", "Apr", "May", "Jun", 114937140Sbostic "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 115037140Sbostic }; 115137140Sbostic static char result[26]; 115237140Sbostic 115337140Sbostic (void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n", 115437140Sbostic wday_name[timeptr->tm_wday], 115537140Sbostic mon_name[timeptr->tm_mon], 115637140Sbostic timeptr->tm_mday, timeptr->tm_hour, 115737140Sbostic timeptr->tm_min, timeptr->tm_sec, 115837140Sbostic TM_YEAR_BASE + timeptr->tm_year); 115937140Sbostic return result; 116037140Sbostic } 116137140Sbostic 116237140Sbostic char * 116337140Sbostic ctime(timep) 116437140Sbostic const time_t * const timep; 116537140Sbostic { 116637140Sbostic return asctime(localtime(timep)); 116737140Sbostic } 116837140Sbostic 116937140Sbostic /* 117037140Sbostic ** Adapted from code provided by Robert Elz, who writes: 117137140Sbostic ** The "best" way to do mktime I think is based on an idea of Bob 117237140Sbostic ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now). 117337140Sbostic ** It does a binary search of the time_t space. Since time_t's are 117437140Sbostic ** just 32 bits, its a max of 32 iterations (even at 64 bits it 117537140Sbostic ** would still be very reasonable). 117637140Sbostic */ 117737140Sbostic 117837140Sbostic #ifndef WRONG 117937140Sbostic #define WRONG (-1) 118037140Sbostic #endif /* !defined WRONG */ 118137140Sbostic 118237140Sbostic static void 118337140Sbostic normalize(tensptr, unitsptr, base) 118437140Sbostic int * const tensptr; 118537140Sbostic int * const unitsptr; 118637140Sbostic const int base; 118737140Sbostic { 118837140Sbostic if (*unitsptr >= base) { 118937140Sbostic *tensptr += *unitsptr / base; 119037140Sbostic *unitsptr %= base; 119137140Sbostic } else if (*unitsptr < 0) { 119237140Sbostic --*tensptr; 119337140Sbostic *unitsptr += base; 119437140Sbostic if (*unitsptr < 0) { 119537140Sbostic *tensptr -= 1 + (-*unitsptr) / base; 119637140Sbostic *unitsptr = base - (-*unitsptr) % base; 119737140Sbostic } 119837140Sbostic } 119937140Sbostic } 120037140Sbostic 120137140Sbostic static int 120237140Sbostic tmcomp(atmp, btmp) 120337140Sbostic register const struct tm * const atmp; 120437140Sbostic register const struct tm * const btmp; 120537140Sbostic { 120637140Sbostic register int result; 120737140Sbostic 120837140Sbostic if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && 120937140Sbostic (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && 121037140Sbostic (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && 121137140Sbostic (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && 121237140Sbostic (result = (atmp->tm_min - btmp->tm_min)) == 0) 121337140Sbostic result = atmp->tm_sec - btmp->tm_sec; 121437140Sbostic return result; 121537140Sbostic } 121637140Sbostic 121737140Sbostic static time_t 121837140Sbostic time2(tmp, funcp, offset, okayp) 121937140Sbostic struct tm * const tmp; 122037140Sbostic void (* const funcp)(); 122137140Sbostic const long offset; 122237140Sbostic int * const okayp; 122337140Sbostic { 122437140Sbostic register const struct state * sp; 122537140Sbostic register int dir; 122637140Sbostic register int bits; 122737140Sbostic register int i, j ; 122837140Sbostic register int saved_seconds; 122937140Sbostic time_t newt; 123037140Sbostic time_t t; 123137140Sbostic struct tm yourtm, mytm; 123237140Sbostic 123337140Sbostic *okayp = FALSE; 123437140Sbostic yourtm = *tmp; 123537140Sbostic if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0) 123637140Sbostic normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN); 123737140Sbostic normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR); 123837140Sbostic normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY); 123937140Sbostic normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR); 124037140Sbostic while (yourtm.tm_mday <= 0) { 124137140Sbostic --yourtm.tm_year; 124237140Sbostic yourtm.tm_mday += 124337140Sbostic year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)]; 124437140Sbostic } 124537140Sbostic for ( ; ; ) { 124637140Sbostic i = mon_lengths[isleap(yourtm.tm_year + 124737140Sbostic TM_YEAR_BASE)][yourtm.tm_mon]; 124837140Sbostic if (yourtm.tm_mday <= i) 124937140Sbostic break; 125037140Sbostic yourtm.tm_mday -= i; 125137140Sbostic if (++yourtm.tm_mon >= MONSPERYEAR) { 125237140Sbostic yourtm.tm_mon = 0; 125337140Sbostic ++yourtm.tm_year; 125437140Sbostic } 125537140Sbostic } 125637140Sbostic saved_seconds = yourtm.tm_sec; 125737140Sbostic yourtm.tm_sec = 0; 125837140Sbostic /* 125937140Sbostic ** Calculate the number of magnitude bits in a time_t 126037140Sbostic ** (this works regardless of whether time_t is 126137140Sbostic ** signed or unsigned, though lint complains if unsigned). 126237140Sbostic */ 126337140Sbostic for (bits = 0, t = 1; t > 0; ++bits, t <<= 1) 126437140Sbostic ; 126537140Sbostic /* 126637140Sbostic ** If time_t is signed, then 0 is the median value, 126737140Sbostic ** if time_t is unsigned, then 1 << bits is median. 126837140Sbostic */ 126937140Sbostic t = (t < 0) ? 0 : ((time_t) 1 << bits); 127037140Sbostic for ( ; ; ) { 127137140Sbostic (*funcp)(&t, offset, &mytm); 127237140Sbostic dir = tmcomp(&mytm, &yourtm); 127337140Sbostic if (dir != 0) { 127437140Sbostic if (bits-- < 0) 127537140Sbostic return WRONG; 127637140Sbostic if (bits < 0) 127737140Sbostic --t; 127837140Sbostic else if (dir > 0) 127937140Sbostic t -= (time_t) 1 << bits; 128037140Sbostic else t += (time_t) 1 << bits; 128137140Sbostic continue; 128237140Sbostic } 128337140Sbostic if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) 128437140Sbostic break; 128537140Sbostic /* 128637140Sbostic ** Right time, wrong type. 128737140Sbostic ** Hunt for right time, right type. 128837140Sbostic ** It's okay to guess wrong since the guess 128937140Sbostic ** gets checked. 129037140Sbostic */ 129137140Sbostic sp = (const struct state *) 129237140Sbostic ((funcp == localsub) ? lclptr : gmtptr); 129337140Sbostic #ifdef ALL_STATE 129437140Sbostic if (sp == NULL) 129537140Sbostic return WRONG; 129637140Sbostic #endif /* defined ALL_STATE */ 129737140Sbostic for (i = 0; i < sp->typecnt; ++i) { 129837140Sbostic if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) 129937140Sbostic continue; 130037140Sbostic for (j = 0; j < sp->typecnt; ++j) { 130137140Sbostic if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) 130237140Sbostic continue; 130337140Sbostic newt = t + sp->ttis[j].tt_gmtoff - 130437140Sbostic sp->ttis[i].tt_gmtoff; 130537140Sbostic (*funcp)(&newt, offset, &mytm); 130637140Sbostic if (tmcomp(&mytm, &yourtm) != 0) 130737140Sbostic continue; 130837140Sbostic if (mytm.tm_isdst != yourtm.tm_isdst) 130937140Sbostic continue; 131037140Sbostic /* 131137140Sbostic ** We have a match. 131237140Sbostic */ 131337140Sbostic t = newt; 131437140Sbostic goto label; 131537140Sbostic } 131637140Sbostic } 131737140Sbostic return WRONG; 131837140Sbostic } 131937140Sbostic label: 132037140Sbostic t += saved_seconds; 132137140Sbostic (*funcp)(&t, offset, tmp); 132237140Sbostic *okayp = TRUE; 132337140Sbostic return t; 132437140Sbostic } 132537140Sbostic 132637140Sbostic static time_t 132737140Sbostic time1(tmp, funcp, offset) 132837140Sbostic struct tm * const tmp; 132937140Sbostic void (* const funcp)(); 133037140Sbostic const long offset; 133137140Sbostic { 133237140Sbostic register time_t t; 133337140Sbostic register const struct state * sp; 133437140Sbostic register int samei, otheri; 133537140Sbostic int okay; 133637140Sbostic 133737140Sbostic if (tmp->tm_isdst > 1) 133838558Sbostic tmp->tm_isdst = 1; 133937140Sbostic t = time2(tmp, funcp, offset, &okay); 134037140Sbostic if (okay || tmp->tm_isdst < 0) 134137140Sbostic return t; 134237140Sbostic /* 134337140Sbostic ** We're supposed to assume that somebody took a time of one type 134437140Sbostic ** and did some math on it that yielded a "struct tm" that's bad. 134537140Sbostic ** We try to divine the type they started from and adjust to the 134637140Sbostic ** type they need. 134737140Sbostic */ 134837140Sbostic sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); 134937140Sbostic #ifdef ALL_STATE 135037140Sbostic if (sp == NULL) 135137140Sbostic return WRONG; 135237140Sbostic #endif /* defined ALL_STATE */ 135337140Sbostic for (samei = 0; samei < sp->typecnt; ++samei) { 135437140Sbostic if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) 135537140Sbostic continue; 135637140Sbostic for (otheri = 0; otheri < sp->typecnt; ++otheri) { 135737140Sbostic if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) 135837140Sbostic continue; 135937140Sbostic tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - 136037140Sbostic sp->ttis[samei].tt_gmtoff; 136137140Sbostic tmp->tm_isdst = !tmp->tm_isdst; 136237140Sbostic t = time2(tmp, funcp, offset, &okay); 136337140Sbostic if (okay) 136437140Sbostic return t; 136537140Sbostic tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - 136637140Sbostic sp->ttis[samei].tt_gmtoff; 136737140Sbostic tmp->tm_isdst = !tmp->tm_isdst; 136837140Sbostic } 136937140Sbostic } 137037140Sbostic return WRONG; 137137140Sbostic } 137237140Sbostic 137337140Sbostic time_t 137437140Sbostic mktime(tmp) 137537140Sbostic struct tm * const tmp; 137637140Sbostic { 137737140Sbostic return time1(tmp, localsub, 0L); 137837140Sbostic } 1379