localtime.c revision 92986
1139823Simp/* 21541Srgrimes** This file is in the public domain, so clarified as of 31541Srgrimes** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov). 41541Srgrimes*/ 51541Srgrimes 61541Srgrimes#ifndef lint 71541Srgrimes#ifndef NOID 81541Srgrimesstatic char elsieid[] = "@(#)localtime.c 7.57"; 91541Srgrimes#endif /* !defined NOID */ 101541Srgrimes#endif /* !defined lint */ 111541Srgrimes#include <sys/cdefs.h> 121541Srgrimes__FBSDID("$FreeBSD: head/lib/libc/stdtime/localtime.c 92986 2002-03-22 21:53:29Z obrien $"); 131541Srgrimes 141541Srgrimes/* 151541Srgrimes** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu). 161541Srgrimes** POSIX-style TZ environment variable handling from Guy Harris 171541Srgrimes** (guy@auspex.com). 181541Srgrimes*/ 191541Srgrimes 201541Srgrimes/*LINTLIBRARY*/ 211541Srgrimes 221541Srgrimes#include "namespace.h" 231541Srgrimes#include <sys/types.h> 241541Srgrimes#include <sys/stat.h> 251541Srgrimes#include <fcntl.h> 261541Srgrimes#include <pthread.h> 271541Srgrimes#include "private.h" 281541Srgrimes#include "un-namespace.h" 291541Srgrimes 3050477Speter#include "tzfile.h" 311541Srgrimes 321541Srgrimes#include "libc_private.h" 332169Spaul 34121498Sume#define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x) 352169Spaul#define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x) 3695336Smike 3793514Smike/* 3895336Smike** SunOS 4.1.1 headers lack O_BINARY. 3993514Smike*/ 4095336Smike 4195336Smike#ifdef O_BINARY 4295336Smike#define OPEN_MODE (O_RDONLY | O_BINARY) 4395336Smike#endif /* defined O_BINARY */ 4495336Smike#ifndef O_BINARY 4595336Smike#define OPEN_MODE O_RDONLY 4695336Smike#endif /* !defined O_BINARY */ 4795336Smike 4895336Smike#ifndef WILDABBR 4995336Smike/* 5095336Smike** Someone might make incorrect use of a time zone abbreviation: 5195336Smike** 1. They might reference tzname[0] before calling tzset (explicitly 5295336Smike** or implicitly). 5395336Smike** 2. They might reference tzname[1] before calling tzset (explicitly 5495336Smike** or implicitly). 5595336Smike** 3. They might reference tzname[1] after setting to a time zone 5695336Smike** in which Daylight Saving Time is never observed. 5795336Smike** 4. They might reference tzname[0] after setting to a time zone 5895336Smike** in which Standard Time is never observed. 5995336Smike** 5. They might reference tm.TM_ZONE after calling offtime. 6095336Smike** What's best to do in the above cases is open to debate; 6195336Smike** for now, we just set things up so that in any of the five cases 6295336Smike** WILDABBR is used. Another possibility: initialize tzname[0] to the 6395336Smike** string "tzname[0] used before set", and similarly for the other cases. 6495336Smike** And another: initialize tzname[0] to "ERA", with an explanation in the 6595336Smike** manual page of what this "time zone abbreviation" means (doing this so 6695336Smike** that tzname[0] has the "normal" length of three characters). 6795336Smike*/ 6895336Smike#define WILDABBR " " 6995336Smike#endif /* !defined WILDABBR */ 7095336Smike 7195336Smikestatic char wildabbr[] = "WILDABBR"; 7295336Smike 7395336Smikestatic const char gmt[] = "GMT"; 74102227Smike 75102227Smikestruct ttinfo { /* time type information */ 76102227Smike long tt_gmtoff; /* GMT offset in seconds */ 7795336Smike int tt_isdst; /* used to set tm_isdst */ 7895336Smike int tt_abbrind; /* abbreviation list index */ 7995336Smike int tt_ttisstd; /* TRUE if transition is std time */ 8095336Smike int tt_ttisgmt; /* TRUE if transition is GMT */ 8195336Smike}; 8295336Smike 8395336Smikestruct lsinfo { /* leap second information */ 8495336Smike time_t ls_trans; /* transition time */ 8595336Smike long ls_corr; /* correction to apply */ 8695336Smike}; 87170613Sbms 88170613Sbms#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) 89170613Sbms 90170613Sbms#ifdef TZNAME_MAX 91170613Sbms#define MY_TZNAME_MAX TZNAME_MAX 92170613Sbms#endif /* defined TZNAME_MAX */ 93170613Sbms#ifndef TZNAME_MAX 94170613Sbms#define MY_TZNAME_MAX 255 95170613Sbms#endif /* !defined TZNAME_MAX */ 96170613Sbms 97170613Sbmsstruct state { 98170613Sbms int leapcnt; 99170613Sbms int timecnt; 100170613Sbms int typecnt; 101170613Sbms int charcnt; 102170613Sbms time_t ats[TZ_MAX_TIMES]; 103170613Sbms unsigned char types[TZ_MAX_TIMES]; 104170613Sbms struct ttinfo ttis[TZ_MAX_TYPES]; 105170613Sbms char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), 106170613Sbms (2 * (MY_TZNAME_MAX + 1)))]; 107170613Sbms struct lsinfo lsis[TZ_MAX_LEAPS]; 108170613Sbms}; 109170613Sbms 110170613Sbmsstruct rule { 111170613Sbms int r_type; /* type of rule--see below */ 112170613Sbms int r_day; /* day number of rule */ 113170613Sbms int r_week; /* week number of rule */ 11495336Smike int r_mon; /* month number of rule */ 11595336Smike long r_time; /* transition time of rule */ 11695336Smike}; 11795336Smike 11895336Smike#define JULIAN_DAY 0 /* Jn - Julian day */ 11995336Smike#define DAY_OF_YEAR 1 /* n - day of year */ 12095336Smike#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ 12195336Smike 12295336Smike/* 12395336Smike** Prototypes for static functions. 12495336Smike*/ 12595336Smike 12695336Smikestatic long detzcode P((const char * codep)); 12795336Smikestatic const char * getzname P((const char * strp)); 12895336Smikestatic const char * getnum P((const char * strp, int * nump, int min, 12995336Smike int max)); 13095336Smikestatic const char * getsecs P((const char * strp, long * secsp)); 13195336Smikestatic const char * getoffset P((const char * strp, long * offsetp)); 13295336Smikestatic const char * getrule P((const char * strp, struct rule * rulep)); 13395336Smikestatic void gmtload P((struct state * sp)); 13495336Smikestatic void gmtsub P((const time_t * timep, long offset, 13595336Smike struct tm * tmp)); 13695336Smikestatic void localsub P((const time_t * timep, long offset, 13795336Smike struct tm * tmp)); 13895336Smikestatic int increment_overflow P((int * number, int delta)); 13995336Smikestatic int normalize_overflow P((int * tensptr, int * unitsptr, 14095336Smike int base)); 14195336Smikestatic void settzname P((void)); 14295336Smikestatic time_t time1 P((struct tm * tmp, 14395336Smike void(*funcp) P((const time_t *, 14495336Smike long, struct tm *)), 14595336Smike long offset)); 14695336Smikestatic time_t time2 P((struct tm *tmp, 14795336Smike void(*funcp) P((const time_t *, 14895336Smike long, struct tm*)), 14995336Smike long offset, int * okayp)); 15095336Smikestatic void timesub P((const time_t * timep, long offset, 1511541Srgrimes const struct state * sp, struct tm * tmp)); 1521541Srgrimesstatic int tmcomp P((const struct tm * atmp, 1531541Srgrimes const struct tm * btmp)); 1541541Srgrimesstatic time_t transtime P((time_t janfirst, int year, 1551541Srgrimes const struct rule * rulep, long offset)); 1561541Srgrimesstatic int tzload P((const char * name, struct state * sp)); 15733804Sjulianstatic int tzparse P((const char * name, struct state * sp, 1581541Srgrimes int lastditch)); 15952904Sshin 1601541Srgrimes#ifdef ALL_STATE 1611541Srgrimesstatic struct state * lclptr; 162133874Srwatsonstatic struct state * gmtptr; 163133874Srwatson#endif /* defined ALL_STATE */ 16433804Sjulian 1651541Srgrimes#ifndef ALL_STATE 16633804Sjulianstatic struct state lclmem; 16733804Sjulianstatic struct state gmtmem; 16833804Sjulian#define lclptr (&lclmem) 1691541Srgrimes#define gmtptr (&gmtmem) 17033804Sjulian#endif /* State Farm */ 17133804Sjulian 17233804Sjulian#ifndef TZ_STRLEN_MAX 17333804Sjulian#define TZ_STRLEN_MAX 255 17433804Sjulian#endif /* !defined TZ_STRLEN_MAX */ 17533804Sjulian 17633804Sjulianstatic char lcl_TZname[TZ_STRLEN_MAX + 1]; 17733804Sjulianstatic int lcl_is_set; 1781541Srgrimesstatic int gmt_is_set; 17933804Sjulianstatic pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER; 18033804Sjulianstatic pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER; 18133804Sjulian 18233804Sjulianchar * tzname[2] = { 18333804Sjulian wildabbr, 18433804Sjulian wildabbr 185133874Srwatson}; 18633804Sjulian 18733804Sjulian/* 18833804Sjulian** Section 4.12.3 of X3.159-1989 requires that 18933804Sjulian** Except for the strftime function, these functions [asctime, 19033804Sjulian** ctime, gmtime, localtime] return values in one of two static 19133804Sjulian** objects: a broken-down time structure and an array of char. 19233804Sjulian** Thanks to Paul Eggert (eggert@twinsun.com) for noting this. 19333804Sjulian*/ 19433804Sjulian 19533804Sjulianstatic struct tm tm; 19633804Sjulian 19752904Sshin#ifdef USG_COMPAT 19833804Sjuliantime_t timezone = 0; 19952904Sshinint daylight = 0; 20052904Sshin#endif /* defined USG_COMPAT */ 20133804Sjulian 202133874Srwatson#ifdef ALTZONE 20333804Sjuliantime_t altzone = 0; 20433804Sjulian#endif /* defined ALTZONE */ 20533804Sjulian 20652904Sshinstatic long 20752904Sshindetzcode(codep) 20833804Sjulianconst char * const codep; 20933804Sjulian{ 21033804Sjulian long result; 211133874Srwatson int i; 212133874Srwatson 213133874Srwatson result = (codep[0] & 0x80) ? ~0L : 0L; 21452904Sshin for (i = 0; i < 4; ++i) 21552904Sshin result = (result << 8) | (codep[i] & 0xff); 21652904Sshin return result; 21733804Sjulian} 21833804Sjulian 21933804Sjulianstatic void 22033804Sjuliansettzname P((void)) 22133804Sjulian{ 22233804Sjulian struct state * sp = lclptr; 22333804Sjulian int i; 22433804Sjulian 22533804Sjulian tzname[0] = wildabbr; 22633804Sjulian tzname[1] = wildabbr; 22733804Sjulian#ifdef USG_COMPAT 22833804Sjulian daylight = 0; 22933804Sjulian timezone = 0; 23033804Sjulian#endif /* defined USG_COMPAT */ 23133804Sjulian#ifdef ALTZONE 23233804Sjulian altzone = 0; 23333804Sjulian#endif /* defined ALTZONE */ 23433804Sjulian#ifdef ALL_STATE 23533804Sjulian if (sp == NULL) { 2361541Srgrimes tzname[0] = tzname[1] = gmt; 23733804Sjulian return; 23833804Sjulian } 23933804Sjulian#endif /* defined ALL_STATE */ 24033804Sjulian for (i = 0; i < sp->typecnt; ++i) { 24133804Sjulian const struct ttinfo * const ttisp = &sp->ttis[i]; 24233804Sjulian 24333814Sjulian tzname[ttisp->tt_isdst] = 24433804Sjulian &sp->chars[ttisp->tt_abbrind]; 24533804Sjulian#ifdef USG_COMPAT 24633804Sjulian if (ttisp->tt_isdst) 24733804Sjulian daylight = 1; 24833804Sjulian if (i == 0 || !ttisp->tt_isdst) 24933804Sjulian timezone = -(ttisp->tt_gmtoff); 25033804Sjulian#endif /* defined USG_COMPAT */ 25133804Sjulian#ifdef ALTZONE 25233804Sjulian if (i == 0 || ttisp->tt_isdst) 25333804Sjulian altzone = -(ttisp->tt_gmtoff); 2541541Srgrimes#endif /* defined ALTZONE */ 25533804Sjulian } 25633804Sjulian /* 25752904Sshin ** And to get the latest zone names into tzname. . . 258153553Sdelphij */ 25946420Sluigi for (i = 0; i < sp->timecnt; ++i) { 26052904Sshin const struct ttinfo * const ttisp = 261142215Sglebius &sp->ttis[ 26246420Sluigi sp->types[i]]; 263130609Smlaier 26433804Sjulian tzname[ttisp->tt_isdst] = 265106152Sfenner &sp->chars[ttisp->tt_abbrind]; 266106152Sfenner } 2671541Srgrimes} 2681541Srgrimes 26952904Sshinstatic int 27052904Sshintzload(name, sp) 2711541Srgrimesconst char * name; 272106152Sfennerstruct state * const sp; 273106152Sfenner{ 274106152Sfenner const char * p; 2751541Srgrimes int i; 276136712Sandre int fid; 277136712Sandre 278136712Sandre /* XXX The following is from OpenBSD, and I'm not sure it is correct */ 279136712Sandre if (name != NULL && issetugid() != 0) 280136712Sandre if ((name[0] == ':' && name[1] == '/') || 281136712Sandre name[0] == '/' || strchr(name, '.')) 2821541Srgrimes name = NULL; 28314195Speter if (name == NULL && (name = TZDEFAULT) == NULL) 28414195Speter return -1; 28514195Speter { 28694291Ssilby int doaccess; 28794291Ssilby struct stat stab; 28814195Speter /* 28914195Speter ** Section 4.9.1 of the C standard says that 29014195Speter ** "FILENAME_MAX expands to an integral constant expression 29114195Speter ** that is the size needed for an array of char large enough 29214195Speter ** to hold the longest file name string that the implementation 29314195Speter ** guarantees can be opened." 29414195Speter */ 29514195Speter char fullname[FILENAME_MAX + 1]; 296176805Srpaulo 29714195Speter if (name[0] == ':') 29814195Speter ++name; 29914195Speter doaccess = name[0] == '/'; 30014195Speter if (!doaccess) { 30114195Speter if ((p = TZDIR) == NULL) 30214195Speter return -1; 30314195Speter if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname) 30417541Speter return -1; 30514195Speter (void) strcpy(fullname, p); 30614195Speter (void) strcat(fullname, "/"); 307108533Sschweikh (void) strcat(fullname, name); 30814195Speter /* 30914195Speter ** Set doaccess if '.' (as in "../") shows up in name. 31014195Speter */ 31114195Speter if (strchr(name, '.') != NULL) 31214195Speter doaccess = TRUE; 31335304Sphk name = fullname; 31435304Sphk } 31535304Sphk if (doaccess && access(name, R_OK) != 0) 31635304Sphk return -1; 31735304Sphk if ((fid = _open(name, OPEN_MODE)) == -1) 31835304Sphk return -1; 31935304Sphk if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) 32035304Sphk return -1; 32135304Sphk } 32235304Sphk { 32335304Sphk struct tzhead * tzhp; 32414195Speter char buf[sizeof *sp + sizeof *tzhp]; 32514195Speter int ttisstdcnt; 32614195Speter int ttisgmtcnt; 3271541Srgrimes 32814195Speter i = _read(fid, buf, sizeof buf); 3291541Srgrimes if (_close(fid) != 0) 3301541Srgrimes return -1; 3311541Srgrimes p = buf; 3321541Srgrimes p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved); 333176805Srpaulo ttisstdcnt = (int) detzcode(p); 33413491Speter p += 4; 335176805Srpaulo ttisgmtcnt = (int) detzcode(p); 336176805Srpaulo p += 4; 337176805Srpaulo sp->leapcnt = (int) detzcode(p); 338176805Srpaulo p += 4; 339176805Srpaulo sp->timecnt = (int) detzcode(p); 340176805Srpaulo p += 4; 34135304Sphk sp->typecnt = (int) detzcode(p); 34235304Sphk p += 4; 34313491Speter sp->charcnt = (int) detzcode(p); 34413491Speter p += 4; 34517541Speter if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 34617541Speter sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 34717541Speter sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 34817541Speter sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 34917541Speter (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || 350133874Srwatson (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) 35117541Speter return -1; 35287158Smike if (i - (p - buf) < sp->timecnt * 4 + /* ats */ 35387158Smike sp->timecnt + /* types */ 35417541Speter sp->typecnt * (4 + 2) + /* ttinfos */ 3551541Srgrimes sp->charcnt + /* chars */ 3561541Srgrimes sp->leapcnt * (4 + 4) + /* lsinfos */ 3571541Srgrimes ttisstdcnt + /* ttisstds */ 3581541Srgrimes ttisgmtcnt) /* ttisgmts */ 35935919Sjb return -1; 3601541Srgrimes for (i = 0; i < sp->timecnt; ++i) { 3611541Srgrimes sp->ats[i] = detzcode(p); 3621541Srgrimes p += 4; 3631541Srgrimes } 3641541Srgrimes for (i = 0; i < sp->timecnt; ++i) { 36535919Sjb sp->types[i] = (unsigned char) *p++; 3661541Srgrimes if (sp->types[i] >= sp->typecnt) 3671541Srgrimes return -1; 3681541Srgrimes } 3691541Srgrimes for (i = 0; i < sp->typecnt; ++i) { 3701541Srgrimes struct ttinfo * ttisp; 37135919Sjb 3721541Srgrimes ttisp = &sp->ttis[i]; 3731541Srgrimes ttisp->tt_gmtoff = detzcode(p); 3741541Srgrimes p += 4; 3751541Srgrimes ttisp->tt_isdst = (unsigned char) *p++; 37635919Sjb if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) 3771541Srgrimes return -1; 3781541Srgrimes ttisp->tt_abbrind = (unsigned char) *p++; 3791541Srgrimes if (ttisp->tt_abbrind < 0 || 3801541Srgrimes ttisp->tt_abbrind > sp->charcnt) 3811541Srgrimes return -1; 38235919Sjb } 38335919Sjb for (i = 0; i < sp->charcnt; ++i) 3841541Srgrimes sp->chars[i] = *p++; 385166368Sbms sp->chars[i] = '\0'; /* ensure '\0' at end */ 386178280Sgnn for (i = 0; i < sp->leapcnt; ++i) { 387178280Sgnn struct lsinfo * lsisp; 388166368Sbms 389166368Sbms lsisp = &sp->lsis[i]; 390166368Sbms lsisp->ls_trans = detzcode(p); 391166368Sbms p += 4; 392166368Sbms lsisp->ls_corr = detzcode(p); 393166368Sbms p += 4; 394166368Sbms } 395166368Sbms for (i = 0; i < sp->typecnt; ++i) { 396166368Sbms struct ttinfo * ttisp; 39735919Sjb 39855205Speter ttisp = &sp->ttis[i]; 3991541Srgrimes if (ttisstdcnt == 0) 4001541Srgrimes ttisp->tt_ttisstd = FALSE; 4011541Srgrimes else { 40235919Sjb ttisp->tt_ttisstd = *p++; 40335919Sjb if (ttisp->tt_ttisstd != TRUE && 40435919Sjb ttisp->tt_ttisstd != FALSE) 405167072Sbms return -1; 406142215Sglebius } 407130609Smlaier } 408114259Smdodd for (i = 0; i < sp->typecnt; ++i) { 40935919Sjb struct ttinfo * ttisp; 4101541Srgrimes 4111541Srgrimes ttisp = &sp->ttis[i]; 4121541Srgrimes if (ttisgmtcnt == 0) 4131541Srgrimes ttisp->tt_ttisgmt = FALSE; 4141541Srgrimes else { 4151541Srgrimes ttisp->tt_ttisgmt = *p++; 4161541Srgrimes if (ttisp->tt_ttisgmt != TRUE && 4171541Srgrimes ttisp->tt_ttisgmt != FALSE) 4181541Srgrimes return -1; 4191541Srgrimes } 4201541Srgrimes } 4211541Srgrimes } 4221541Srgrimes return 0; 4231541Srgrimes} 424105651Siedowse 4251541Srgrimesstatic const int mon_lengths[2][MONSPERYEAR] = { 426170613Sbms { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 427170613Sbms { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } 4281541Srgrimes}; 4291541Srgrimes 4301541Srgrimesstatic const int year_lengths[2] = { 4311541Srgrimes DAYSPERNYEAR, DAYSPERLYEAR 432133874Srwatson}; 433133874Srwatson 434133874Srwatson/* 435133874Srwatson** Given a pointer into a time zone string, scan until a character that is not 436133874Srwatson** a valid character in a zone name is found. Return a pointer to that 437133874Srwatson** character. 43819622Sfenner*/ 43952904Sshin 44052904Sshinstatic const char * 44152904Sshingetzname(strp) 4421541Srgrimesconst char * strp; 443119178Sbms{ 444186960Sadrian char c; 445186960Sadrian 446119178Sbms while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && 447130281Sru c != '+') 448130281Sru ++strp; 449130281Sru return strp; 450130281Sru} 451130281Sru 452130281Sru/* 453133874Srwatson** Given a pointer into a time zone string, extract a number from that string. 454133874Srwatson** Check that the number is within a specified range; if it is not, return 455133874Srwatson** NULL. 456133874Srwatson** Otherwise, return a pointer to the first character not part of the number. 457133874Srwatson*/ 45852904Sshin 45917758Ssosstatic const char * 460165648Spisogetnum(strp, nump, min, max) 461165648Spisoconst char * strp; 462165648Spisoint * const nump; 463165648Spisoconst int min; 464165648Spisoconst int max; 46541793Sluigi{ 46641793Sluigi char c; 46741793Sluigi int num; 46841793Sluigi 46941793Sluigi if (strp == NULL || !is_digit(c = *strp)) 470114258Smdodd return NULL; 471149371Sandre num = 0; 472150594Sandre do { 473114258Smdodd num = num * 10 + (c - '0'); 474170613Sbms if (num > max) 475170613Sbms return NULL; /* illegal value */ 476170613Sbms c = *++strp; 477170613Sbms } while (is_digit(c)); 478170613Sbms if (num < min) 479170613Sbms return NULL; /* illegal value */ 480170613Sbms *nump = num; 481170613Sbms return strp; 482170613Sbms} 483170613Sbms 484170613Sbms/* 485170613Sbms** Given a pointer into a time zone string, extract a number of seconds, 486170613Sbms** in hh[:mm[:ss]] form, from the string. 487170613Sbms** If any error occurs, return NULL. 488170613Sbms** Otherwise, return a pointer to the first character not part of the number 489170613Sbms** of seconds. 490170613Sbms*/ 4911541Srgrimes 4921541Srgrimesstatic const char * 4931541Srgrimesgetsecs(strp, secsp) 4941541Srgrimesconst char * strp; 4951541Srgrimeslong * const secsp; 4961541Srgrimes{ 4971541Srgrimes int num; 498158563Sbms 499158563Sbms /* 500158563Sbms ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like 501158563Sbms ** "M10.4.6/26", which does not conform to Posix, 502158563Sbms ** but which specifies the equivalent of 503158563Sbms ** ``02:00 on the first Sunday on or after 23 Oct''. 504189346Sbms */ 505158563Sbms strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); 506158563Sbms if (strp == NULL) 507189346Sbms return NULL; 508189346Sbms *secsp = num * (long) SECSPERHOUR; 509189346Sbms if (*strp == ':') { 510189346Sbms ++strp; 511189346Sbms strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 512189592Sbms if (strp == NULL) 513189346Sbms return NULL; 514189346Sbms *secsp += num * SECSPERMIN; 5151541Srgrimes if (*strp == ':') { 5161541Srgrimes ++strp; 5171541Srgrimes /* `SECSPERMIN' allows for leap seconds. */ 5181541Srgrimes strp = getnum(strp, &num, 0, SECSPERMIN); 5191541Srgrimes if (strp == NULL) 5201541Srgrimes return NULL; 5211541Srgrimes *secsp += num; 5221541Srgrimes } 523170613Sbms } 524170613Sbms return strp; 525170613Sbms} 526170613Sbms 527170613Sbms/* 528170613Sbms** Given a pointer into a time zone string, extract an offset, in 529170613Sbms** [+-]hh[:mm[:ss]] form, from the string. 530170613Sbms** If any error occurs, return NULL. 531170613Sbms** Otherwise, return a pointer to the first character not part of the time. 532170613Sbms*/ 533170613Sbms 534170613Sbmsstatic const char * 535170613Sbmsgetoffset(strp, offsetp) 536170613Sbmsconst char * strp; 537170613Sbmslong * const offsetp; 538170613Sbms{ 539170613Sbms int neg = 0; 540170613Sbms 541170613Sbms if (*strp == '-') { 542170613Sbms neg = 1; 543170613Sbms ++strp; 544170613Sbms } else if (*strp == '+') 545170613Sbms ++strp; 546170613Sbms strp = getsecs(strp, offsetp); 547170613Sbms if (strp == NULL) 548170613Sbms return NULL; /* illegal time */ 549170613Sbms if (neg) 550170613Sbms *offsetp = -*offsetp; 551170613Sbms return strp; 552170613Sbms} 553170613Sbms 554170613Sbms/* 555170613Sbms** Given a pointer into a time zone string, extract a rule in the form 556170613Sbms** date[/time]. See POSIX section 8 for the format of "date" and "time". 557170613Sbms** If a valid rule is not found, return NULL. 558170613Sbms** Otherwise, return a pointer to the first character not part of the rule. 559170613Sbms*/ 560170613Sbms 561170613Sbmsstatic const char * 562170613Sbmsgetrule(strp, rulep) 563170613Sbmsconst char * strp; 564170613Sbmsstruct rule * const rulep; 565170613Sbms{ 566170613Sbms if (*strp == 'J') { 567170613Sbms /* 568170613Sbms ** Julian day. 569170613Sbms */ 570170613Sbms rulep->r_type = JULIAN_DAY; 571170613Sbms ++strp; 572170613Sbms strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 573170613Sbms } else if (*strp == 'M') { 574170613Sbms /* 575170613Sbms ** Month, week, day. 576170613Sbms */ 577170613Sbms rulep->r_type = MONTH_NTH_DAY_OF_WEEK; 578170613Sbms ++strp; 579170613Sbms strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 580170613Sbms if (strp == NULL) 581170613Sbms return NULL; 582170613Sbms if (*strp++ != '.') 583170613Sbms return NULL; 584170613Sbms strp = getnum(strp, &rulep->r_week, 1, 5); 585170613Sbms if (strp == NULL) 586170613Sbms return NULL; 587170613Sbms if (*strp++ != '.') 588170613Sbms return NULL; 589170613Sbms strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 590170613Sbms } else if (is_digit(*strp)) { 591170613Sbms /* 592170613Sbms ** Day of year. 593170613Sbms */ 594170613Sbms rulep->r_type = DAY_OF_YEAR; 595189346Sbms strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 596170613Sbms } else return NULL; /* invalid format */ 597170613Sbms if (strp == NULL) 598170613Sbms return NULL; 599170613Sbms if (*strp == '/') { 60014195Speter /* 60114195Speter ** Time specified. 60214195Speter */ 60314195Speter ++strp; 60414195Speter strp = getsecs(strp, &rulep->r_time); 60514195Speter } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 60614195Speter return strp; 60714195Speter} 6081541Srgrimes 6091541Srgrimes/* 6101541Srgrimes** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the 6111541Srgrimes** year, a rule, and the offset from GMT at the time that rule takes effect, 6121541Srgrimes** calculate the Epoch-relative time that rule takes effect. 61362587Sitojun*/ 6141541Srgrimes 6151541Srgrimesstatic time_t 6161541Srgrimestranstime(janfirst, year, rulep, offset) 6171541Srgrimesconst time_t janfirst; 6181541Srgrimesconst int year; 6191541Srgrimesconst struct rule * const rulep; 6201541Srgrimesconst long offset; 6211541Srgrimes{ 6221541Srgrimes int leapyear; 6231541Srgrimes time_t value; 6241541Srgrimes int i; 6251541Srgrimes int d, m1, yy0, yy1, yy2, dow; 6261541Srgrimes 6271541Srgrimes INITIALIZE(value); 6281541Srgrimes leapyear = isleap(year); 6291541Srgrimes switch (rulep->r_type) { 6301541Srgrimes 6311541Srgrimes case JULIAN_DAY: 6321541Srgrimes /* 6331541Srgrimes ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap 6341541Srgrimes ** years. 6351541Srgrimes ** In non-leap years, or if the day number is 59 or less, just 6361541Srgrimes ** add SECSPERDAY times the day number-1 to the time of 6371541Srgrimes ** January 1, midnight, to get the day. 6381541Srgrimes */ 63962587Sitojun value = janfirst + (rulep->r_day - 1) * SECSPERDAY; 64062587Sitojun if (leapyear && rulep->r_day >= 60) 64162587Sitojun value += SECSPERDAY; 64262587Sitojun break; 64362587Sitojun 64462587Sitojun case DAY_OF_YEAR: 64562587Sitojun /* 64662587Sitojun ** n - day of year. 64762587Sitojun ** Just add SECSPERDAY times the day number to the time of 64862587Sitojun ** January 1, midnight, to get the day. 64962587Sitojun */ 65062587Sitojun value = janfirst + rulep->r_day * SECSPERDAY; 65162587Sitojun break; 65262587Sitojun 65362587Sitojun case MONTH_NTH_DAY_OF_WEEK: 65462587Sitojun /* 65562587Sitojun ** Mm.n.d - nth "dth day" of month m. 65662587Sitojun */ 65762587Sitojun value = janfirst; 65862587Sitojun for (i = 0; i < rulep->r_mon - 1; ++i) 65962587Sitojun value += mon_lengths[leapyear][i] * SECSPERDAY; 66062587Sitojun 66162587Sitojun /* 66262587Sitojun ** Use Zeller's Congruence to get day-of-week of first day of 66362587Sitojun ** month. 66462587Sitojun */ 66562587Sitojun m1 = (rulep->r_mon + 9) % 12 + 1; 66662587Sitojun yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 66762587Sitojun yy1 = yy0 / 100; 668118622Shsu yy2 = yy0 % 100; 669118622Shsu dow = ((26 * m1 - 2) / 10 + 670118622Shsu 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 671118622Shsu if (dow < 0) 672118622Shsu dow += DAYSPERWEEK; 673118622Shsu 674118622Shsu /* 675118622Shsu ** "dow" is the day-of-week of the first day of the month. Get 676118622Shsu ** the day-of-month (zero-origin) of the first "dow" day of the 677118622Shsu ** month. 678118622Shsu */ 679118622Shsu d = rulep->r_day - dow; 680118622Shsu if (d < 0) 681118622Shsu d += DAYSPERWEEK; 682118622Shsu for (i = 1; i < rulep->r_week; ++i) { 683118622Shsu if (d + DAYSPERWEEK >= 6841541Srgrimes mon_lengths[leapyear][rulep->r_mon - 1]) 6851541Srgrimes break; 6861541Srgrimes d += DAYSPERWEEK; 6871541Srgrimes } 6881541Srgrimes 6891541Srgrimes /* 6901541Srgrimes ** "d" is the day-of-month (zero-origin) of the day we want. 6911541Srgrimes */ 6921541Srgrimes value += d * SECSPERDAY; 6931541Srgrimes break; 6941541Srgrimes } 695133874Srwatson 696133874Srwatson /* 697133874Srwatson ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in 6987091Swollman ** question. To get the Epoch-relative time of the specified local 6999575Speter ** time on that day, add the transition time and the current offset 700133874Srwatson ** from GMT. 70152904Sshin */ 70229838Swollman return value + rulep->r_time + offset; 70333440Sguido} 70452904Sshin 70555009Sshin/* 70652904Sshin** Given a POSIX section 8-style TZ string, fill in the rule tables as 70752904Sshin** appropriate. 7081541Srgrimes*/ 7091541Srgrimes 7101541Srgrimesstatic int 7111541Srgrimestzparse(name, sp, lastditch) 7121541Srgrimesconst char * name; 7131541Srgrimesstruct state * const sp; 7141541Srgrimesconst int lastditch; 7155109Swollman{ 7166399Swollman const char * stdname; 7176399Swollman const char * dstname; 7187091Swollman size_t stdlen; 719133874Srwatson size_t dstlen; 72012003Swollman long stdoffset; 72112003Swollman long dstoffset; 72229838Swollman time_t * atp; 72333440Sguido unsigned char * typep; 72436192Sdg char * cp; 7251541Srgrimes int load_result; 7261541Srgrimes 72795336Smike INITIALIZE(dstname); 72895336Smike stdname = name; 72978243Speter if (lastditch) { 73095336Smike stdlen = strlen(name); /* length of standard zone name */ 73178243Speter name += stdlen; 73278243Speter if (stdlen >= sizeof sp->chars) 73392723Salfred stdlen = (sizeof sp->chars) - 1; 73492723Salfred stdoffset = 0; 73592723Salfred } else { 736133486Sandre name = getzname(name); 737133874Srwatson stdlen = name - stdname; 73892723Salfred if (stdlen < 3) 739150296Srwatson return -1; 7402169Spaul if (*name == '\0') 741133874Srwatson return -1; /* was "stdoffset = 0;" */ 742133874Srwatson else { 743189346Sbms name = getoffset(name, &stdoffset); 744102925Ssobomax if (name == NULL) 745133874Srwatson return -1; 746133874Srwatson } 747133874Srwatson } 74884101Sjlemon load_result = tzload(TZDEFRULES, sp); 74995336Smike if (load_result != 0) 75090868Smike sp->leapcnt = 0; /* so, we're off a little */ 75195336Smike if (*name != '\0') { 75295336Smike dstname = name; 75395336Smike name = getzname(name); 75495336Smike dstlen = name - dstname; /* length of DST zone name */ 75595336Smike if (dstlen < 3) 75691959Smike return -1; 75791959Smike if (*name != '\0' && *name != ',' && *name != ';') { 75895336Smike name = getoffset(name, &dstoffset); 759 if (name == NULL) 760 return -1; 761 } else dstoffset = stdoffset - SECSPERHOUR; 762 if (*name == ',' || *name == ';') { 763 struct rule start; 764 struct rule end; 765 int year; 766 time_t janfirst; 767 time_t starttime; 768 time_t endtime; 769 770 ++name; 771 if ((name = getrule(name, &start)) == NULL) 772 return -1; 773 if (*name++ != ',') 774 return -1; 775 if ((name = getrule(name, &end)) == NULL) 776 return -1; 777 if (*name != '\0') 778 return -1; 779 sp->typecnt = 2; /* standard time and DST */ 780 /* 781 ** Two transitions per year, from EPOCH_YEAR to 2037. 782 */ 783 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1); 784 if (sp->timecnt > TZ_MAX_TIMES) 785 return -1; 786 sp->ttis[0].tt_gmtoff = -dstoffset; 787 sp->ttis[0].tt_isdst = 1; 788 sp->ttis[0].tt_abbrind = stdlen + 1; 789 sp->ttis[1].tt_gmtoff = -stdoffset; 790 sp->ttis[1].tt_isdst = 0; 791 sp->ttis[1].tt_abbrind = 0; 792 atp = sp->ats; 793 typep = sp->types; 794 janfirst = 0; 795 for (year = EPOCH_YEAR; year <= 2037; ++year) { 796 starttime = transtime(janfirst, year, &start, 797 stdoffset); 798 endtime = transtime(janfirst, year, &end, 799 dstoffset); 800 if (starttime > endtime) { 801 *atp++ = endtime; 802 *typep++ = 1; /* DST ends */ 803 *atp++ = starttime; 804 *typep++ = 0; /* DST begins */ 805 } else { 806 *atp++ = starttime; 807 *typep++ = 0; /* DST begins */ 808 *atp++ = endtime; 809 *typep++ = 1; /* DST ends */ 810 } 811 janfirst += year_lengths[isleap(year)] * 812 SECSPERDAY; 813 } 814 } else { 815 long theirstdoffset; 816 long theirdstoffset; 817 long theiroffset; 818 int isdst; 819 int i; 820 int j; 821 822 if (*name != '\0') 823 return -1; 824 if (load_result != 0) 825 return -1; 826 /* 827 ** Initial values of theirstdoffset and theirdstoffset. 828 */ 829 theirstdoffset = 0; 830 for (i = 0; i < sp->timecnt; ++i) { 831 j = sp->types[i]; 832 if (!sp->ttis[j].tt_isdst) { 833 theirstdoffset = 834 -sp->ttis[j].tt_gmtoff; 835 break; 836 } 837 } 838 theirdstoffset = 0; 839 for (i = 0; i < sp->timecnt; ++i) { 840 j = sp->types[i]; 841 if (sp->ttis[j].tt_isdst) { 842 theirdstoffset = 843 -sp->ttis[j].tt_gmtoff; 844 break; 845 } 846 } 847 /* 848 ** Initially we're assumed to be in standard time. 849 */ 850 isdst = FALSE; 851 theiroffset = theirstdoffset; 852 /* 853 ** Now juggle transition times and types 854 ** tracking offsets as you do. 855 */ 856 for (i = 0; i < sp->timecnt; ++i) { 857 j = sp->types[i]; 858 sp->types[i] = sp->ttis[j].tt_isdst; 859 if (sp->ttis[j].tt_ttisgmt) { 860 /* No adjustment to transition time */ 861 } else { 862 /* 863 ** If summer time is in effect, and the 864 ** transition time was not specified as 865 ** standard time, add the summer time 866 ** offset to the transition time; 867 ** otherwise, add the standard time 868 ** offset to the transition time. 869 */ 870 /* 871 ** Transitions from DST to DDST 872 ** will effectively disappear since 873 ** POSIX provides for only one DST 874 ** offset. 875 */ 876 if (isdst && !sp->ttis[j].tt_ttisstd) { 877 sp->ats[i] += dstoffset - 878 theirdstoffset; 879 } else { 880 sp->ats[i] += stdoffset - 881 theirstdoffset; 882 } 883 } 884 theiroffset = -sp->ttis[j].tt_gmtoff; 885 if (sp->ttis[j].tt_isdst) 886 theirdstoffset = theiroffset; 887 else theirstdoffset = theiroffset; 888 } 889 /* 890 ** Finally, fill in ttis. 891 ** ttisstd and ttisgmt need not be handled. 892 */ 893 sp->ttis[0].tt_gmtoff = -stdoffset; 894 sp->ttis[0].tt_isdst = FALSE; 895 sp->ttis[0].tt_abbrind = 0; 896 sp->ttis[1].tt_gmtoff = -dstoffset; 897 sp->ttis[1].tt_isdst = TRUE; 898 sp->ttis[1].tt_abbrind = stdlen + 1; 899 } 900 } else { 901 dstlen = 0; 902 sp->typecnt = 1; /* only standard time */ 903 sp->timecnt = 0; 904 sp->ttis[0].tt_gmtoff = -stdoffset; 905 sp->ttis[0].tt_isdst = 0; 906 sp->ttis[0].tt_abbrind = 0; 907 } 908 sp->charcnt = stdlen + 1; 909 if (dstlen != 0) 910 sp->charcnt += dstlen + 1; 911 if (sp->charcnt > sizeof sp->chars) 912 return -1; 913 cp = sp->chars; 914 (void) strncpy(cp, stdname, stdlen); 915 cp += stdlen; 916 *cp++ = '\0'; 917 if (dstlen != 0) { 918 (void) strncpy(cp, dstname, dstlen); 919 *(cp + dstlen) = '\0'; 920 } 921 return 0; 922} 923 924static void 925gmtload(sp) 926struct state * const sp; 927{ 928 if (tzload(gmt, sp) != 0) 929 (void) tzparse(gmt, sp, TRUE); 930} 931 932static void 933tzsetwall_basic(void) 934{ 935 if (lcl_is_set < 0) 936 return; 937 lcl_is_set = -1; 938 939#ifdef ALL_STATE 940 if (lclptr == NULL) { 941 lclptr = (struct state *) malloc(sizeof *lclptr); 942 if (lclptr == NULL) { 943 settzname(); /* all we can do */ 944 return; 945 } 946 } 947#endif /* defined ALL_STATE */ 948 if (tzload((char *) NULL, lclptr) != 0) 949 gmtload(lclptr); 950 settzname(); 951} 952 953void 954tzsetwall P((void)) 955{ 956 _MUTEX_LOCK(&lcl_mutex); 957 tzsetwall_basic(); 958 _MUTEX_UNLOCK(&lcl_mutex); 959} 960 961static void 962tzset_basic(void) 963{ 964 const char * name; 965 966 name = getenv("TZ"); 967 if (name == NULL) { 968 tzsetwall_basic(); 969 return; 970 } 971 972 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) 973 return; 974 lcl_is_set = (strlen(name) < sizeof(lcl_TZname)); 975 if (lcl_is_set) 976 (void) strcpy(lcl_TZname, name); 977 978#ifdef ALL_STATE 979 if (lclptr == NULL) { 980 lclptr = (struct state *) malloc(sizeof *lclptr); 981 if (lclptr == NULL) { 982 settzname(); /* all we can do */ 983 return; 984 } 985 } 986#endif /* defined ALL_STATE */ 987 if (*name == '\0') { 988 /* 989 ** User wants it fast rather than right. 990 */ 991 lclptr->leapcnt = 0; /* so, we're off a little */ 992 lclptr->timecnt = 0; 993 lclptr->ttis[0].tt_gmtoff = 0; 994 lclptr->ttis[0].tt_abbrind = 0; 995 (void) strcpy(lclptr->chars, gmt); 996 } else if (tzload(name, lclptr) != 0) 997 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) 998 (void) gmtload(lclptr); 999 settzname(); 1000} 1001 1002void 1003tzset P((void)) 1004{ 1005 _MUTEX_LOCK(&lcl_mutex); 1006 tzset_basic(); 1007 _MUTEX_UNLOCK(&lcl_mutex); 1008} 1009 1010/* 1011** The easy way to behave "as if no library function calls" localtime 1012** is to not call it--so we drop its guts into "localsub", which can be 1013** freely called. (And no, the PANS doesn't require the above behavior-- 1014** but it *is* desirable.) 1015** 1016** The unused offset argument is for the benefit of mktime variants. 1017*/ 1018 1019/*ARGSUSED*/ 1020static void 1021localsub(timep, offset, tmp) 1022const time_t * const timep; 1023const long offset; 1024struct tm * const tmp; 1025{ 1026 struct state * sp; 1027 const struct ttinfo * ttisp; 1028 int i; 1029 const time_t t = *timep; 1030 1031 sp = lclptr; 1032#ifdef ALL_STATE 1033 if (sp == NULL) { 1034 gmtsub(timep, offset, tmp); 1035 return; 1036 } 1037#endif /* defined ALL_STATE */ 1038 if (sp->timecnt == 0 || t < sp->ats[0]) { 1039 i = 0; 1040 while (sp->ttis[i].tt_isdst) 1041 if (++i >= sp->typecnt) { 1042 i = 0; 1043 break; 1044 } 1045 } else { 1046 for (i = 1; i < sp->timecnt; ++i) 1047 if (t < sp->ats[i]) 1048 break; 1049 i = sp->types[i - 1]; 1050 } 1051 ttisp = &sp->ttis[i]; 1052 /* 1053 ** To get (wrong) behavior that's compatible with System V Release 2.0 1054 ** you'd replace the statement below with 1055 ** t += ttisp->tt_gmtoff; 1056 ** timesub(&t, 0L, sp, tmp); 1057 */ 1058 timesub(&t, ttisp->tt_gmtoff, sp, tmp); 1059 tmp->tm_isdst = ttisp->tt_isdst; 1060 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; 1061#ifdef TM_ZONE 1062 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; 1063#endif /* defined TM_ZONE */ 1064} 1065 1066struct tm * 1067localtime_r(timep, p_tm) 1068const time_t * const timep; 1069struct tm *p_tm; 1070{ 1071 _MUTEX_LOCK(&lcl_mutex); 1072 tzset_basic(); 1073 localsub(timep, 0L, p_tm); 1074 _MUTEX_UNLOCK(&lcl_mutex); 1075 return(p_tm); 1076} 1077 1078struct tm * 1079localtime(timep) 1080const time_t * const timep; 1081{ 1082 static pthread_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER; 1083 static pthread_key_t localtime_key = -1; 1084 struct tm *p_tm; 1085 1086 if (__isthreaded != 0) { 1087 _pthread_mutex_lock(&localtime_mutex); 1088 if (localtime_key < 0) { 1089 if (_pthread_key_create(&localtime_key, free) < 0) { 1090 _pthread_mutex_unlock(&localtime_mutex); 1091 return(NULL); 1092 } 1093 } 1094 _pthread_mutex_unlock(&localtime_mutex); 1095 p_tm = _pthread_getspecific(localtime_key); 1096 if (p_tm == NULL) { 1097 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) 1098 == NULL) 1099 return(NULL); 1100 _pthread_setspecific(localtime_key, p_tm); 1101 } 1102 _pthread_mutex_lock(&lcl_mutex); 1103 tzset_basic(); 1104 localsub(timep, 0L, p_tm); 1105 _pthread_mutex_unlock(&lcl_mutex); 1106 return(p_tm); 1107 } else { 1108 tzset_basic(); 1109 localsub(timep, 0L, &tm); 1110 return(&tm); 1111 } 1112} 1113 1114/* 1115** gmtsub is to gmtime as localsub is to localtime. 1116*/ 1117 1118static void 1119gmtsub(timep, offset, tmp) 1120const time_t * const timep; 1121const long offset; 1122struct tm * const tmp; 1123{ 1124 _MUTEX_LOCK(&gmt_mutex); 1125 if (!gmt_is_set) { 1126 gmt_is_set = TRUE; 1127#ifdef ALL_STATE 1128 gmtptr = (struct state *) malloc(sizeof *gmtptr); 1129 if (gmtptr != NULL) 1130#endif /* defined ALL_STATE */ 1131 gmtload(gmtptr); 1132 } 1133 _MUTEX_UNLOCK(&gmt_mutex); 1134 timesub(timep, offset, gmtptr, tmp); 1135#ifdef TM_ZONE 1136 /* 1137 ** Could get fancy here and deliver something such as 1138 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero, 1139 ** but this is no time for a treasure hunt. 1140 */ 1141 if (offset != 0) 1142 tmp->TM_ZONE = wildabbr; 1143 else { 1144#ifdef ALL_STATE 1145 if (gmtptr == NULL) 1146 tmp->TM_ZONE = gmt; 1147 else tmp->TM_ZONE = gmtptr->chars; 1148#endif /* defined ALL_STATE */ 1149#ifndef ALL_STATE 1150 tmp->TM_ZONE = gmtptr->chars; 1151#endif /* State Farm */ 1152 } 1153#endif /* defined TM_ZONE */ 1154} 1155 1156struct tm * 1157gmtime(timep) 1158const time_t * const timep; 1159{ 1160 static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER; 1161 static pthread_key_t gmtime_key = -1; 1162 struct tm *p_tm; 1163 1164 if (__isthreaded != 0) { 1165 _pthread_mutex_lock(&gmtime_mutex); 1166 if (gmtime_key < 0) { 1167 if (_pthread_key_create(&gmtime_key, free) < 0) { 1168 _pthread_mutex_unlock(&gmtime_mutex); 1169 return(NULL); 1170 } 1171 } 1172 _pthread_mutex_unlock(&gmtime_mutex); 1173 /* 1174 * Changed to follow POSIX.1 threads standard, which 1175 * is what BSD currently has. 1176 */ 1177 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) { 1178 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) 1179 == NULL) { 1180 return(NULL); 1181 } 1182 _pthread_setspecific(gmtime_key, p_tm); 1183 } 1184 gmtsub(timep, 0L, p_tm); 1185 return(p_tm); 1186 } 1187 else { 1188 gmtsub(timep, 0L, &tm); 1189 return(&tm); 1190 } 1191} 1192 1193struct tm * 1194gmtime_r(const time_t * timep, struct tm * tm) 1195{ 1196 gmtsub(timep, 0L, tm); 1197 return(tm); 1198} 1199 1200#ifdef STD_INSPIRED 1201 1202struct tm * 1203offtime(timep, offset) 1204const time_t * const timep; 1205const long offset; 1206{ 1207 gmtsub(timep, offset, &tm); 1208 return &tm; 1209} 1210 1211#endif /* defined STD_INSPIRED */ 1212 1213static void 1214timesub(timep, offset, sp, tmp) 1215const time_t * const timep; 1216const long offset; 1217const struct state * const sp; 1218struct tm * const tmp; 1219{ 1220 const struct lsinfo * lp; 1221 long days; 1222 long rem; 1223 int y; 1224 int yleap; 1225 const int * ip; 1226 long corr; 1227 int hit; 1228 int i; 1229 1230 corr = 0; 1231 hit = 0; 1232#ifdef ALL_STATE 1233 i = (sp == NULL) ? 0 : sp->leapcnt; 1234#endif /* defined ALL_STATE */ 1235#ifndef ALL_STATE 1236 i = sp->leapcnt; 1237#endif /* State Farm */ 1238 while (--i >= 0) { 1239 lp = &sp->lsis[i]; 1240 if (*timep >= lp->ls_trans) { 1241 if (*timep == lp->ls_trans) { 1242 hit = ((i == 0 && lp->ls_corr > 0) || 1243 lp->ls_corr > sp->lsis[i - 1].ls_corr); 1244 if (hit) 1245 while (i > 0 && 1246 sp->lsis[i].ls_trans == 1247 sp->lsis[i - 1].ls_trans + 1 && 1248 sp->lsis[i].ls_corr == 1249 sp->lsis[i - 1].ls_corr + 1) { 1250 ++hit; 1251 --i; 1252 } 1253 } 1254 corr = lp->ls_corr; 1255 break; 1256 } 1257 } 1258 days = *timep / SECSPERDAY; 1259 rem = *timep % SECSPERDAY; 1260#ifdef mc68k 1261 if (*timep == 0x80000000) { 1262 /* 1263 ** A 3B1 muffs the division on the most negative number. 1264 */ 1265 days = -24855; 1266 rem = -11648; 1267 } 1268#endif /* defined mc68k */ 1269 rem += (offset - corr); 1270 while (rem < 0) { 1271 rem += SECSPERDAY; 1272 --days; 1273 } 1274 while (rem >= SECSPERDAY) { 1275 rem -= SECSPERDAY; 1276 ++days; 1277 } 1278 tmp->tm_hour = (int) (rem / SECSPERHOUR); 1279 rem = rem % SECSPERHOUR; 1280 tmp->tm_min = (int) (rem / SECSPERMIN); 1281 /* 1282 ** A positive leap second requires a special 1283 ** representation. This uses "... ??:59:60" et seq. 1284 */ 1285 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; 1286 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK); 1287 if (tmp->tm_wday < 0) 1288 tmp->tm_wday += DAYSPERWEEK; 1289 y = EPOCH_YEAR; 1290#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) 1291 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) { 1292 int newy; 1293 1294 newy = y + days / DAYSPERNYEAR; 1295 if (days < 0) 1296 --newy; 1297 days -= (newy - y) * DAYSPERNYEAR + 1298 LEAPS_THRU_END_OF(newy - 1) - 1299 LEAPS_THRU_END_OF(y - 1); 1300 y = newy; 1301 } 1302 tmp->tm_year = y - TM_YEAR_BASE; 1303 tmp->tm_yday = (int) days; 1304 ip = mon_lengths[yleap]; 1305 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon)) 1306 days = days - (long) ip[tmp->tm_mon]; 1307 tmp->tm_mday = (int) (days + 1); 1308 tmp->tm_isdst = 0; 1309#ifdef TM_GMTOFF 1310 tmp->TM_GMTOFF = offset; 1311#endif /* defined TM_GMTOFF */ 1312} 1313 1314char * 1315ctime(timep) 1316const time_t * const timep; 1317{ 1318/* 1319** Section 4.12.3.2 of X3.159-1989 requires that 1320** The ctime funciton converts the calendar time pointed to by timer 1321** to local time in the form of a string. It is equivalent to 1322** asctime(localtime(timer)) 1323*/ 1324 return asctime(localtime(timep)); 1325} 1326 1327char * 1328ctime_r(timep, buf) 1329const time_t * const timep; 1330char *buf; 1331{ 1332 struct tm tm; 1333 return asctime_r(localtime_r(timep, &tm), buf); 1334} 1335 1336/* 1337** Adapted from code provided by Robert Elz, who writes: 1338** The "best" way to do mktime I think is based on an idea of Bob 1339** Kridle's (so its said...) from a long time ago. 1340** [kridle@xinet.com as of 1996-01-16.] 1341** It does a binary search of the time_t space. Since time_t's are 1342** just 32 bits, its a max of 32 iterations (even at 64 bits it 1343** would still be very reasonable). 1344*/ 1345 1346#ifndef WRONG 1347#define WRONG (-1) 1348#endif /* !defined WRONG */ 1349 1350/* 1351** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com). 1352*/ 1353 1354static int 1355increment_overflow(number, delta) 1356int * number; 1357int delta; 1358{ 1359 int number0; 1360 1361 number0 = *number; 1362 *number += delta; 1363 return (*number < number0) != (delta < 0); 1364} 1365 1366static int 1367normalize_overflow(tensptr, unitsptr, base) 1368int * const tensptr; 1369int * const unitsptr; 1370const int base; 1371{ 1372 int tensdelta; 1373 1374 tensdelta = (*unitsptr >= 0) ? 1375 (*unitsptr / base) : 1376 (-1 - (-1 - *unitsptr) / base); 1377 *unitsptr -= tensdelta * base; 1378 return increment_overflow(tensptr, tensdelta); 1379} 1380 1381static int 1382tmcomp(atmp, btmp) 1383const struct tm * const atmp; 1384const struct tm * const btmp; 1385{ 1386 int result; 1387 1388 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && 1389 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && 1390 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && 1391 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && 1392 (result = (atmp->tm_min - btmp->tm_min)) == 0) 1393 result = atmp->tm_sec - btmp->tm_sec; 1394 return result; 1395} 1396 1397static time_t 1398time2(tmp, funcp, offset, okayp) 1399struct tm * const tmp; 1400void (* const funcp) P((const time_t*, long, struct tm*)); 1401const long offset; 1402int * const okayp; 1403{ 1404 const struct state * sp; 1405 int dir; 1406 int bits; 1407 int i, j ; 1408 int saved_seconds; 1409 time_t newt; 1410 time_t t; 1411 struct tm yourtm, mytm; 1412 1413 *okayp = FALSE; 1414 yourtm = *tmp; 1415 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) 1416 return WRONG; 1417 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) 1418 return WRONG; 1419 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR)) 1420 return WRONG; 1421 /* 1422 ** Turn yourtm.tm_year into an actual year number for now. 1423 ** It is converted back to an offset from TM_YEAR_BASE later. 1424 */ 1425 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE)) 1426 return WRONG; 1427 while (yourtm.tm_mday <= 0) { 1428 if (increment_overflow(&yourtm.tm_year, -1)) 1429 return WRONG; 1430 i = yourtm.tm_year + (1 < yourtm.tm_mon); 1431 yourtm.tm_mday += year_lengths[isleap(i)]; 1432 } 1433 while (yourtm.tm_mday > DAYSPERLYEAR) { 1434 i = yourtm.tm_year + (1 < yourtm.tm_mon); 1435 yourtm.tm_mday -= year_lengths[isleap(i)]; 1436 if (increment_overflow(&yourtm.tm_year, 1)) 1437 return WRONG; 1438 } 1439 for ( ; ; ) { 1440 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon]; 1441 if (yourtm.tm_mday <= i) 1442 break; 1443 yourtm.tm_mday -= i; 1444 if (++yourtm.tm_mon >= MONSPERYEAR) { 1445 yourtm.tm_mon = 0; 1446 if (increment_overflow(&yourtm.tm_year, 1)) 1447 return WRONG; 1448 } 1449 } 1450 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE)) 1451 return WRONG; 1452 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) 1453 saved_seconds = 0; 1454 else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) { 1455 /* 1456 ** We can't set tm_sec to 0, because that might push the 1457 ** time below the minimum representable time. 1458 ** Set tm_sec to 59 instead. 1459 ** This assumes that the minimum representable time is 1460 ** not in the same minute that a leap second was deleted from, 1461 ** which is a safer assumption than using 58 would be. 1462 */ 1463 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) 1464 return WRONG; 1465 saved_seconds = yourtm.tm_sec; 1466 yourtm.tm_sec = SECSPERMIN - 1; 1467 } else { 1468 saved_seconds = yourtm.tm_sec; 1469 yourtm.tm_sec = 0; 1470 } 1471 /* 1472 ** Divide the search space in half 1473 ** (this works whether time_t is signed or unsigned). 1474 */ 1475 bits = TYPE_BIT(time_t) - 1; 1476 /* 1477 ** If time_t is signed, then 0 is just above the median, 1478 ** assuming two's complement arithmetic. 1479 ** If time_t is unsigned, then (1 << bits) is just above the median. 1480 */ 1481 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits); 1482 for ( ; ; ) { 1483 (*funcp)(&t, offset, &mytm); 1484 dir = tmcomp(&mytm, &yourtm); 1485 if (dir != 0) { 1486 if (bits-- < 0) 1487 return WRONG; 1488 if (bits < 0) 1489 --t; /* may be needed if new t is minimal */ 1490 else if (dir > 0) 1491 t -= ((time_t) 1) << bits; 1492 else t += ((time_t) 1) << bits; 1493 continue; 1494 } 1495 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) 1496 break; 1497 /* 1498 ** Right time, wrong type. 1499 ** Hunt for right time, right type. 1500 ** It's okay to guess wrong since the guess 1501 ** gets checked. 1502 */ 1503 /* 1504 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's. 1505 */ 1506 sp = (const struct state *) 1507 (((void *) funcp == (void *) localsub) ? 1508 lclptr : gmtptr); 1509#ifdef ALL_STATE 1510 if (sp == NULL) 1511 return WRONG; 1512#endif /* defined ALL_STATE */ 1513 for (i = sp->typecnt - 1; i >= 0; --i) { 1514 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) 1515 continue; 1516 for (j = sp->typecnt - 1; j >= 0; --j) { 1517 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) 1518 continue; 1519 newt = t + sp->ttis[j].tt_gmtoff - 1520 sp->ttis[i].tt_gmtoff; 1521 (*funcp)(&newt, offset, &mytm); 1522 if (tmcomp(&mytm, &yourtm) != 0) 1523 continue; 1524 if (mytm.tm_isdst != yourtm.tm_isdst) 1525 continue; 1526 /* 1527 ** We have a match. 1528 */ 1529 t = newt; 1530 goto label; 1531 } 1532 } 1533 return WRONG; 1534 } 1535label: 1536 newt = t + saved_seconds; 1537 if ((newt < t) != (saved_seconds < 0)) 1538 return WRONG; 1539 t = newt; 1540 (*funcp)(&t, offset, tmp); 1541 *okayp = TRUE; 1542 return t; 1543} 1544 1545static time_t 1546time1(tmp, funcp, offset) 1547struct tm * const tmp; 1548void (* const funcp) P((const time_t *, long, struct tm *)); 1549const long offset; 1550{ 1551 time_t t; 1552 const struct state * sp; 1553 int samei, otheri; 1554 int okay; 1555 1556 if (tmp->tm_isdst > 1) 1557 tmp->tm_isdst = 1; 1558 t = time2(tmp, funcp, offset, &okay); 1559#ifdef PCTS 1560 /* 1561 ** PCTS code courtesy Grant Sullivan (grant@osf.org). 1562 */ 1563 if (okay) 1564 return t; 1565 if (tmp->tm_isdst < 0) 1566 tmp->tm_isdst = 0; /* reset to std and try again */ 1567#endif /* defined PCTS */ 1568#ifndef PCTS 1569 if (okay || tmp->tm_isdst < 0) 1570 return t; 1571#endif /* !defined PCTS */ 1572 /* 1573 ** We're supposed to assume that somebody took a time of one type 1574 ** and did some math on it that yielded a "struct tm" that's bad. 1575 ** We try to divine the type they started from and adjust to the 1576 ** type they need. 1577 */ 1578 /* 1579 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's. 1580 */ 1581 sp = (const struct state *) (((void *) funcp == (void *) localsub) ? 1582 lclptr : gmtptr); 1583#ifdef ALL_STATE 1584 if (sp == NULL) 1585 return WRONG; 1586#endif /* defined ALL_STATE */ 1587 for (samei = sp->typecnt - 1; samei >= 0; --samei) { 1588 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) 1589 continue; 1590 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) { 1591 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) 1592 continue; 1593 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - 1594 sp->ttis[samei].tt_gmtoff; 1595 tmp->tm_isdst = !tmp->tm_isdst; 1596 t = time2(tmp, funcp, offset, &okay); 1597 if (okay) 1598 return t; 1599 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - 1600 sp->ttis[samei].tt_gmtoff; 1601 tmp->tm_isdst = !tmp->tm_isdst; 1602 } 1603 } 1604 return WRONG; 1605} 1606 1607time_t 1608mktime(tmp) 1609struct tm * const tmp; 1610{ 1611 time_t mktime_return_value; 1612 _MUTEX_LOCK(&lcl_mutex); 1613 tzset_basic(); 1614 mktime_return_value = time1(tmp, localsub, 0L); 1615 _MUTEX_UNLOCK(&lcl_mutex); 1616 return(mktime_return_value); 1617} 1618 1619#ifdef STD_INSPIRED 1620 1621time_t 1622timelocal(tmp) 1623struct tm * const tmp; 1624{ 1625 tmp->tm_isdst = -1; /* in case it wasn't initialized */ 1626 return mktime(tmp); 1627} 1628 1629time_t 1630timegm(tmp) 1631struct tm * const tmp; 1632{ 1633 tmp->tm_isdst = 0; 1634 return time1(tmp, gmtsub, 0L); 1635} 1636 1637time_t 1638timeoff(tmp, offset) 1639struct tm * const tmp; 1640const long offset; 1641{ 1642 tmp->tm_isdst = 0; 1643 return time1(tmp, gmtsub, offset); 1644} 1645 1646#endif /* defined STD_INSPIRED */ 1647 1648#ifdef CMUCS 1649 1650/* 1651** The following is supplied for compatibility with 1652** previous versions of the CMUCS runtime library. 1653*/ 1654 1655long 1656gtime(tmp) 1657struct tm * const tmp; 1658{ 1659 const time_t t = mktime(tmp); 1660 1661 if (t == WRONG) 1662 return -1; 1663 return t; 1664} 1665 1666#endif /* defined CMUCS */ 1667 1668/* 1669** XXX--is the below the right way to conditionalize?? 1670*/ 1671 1672#ifdef STD_INSPIRED 1673 1674/* 1675** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 1676** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which 1677** is not the case if we are accounting for leap seconds. 1678** So, we provide the following conversion routines for use 1679** when exchanging timestamps with POSIX conforming systems. 1680*/ 1681 1682static long 1683leapcorr(timep) 1684time_t * timep; 1685{ 1686 struct state * sp; 1687 struct lsinfo * lp; 1688 int i; 1689 1690 sp = lclptr; 1691 i = sp->leapcnt; 1692 while (--i >= 0) { 1693 lp = &sp->lsis[i]; 1694 if (*timep >= lp->ls_trans) 1695 return lp->ls_corr; 1696 } 1697 return 0; 1698} 1699 1700time_t 1701time2posix(t) 1702time_t t; 1703{ 1704 tzset(); 1705 return t - leapcorr(&t); 1706} 1707 1708time_t 1709posix2time(t) 1710time_t t; 1711{ 1712 time_t x; 1713 time_t y; 1714 1715 tzset(); 1716 /* 1717 ** For a positive leap second hit, the result 1718 ** is not unique. For a negative leap second 1719 ** hit, the corresponding time doesn't exist, 1720 ** so we return an adjacent second. 1721 */ 1722 x = t + leapcorr(&t); 1723 y = x - leapcorr(&x); 1724 if (y < t) { 1725 do { 1726 x++; 1727 y = x - leapcorr(&x); 1728 } while (y < t); 1729 if (t != y) 1730 return x - 1; 1731 } else if (y > t) { 1732 do { 1733 --x; 1734 y = x - leapcorr(&x); 1735 } while (y > t); 1736 if (t != y) 1737 return x + 1; 1738 } 1739 return x; 1740} 1741 1742#endif /* defined STD_INSPIRED */ 1743