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*40007Sbostic static char sccsid[] = "@(#)ctime.c 5.20 (Berkeley) 02/02/90"; 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 5239720Sbostic #define P(s) () 5337140Sbostic #define const 5437140Sbostic #define volatile 5537140Sbostic 5637140Sbostic typedef char * genericptr_t; 5737140Sbostic typedef unsigned alloc_size_t; 5837140Sbostic typedef int qsort_size_t; 5937140Sbostic typedef int fread_size_t; 6037140Sbostic typedef int fwrite_size_t; 6137140Sbostic 6237140Sbostic extern char * calloc(); 6337140Sbostic extern char * malloc(); 6437140Sbostic extern char * realloc(); 6537140Sbostic extern char * getenv(); 6637140Sbostic 6737140Sbostic #endif /* !defined __STDC__ */ 6837140Sbostic 6937140Sbostic extern time_t time(); 7037140Sbostic 7137140Sbostic #define FILENAME_MAX MAXPATHLEN 7237140Sbostic #define ACCESS_MODE O_RDONLY 7337140Sbostic #define OPEN_MODE O_RDONLY 7437140Sbostic 7537140Sbostic #ifndef WILDABBR 761959Swnj /* 7737140Sbostic ** Someone might make incorrect use of a time zone abbreviation: 7837140Sbostic ** 1. They might reference tzname[0] before calling tzset (explicitly 7937140Sbostic ** or implicitly). 8037140Sbostic ** 2. They might reference tzname[1] before calling tzset (explicitly 8137140Sbostic ** or implicitly). 8237140Sbostic ** 3. They might reference tzname[1] after setting to a time zone 8337140Sbostic ** in which Daylight Saving Time is never observed. 8437140Sbostic ** 4. They might reference tzname[0] after setting to a time zone 8537140Sbostic ** in which Standard Time is never observed. 8637140Sbostic ** 5. They might reference tm.TM_ZONE after calling offtime. 8737140Sbostic ** What's best to do in the above cases is open to debate; 8837140Sbostic ** for now, we just set things up so that in any of the five cases 8937140Sbostic ** WILDABBR is used. Another possibility: initialize tzname[0] to the 9037140Sbostic ** string "tzname[0] used before set", and similarly for the other cases. 9137140Sbostic ** And another: initialize tzname[0] to "ERA", with an explanation in the 9237140Sbostic ** manual page of what this "time zone abbreviation" means (doing this so 9337140Sbostic ** that tzname[0] has the "normal" length of three characters). 9430608Sbostic */ 9537140Sbostic #define WILDABBR " " 9637140Sbostic #endif /* !defined WILDABBR */ 971959Swnj 9830608Sbostic #ifndef TRUE 9930608Sbostic #define TRUE 1 10030608Sbostic #define FALSE 0 10137140Sbostic #endif /* !defined TRUE */ 10230608Sbostic 10337140Sbostic static const char GMT[] = "GMT"; 10430608Sbostic 10530608Sbostic struct ttinfo { /* time type information */ 10630608Sbostic long tt_gmtoff; /* GMT offset in seconds */ 10730608Sbostic int tt_isdst; /* used to set tm_isdst */ 10830608Sbostic int tt_abbrind; /* abbreviation list index */ 10937140Sbostic int tt_ttisstd; /* TRUE if transition is std time */ 11013876Ssam }; 11113876Ssam 11237140Sbostic struct lsinfo { /* leap second information */ 11337140Sbostic time_t ls_trans; /* transition time */ 11437140Sbostic long ls_corr; /* correction to apply */ 11537140Sbostic }; 11637140Sbostic 11730608Sbostic struct state { 11837140Sbostic int leapcnt; 11930608Sbostic int timecnt; 12030608Sbostic int typecnt; 12130608Sbostic int charcnt; 12230608Sbostic time_t ats[TZ_MAX_TIMES]; 12330608Sbostic unsigned char types[TZ_MAX_TIMES]; 12430608Sbostic struct ttinfo ttis[TZ_MAX_TYPES]; 12537140Sbostic char chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ? 12637140Sbostic TZ_MAX_CHARS + 1 : sizeof GMT]; 12737140Sbostic struct lsinfo lsis[TZ_MAX_LEAPS]; 12823720Skre }; 12923720Skre 13037140Sbostic struct rule { 13137140Sbostic int r_type; /* type of rule--see below */ 13237140Sbostic int r_day; /* day number of rule */ 13337140Sbostic int r_week; /* week number of rule */ 13437140Sbostic int r_mon; /* month number of rule */ 13537140Sbostic long r_time; /* transition time of rule */ 13637140Sbostic }; 13730608Sbostic 13837140Sbostic #define JULIAN_DAY 0 /* Jn - Julian day */ 13937140Sbostic #define DAY_OF_YEAR 1 /* n - day of year */ 14037140Sbostic #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ 14130608Sbostic 14237140Sbostic /* 14337140Sbostic ** Prototypes for static functions. 14437140Sbostic */ 14537140Sbostic 14637140Sbostic static long detzcode P((const char * codep)); 14737140Sbostic static const char * getzname P((const char * strp)); 14837140Sbostic static const char * getnum P((const char * strp, int * nump, int min, 14937140Sbostic int max)); 15037140Sbostic static const char * getsecs P((const char * strp, long * secsp)); 15137140Sbostic static const char * getoffset P((const char * strp, long * offsetp)); 15237140Sbostic static const char * getrule P((const char * strp, struct rule * rulep)); 15337140Sbostic static void gmtload P((struct state * sp)); 15437140Sbostic static void gmtsub P((const time_t * timep, long offset, 15537140Sbostic struct tm * tmp)); 15637140Sbostic static void localsub P((const time_t * timep, long offset, 15737140Sbostic struct tm * tmp)); 15837140Sbostic static void normalize P((int * tensptr, int * unitsptr, int base)); 15937140Sbostic static void settzname P((void)); 16037140Sbostic static time_t time1 P((struct tm * tmp, void (* funcp)(), 16137140Sbostic long offset)); 16237140Sbostic static time_t time2 P((struct tm *tmp, void (* funcp)(), 16337140Sbostic long offset, int * okayp)); 16437140Sbostic static void timesub P((const time_t * timep, long offset, 16537140Sbostic const struct state * sp, struct tm * tmp)); 16637140Sbostic static int tmcomp P((const struct tm * atmp, 16737140Sbostic const struct tm * btmp)); 16837140Sbostic static time_t transtime P((time_t janfirst, int year, 16937140Sbostic const struct rule * rulep, long offset)); 17037140Sbostic static int tzload P((const char * name, struct state * sp)); 17137140Sbostic static int tzparse P((const char * name, struct state * sp, 17237140Sbostic int lastditch)); 17337140Sbostic 17437140Sbostic #ifdef ALL_STATE 17537140Sbostic static struct state * lclptr; 17637140Sbostic static struct state * gmtptr; 17737140Sbostic #endif /* defined ALL_STATE */ 17837140Sbostic 17937140Sbostic #ifndef ALL_STATE 18037140Sbostic static struct state lclmem; 18137140Sbostic static struct state gmtmem; 18237140Sbostic #define lclptr (&lclmem) 18337140Sbostic #define gmtptr (&gmtmem) 18437140Sbostic #endif /* State Farm */ 18537140Sbostic 18637140Sbostic static int lcl_is_set; 18737140Sbostic static int gmt_is_set; 18837140Sbostic 18930608Sbostic char * tzname[2] = { 19037140Sbostic WILDABBR, 19137140Sbostic WILDABBR 19212974Ssam }; 19312974Ssam 19430608Sbostic #ifdef USG_COMPAT 19530608Sbostic time_t timezone = 0; 19630608Sbostic int daylight = 0; 19737140Sbostic #endif /* defined USG_COMPAT */ 1981959Swnj 19937140Sbostic #ifdef ALTZONE 20037140Sbostic time_t altzone = 0; 20137140Sbostic #endif /* defined ALTZONE */ 20237140Sbostic 20330608Sbostic static long 20430608Sbostic detzcode(codep) 20537140Sbostic const char * const codep; 2061959Swnj { 20730608Sbostic register long result; 20830608Sbostic register int i; 20930608Sbostic 21030608Sbostic result = 0; 21130608Sbostic for (i = 0; i < 4; ++i) 21230608Sbostic result = (result << 8) | (codep[i] & 0xff); 21330608Sbostic return result; 2141959Swnj } 2151959Swnj 21637140Sbostic static void 21737140Sbostic settzname() 2181959Swnj { 21937140Sbostic register const struct state * const sp = lclptr; 22037140Sbostic register int i; 2211959Swnj 22237140Sbostic tzname[0] = WILDABBR; 22337140Sbostic tzname[1] = WILDABBR; 22437140Sbostic #ifdef USG_COMPAT 22537140Sbostic daylight = 0; 22637140Sbostic timezone = 0; 22737140Sbostic #endif /* defined USG_COMPAT */ 22837140Sbostic #ifdef ALTZONE 22937140Sbostic altzone = 0; 23037140Sbostic #endif /* defined ALTZONE */ 23137140Sbostic #ifdef ALL_STATE 23237140Sbostic if (sp == NULL) { 23337140Sbostic tzname[0] = tzname[1] = GMT; 23437140Sbostic return; 23537140Sbostic } 23637140Sbostic #endif /* defined ALL_STATE */ 23737140Sbostic for (i = 0; i < sp->typecnt; ++i) { 23837140Sbostic register const struct ttinfo * const ttisp = &sp->ttis[i]; 23937140Sbostic 24037140Sbostic tzname[ttisp->tt_isdst] = 24137140Sbostic (char *) &sp->chars[ttisp->tt_abbrind]; 24237140Sbostic #ifdef USG_COMPAT 24337140Sbostic if (ttisp->tt_isdst) 24437140Sbostic daylight = 1; 24537140Sbostic if (i == 0 || !ttisp->tt_isdst) 24637140Sbostic timezone = -(ttisp->tt_gmtoff); 24737140Sbostic #endif /* defined USG_COMPAT */ 24837140Sbostic #ifdef ALTZONE 24937140Sbostic if (i == 0 || ttisp->tt_isdst) 25037140Sbostic altzone = -(ttisp->tt_gmtoff); 25137140Sbostic #endif /* defined ALTZONE */ 25237140Sbostic } 25337140Sbostic /* 25437140Sbostic ** And to get the latest zone names into tzname. . . 25537140Sbostic */ 25637140Sbostic for (i = 0; i < sp->timecnt; ++i) { 25737140Sbostic register const struct ttinfo * const ttisp = 25837140Sbostic &sp->ttis[sp->types[i]]; 25937140Sbostic 26037140Sbostic tzname[ttisp->tt_isdst] = 26137140Sbostic (char *) &sp->chars[ttisp->tt_abbrind]; 26237140Sbostic } 26337140Sbostic } 26437140Sbostic 26537140Sbostic static int 26637140Sbostic tzload(name, sp) 26737140Sbostic register const char * name; 26837140Sbostic register struct state * const sp; 26937140Sbostic { 27037140Sbostic register const char * p; 27137140Sbostic register int i; 27237140Sbostic register int fid; 27337140Sbostic 27437140Sbostic if (name == NULL && (name = TZDEFAULT) == NULL) 27530608Sbostic return -1; 27630608Sbostic { 27737140Sbostic char fullname[FILENAME_MAX + 1]; 27830608Sbostic 27937140Sbostic if (name[0] == ':') 28037140Sbostic ++name; 281*40007Sbostic if (name[0] != '/') { 28237140Sbostic if ((p = TZDIR) == NULL) 28330608Sbostic return -1; 28430608Sbostic if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) 28530608Sbostic return -1; 28630608Sbostic (void) strcpy(fullname, p); 28730608Sbostic (void) strcat(fullname, "/"); 28830608Sbostic (void) strcat(fullname, name); 28930608Sbostic name = fullname; 29030608Sbostic } 29137140Sbostic if ((fid = open(name, OPEN_MODE)) == -1) 29230608Sbostic return -1; 29325662Sbloom } 29430608Sbostic { 29537140Sbostic register const struct tzhead * tzhp; 29637140Sbostic char buf[sizeof *sp + sizeof *tzhp]; 29737140Sbostic int ttisstdcnt; 29830608Sbostic 29930608Sbostic i = read(fid, buf, sizeof buf); 30030608Sbostic if (close(fid) != 0 || i < sizeof *tzhp) 30130608Sbostic return -1; 30230608Sbostic tzhp = (struct tzhead *) buf; 30337140Sbostic ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt); 30437140Sbostic sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt); 30537140Sbostic sp->timecnt = (int) detzcode(tzhp->tzh_timecnt); 30637140Sbostic sp->typecnt = (int) detzcode(tzhp->tzh_typecnt); 30737140Sbostic sp->charcnt = (int) detzcode(tzhp->tzh_charcnt); 30837140Sbostic if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 30937140Sbostic sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 31037140Sbostic sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 31137140Sbostic sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 31237140Sbostic (ttisstdcnt != sp->typecnt && ttisstdcnt != 0)) 31330608Sbostic return -1; 31430608Sbostic if (i < sizeof *tzhp + 31537140Sbostic sp->timecnt * (4 + sizeof (char)) + 31637140Sbostic sp->typecnt * (4 + 2 * sizeof (char)) + 31737140Sbostic sp->charcnt * sizeof (char) + 31837140Sbostic sp->leapcnt * 2 * 4 + 31937140Sbostic ttisstdcnt * sizeof (char)) 32030608Sbostic return -1; 32130608Sbostic p = buf + sizeof *tzhp; 32237140Sbostic for (i = 0; i < sp->timecnt; ++i) { 32337140Sbostic sp->ats[i] = detzcode(p); 32430608Sbostic p += 4; 32512974Ssam } 32637140Sbostic for (i = 0; i < sp->timecnt; ++i) { 32737140Sbostic sp->types[i] = (unsigned char) *p++; 32837140Sbostic if (sp->types[i] >= sp->typecnt) 32937140Sbostic return -1; 33037140Sbostic } 33137140Sbostic for (i = 0; i < sp->typecnt; ++i) { 33230608Sbostic register struct ttinfo * ttisp; 33330608Sbostic 33437140Sbostic ttisp = &sp->ttis[i]; 33530608Sbostic ttisp->tt_gmtoff = detzcode(p); 33630608Sbostic p += 4; 33730608Sbostic ttisp->tt_isdst = (unsigned char) *p++; 33837140Sbostic if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) 33937140Sbostic return -1; 34030608Sbostic ttisp->tt_abbrind = (unsigned char) *p++; 34137140Sbostic if (ttisp->tt_abbrind < 0 || 34237140Sbostic ttisp->tt_abbrind > sp->charcnt) 34337140Sbostic return -1; 34430608Sbostic } 34537140Sbostic for (i = 0; i < sp->charcnt; ++i) 34637140Sbostic sp->chars[i] = *p++; 34737140Sbostic sp->chars[i] = '\0'; /* ensure '\0' at end */ 34837140Sbostic for (i = 0; i < sp->leapcnt; ++i) { 34937140Sbostic register struct lsinfo * lsisp; 35037140Sbostic 35137140Sbostic lsisp = &sp->lsis[i]; 35237140Sbostic lsisp->ls_trans = detzcode(p); 35337140Sbostic p += 4; 35437140Sbostic lsisp->ls_corr = detzcode(p); 35537140Sbostic p += 4; 35637140Sbostic } 35737140Sbostic for (i = 0; i < sp->typecnt; ++i) { 35837140Sbostic register struct ttinfo * ttisp; 35937140Sbostic 36037140Sbostic ttisp = &sp->ttis[i]; 36137140Sbostic if (ttisstdcnt == 0) 36237140Sbostic ttisp->tt_ttisstd = FALSE; 36337140Sbostic else { 36437140Sbostic ttisp->tt_ttisstd = *p++; 36537140Sbostic if (ttisp->tt_ttisstd != TRUE && 36637140Sbostic ttisp->tt_ttisstd != FALSE) 36737140Sbostic return -1; 36837140Sbostic } 36937140Sbostic } 3701959Swnj } 37137140Sbostic return 0; 37237140Sbostic } 37337140Sbostic 37437140Sbostic static const int mon_lengths[2][MONSPERYEAR] = { 37537140Sbostic 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 37637140Sbostic 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 37737140Sbostic }; 37837140Sbostic 37937140Sbostic static const int year_lengths[2] = { 38037140Sbostic DAYSPERNYEAR, DAYSPERLYEAR 38137140Sbostic }; 38237140Sbostic 38337140Sbostic /* 38437140Sbostic ** Given a pointer into a time zone string, scan until a character that is not 38537140Sbostic ** a valid character in a zone name is found. Return a pointer to that 38637140Sbostic ** character. 38737140Sbostic */ 38837140Sbostic 38937140Sbostic static const char * 39037140Sbostic getzname(strp) 39137140Sbostic register const char * strp; 39237140Sbostic { 39337140Sbostic register char c; 39437140Sbostic 39537140Sbostic while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' && 39637140Sbostic c != '+') 39737140Sbostic ++strp; 39837140Sbostic return strp; 39937140Sbostic } 40037140Sbostic 40137140Sbostic /* 40237140Sbostic ** Given a pointer into a time zone string, extract a number from that string. 40337140Sbostic ** Check that the number is within a specified range; if it is not, return 40437140Sbostic ** NULL. 40537140Sbostic ** Otherwise, return a pointer to the first character not part of the number. 40637140Sbostic */ 40737140Sbostic 40837140Sbostic static const char * 40937140Sbostic getnum(strp, nump, min, max) 41037140Sbostic register const char * strp; 41137140Sbostic int * const nump; 41237140Sbostic const int min; 41337140Sbostic const int max; 41437140Sbostic { 41537140Sbostic register char c; 41637140Sbostic register int num; 41737140Sbostic 41837140Sbostic if (strp == NULL || !isdigit(*strp)) 41937140Sbostic return NULL; 42037140Sbostic num = 0; 42137140Sbostic while ((c = *strp) != '\0' && isdigit(c)) { 42237140Sbostic num = num * 10 + (c - '0'); 42337140Sbostic if (num > max) 42437140Sbostic return NULL; /* illegal value */ 42537140Sbostic ++strp; 42637140Sbostic } 42737140Sbostic if (num < min) 42837140Sbostic return NULL; /* illegal value */ 42937140Sbostic *nump = num; 43037140Sbostic return strp; 43137140Sbostic } 43237140Sbostic 43337140Sbostic /* 43437140Sbostic ** Given a pointer into a time zone string, extract a number of seconds, 43537140Sbostic ** in hh[:mm[:ss]] form, from the string. 43637140Sbostic ** If any error occurs, return NULL. 43737140Sbostic ** Otherwise, return a pointer to the first character not part of the number 43837140Sbostic ** of seconds. 43937140Sbostic */ 44037140Sbostic 44137140Sbostic static const char * 44237140Sbostic getsecs(strp, secsp) 44337140Sbostic register const char * strp; 44437140Sbostic long * const secsp; 44537140Sbostic { 44637140Sbostic int num; 44737140Sbostic 44837140Sbostic strp = getnum(strp, &num, 0, HOURSPERDAY); 44937140Sbostic if (strp == NULL) 45037140Sbostic return NULL; 45137140Sbostic *secsp = num * SECSPERHOUR; 45237140Sbostic if (*strp == ':') { 45337140Sbostic ++strp; 45437140Sbostic strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 45537140Sbostic if (strp == NULL) 45637140Sbostic return NULL; 45737140Sbostic *secsp += num * SECSPERMIN; 45837140Sbostic if (*strp == ':') { 45937140Sbostic ++strp; 46037140Sbostic strp = getnum(strp, &num, 0, SECSPERMIN - 1); 46137140Sbostic if (strp == NULL) 46237140Sbostic return NULL; 46337140Sbostic *secsp += num; 46437140Sbostic } 46537140Sbostic } 46637140Sbostic return strp; 46737140Sbostic } 46837140Sbostic 46937140Sbostic /* 47037140Sbostic ** Given a pointer into a time zone string, extract an offset, in 47137140Sbostic ** [+-]hh[:mm[:ss]] form, from the string. 47237140Sbostic ** If any error occurs, return NULL. 47337140Sbostic ** Otherwise, return a pointer to the first character not part of the time. 47437140Sbostic */ 47537140Sbostic 47637140Sbostic static const char * 47737140Sbostic getoffset(strp, offsetp) 47837140Sbostic register const char * strp; 47937140Sbostic long * const offsetp; 48037140Sbostic { 48137140Sbostic register int neg; 48237140Sbostic 48337140Sbostic if (*strp == '-') { 48437140Sbostic neg = 1; 48537140Sbostic ++strp; 48637140Sbostic } else if (isdigit(*strp) || *strp++ == '+') 48737140Sbostic neg = 0; 48837140Sbostic else return NULL; /* illegal offset */ 48937140Sbostic strp = getsecs(strp, offsetp); 49037140Sbostic if (strp == NULL) 49137140Sbostic return NULL; /* illegal time */ 49237140Sbostic if (neg) 49337140Sbostic *offsetp = -*offsetp; 49437140Sbostic return strp; 49537140Sbostic } 49637140Sbostic 49737140Sbostic /* 49837140Sbostic ** Given a pointer into a time zone string, extract a rule in the form 49937140Sbostic ** date[/time]. See POSIX section 8 for the format of "date" and "time". 50037140Sbostic ** If a valid rule is not found, return NULL. 50137140Sbostic ** Otherwise, return a pointer to the first character not part of the rule. 50237140Sbostic */ 50337140Sbostic 50437140Sbostic static const char * 50537140Sbostic getrule(strp, rulep) 50637140Sbostic const char * strp; 50737140Sbostic register struct rule * const rulep; 50837140Sbostic { 50937140Sbostic if (*strp == 'J') { 51037140Sbostic /* 51137140Sbostic ** Julian day. 51237140Sbostic */ 51337140Sbostic rulep->r_type = JULIAN_DAY; 51437140Sbostic ++strp; 51537140Sbostic strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 51637140Sbostic } else if (*strp == 'M') { 51737140Sbostic /* 51837140Sbostic ** Month, week, day. 51937140Sbostic */ 52037140Sbostic rulep->r_type = MONTH_NTH_DAY_OF_WEEK; 52137140Sbostic ++strp; 52237140Sbostic strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 52337140Sbostic if (strp == NULL) 52437140Sbostic return NULL; 52537140Sbostic if (*strp++ != '.') 52637140Sbostic return NULL; 52737140Sbostic strp = getnum(strp, &rulep->r_week, 1, 5); 52837140Sbostic if (strp == NULL) 52937140Sbostic return NULL; 53037140Sbostic if (*strp++ != '.') 53137140Sbostic return NULL; 53237140Sbostic strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 53337140Sbostic } else if (isdigit(*strp)) { 53437140Sbostic /* 53537140Sbostic ** Day of year. 53637140Sbostic */ 53737140Sbostic rulep->r_type = DAY_OF_YEAR; 53837140Sbostic strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 53937140Sbostic } else return NULL; /* invalid format */ 54037140Sbostic if (strp == NULL) 54137140Sbostic return NULL; 54237140Sbostic if (*strp == '/') { 54337140Sbostic /* 54437140Sbostic ** Time specified. 54537140Sbostic */ 54637140Sbostic ++strp; 54737140Sbostic strp = getsecs(strp, &rulep->r_time); 54837140Sbostic } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 54937140Sbostic return strp; 55037140Sbostic } 55137140Sbostic 55237140Sbostic /* 55337140Sbostic ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the 55437140Sbostic ** year, a rule, and the offset from GMT at the time that rule takes effect, 55537140Sbostic ** calculate the Epoch-relative time that rule takes effect. 55637140Sbostic */ 55737140Sbostic 55837140Sbostic static time_t 55937140Sbostic transtime(janfirst, year, rulep, offset) 56037140Sbostic const time_t janfirst; 56137140Sbostic const int year; 56237140Sbostic register const struct rule * const rulep; 56337140Sbostic const long offset; 56437140Sbostic { 56537140Sbostic register int leapyear; 56637140Sbostic register time_t value; 56737140Sbostic register int i; 56837140Sbostic int d, m1, yy0, yy1, yy2, dow; 56937140Sbostic 57037140Sbostic leapyear = isleap(year); 57137140Sbostic switch (rulep->r_type) { 57237140Sbostic 57337140Sbostic case JULIAN_DAY: 57437140Sbostic /* 57537140Sbostic ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap 57637140Sbostic ** years. 57737140Sbostic ** In non-leap years, or if the day number is 59 or less, just 57837140Sbostic ** add SECSPERDAY times the day number-1 to the time of 57937140Sbostic ** January 1, midnight, to get the day. 58037140Sbostic */ 58137140Sbostic value = janfirst + (rulep->r_day - 1) * SECSPERDAY; 58237140Sbostic if (leapyear && rulep->r_day >= 60) 58337140Sbostic value += SECSPERDAY; 58437140Sbostic break; 58537140Sbostic 58637140Sbostic case DAY_OF_YEAR: 58737140Sbostic /* 58837140Sbostic ** n - day of year. 58937140Sbostic ** Just add SECSPERDAY times the day number to the time of 59037140Sbostic ** January 1, midnight, to get the day. 59137140Sbostic */ 59237140Sbostic value = janfirst + rulep->r_day * SECSPERDAY; 59337140Sbostic break; 59437140Sbostic 59537140Sbostic case MONTH_NTH_DAY_OF_WEEK: 59637140Sbostic /* 59737140Sbostic ** Mm.n.d - nth "dth day" of month m. 59837140Sbostic */ 59937140Sbostic value = janfirst; 60037140Sbostic for (i = 0; i < rulep->r_mon - 1; ++i) 60137140Sbostic value += mon_lengths[leapyear][i] * SECSPERDAY; 60237140Sbostic 60337140Sbostic /* 60437140Sbostic ** Use Zeller's Congruence to get day-of-week of first day of 60537140Sbostic ** month. 60637140Sbostic */ 60737140Sbostic m1 = (rulep->r_mon + 9) % 12 + 1; 60837140Sbostic yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 60937140Sbostic yy1 = yy0 / 100; 61037140Sbostic yy2 = yy0 % 100; 61137140Sbostic dow = ((26 * m1 - 2) / 10 + 61237140Sbostic 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 61337140Sbostic if (dow < 0) 61437140Sbostic dow += DAYSPERWEEK; 61537140Sbostic 61637140Sbostic /* 61737140Sbostic ** "dow" is the day-of-week of the first day of the month. Get 61837140Sbostic ** the day-of-month (zero-origin) of the first "dow" day of the 61937140Sbostic ** month. 62037140Sbostic */ 62137140Sbostic d = rulep->r_day - dow; 62237140Sbostic if (d < 0) 62337140Sbostic d += DAYSPERWEEK; 62437140Sbostic for (i = 1; i < rulep->r_week; ++i) { 62537140Sbostic if (d + DAYSPERWEEK >= 62637140Sbostic mon_lengths[leapyear][rulep->r_mon - 1]) 62737140Sbostic break; 62837140Sbostic d += DAYSPERWEEK; 62937140Sbostic } 63037140Sbostic 63137140Sbostic /* 63237140Sbostic ** "d" is the day-of-month (zero-origin) of the day we want. 63337140Sbostic */ 63437140Sbostic value += d * SECSPERDAY; 63537140Sbostic break; 63637140Sbostic } 63737140Sbostic 63830608Sbostic /* 63937140Sbostic ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in 64037140Sbostic ** question. To get the Epoch-relative time of the specified local 64137140Sbostic ** time on that day, add the transition time and the current offset 64237140Sbostic ** from GMT. 64330608Sbostic */ 64437140Sbostic return value + rulep->r_time + offset; 64537140Sbostic } 64637140Sbostic 64737140Sbostic /* 64837140Sbostic ** Given a POSIX section 8-style TZ string, fill in the rule tables as 64937140Sbostic ** appropriate. 65037140Sbostic */ 65137140Sbostic 65237140Sbostic static int 65337140Sbostic tzparse(name, sp, lastditch) 65437140Sbostic const char * name; 65537140Sbostic register struct state * const sp; 65637140Sbostic const int lastditch; 65737140Sbostic { 65837140Sbostic const char * stdname; 65937140Sbostic const char * dstname; 66037140Sbostic int stdlen; 66137140Sbostic int dstlen; 66237140Sbostic long stdoffset; 66337140Sbostic long dstoffset; 66437140Sbostic register time_t * atp; 66537140Sbostic register unsigned char * typep; 66637140Sbostic register char * cp; 66737140Sbostic register int load_result; 66837140Sbostic 66937140Sbostic stdname = name; 67037140Sbostic if (lastditch) { 67137140Sbostic stdlen = strlen(name); /* length of standard zone name */ 67237140Sbostic name += stdlen; 67337140Sbostic if (stdlen >= sizeof sp->chars) 67437140Sbostic stdlen = (sizeof sp->chars) - 1; 67537140Sbostic } else { 67637140Sbostic name = getzname(name); 67737140Sbostic stdlen = name - stdname; 67837140Sbostic if (stdlen < 3) 67930608Sbostic return -1; 68037140Sbostic } 68137140Sbostic if (*name == '\0') 68239105Sbostic return -1; 68337140Sbostic else { 68437140Sbostic name = getoffset(name, &stdoffset); 68537140Sbostic if (name == NULL) 68630608Sbostic return -1; 68737140Sbostic } 68837140Sbostic load_result = tzload(TZDEFRULES, sp); 68937140Sbostic if (load_result != 0) 69037140Sbostic sp->leapcnt = 0; /* so, we're off a little */ 69137140Sbostic if (*name != '\0') { 69237140Sbostic dstname = name; 69337140Sbostic name = getzname(name); 69437140Sbostic dstlen = name - dstname; /* length of DST zone name */ 69537140Sbostic if (dstlen < 3) 69637140Sbostic return -1; 69737140Sbostic if (*name != '\0' && *name != ',' && *name != ';') { 69837140Sbostic name = getoffset(name, &dstoffset); 69937140Sbostic if (name == NULL) 70037140Sbostic return -1; 70137140Sbostic } else dstoffset = stdoffset - SECSPERHOUR; 70237140Sbostic if (*name == ',' || *name == ';') { 70337140Sbostic struct rule start; 70437140Sbostic struct rule end; 70537140Sbostic register int year; 70637140Sbostic register time_t janfirst; 70737140Sbostic time_t starttime; 70837140Sbostic time_t endtime; 70930608Sbostic 71037140Sbostic ++name; 71137140Sbostic if ((name = getrule(name, &start)) == NULL) 71237140Sbostic return -1; 71337140Sbostic if (*name++ != ',') 71437140Sbostic return -1; 71537140Sbostic if ((name = getrule(name, &end)) == NULL) 71637140Sbostic return -1; 71737140Sbostic if (*name != '\0') 71837140Sbostic return -1; 71937140Sbostic sp->typecnt = 2; /* standard time and DST */ 72037140Sbostic /* 72137140Sbostic ** Two transitions per year, from EPOCH_YEAR to 2037. 72237140Sbostic */ 72337140Sbostic sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1); 72437140Sbostic if (sp->timecnt > TZ_MAX_TIMES) 72537140Sbostic return -1; 72637140Sbostic sp->ttis[0].tt_gmtoff = -dstoffset; 72737140Sbostic sp->ttis[0].tt_isdst = 1; 72837140Sbostic sp->ttis[0].tt_abbrind = stdlen + 1; 72937140Sbostic sp->ttis[1].tt_gmtoff = -stdoffset; 73037140Sbostic sp->ttis[1].tt_isdst = 0; 73137140Sbostic sp->ttis[1].tt_abbrind = 0; 73237140Sbostic atp = sp->ats; 73337140Sbostic typep = sp->types; 73437140Sbostic janfirst = 0; 73537140Sbostic for (year = EPOCH_YEAR; year <= 2037; ++year) { 73637140Sbostic starttime = transtime(janfirst, year, &start, 73737140Sbostic stdoffset); 73837140Sbostic endtime = transtime(janfirst, year, &end, 73937140Sbostic dstoffset); 74037140Sbostic if (starttime > endtime) { 74137140Sbostic *atp++ = endtime; 74237140Sbostic *typep++ = 1; /* DST ends */ 74337140Sbostic *atp++ = starttime; 74437140Sbostic *typep++ = 0; /* DST begins */ 74537140Sbostic } else { 74637140Sbostic *atp++ = starttime; 74737140Sbostic *typep++ = 0; /* DST begins */ 74837140Sbostic *atp++ = endtime; 74937140Sbostic *typep++ = 1; /* DST ends */ 75037140Sbostic } 75137140Sbostic janfirst += 75237140Sbostic year_lengths[isleap(year)] * SECSPERDAY; 75337140Sbostic } 75430608Sbostic } else { 75537140Sbostic int sawstd; 75637140Sbostic int sawdst; 75737140Sbostic long stdfix; 75837140Sbostic long dstfix; 75937140Sbostic long oldfix; 76037140Sbostic int isdst; 76137140Sbostic register int i; 76237140Sbostic 76337140Sbostic if (*name != '\0') 76437140Sbostic return -1; 76537140Sbostic if (load_result != 0) 76637140Sbostic return -1; 76737140Sbostic /* 76837140Sbostic ** Compute the difference between the real and 76937140Sbostic ** prototype standard and summer time offsets 77037140Sbostic ** from GMT, and put the real standard and summer 77137140Sbostic ** time offsets into the rules in place of the 77237140Sbostic ** prototype offsets. 77337140Sbostic */ 77437140Sbostic sawstd = FALSE; 77537140Sbostic sawdst = FALSE; 77637140Sbostic stdfix = 0; 77737140Sbostic dstfix = 0; 77837140Sbostic for (i = 0; i < sp->typecnt; ++i) { 77937140Sbostic if (sp->ttis[i].tt_isdst) { 78037140Sbostic oldfix = dstfix; 78137140Sbostic dstfix = 78237140Sbostic sp->ttis[i].tt_gmtoff + dstoffset; 78337140Sbostic if (sawdst && (oldfix != dstfix)) 78437140Sbostic return -1; 78537140Sbostic sp->ttis[i].tt_gmtoff = -dstoffset; 78637140Sbostic sp->ttis[i].tt_abbrind = stdlen + 1; 78737140Sbostic sawdst = TRUE; 78837140Sbostic } else { 78937140Sbostic oldfix = stdfix; 79037140Sbostic stdfix = 79137140Sbostic sp->ttis[i].tt_gmtoff + stdoffset; 79237140Sbostic if (sawstd && (oldfix != stdfix)) 79337140Sbostic return -1; 79437140Sbostic sp->ttis[i].tt_gmtoff = -stdoffset; 79537140Sbostic sp->ttis[i].tt_abbrind = 0; 79637140Sbostic sawstd = TRUE; 79737140Sbostic } 79837140Sbostic } 79937140Sbostic /* 80037140Sbostic ** Make sure we have both standard and summer time. 80137140Sbostic */ 80237140Sbostic if (!sawdst || !sawstd) 80337140Sbostic return -1; 80437140Sbostic /* 80537140Sbostic ** Now correct the transition times by shifting 80637140Sbostic ** them by the difference between the real and 80737140Sbostic ** prototype offsets. Note that this difference 80837140Sbostic ** can be different in standard and summer time; 80937140Sbostic ** the prototype probably has a 1-hour difference 81037140Sbostic ** between standard and summer time, but a different 81137140Sbostic ** difference can be specified in TZ. 81237140Sbostic */ 81337140Sbostic isdst = FALSE; /* we start in standard time */ 81437140Sbostic for (i = 0; i < sp->timecnt; ++i) { 81537140Sbostic register const struct ttinfo * ttisp; 81637140Sbostic 81737140Sbostic /* 81837140Sbostic ** If summer time is in effect, and the 81937140Sbostic ** transition time was not specified as 82037140Sbostic ** standard time, add the summer time 82137140Sbostic ** offset to the transition time; 82237140Sbostic ** otherwise, add the standard time offset 82337140Sbostic ** to the transition time. 82437140Sbostic */ 82537140Sbostic ttisp = &sp->ttis[sp->types[i]]; 82637140Sbostic sp->ats[i] += 82737140Sbostic (isdst && !ttisp->tt_ttisstd) ? 82837140Sbostic dstfix : stdfix; 82937140Sbostic isdst = ttisp->tt_isdst; 83037140Sbostic } 83130608Sbostic } 83237140Sbostic } else { 83337140Sbostic dstlen = 0; 83437140Sbostic sp->typecnt = 1; /* only standard time */ 83537140Sbostic sp->timecnt = 0; 83637140Sbostic sp->ttis[0].tt_gmtoff = -stdoffset; 83737140Sbostic sp->ttis[0].tt_isdst = 0; 83837140Sbostic sp->ttis[0].tt_abbrind = 0; 83930608Sbostic } 84037140Sbostic sp->charcnt = stdlen + 1; 84137140Sbostic if (dstlen != 0) 84237140Sbostic sp->charcnt += dstlen + 1; 84337140Sbostic if (sp->charcnt > sizeof sp->chars) 84430682Sbostic return -1; 84537140Sbostic cp = sp->chars; 84637140Sbostic (void) strncpy(cp, stdname, stdlen); 84737140Sbostic cp += stdlen; 84837140Sbostic *cp++ = '\0'; 84937140Sbostic if (dstlen != 0) { 85037140Sbostic (void) strncpy(cp, dstname, dstlen); 85137140Sbostic *(cp + dstlen) = '\0'; 85237140Sbostic } 85330682Sbostic return 0; 85430682Sbostic } 85530682Sbostic 85637140Sbostic static void 85737140Sbostic gmtload(sp) 85837140Sbostic struct state * const sp; 8591959Swnj { 86037140Sbostic if (tzload(GMT, sp) != 0) 86137140Sbostic (void) tzparse(GMT, sp, TRUE); 8621959Swnj } 8631959Swnj 86430608Sbostic void 86530608Sbostic tzset() 86630608Sbostic { 86737140Sbostic register const char * name; 86837140Sbostic void tzsetwall(); 86930608Sbostic 87030608Sbostic name = getenv("TZ"); 87137140Sbostic if (name == NULL) { 87237140Sbostic tzsetwall(); 87337140Sbostic return; 87437140Sbostic } 87537140Sbostic lcl_is_set = TRUE; 87637140Sbostic #ifdef ALL_STATE 87737140Sbostic if (lclptr == NULL) { 87837140Sbostic lclptr = (struct state *) malloc(sizeof *lclptr); 87937140Sbostic if (lclptr == NULL) { 88037140Sbostic settzname(); /* all we can do */ 88130682Sbostic return; 88237140Sbostic } 88337140Sbostic } 88437140Sbostic #endif /* defined ALL_STATE */ 88537140Sbostic if (*name == '\0') { 88637140Sbostic /* 88737140Sbostic ** User wants it fast rather than right. 88837140Sbostic */ 88937140Sbostic lclptr->leapcnt = 0; /* so, we're off a little */ 89037140Sbostic lclptr->timecnt = 0; 89137140Sbostic lclptr->ttis[0].tt_gmtoff = 0; 89237140Sbostic lclptr->ttis[0].tt_abbrind = 0; 89337140Sbostic (void) strcpy(lclptr->chars, GMT); 89437140Sbostic } else if (tzload(name, lclptr) != 0) 89537140Sbostic if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) 89639105Sbostic (void) gmtload(lclptr); 89737140Sbostic settzname(); 89837140Sbostic } 89937140Sbostic 90037140Sbostic void 90137140Sbostic tzsetwall() 90237140Sbostic { 90337140Sbostic lcl_is_set = TRUE; 90437140Sbostic #ifdef ALL_STATE 90537140Sbostic if (lclptr == NULL) { 90637140Sbostic lclptr = (struct state *) malloc(sizeof *lclptr); 90737140Sbostic if (lclptr == NULL) { 90837140Sbostic settzname(); /* all we can do */ 90930682Sbostic return; 91037140Sbostic } 91130682Sbostic } 91237140Sbostic #endif /* defined ALL_STATE */ 91337140Sbostic if (tzload((char *) NULL, lclptr) != 0) 91437140Sbostic gmtload(lclptr); 91537140Sbostic settzname(); 91630608Sbostic } 91730608Sbostic 91837140Sbostic /* 91937140Sbostic ** The easy way to behave "as if no library function calls" localtime 92037140Sbostic ** is to not call it--so we drop its guts into "localsub", which can be 92137140Sbostic ** freely called. (And no, the PANS doesn't require the above behavior-- 92237140Sbostic ** but it *is* desirable.) 92337140Sbostic ** 92437140Sbostic ** The unused offset argument is for the benefit of mktime variants. 92537140Sbostic */ 92637140Sbostic 92737140Sbostic /*ARGSUSED*/ 92837140Sbostic static void 92937140Sbostic localsub(timep, offset, tmp) 93037140Sbostic const time_t * const timep; 93137140Sbostic const long offset; 93237140Sbostic struct tm * const tmp; 9331959Swnj { 93437140Sbostic register const struct state * sp; 93537140Sbostic register const struct ttinfo * ttisp; 93630608Sbostic register int i; 93737140Sbostic const time_t t = *timep; 9381959Swnj 93937140Sbostic if (!lcl_is_set) 94037140Sbostic tzset(); 94137140Sbostic sp = lclptr; 94237140Sbostic #ifdef ALL_STATE 94337140Sbostic if (sp == NULL) { 94437140Sbostic gmtsub(timep, offset, tmp); 94537140Sbostic return; 94637140Sbostic } 94737140Sbostic #endif /* defined ALL_STATE */ 94837140Sbostic if (sp->timecnt == 0 || t < sp->ats[0]) { 94930608Sbostic i = 0; 95037140Sbostic while (sp->ttis[i].tt_isdst) 95137140Sbostic if (++i >= sp->typecnt) { 95230608Sbostic i = 0; 95330608Sbostic break; 95430608Sbostic } 95530608Sbostic } else { 95637140Sbostic for (i = 1; i < sp->timecnt; ++i) 95737140Sbostic if (t < sp->ats[i]) 95830608Sbostic break; 95937140Sbostic i = sp->types[i - 1]; 9601959Swnj } 96137140Sbostic ttisp = &sp->ttis[i]; 9621959Swnj /* 96330608Sbostic ** To get (wrong) behavior that's compatible with System V Release 2.0 96430608Sbostic ** you'd replace the statement below with 96537140Sbostic ** t += ttisp->tt_gmtoff; 96637140Sbostic ** timesub(&t, 0L, sp, tmp); 96730608Sbostic */ 96837140Sbostic timesub(&t, ttisp->tt_gmtoff, sp, tmp); 96930608Sbostic tmp->tm_isdst = ttisp->tt_isdst; 97037140Sbostic tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind]; 97137140Sbostic tmp->tm_zone = &sp->chars[ttisp->tt_abbrind]; 97230608Sbostic } 9731959Swnj 97430608Sbostic struct tm * 97537140Sbostic localtime(timep) 97637140Sbostic const time_t * const timep; 97730608Sbostic { 97837140Sbostic static struct tm tm; 9791959Swnj 98037140Sbostic localsub(timep, 0L, &tm); 98137140Sbostic return &tm; 98230608Sbostic } 9831959Swnj 98437140Sbostic /* 98537140Sbostic ** gmtsub is to gmtime as localsub is to localtime. 98637140Sbostic */ 9871959Swnj 98837140Sbostic static void 98937140Sbostic gmtsub(timep, offset, tmp) 99037140Sbostic const time_t * const timep; 99137140Sbostic const long offset; 99237140Sbostic struct tm * const tmp; 99337140Sbostic { 99437140Sbostic if (!gmt_is_set) { 99537140Sbostic gmt_is_set = TRUE; 99637140Sbostic #ifdef ALL_STATE 99737140Sbostic gmtptr = (struct state *) malloc(sizeof *gmtptr); 99837140Sbostic if (gmtptr != NULL) 99937140Sbostic #endif /* defined ALL_STATE */ 100037140Sbostic gmtload(gmtptr); 100137140Sbostic } 100237140Sbostic timesub(timep, offset, gmtptr, tmp); 100337140Sbostic /* 100437140Sbostic ** Could get fancy here and deliver something such as 100537140Sbostic ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero, 100637140Sbostic ** but this is no time for a treasure hunt. 100737140Sbostic */ 100837140Sbostic if (offset != 0) 100937140Sbostic tmp->tm_zone = WILDABBR; 101037140Sbostic else { 101137140Sbostic #ifdef ALL_STATE 101237140Sbostic if (gmtptr == NULL) 101337140Sbostic tmp->TM_ZONE = GMT; 101437140Sbostic else tmp->TM_ZONE = gmtptr->chars; 101537140Sbostic #endif /* defined ALL_STATE */ 101637140Sbostic #ifndef ALL_STATE 101737140Sbostic tmp->tm_zone = gmtptr->chars; 101837140Sbostic #endif /* State Farm */ 101937140Sbostic } 102037140Sbostic } 10211959Swnj 102230608Sbostic struct tm * 102337140Sbostic gmtime(timep) 102437140Sbostic const time_t * const timep; 10251959Swnj { 102630608Sbostic static struct tm tm; 10271959Swnj 102837140Sbostic gmtsub(timep, 0L, &tm); 102937140Sbostic return &tm; 103037140Sbostic } 103137140Sbostic 103237140Sbostic static void 103337140Sbostic timesub(timep, offset, sp, tmp) 103437140Sbostic const time_t * const timep; 103537140Sbostic const long offset; 103637140Sbostic register const struct state * const sp; 103737140Sbostic register struct tm * const tmp; 103837140Sbostic { 103937140Sbostic register const struct lsinfo * lp; 104037140Sbostic register long days; 104137140Sbostic register long rem; 104237140Sbostic register int y; 104337140Sbostic register int yleap; 104437140Sbostic register const int * ip; 104537140Sbostic register long corr; 104637140Sbostic register int hit; 104737140Sbostic register int i; 104837140Sbostic 104937140Sbostic corr = 0; 105037140Sbostic hit = FALSE; 105137140Sbostic #ifdef ALL_STATE 105237140Sbostic i = (sp == NULL) ? 0 : sp->leapcnt; 105337140Sbostic #endif /* defined ALL_STATE */ 105437140Sbostic #ifndef ALL_STATE 105537140Sbostic i = sp->leapcnt; 105637140Sbostic #endif /* State Farm */ 105737140Sbostic while (--i >= 0) { 105837140Sbostic lp = &sp->lsis[i]; 105937140Sbostic if (*timep >= lp->ls_trans) { 106037140Sbostic if (*timep == lp->ls_trans) 106137140Sbostic hit = ((i == 0 && lp->ls_corr > 0) || 106237140Sbostic lp->ls_corr > sp->lsis[i - 1].ls_corr); 106337140Sbostic corr = lp->ls_corr; 106437140Sbostic break; 106537140Sbostic } 106637140Sbostic } 106737140Sbostic days = *timep / SECSPERDAY; 106837140Sbostic rem = *timep % SECSPERDAY; 106937140Sbostic #ifdef mc68k 107037140Sbostic if (*timep == 0x80000000) { 107137140Sbostic /* 107237140Sbostic ** A 3B1 muffs the division on the most negative number. 107337140Sbostic */ 107437140Sbostic days = -24855; 107537140Sbostic rem = -11648; 107637140Sbostic } 107737140Sbostic #endif /* mc68k */ 107837140Sbostic rem += (offset - corr); 107930608Sbostic while (rem < 0) { 108037140Sbostic rem += SECSPERDAY; 108130608Sbostic --days; 10821959Swnj } 108337140Sbostic while (rem >= SECSPERDAY) { 108437140Sbostic rem -= SECSPERDAY; 108530608Sbostic ++days; 108630608Sbostic } 108737140Sbostic tmp->tm_hour = (int) (rem / SECSPERHOUR); 108837140Sbostic rem = rem % SECSPERHOUR; 108937140Sbostic tmp->tm_min = (int) (rem / SECSPERMIN); 109037140Sbostic tmp->tm_sec = (int) (rem % SECSPERMIN); 109137140Sbostic if (hit) 109237140Sbostic /* 109337140Sbostic ** A positive leap second requires a special 109437140Sbostic ** representation. This uses "... ??:59:60". 109537140Sbostic */ 109637140Sbostic ++(tmp->tm_sec); 109737140Sbostic tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK); 109830608Sbostic if (tmp->tm_wday < 0) 109937140Sbostic tmp->tm_wday += DAYSPERWEEK; 110030608Sbostic y = EPOCH_YEAR; 110130608Sbostic if (days >= 0) 110230608Sbostic for ( ; ; ) { 110330608Sbostic yleap = isleap(y); 110430608Sbostic if (days < (long) year_lengths[yleap]) 110530608Sbostic break; 110630608Sbostic ++y; 110730608Sbostic days = days - (long) year_lengths[yleap]; 110830608Sbostic } 110930608Sbostic else do { 111030608Sbostic --y; 111130608Sbostic yleap = isleap(y); 111230608Sbostic days = days + (long) year_lengths[yleap]; 111330608Sbostic } while (days < 0); 111430608Sbostic tmp->tm_year = y - TM_YEAR_BASE; 111530608Sbostic tmp->tm_yday = (int) days; 111630608Sbostic ip = mon_lengths[yleap]; 111730608Sbostic for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon)) 111830608Sbostic days = days - (long) ip[tmp->tm_mon]; 111930608Sbostic tmp->tm_mday = (int) (days + 1); 112030608Sbostic tmp->tm_isdst = 0; 112137140Sbostic #ifdef TM_GMTOFF 112237140Sbostic tmp->TM_GMTOFF = offset; 112337140Sbostic #endif /* defined TM_GMTOFF */ 11241959Swnj } 112537140Sbostic 112637140Sbostic /* 112737140Sbostic ** A la X3J11 112837140Sbostic */ 112937140Sbostic 113037140Sbostic char * 113137140Sbostic asctime(timeptr) 113237140Sbostic register const struct tm * timeptr; 113337140Sbostic { 113437140Sbostic static const char wday_name[DAYSPERWEEK][3] = { 113537140Sbostic "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 113637140Sbostic }; 113737140Sbostic static const char mon_name[MONSPERYEAR][3] = { 113837140Sbostic "Jan", "Feb", "Mar", "Apr", "May", "Jun", 113937140Sbostic "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 114037140Sbostic }; 114137140Sbostic static char result[26]; 114237140Sbostic 114337140Sbostic (void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n", 114437140Sbostic wday_name[timeptr->tm_wday], 114537140Sbostic mon_name[timeptr->tm_mon], 114637140Sbostic timeptr->tm_mday, timeptr->tm_hour, 114737140Sbostic timeptr->tm_min, timeptr->tm_sec, 114837140Sbostic TM_YEAR_BASE + timeptr->tm_year); 114937140Sbostic return result; 115037140Sbostic } 115137140Sbostic 115237140Sbostic char * 115337140Sbostic ctime(timep) 115437140Sbostic const time_t * const timep; 115537140Sbostic { 115637140Sbostic return asctime(localtime(timep)); 115737140Sbostic } 115837140Sbostic 115937140Sbostic /* 116037140Sbostic ** Adapted from code provided by Robert Elz, who writes: 116137140Sbostic ** The "best" way to do mktime I think is based on an idea of Bob 116237140Sbostic ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now). 116337140Sbostic ** It does a binary search of the time_t space. Since time_t's are 116437140Sbostic ** just 32 bits, its a max of 32 iterations (even at 64 bits it 116537140Sbostic ** would still be very reasonable). 116637140Sbostic */ 116737140Sbostic 116837140Sbostic #ifndef WRONG 116937140Sbostic #define WRONG (-1) 117037140Sbostic #endif /* !defined WRONG */ 117137140Sbostic 117237140Sbostic static void 117337140Sbostic normalize(tensptr, unitsptr, base) 117437140Sbostic int * const tensptr; 117537140Sbostic int * const unitsptr; 117637140Sbostic const int base; 117737140Sbostic { 117837140Sbostic if (*unitsptr >= base) { 117937140Sbostic *tensptr += *unitsptr / base; 118037140Sbostic *unitsptr %= base; 118137140Sbostic } else if (*unitsptr < 0) { 118237140Sbostic --*tensptr; 118337140Sbostic *unitsptr += base; 118437140Sbostic if (*unitsptr < 0) { 118537140Sbostic *tensptr -= 1 + (-*unitsptr) / base; 118637140Sbostic *unitsptr = base - (-*unitsptr) % base; 118737140Sbostic } 118837140Sbostic } 118937140Sbostic } 119037140Sbostic 119137140Sbostic static int 119237140Sbostic tmcomp(atmp, btmp) 119337140Sbostic register const struct tm * const atmp; 119437140Sbostic register const struct tm * const btmp; 119537140Sbostic { 119637140Sbostic register int result; 119737140Sbostic 119837140Sbostic if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && 119937140Sbostic (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && 120037140Sbostic (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && 120137140Sbostic (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && 120237140Sbostic (result = (atmp->tm_min - btmp->tm_min)) == 0) 120337140Sbostic result = atmp->tm_sec - btmp->tm_sec; 120437140Sbostic return result; 120537140Sbostic } 120637140Sbostic 120737140Sbostic static time_t 120837140Sbostic time2(tmp, funcp, offset, okayp) 120937140Sbostic struct tm * const tmp; 121037140Sbostic void (* const funcp)(); 121137140Sbostic const long offset; 121237140Sbostic int * const okayp; 121337140Sbostic { 121437140Sbostic register const struct state * sp; 121537140Sbostic register int dir; 121637140Sbostic register int bits; 121737140Sbostic register int i, j ; 121837140Sbostic register int saved_seconds; 121937140Sbostic time_t newt; 122037140Sbostic time_t t; 122137140Sbostic struct tm yourtm, mytm; 122237140Sbostic 122337140Sbostic *okayp = FALSE; 122437140Sbostic yourtm = *tmp; 122537140Sbostic if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0) 122637140Sbostic normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN); 122737140Sbostic normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR); 122837140Sbostic normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY); 122937140Sbostic normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR); 123037140Sbostic while (yourtm.tm_mday <= 0) { 123137140Sbostic --yourtm.tm_year; 123237140Sbostic yourtm.tm_mday += 123337140Sbostic year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)]; 123437140Sbostic } 123537140Sbostic for ( ; ; ) { 123637140Sbostic i = mon_lengths[isleap(yourtm.tm_year + 123737140Sbostic TM_YEAR_BASE)][yourtm.tm_mon]; 123837140Sbostic if (yourtm.tm_mday <= i) 123937140Sbostic break; 124037140Sbostic yourtm.tm_mday -= i; 124137140Sbostic if (++yourtm.tm_mon >= MONSPERYEAR) { 124237140Sbostic yourtm.tm_mon = 0; 124337140Sbostic ++yourtm.tm_year; 124437140Sbostic } 124537140Sbostic } 124637140Sbostic saved_seconds = yourtm.tm_sec; 124737140Sbostic yourtm.tm_sec = 0; 124837140Sbostic /* 124937140Sbostic ** Calculate the number of magnitude bits in a time_t 125037140Sbostic ** (this works regardless of whether time_t is 125137140Sbostic ** signed or unsigned, though lint complains if unsigned). 125237140Sbostic */ 125337140Sbostic for (bits = 0, t = 1; t > 0; ++bits, t <<= 1) 125437140Sbostic ; 125537140Sbostic /* 125637140Sbostic ** If time_t is signed, then 0 is the median value, 125737140Sbostic ** if time_t is unsigned, then 1 << bits is median. 125837140Sbostic */ 125937140Sbostic t = (t < 0) ? 0 : ((time_t) 1 << bits); 126037140Sbostic for ( ; ; ) { 126137140Sbostic (*funcp)(&t, offset, &mytm); 126237140Sbostic dir = tmcomp(&mytm, &yourtm); 126337140Sbostic if (dir != 0) { 126437140Sbostic if (bits-- < 0) 126537140Sbostic return WRONG; 126637140Sbostic if (bits < 0) 126737140Sbostic --t; 126837140Sbostic else if (dir > 0) 126937140Sbostic t -= (time_t) 1 << bits; 127037140Sbostic else t += (time_t) 1 << bits; 127137140Sbostic continue; 127237140Sbostic } 127337140Sbostic if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) 127437140Sbostic break; 127537140Sbostic /* 127637140Sbostic ** Right time, wrong type. 127737140Sbostic ** Hunt for right time, right type. 127837140Sbostic ** It's okay to guess wrong since the guess 127937140Sbostic ** gets checked. 128037140Sbostic */ 128137140Sbostic sp = (const struct state *) 128237140Sbostic ((funcp == localsub) ? lclptr : gmtptr); 128337140Sbostic #ifdef ALL_STATE 128437140Sbostic if (sp == NULL) 128537140Sbostic return WRONG; 128637140Sbostic #endif /* defined ALL_STATE */ 128737140Sbostic for (i = 0; i < sp->typecnt; ++i) { 128837140Sbostic if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) 128937140Sbostic continue; 129037140Sbostic for (j = 0; j < sp->typecnt; ++j) { 129137140Sbostic if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) 129237140Sbostic continue; 129337140Sbostic newt = t + sp->ttis[j].tt_gmtoff - 129437140Sbostic sp->ttis[i].tt_gmtoff; 129537140Sbostic (*funcp)(&newt, offset, &mytm); 129637140Sbostic if (tmcomp(&mytm, &yourtm) != 0) 129737140Sbostic continue; 129837140Sbostic if (mytm.tm_isdst != yourtm.tm_isdst) 129937140Sbostic continue; 130037140Sbostic /* 130137140Sbostic ** We have a match. 130237140Sbostic */ 130337140Sbostic t = newt; 130437140Sbostic goto label; 130537140Sbostic } 130637140Sbostic } 130737140Sbostic return WRONG; 130837140Sbostic } 130937140Sbostic label: 131037140Sbostic t += saved_seconds; 131137140Sbostic (*funcp)(&t, offset, tmp); 131237140Sbostic *okayp = TRUE; 131337140Sbostic return t; 131437140Sbostic } 131537140Sbostic 131637140Sbostic static time_t 131737140Sbostic time1(tmp, funcp, offset) 131837140Sbostic struct tm * const tmp; 131937140Sbostic void (* const funcp)(); 132037140Sbostic const long offset; 132137140Sbostic { 132237140Sbostic register time_t t; 132337140Sbostic register const struct state * sp; 132437140Sbostic register int samei, otheri; 132537140Sbostic int okay; 132637140Sbostic 132737140Sbostic if (tmp->tm_isdst > 1) 132838558Sbostic tmp->tm_isdst = 1; 132937140Sbostic t = time2(tmp, funcp, offset, &okay); 133037140Sbostic if (okay || tmp->tm_isdst < 0) 133137140Sbostic return t; 133237140Sbostic /* 133337140Sbostic ** We're supposed to assume that somebody took a time of one type 133437140Sbostic ** and did some math on it that yielded a "struct tm" that's bad. 133537140Sbostic ** We try to divine the type they started from and adjust to the 133637140Sbostic ** type they need. 133737140Sbostic */ 133837140Sbostic sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); 133937140Sbostic #ifdef ALL_STATE 134037140Sbostic if (sp == NULL) 134137140Sbostic return WRONG; 134237140Sbostic #endif /* defined ALL_STATE */ 134337140Sbostic for (samei = 0; samei < sp->typecnt; ++samei) { 134437140Sbostic if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) 134537140Sbostic continue; 134637140Sbostic for (otheri = 0; otheri < sp->typecnt; ++otheri) { 134737140Sbostic if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) 134837140Sbostic continue; 134937140Sbostic tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - 135037140Sbostic sp->ttis[samei].tt_gmtoff; 135137140Sbostic tmp->tm_isdst = !tmp->tm_isdst; 135237140Sbostic t = time2(tmp, funcp, offset, &okay); 135337140Sbostic if (okay) 135437140Sbostic return t; 135537140Sbostic tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - 135637140Sbostic sp->ttis[samei].tt_gmtoff; 135737140Sbostic tmp->tm_isdst = !tmp->tm_isdst; 135837140Sbostic } 135937140Sbostic } 136037140Sbostic return WRONG; 136137140Sbostic } 136237140Sbostic 136337140Sbostic time_t 136437140Sbostic mktime(tmp) 136537140Sbostic struct tm * const tmp; 136637140Sbostic { 136737140Sbostic return time1(tmp, localsub, 0L); 136837140Sbostic } 1369