localtime.c revision 17706
1/* 2** This file is in the public domain, so clarified as of 3** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov). 4*/ 5 6#ifndef lint 7#ifndef NOID 8static char elsieid[] = "@(#)localtime.c 7.57"; 9#endif /* !defined NOID */ 10#endif /* !defined lint */ 11 12/* 13** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu). 14** POSIX-style TZ environment variable handling from Guy Harris 15** (guy@auspex.com). 16*/ 17 18/*LINTLIBRARY*/ 19 20#include "private.h" 21#include "tzfile.h" 22#include "fcntl.h" 23#ifdef _THREAD_SAFE 24#include <pthread.h> 25#include "pthread_private.h" 26#endif 27 28/* 29** SunOS 4.1.1 headers lack O_BINARY. 30*/ 31 32#ifdef O_BINARY 33#define OPEN_MODE (O_RDONLY | O_BINARY) 34#endif /* defined O_BINARY */ 35#ifndef O_BINARY 36#define OPEN_MODE O_RDONLY 37#endif /* !defined O_BINARY */ 38 39#ifndef WILDABBR 40/* 41** Someone might make incorrect use of a time zone abbreviation: 42** 1. They might reference tzname[0] before calling tzset (explicitly 43** or implicitly). 44** 2. They might reference tzname[1] before calling tzset (explicitly 45** or implicitly). 46** 3. They might reference tzname[1] after setting to a time zone 47** in which Daylight Saving Time is never observed. 48** 4. They might reference tzname[0] after setting to a time zone 49** in which Standard Time is never observed. 50** 5. They might reference tm.TM_ZONE after calling offtime. 51** What's best to do in the above cases is open to debate; 52** for now, we just set things up so that in any of the five cases 53** WILDABBR is used. Another possibility: initialize tzname[0] to the 54** string "tzname[0] used before set", and similarly for the other cases. 55** And another: initialize tzname[0] to "ERA", with an explanation in the 56** manual page of what this "time zone abbreviation" means (doing this so 57** that tzname[0] has the "normal" length of three characters). 58*/ 59#define WILDABBR " " 60#endif /* !defined WILDABBR */ 61 62static char wildabbr[] = "WILDABBR"; 63 64static const char gmt[] = "GMT"; 65 66struct ttinfo { /* time type information */ 67 long tt_gmtoff; /* GMT offset in seconds */ 68 int tt_isdst; /* used to set tm_isdst */ 69 int tt_abbrind; /* abbreviation list index */ 70 int tt_ttisstd; /* TRUE if transition is std time */ 71 int tt_ttisgmt; /* TRUE if transition is GMT */ 72}; 73 74struct lsinfo { /* leap second information */ 75 time_t ls_trans; /* transition time */ 76 long ls_corr; /* correction to apply */ 77}; 78 79#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) 80 81#ifdef TZNAME_MAX 82#define MY_TZNAME_MAX TZNAME_MAX 83#endif /* defined TZNAME_MAX */ 84#ifndef TZNAME_MAX 85#define MY_TZNAME_MAX 255 86#endif /* !defined TZNAME_MAX */ 87 88struct state { 89 int leapcnt; 90 int timecnt; 91 int typecnt; 92 int charcnt; 93 time_t ats[TZ_MAX_TIMES]; 94 unsigned char types[TZ_MAX_TIMES]; 95 struct ttinfo ttis[TZ_MAX_TYPES]; 96 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), 97 (2 * (MY_TZNAME_MAX + 1)))]; 98 struct lsinfo lsis[TZ_MAX_LEAPS]; 99}; 100 101struct rule { 102 int r_type; /* type of rule--see below */ 103 int r_day; /* day number of rule */ 104 int r_week; /* week number of rule */ 105 int r_mon; /* month number of rule */ 106 long r_time; /* transition time of rule */ 107}; 108 109#define JULIAN_DAY 0 /* Jn - Julian day */ 110#define DAY_OF_YEAR 1 /* n - day of year */ 111#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ 112 113/* 114** Prototypes for static functions. 115*/ 116 117static long detzcode P((const char * codep)); 118static const char * getzname P((const char * strp)); 119static const char * getnum P((const char * strp, int * nump, int min, 120 int max)); 121static const char * getsecs P((const char * strp, long * secsp)); 122static const char * getoffset P((const char * strp, long * offsetp)); 123static const char * getrule P((const char * strp, struct rule * rulep)); 124static void gmtload P((struct state * sp)); 125static void gmtsub P((const time_t * timep, long offset, 126 struct tm * tmp)); 127static void localsub P((const time_t * timep, long offset, 128 struct tm * tmp)); 129static int increment_overflow P((int * number, int delta)); 130static int normalize_overflow P((int * tensptr, int * unitsptr, 131 int base)); 132static void settzname P((void)); 133static time_t time1 P((struct tm * tmp, 134 void(*funcp) P((const time_t *, 135 long, struct tm *)), 136 long offset)); 137static time_t time2 P((struct tm *tmp, 138 void(*funcp) P((const time_t *, 139 long, struct tm*)), 140 long offset, int * okayp)); 141static void timesub P((const time_t * timep, long offset, 142 const struct state * sp, struct tm * tmp)); 143static int tmcomp P((const struct tm * atmp, 144 const struct tm * btmp)); 145static time_t transtime P((time_t janfirst, int year, 146 const struct rule * rulep, long offset)); 147static int tzload P((const char * name, struct state * sp)); 148static int tzparse P((const char * name, struct state * sp, 149 int lastditch)); 150 151#ifdef ALL_STATE 152static struct state * lclptr; 153static struct state * gmtptr; 154#endif /* defined ALL_STATE */ 155 156#ifndef ALL_STATE 157static struct state lclmem; 158static struct state gmtmem; 159#define lclptr (&lclmem) 160#define gmtptr (&gmtmem) 161#endif /* State Farm */ 162 163#ifndef TZ_STRLEN_MAX 164#define TZ_STRLEN_MAX 255 165#endif /* !defined TZ_STRLEN_MAX */ 166 167static char lcl_TZname[TZ_STRLEN_MAX + 1]; 168static int lcl_is_set; 169static int gmt_is_set; 170#ifdef _THREAD_SAFE 171static struct pthread_mutex _lcl_mutexd = PTHREAD_MUTEX_INITIALIZER; 172static struct pthread_mutex _gmt_mutexd = PTHREAD_MUTEX_INITIALIZER; 173static pthread_mutex_t lcl_mutex = &_lcl_mutexd; 174static pthread_mutex_t gmt_mutex = &_gmt_mutexd; 175#endif 176 177char * tzname[2] = { 178 wildabbr, 179 wildabbr 180}; 181 182/* 183** Section 4.12.3 of X3.159-1989 requires that 184** Except for the strftime function, these functions [asctime, 185** ctime, gmtime, localtime] return values in one of two static 186** objects: a broken-down time structure and an array of char. 187** Thanks to Paul Eggert (eggert@twinsun.com) for noting this. 188*/ 189 190static struct tm tm; 191 192#ifdef USG_COMPAT 193time_t timezone = 0; 194int daylight = 0; 195#endif /* defined USG_COMPAT */ 196 197#ifdef ALTZONE 198time_t altzone = 0; 199#endif /* defined ALTZONE */ 200 201static long 202detzcode(codep) 203const char * const codep; 204{ 205 register long result; 206 register int i; 207 208 result = (codep[0] & 0x80) ? ~0L : 0L; 209 for (i = 0; i < 4; ++i) 210 result = (result << 8) | (codep[i] & 0xff); 211 return result; 212} 213 214static void 215settzname P((void)) 216{ 217 register struct state * const sp = lclptr; 218 register int i; 219 220 tzname[0] = wildabbr; 221 tzname[1] = wildabbr; 222#ifdef USG_COMPAT 223 daylight = 0; 224 timezone = 0; 225#endif /* defined USG_COMPAT */ 226#ifdef ALTZONE 227 altzone = 0; 228#endif /* defined ALTZONE */ 229#ifdef ALL_STATE 230 if (sp == NULL) { 231 tzname[0] = tzname[1] = gmt; 232 return; 233 } 234#endif /* defined ALL_STATE */ 235 for (i = 0; i < sp->typecnt; ++i) { 236 register const struct ttinfo * const ttisp = &sp->ttis[i]; 237 238 tzname[ttisp->tt_isdst] = 239 &sp->chars[ttisp->tt_abbrind]; 240#ifdef USG_COMPAT 241 if (ttisp->tt_isdst) 242 daylight = 1; 243 if (i == 0 || !ttisp->tt_isdst) 244 timezone = -(ttisp->tt_gmtoff); 245#endif /* defined USG_COMPAT */ 246#ifdef ALTZONE 247 if (i == 0 || ttisp->tt_isdst) 248 altzone = -(ttisp->tt_gmtoff); 249#endif /* defined ALTZONE */ 250 } 251 /* 252 ** And to get the latest zone names into tzname. . . 253 */ 254 for (i = 0; i < sp->timecnt; ++i) { 255 register const struct ttinfo * const ttisp = 256 &sp->ttis[ 257 sp->types[i]]; 258 259 tzname[ttisp->tt_isdst] = 260 &sp->chars[ttisp->tt_abbrind]; 261 } 262} 263 264static int 265tzload(name, sp) 266register const char * name; 267register struct state * const sp; 268{ 269 register const char * p; 270 register int i; 271 register int fid; 272 273 if (name == NULL && (name = TZDEFAULT) == NULL) 274 return -1; 275 { 276 register int doaccess; 277 /* 278 ** Section 4.9.1 of the C standard says that 279 ** "FILENAME_MAX expands to an integral constant expression 280 ** that is the sie needed for an array of char large enough 281 ** to hold the longest file name string that the implementation 282 ** guarantees can be opened." 283 */ 284 char fullname[FILENAME_MAX + 1]; 285 286 if (name[0] == ':') 287 ++name; 288 doaccess = name[0] == '/'; 289 if (!doaccess) { 290 if ((p = TZDIR) == NULL) 291 return -1; 292 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) 293 return -1; 294 (void) strcpy(fullname, p); 295 (void) strcat(fullname, "/"); 296 (void) strcat(fullname, name); 297 /* 298 ** Set doaccess if '.' (as in "../") shows up in name. 299 */ 300 if (strchr(name, '.') != NULL) 301 doaccess = TRUE; 302 name = fullname; 303 } 304 if (doaccess && access(name, R_OK) != 0) 305 return -1; 306 if ((fid = open(name, OPEN_MODE)) == -1) 307 return -1; 308 } 309 { 310 struct tzhead * tzhp; 311 char buf[sizeof *sp + sizeof *tzhp]; 312 int ttisstdcnt; 313 int ttisgmtcnt; 314 315 i = read(fid, buf, sizeof buf); 316 if (close(fid) != 0) 317 return -1; 318 p = buf; 319 p += sizeof tzhp->tzh_reserved; 320 ttisstdcnt = (int) detzcode(p); 321 p += 4; 322 ttisgmtcnt = (int) detzcode(p); 323 p += 4; 324 sp->leapcnt = (int) detzcode(p); 325 p += 4; 326 sp->timecnt = (int) detzcode(p); 327 p += 4; 328 sp->typecnt = (int) detzcode(p); 329 p += 4; 330 sp->charcnt = (int) detzcode(p); 331 p += 4; 332 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 333 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 334 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 335 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 336 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || 337 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) 338 return -1; 339 if (i - (p - buf) < sp->timecnt * 4 + /* ats */ 340 sp->timecnt + /* types */ 341 sp->typecnt * (4 + 2) + /* ttinfos */ 342 sp->charcnt + /* chars */ 343 sp->leapcnt * (4 + 4) + /* lsinfos */ 344 ttisstdcnt + /* ttisstds */ 345 ttisgmtcnt) /* ttisgmts */ 346 return -1; 347 for (i = 0; i < sp->timecnt; ++i) { 348 sp->ats[i] = detzcode(p); 349 p += 4; 350 } 351 for (i = 0; i < sp->timecnt; ++i) { 352 sp->types[i] = (unsigned char) *p++; 353 if (sp->types[i] >= sp->typecnt) 354 return -1; 355 } 356 for (i = 0; i < sp->typecnt; ++i) { 357 register struct ttinfo * ttisp; 358 359 ttisp = &sp->ttis[i]; 360 ttisp->tt_gmtoff = detzcode(p); 361 p += 4; 362 ttisp->tt_isdst = (unsigned char) *p++; 363 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) 364 return -1; 365 ttisp->tt_abbrind = (unsigned char) *p++; 366 if (ttisp->tt_abbrind < 0 || 367 ttisp->tt_abbrind > sp->charcnt) 368 return -1; 369 } 370 for (i = 0; i < sp->charcnt; ++i) 371 sp->chars[i] = *p++; 372 sp->chars[i] = '\0'; /* ensure '\0' at end */ 373 for (i = 0; i < sp->leapcnt; ++i) { 374 register struct lsinfo * lsisp; 375 376 lsisp = &sp->lsis[i]; 377 lsisp->ls_trans = detzcode(p); 378 p += 4; 379 lsisp->ls_corr = detzcode(p); 380 p += 4; 381 } 382 for (i = 0; i < sp->typecnt; ++i) { 383 register struct ttinfo * ttisp; 384 385 ttisp = &sp->ttis[i]; 386 if (ttisstdcnt == 0) 387 ttisp->tt_ttisstd = FALSE; 388 else { 389 ttisp->tt_ttisstd = *p++; 390 if (ttisp->tt_ttisstd != TRUE && 391 ttisp->tt_ttisstd != FALSE) 392 return -1; 393 } 394 } 395 for (i = 0; i < sp->typecnt; ++i) { 396 register struct ttinfo * ttisp; 397 398 ttisp = &sp->ttis[i]; 399 if (ttisgmtcnt == 0) 400 ttisp->tt_ttisgmt = FALSE; 401 else { 402 ttisp->tt_ttisgmt = *p++; 403 if (ttisp->tt_ttisgmt != TRUE && 404 ttisp->tt_ttisgmt != FALSE) 405 return -1; 406 } 407 } 408 } 409 return 0; 410} 411 412static const int mon_lengths[2][MONSPERYEAR] = { 413 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 414 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } 415}; 416 417static const int year_lengths[2] = { 418 DAYSPERNYEAR, DAYSPERLYEAR 419}; 420 421/* 422** Given a pointer into a time zone string, scan until a character that is not 423** a valid character in a zone name is found. Return a pointer to that 424** character. 425*/ 426 427static const char * 428getzname(strp) 429register const char * strp; 430{ 431 register char c; 432 433 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && 434 c != '+') 435 ++strp; 436 return strp; 437} 438 439/* 440** Given a pointer into a time zone string, extract a number from that string. 441** Check that the number is within a specified range; if it is not, return 442** NULL. 443** Otherwise, return a pointer to the first character not part of the number. 444*/ 445 446static const char * 447getnum(strp, nump, min, max) 448register const char * strp; 449int * const nump; 450const int min; 451const int max; 452{ 453 register char c; 454 register int num; 455 456 if (strp == NULL || !is_digit(c = *strp)) 457 return NULL; 458 num = 0; 459 do { 460 num = num * 10 + (c - '0'); 461 if (num > max) 462 return NULL; /* illegal value */ 463 c = *++strp; 464 } while (is_digit(c)); 465 if (num < min) 466 return NULL; /* illegal value */ 467 *nump = num; 468 return strp; 469} 470 471/* 472** Given a pointer into a time zone string, extract a number of seconds, 473** in hh[:mm[:ss]] form, from the string. 474** If any error occurs, return NULL. 475** Otherwise, return a pointer to the first character not part of the number 476** of seconds. 477*/ 478 479static const char * 480getsecs(strp, secsp) 481register const char * strp; 482long * const secsp; 483{ 484 int num; 485 486 /* 487 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like 488 ** "M10.4.6/26", which does not conform to Posix, 489 ** but which specifies the equivalent of 490 ** ``02:00 on the first Sunday on or after 23 Oct''. 491 */ 492 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); 493 if (strp == NULL) 494 return NULL; 495 *secsp = num * (long) SECSPERHOUR; 496 if (*strp == ':') { 497 ++strp; 498 strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 499 if (strp == NULL) 500 return NULL; 501 *secsp += num * SECSPERMIN; 502 if (*strp == ':') { 503 ++strp; 504 /* `SECSPERMIN' allows for leap seconds. */ 505 strp = getnum(strp, &num, 0, SECSPERMIN); 506 if (strp == NULL) 507 return NULL; 508 *secsp += num; 509 } 510 } 511 return strp; 512} 513 514/* 515** Given a pointer into a time zone string, extract an offset, in 516** [+-]hh[:mm[:ss]] form, from the string. 517** If any error occurs, return NULL. 518** Otherwise, return a pointer to the first character not part of the time. 519*/ 520 521static const char * 522getoffset(strp, offsetp) 523register const char * strp; 524long * const offsetp; 525{ 526 register int neg = 0; 527 528 if (*strp == '-') { 529 neg = 1; 530 ++strp; 531 } else if (*strp == '+') 532 ++strp; 533 strp = getsecs(strp, offsetp); 534 if (strp == NULL) 535 return NULL; /* illegal time */ 536 if (neg) 537 *offsetp = -*offsetp; 538 return strp; 539} 540 541/* 542** Given a pointer into a time zone string, extract a rule in the form 543** date[/time]. See POSIX section 8 for the format of "date" and "time". 544** If a valid rule is not found, return NULL. 545** Otherwise, return a pointer to the first character not part of the rule. 546*/ 547 548static const char * 549getrule(strp, rulep) 550const char * strp; 551register struct rule * const rulep; 552{ 553 if (*strp == 'J') { 554 /* 555 ** Julian day. 556 */ 557 rulep->r_type = JULIAN_DAY; 558 ++strp; 559 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 560 } else if (*strp == 'M') { 561 /* 562 ** Month, week, day. 563 */ 564 rulep->r_type = MONTH_NTH_DAY_OF_WEEK; 565 ++strp; 566 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 567 if (strp == NULL) 568 return NULL; 569 if (*strp++ != '.') 570 return NULL; 571 strp = getnum(strp, &rulep->r_week, 1, 5); 572 if (strp == NULL) 573 return NULL; 574 if (*strp++ != '.') 575 return NULL; 576 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 577 } else if (is_digit(*strp)) { 578 /* 579 ** Day of year. 580 */ 581 rulep->r_type = DAY_OF_YEAR; 582 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 583 } else return NULL; /* invalid format */ 584 if (strp == NULL) 585 return NULL; 586 if (*strp == '/') { 587 /* 588 ** Time specified. 589 */ 590 ++strp; 591 strp = getsecs(strp, &rulep->r_time); 592 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 593 return strp; 594} 595 596/* 597** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the 598** year, a rule, and the offset from GMT at the time that rule takes effect, 599** calculate the Epoch-relative time that rule takes effect. 600*/ 601 602static time_t 603transtime(janfirst, year, rulep, offset) 604const time_t janfirst; 605const int year; 606register const struct rule * const rulep; 607const long offset; 608{ 609 register int leapyear; 610 register time_t value; 611 register int i; 612 int d, m1, yy0, yy1, yy2, dow; 613 614 INITIALIZE(value); 615 leapyear = isleap(year); 616 switch (rulep->r_type) { 617 618 case JULIAN_DAY: 619 /* 620 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap 621 ** years. 622 ** In non-leap years, or if the day number is 59 or less, just 623 ** add SECSPERDAY times the day number-1 to the time of 624 ** January 1, midnight, to get the day. 625 */ 626 value = janfirst + (rulep->r_day - 1) * SECSPERDAY; 627 if (leapyear && rulep->r_day >= 60) 628 value += SECSPERDAY; 629 break; 630 631 case DAY_OF_YEAR: 632 /* 633 ** n - day of year. 634 ** Just add SECSPERDAY times the day number to the time of 635 ** January 1, midnight, to get the day. 636 */ 637 value = janfirst + rulep->r_day * SECSPERDAY; 638 break; 639 640 case MONTH_NTH_DAY_OF_WEEK: 641 /* 642 ** Mm.n.d - nth "dth day" of month m. 643 */ 644 value = janfirst; 645 for (i = 0; i < rulep->r_mon - 1; ++i) 646 value += mon_lengths[leapyear][i] * SECSPERDAY; 647 648 /* 649 ** Use Zeller's Congruence to get day-of-week of first day of 650 ** month. 651 */ 652 m1 = (rulep->r_mon + 9) % 12 + 1; 653 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 654 yy1 = yy0 / 100; 655 yy2 = yy0 % 100; 656 dow = ((26 * m1 - 2) / 10 + 657 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 658 if (dow < 0) 659 dow += DAYSPERWEEK; 660 661 /* 662 ** "dow" is the day-of-week of the first day of the month. Get 663 ** the day-of-month (zero-origin) of the first "dow" day of the 664 ** month. 665 */ 666 d = rulep->r_day - dow; 667 if (d < 0) 668 d += DAYSPERWEEK; 669 for (i = 1; i < rulep->r_week; ++i) { 670 if (d + DAYSPERWEEK >= 671 mon_lengths[leapyear][rulep->r_mon - 1]) 672 break; 673 d += DAYSPERWEEK; 674 } 675 676 /* 677 ** "d" is the day-of-month (zero-origin) of the day we want. 678 */ 679 value += d * SECSPERDAY; 680 break; 681 } 682 683 /* 684 ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in 685 ** question. To get the Epoch-relative time of the specified local 686 ** time on that day, add the transition time and the current offset 687 ** from GMT. 688 */ 689 return value + rulep->r_time + offset; 690} 691 692/* 693** Given a POSIX section 8-style TZ string, fill in the rule tables as 694** appropriate. 695*/ 696 697static int 698tzparse(name, sp, lastditch) 699const char * name; 700register struct state * const sp; 701const int lastditch; 702{ 703 const char * stdname; 704 const char * dstname; 705 size_t stdlen; 706 size_t dstlen; 707 long stdoffset; 708 long dstoffset; 709 register time_t * atp; 710 register unsigned char * typep; 711 register char * cp; 712 register int load_result; 713 714 INITIALIZE(dstname); 715 stdname = name; 716 if (lastditch) { 717 stdlen = strlen(name); /* length of standard zone name */ 718 name += stdlen; 719 if (stdlen >= sizeof sp->chars) 720 stdlen = (sizeof sp->chars) - 1; 721 } else { 722 name = getzname(name); 723 stdlen = name - stdname; 724 if (stdlen < 3) 725 return -1; 726 } 727 if (*name == '\0') 728 return -1; /* was "stdoffset = 0;" */ 729 else { 730 name = getoffset(name, &stdoffset); 731 if (name == NULL) 732 return -1; 733 } 734 load_result = tzload(TZDEFRULES, sp); 735 if (load_result != 0) 736 sp->leapcnt = 0; /* so, we're off a little */ 737 if (*name != '\0') { 738 dstname = name; 739 name = getzname(name); 740 dstlen = name - dstname; /* length of DST zone name */ 741 if (dstlen < 3) 742 return -1; 743 if (*name != '\0' && *name != ',' && *name != ';') { 744 name = getoffset(name, &dstoffset); 745 if (name == NULL) 746 return -1; 747 } else dstoffset = stdoffset - SECSPERHOUR; 748 if (*name == ',' || *name == ';') { 749 struct rule start; 750 struct rule end; 751 register int year; 752 register time_t janfirst; 753 time_t starttime; 754 time_t endtime; 755 756 ++name; 757 if ((name = getrule(name, &start)) == NULL) 758 return -1; 759 if (*name++ != ',') 760 return -1; 761 if ((name = getrule(name, &end)) == NULL) 762 return -1; 763 if (*name != '\0') 764 return -1; 765 sp->typecnt = 2; /* standard time and DST */ 766 /* 767 ** Two transitions per year, from EPOCH_YEAR to 2037. 768 */ 769 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1); 770 if (sp->timecnt > TZ_MAX_TIMES) 771 return -1; 772 sp->ttis[0].tt_gmtoff = -dstoffset; 773 sp->ttis[0].tt_isdst = 1; 774 sp->ttis[0].tt_abbrind = stdlen + 1; 775 sp->ttis[1].tt_gmtoff = -stdoffset; 776 sp->ttis[1].tt_isdst = 0; 777 sp->ttis[1].tt_abbrind = 0; 778 atp = sp->ats; 779 typep = sp->types; 780 janfirst = 0; 781 for (year = EPOCH_YEAR; year <= 2037; ++year) { 782 starttime = transtime(janfirst, year, &start, 783 stdoffset); 784 endtime = transtime(janfirst, year, &end, 785 dstoffset); 786 if (starttime > endtime) { 787 *atp++ = endtime; 788 *typep++ = 1; /* DST ends */ 789 *atp++ = starttime; 790 *typep++ = 0; /* DST begins */ 791 } else { 792 *atp++ = starttime; 793 *typep++ = 0; /* DST begins */ 794 *atp++ = endtime; 795 *typep++ = 1; /* DST ends */ 796 } 797 janfirst += year_lengths[isleap(year)] * 798 SECSPERDAY; 799 } 800 } else { 801 register long theirstdoffset; 802 register long theirdstoffset; 803 register long theiroffset; 804 register int isdst; 805 register int i; 806 register int j; 807 808 if (*name != '\0') 809 return -1; 810 if (load_result != 0) 811 return -1; 812 /* 813 ** Initial values of theirstdoffset and theirdstoffset. 814 */ 815 theirstdoffset = 0; 816 for (i = 0; i < sp->timecnt; ++i) { 817 j = sp->types[i]; 818 if (!sp->ttis[j].tt_isdst) { 819 theirstdoffset = 820 -sp->ttis[j].tt_gmtoff; 821 break; 822 } 823 } 824 theirdstoffset = 0; 825 for (i = 0; i < sp->timecnt; ++i) { 826 j = sp->types[i]; 827 if (sp->ttis[j].tt_isdst) { 828 theirdstoffset = 829 -sp->ttis[j].tt_gmtoff; 830 break; 831 } 832 } 833 /* 834 ** Initially we're assumed to be in standard time. 835 */ 836 isdst = FALSE; 837 theiroffset = theirstdoffset; 838 /* 839 ** Now juggle transition times and types 840 ** tracking offsets as you do. 841 */ 842 for (i = 0; i < sp->timecnt; ++i) { 843 j = sp->types[i]; 844 sp->types[i] = sp->ttis[j].tt_isdst; 845 if (sp->ttis[j].tt_ttisgmt) { 846 /* No adjustment to transition time */ 847 } else { 848 /* 849 ** If summer time is in effect, and the 850 ** transition time was not specified as 851 ** standard time, add the summer time 852 ** offset to the transition time; 853 ** otherwise, add the standard time 854 ** offset to the transition time. 855 */ 856 /* 857 ** Transitions from DST to DDST 858 ** will effectively disappear since 859 ** POSIX provides for only one DST 860 ** offset. 861 */ 862 if (isdst && !sp->ttis[j].tt_ttisstd) { 863 sp->ats[i] += dstoffset - 864 theirdstoffset; 865 } else { 866 sp->ats[i] += stdoffset - 867 theirstdoffset; 868 } 869 } 870 theiroffset = -sp->ttis[j].tt_gmtoff; 871 if (sp->ttis[j].tt_isdst) 872 theirdstoffset = theiroffset; 873 else theirstdoffset = theiroffset; 874 } 875 /* 876 ** Finally, fill in ttis. 877 ** ttisstd and ttisgmt need not be handled. 878 */ 879 sp->ttis[0].tt_gmtoff = -stdoffset; 880 sp->ttis[0].tt_isdst = FALSE; 881 sp->ttis[0].tt_abbrind = 0; 882 sp->ttis[1].tt_gmtoff = -dstoffset; 883 sp->ttis[1].tt_isdst = TRUE; 884 sp->ttis[1].tt_abbrind = stdlen + 1; 885 } 886 } else { 887 dstlen = 0; 888 sp->typecnt = 1; /* only standard time */ 889 sp->timecnt = 0; 890 sp->ttis[0].tt_gmtoff = -stdoffset; 891 sp->ttis[0].tt_isdst = 0; 892 sp->ttis[0].tt_abbrind = 0; 893 } 894 sp->charcnt = stdlen + 1; 895 if (dstlen != 0) 896 sp->charcnt += dstlen + 1; 897 if (sp->charcnt > sizeof sp->chars) 898 return -1; 899 cp = sp->chars; 900 (void) strncpy(cp, stdname, stdlen); 901 cp += stdlen; 902 *cp++ = '\0'; 903 if (dstlen != 0) { 904 (void) strncpy(cp, dstname, dstlen); 905 *(cp + dstlen) = '\0'; 906 } 907 return 0; 908} 909 910static void 911gmtload(sp) 912struct state * const sp; 913{ 914 if (tzload(gmt, sp) != 0) 915 (void) tzparse(gmt, sp, TRUE); 916} 917 918#ifndef STD_INSPIRED 919/* 920** A non-static declaration of tzsetwall in a system header file 921** may cause a warning about this upcoming static declaration... 922*/ 923static 924#endif /* !defined STD_INSPIRED */ 925#ifdef _THREAD_SAFE 926void 927tzsetwall_basic P((void)) 928#else 929void 930tzsetwall P((void)) 931#endif 932{ 933 if (lcl_is_set < 0) 934 return; 935 lcl_is_set = -1; 936 937#ifdef ALL_STATE 938 if (lclptr == NULL) { 939 lclptr = (struct state *) malloc(sizeof *lclptr); 940 if (lclptr == NULL) { 941 settzname(); /* all we can do */ 942 return; 943 } 944 } 945#endif /* defined ALL_STATE */ 946 if (tzload((char *) NULL, lclptr) != 0) 947 gmtload(lclptr); 948 settzname(); 949} 950 951#ifdef _THREAD_SAFE 952void 953tzsetwall P((void)) 954{ 955 pthread_mutex_lock(&lcl_mutex); 956 tzsetwall_basic(); 957 pthread_mutex_unlock(&lcl_mutex); 958} 959#endif 960 961#ifdef _THREAD_SAFE 962static void 963tzset_basic P((void)) 964#else 965void 966tzset P((void)) 967#endif 968{ 969 register const char * name; 970 971 name = getenv("TZ"); 972 if (name == NULL) { 973 tzsetwall(); 974 return; 975 } 976 977 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) 978 return; 979 lcl_is_set = (strlen(name) < sizeof(lcl_TZname)); 980 if (lcl_is_set) 981 (void) strcpy(lcl_TZname, name); 982 983#ifdef ALL_STATE 984 if (lclptr == NULL) { 985 lclptr = (struct state *) malloc(sizeof *lclptr); 986 if (lclptr == NULL) { 987 settzname(); /* all we can do */ 988 return; 989 } 990 } 991#endif /* defined ALL_STATE */ 992 if (*name == '\0') { 993 /* 994 ** User wants it fast rather than right. 995 */ 996 lclptr->leapcnt = 0; /* so, we're off a little */ 997 lclptr->timecnt = 0; 998 lclptr->ttis[0].tt_gmtoff = 0; 999 lclptr->ttis[0].tt_abbrind = 0; 1000 (void) strcpy(lclptr->chars, gmt); 1001 } else if (tzload(name, lclptr) != 0) 1002 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) 1003 (void) gmtload(lclptr); 1004 settzname(); 1005} 1006 1007#ifdef _THREAD_SAFE 1008void 1009tzset P((void)) 1010{ 1011 pthread_mutex_lock(&lcl_mutex); 1012 tzset_basic(); 1013 pthread_mutex_unlock(&lcl_mutex); 1014} 1015#endif 1016 1017/* 1018** The easy way to behave "as if no library function calls" localtime 1019** is to not call it--so we drop its guts into "localsub", which can be 1020** freely called. (And no, the PANS doesn't require the above behavior-- 1021** but it *is* desirable.) 1022** 1023** The unused offset argument is for the benefit of mktime variants. 1024*/ 1025 1026/*ARGSUSED*/ 1027static void 1028localsub(timep, offset, tmp) 1029const time_t * const timep; 1030const long offset; 1031struct tm * const tmp; 1032{ 1033 register struct state * sp; 1034 register const struct ttinfo * ttisp; 1035 register int i; 1036 const time_t t = *timep; 1037 1038 sp = lclptr; 1039#ifdef ALL_STATE 1040 if (sp == NULL) { 1041 gmtsub(timep, offset, tmp); 1042 return; 1043 } 1044#endif /* defined ALL_STATE */ 1045 if (sp->timecnt == 0 || t < sp->ats[0]) { 1046 i = 0; 1047 while (sp->ttis[i].tt_isdst) 1048 if (++i >= sp->typecnt) { 1049 i = 0; 1050 break; 1051 } 1052 } else { 1053 for (i = 1; i < sp->timecnt; ++i) 1054 if (t < sp->ats[i]) 1055 break; 1056 i = sp->types[i - 1]; 1057 } 1058 ttisp = &sp->ttis[i]; 1059 /* 1060 ** To get (wrong) behavior that's compatible with System V Release 2.0 1061 ** you'd replace the statement below with 1062 ** t += ttisp->tt_gmtoff; 1063 ** timesub(&t, 0L, sp, tmp); 1064 */ 1065 timesub(&t, ttisp->tt_gmtoff, sp, tmp); 1066 tmp->tm_isdst = ttisp->tt_isdst; 1067 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; 1068#ifdef TM_ZONE 1069 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; 1070#endif /* defined TM_ZONE */ 1071} 1072 1073#ifdef _THREAD_SAFE 1074int 1075localtime_r(timep, p_tm) 1076const time_t * const timep; 1077struct tm *p_tm; 1078{ 1079 pthread_mutex_lock(&lcl_mutex); 1080 tzset(); 1081 localsub(timep, 0L, p_tm); 1082 pthread_mutex_unlock(&lcl_mutex); 1083 return(0); 1084} 1085#endif 1086 1087struct tm * 1088localtime(timep) 1089const time_t * const timep; 1090{ 1091#ifdef _THREAD_SAFE 1092 static struct pthread_mutex _localtime_mutex = PTHREAD_MUTEX_INITIALIZER; 1093 static pthread_mutex_t localtime_mutex = &_localtime_mutex; 1094 static pthread_key_t localtime_key = -1; 1095 struct tm *p_tm; 1096 1097 pthread_mutex_lock(&localtime_mutex); 1098 if (localtime_key < 0) { 1099 if (pthread_keycreate(&localtime_key, free) < 0) { 1100 pthread_mutex_unlock(&localtime_mutex); 1101 return(NULL); 1102 } 1103 } 1104 pthread_mutex_unlock(&localtime_mutex); 1105 if (pthread_getspecific(localtime_key,(void **) &p_tm) != 0) { 1106 return(NULL); 1107 } else if (p_tm == NULL) { 1108 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) { 1109 return(NULL); 1110 } 1111 pthread_setspecific(localtime_key, p_tm); 1112 } 1113 pthread_mutex_lock(&lcl_mutex); 1114 tzset(); 1115 localsub(timep, 0L, p_tm); 1116 pthread_mutex_unlock(&lcl_mutex); 1117 return p_tm; 1118#else 1119 tzset(); 1120 localsub(timep, 0L, &tm); 1121 return &tm; 1122#endif 1123} 1124 1125/* 1126** gmtsub is to gmtime as localsub is to localtime. 1127*/ 1128 1129static void 1130gmtsub(timep, offset, tmp) 1131const time_t * const timep; 1132const long offset; 1133struct tm * const tmp; 1134{ 1135#ifdef _THREAD_SAFE 1136 pthread_mutex_lock(&gmt_mutex); 1137#endif 1138 if (!gmt_is_set) { 1139 gmt_is_set = TRUE; 1140#ifdef ALL_STATE 1141 gmtptr = (struct state *) malloc(sizeof *gmtptr); 1142 if (gmtptr != NULL) 1143#endif /* defined ALL_STATE */ 1144 gmtload(gmtptr); 1145 } 1146#ifdef _THREAD_SAFE 1147 pthread_mutex_unlock(&gmt_mutex); 1148#endif 1149 timesub(timep, offset, gmtptr, tmp); 1150#ifdef TM_ZONE 1151 /* 1152 ** Could get fancy here and deliver something such as 1153 ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero, 1154 ** but this is no time for a treasure hunt. 1155 */ 1156 if (offset != 0) 1157 tmp->TM_ZONE = wildabbr; 1158 else { 1159#ifdef ALL_STATE 1160 if (gmtptr == NULL) 1161 tmp->TM_ZONE = gmt; 1162 else tmp->TM_ZONE = gmtptr->chars; 1163#endif /* defined ALL_STATE */ 1164#ifndef ALL_STATE 1165 tmp->TM_ZONE = gmtptr->chars; 1166#endif /* State Farm */ 1167 } 1168#endif /* defined TM_ZONE */ 1169} 1170 1171struct tm * 1172gmtime(timep) 1173const time_t * const timep; 1174{ 1175#ifdef _THREAD_SAFE 1176 static struct pthread_mutex _gmtime_mutex = PTHREAD_MUTEX_INITIALIZER; 1177 static pthread_mutex_t gmtime_mutex = &_gmtime_mutex; 1178 static pthread_key_t gmtime_key = -1; 1179 struct tm *p_tm; 1180 1181 pthread_mutex_lock(&gmtime_mutex); 1182 if (gmtime_key < 0) { 1183 if (pthread_keycreate(&gmtime_key, free) < 0) { 1184 pthread_mutex_unlock(&gmtime_mutex); 1185 return(NULL); 1186 } 1187 } 1188 pthread_mutex_unlock(&gmtime_mutex); 1189 if (pthread_getspecific(gmtime_key,(void **) &p_tm) != 0) { 1190 return(NULL); 1191 } else if (p_tm == NULL) { 1192 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) { 1193 return(NULL); 1194 } 1195 pthread_setspecific(gmtime_key, p_tm); 1196 } 1197 gmtsub(timep, 0L, p_tm); 1198 return(p_tm); 1199#else 1200 gmtsub(timep, 0L, &tm); 1201 return &tm; 1202#endif 1203} 1204 1205#ifdef _THREAD_SAFE 1206int 1207gmtime_r(const time_t * timep, struct tm * tm) 1208{ 1209 gmtsub(timep, 0L, tm); 1210 return(0); 1211} 1212#endif 1213 1214#ifdef STD_INSPIRED 1215 1216struct tm * 1217offtime(timep, offset) 1218const time_t * const timep; 1219const long offset; 1220{ 1221 gmtsub(timep, offset, &tm); 1222 return &tm; 1223} 1224 1225#endif /* defined STD_INSPIRED */ 1226 1227static void 1228timesub(timep, offset, sp, tmp) 1229const time_t * const timep; 1230const long offset; 1231register const struct state * const sp; 1232register struct tm * const tmp; 1233{ 1234 register const struct lsinfo * lp; 1235 register long days; 1236 register long rem; 1237 register int y; 1238 register int yleap; 1239 register const int * ip; 1240 register long corr; 1241 register int hit; 1242 register int i; 1243 1244 corr = 0; 1245 hit = 0; 1246#ifdef ALL_STATE 1247 i = (sp == NULL) ? 0 : sp->leapcnt; 1248#endif /* defined ALL_STATE */ 1249#ifndef ALL_STATE 1250 i = sp->leapcnt; 1251#endif /* State Farm */ 1252 while (--i >= 0) { 1253 lp = &sp->lsis[i]; 1254 if (*timep >= lp->ls_trans) { 1255 if (*timep == lp->ls_trans) { 1256 hit = ((i == 0 && lp->ls_corr > 0) || 1257 lp->ls_corr > sp->lsis[i - 1].ls_corr); 1258 if (hit) 1259 while (i > 0 && 1260 sp->lsis[i].ls_trans == 1261 sp->lsis[i - 1].ls_trans + 1 && 1262 sp->lsis[i].ls_corr == 1263 sp->lsis[i - 1].ls_corr + 1) { 1264 ++hit; 1265 --i; 1266 } 1267 } 1268 corr = lp->ls_corr; 1269 break; 1270 } 1271 } 1272 days = *timep / SECSPERDAY; 1273 rem = *timep % SECSPERDAY; 1274#ifdef mc68k 1275 if (*timep == 0x80000000) { 1276 /* 1277 ** A 3B1 muffs the division on the most negative number. 1278 */ 1279 days = -24855; 1280 rem = -11648; 1281 } 1282#endif /* defined mc68k */ 1283 rem += (offset - corr); 1284 while (rem < 0) { 1285 rem += SECSPERDAY; 1286 --days; 1287 } 1288 while (rem >= SECSPERDAY) { 1289 rem -= SECSPERDAY; 1290 ++days; 1291 } 1292 tmp->tm_hour = (int) (rem / SECSPERHOUR); 1293 rem = rem % SECSPERHOUR; 1294 tmp->tm_min = (int) (rem / SECSPERMIN); 1295 /* 1296 ** A positive leap second requires a special 1297 ** representation. This uses "... ??:59:60" et seq. 1298 */ 1299 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; 1300 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK); 1301 if (tmp->tm_wday < 0) 1302 tmp->tm_wday += DAYSPERWEEK; 1303 y = EPOCH_YEAR; 1304#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) 1305 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) { 1306 register int newy; 1307 1308 newy = y + days / DAYSPERNYEAR; 1309 if (days < 0) 1310 --newy; 1311 days -= (newy - y) * DAYSPERNYEAR + 1312 LEAPS_THRU_END_OF(newy - 1) - 1313 LEAPS_THRU_END_OF(y - 1); 1314 y = newy; 1315 } 1316 tmp->tm_year = y - TM_YEAR_BASE; 1317 tmp->tm_yday = (int) days; 1318 ip = mon_lengths[yleap]; 1319 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon)) 1320 days = days - (long) ip[tmp->tm_mon]; 1321 tmp->tm_mday = (int) (days + 1); 1322 tmp->tm_isdst = 0; 1323#ifdef TM_GMTOFF 1324 tmp->TM_GMTOFF = offset; 1325#endif /* defined TM_GMTOFF */ 1326} 1327 1328char * 1329ctime(timep) 1330const time_t * const timep; 1331{ 1332/* 1333** Section 4.12.3.2 of X3.159-1989 requires that 1334** The ctime funciton converts the calendar time pointed to by timer 1335** to local time in the form of a string. It is equivalent to 1336** asctime(localtime(timer)) 1337*/ 1338 return asctime(localtime(timep)); 1339} 1340 1341/* 1342** Adapted from code provided by Robert Elz, who writes: 1343** The "best" way to do mktime I think is based on an idea of Bob 1344** Kridle's (so its said...) from a long time ago. 1345** [kridle@xinet.com as of 1996-01-16.] 1346** It does a binary search of the time_t space. Since time_t's are 1347** just 32 bits, its a max of 32 iterations (even at 64 bits it 1348** would still be very reasonable). 1349*/ 1350 1351#ifndef WRONG 1352#define WRONG (-1) 1353#endif /* !defined WRONG */ 1354 1355/* 1356** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com). 1357*/ 1358 1359static int 1360increment_overflow(number, delta) 1361int * number; 1362int delta; 1363{ 1364 int number0; 1365 1366 number0 = *number; 1367 *number += delta; 1368 return (*number < number0) != (delta < 0); 1369} 1370 1371static int 1372normalize_overflow(tensptr, unitsptr, base) 1373int * const tensptr; 1374int * const unitsptr; 1375const int base; 1376{ 1377 register int tensdelta; 1378 1379 tensdelta = (*unitsptr >= 0) ? 1380 (*unitsptr / base) : 1381 (-1 - (-1 - *unitsptr) / base); 1382 *unitsptr -= tensdelta * base; 1383 return increment_overflow(tensptr, tensdelta); 1384} 1385 1386static int 1387tmcomp(atmp, btmp) 1388register const struct tm * const atmp; 1389register const struct tm * const btmp; 1390{ 1391 register int result; 1392 1393 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && 1394 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && 1395 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && 1396 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && 1397 (result = (atmp->tm_min - btmp->tm_min)) == 0) 1398 result = atmp->tm_sec - btmp->tm_sec; 1399 return result; 1400} 1401 1402static time_t 1403time2(tmp, funcp, offset, okayp) 1404struct tm * const tmp; 1405void (* const funcp) P((const time_t*, long, struct tm*)); 1406const long offset; 1407int * const okayp; 1408{ 1409 register const struct state * sp; 1410 register int dir; 1411 register int bits; 1412 register int i, j ; 1413 register int saved_seconds; 1414 time_t newt; 1415 time_t t; 1416 struct tm yourtm, mytm; 1417 1418 *okayp = FALSE; 1419 yourtm = *tmp; 1420 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) 1421 return WRONG; 1422 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) 1423 return WRONG; 1424 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR)) 1425 return WRONG; 1426 /* 1427 ** Turn yourtm.tm_year into an actual year number for now. 1428 ** It is converted back to an offset from TM_YEAR_BASE later. 1429 */ 1430 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE)) 1431 return WRONG; 1432 while (yourtm.tm_mday <= 0) { 1433 if (increment_overflow(&yourtm.tm_year, -1)) 1434 return WRONG; 1435 i = yourtm.tm_year + (1 < yourtm.tm_mon); 1436 yourtm.tm_mday += year_lengths[isleap(i)]; 1437 } 1438 while (yourtm.tm_mday > DAYSPERLYEAR) { 1439 i = yourtm.tm_year + (1 < yourtm.tm_mon); 1440 yourtm.tm_mday -= year_lengths[isleap(i)]; 1441 if (increment_overflow(&yourtm.tm_year, 1)) 1442 return WRONG; 1443 } 1444 for ( ; ; ) { 1445 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon]; 1446 if (yourtm.tm_mday <= i) 1447 break; 1448 yourtm.tm_mday -= i; 1449 if (++yourtm.tm_mon >= MONSPERYEAR) { 1450 yourtm.tm_mon = 0; 1451 if (increment_overflow(&yourtm.tm_year, 1)) 1452 return WRONG; 1453 } 1454 } 1455 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE)) 1456 return WRONG; 1457 if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) { 1458 /* 1459 ** We can't set tm_sec to 0, because that might push the 1460 ** time below the minimum representable time. 1461 ** Set tm_sec to 59 instead. 1462 ** This assumes that the minimum representable time is 1463 ** not in the same minute that a leap second was deleted from, 1464 ** which is a safer assumption than using 58 would be. 1465 */ 1466 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) 1467 return WRONG; 1468 saved_seconds = yourtm.tm_sec; 1469 yourtm.tm_sec = SECSPERMIN - 1; 1470 } else { 1471 saved_seconds = yourtm.tm_sec; 1472 yourtm.tm_sec = 0; 1473 } 1474 /* 1475 ** Divide the search space in half 1476 ** (this works whether time_t is signed or unsigned). 1477 */ 1478 bits = TYPE_BIT(time_t) - 1; 1479 /* 1480 ** If time_t is signed, then 0 is just above the median, 1481 ** assuming two's complement arithmetic. 1482 ** If time_t is unsigned, then (1 << bits) is just above the median. 1483 */ 1484 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits); 1485 for ( ; ; ) { 1486 (*funcp)(&t, offset, &mytm); 1487 dir = tmcomp(&mytm, &yourtm); 1488 if (dir != 0) { 1489 if (bits-- < 0) 1490 return WRONG; 1491 if (bits < 0) 1492 --t; /* may be needed if new t is minimal */ 1493 else if (dir > 0) 1494 t -= ((time_t) 1) << bits; 1495 else t += ((time_t) 1) << bits; 1496 continue; 1497 } 1498 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) 1499 break; 1500 /* 1501 ** Right time, wrong type. 1502 ** Hunt for right time, right type. 1503 ** It's okay to guess wrong since the guess 1504 ** gets checked. 1505 */ 1506 /* 1507 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's. 1508 */ 1509 sp = (const struct state *) 1510 (((void *) funcp == (void *) localsub) ? 1511 lclptr : gmtptr); 1512#ifdef ALL_STATE 1513 if (sp == NULL) 1514 return WRONG; 1515#endif /* defined ALL_STATE */ 1516 for (i = sp->typecnt - 1; i >= 0; --i) { 1517 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) 1518 continue; 1519 for (j = sp->typecnt - 1; j >= 0; --j) { 1520 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) 1521 continue; 1522 newt = t + sp->ttis[j].tt_gmtoff - 1523 sp->ttis[i].tt_gmtoff; 1524 (*funcp)(&newt, offset, &mytm); 1525 if (tmcomp(&mytm, &yourtm) != 0) 1526 continue; 1527 if (mytm.tm_isdst != yourtm.tm_isdst) 1528 continue; 1529 /* 1530 ** We have a match. 1531 */ 1532 t = newt; 1533 goto label; 1534 } 1535 } 1536 return WRONG; 1537 } 1538label: 1539 newt = t + saved_seconds; 1540 if ((newt < t) != (saved_seconds < 0)) 1541 return WRONG; 1542 t = newt; 1543 (*funcp)(&t, offset, tmp); 1544 *okayp = TRUE; 1545 return t; 1546} 1547 1548static time_t 1549time1(tmp, funcp, offset) 1550struct tm * const tmp; 1551void (* const funcp) P((const time_t *, long, struct tm *)); 1552const long offset; 1553{ 1554 register time_t t; 1555 register const struct state * sp; 1556 register int samei, otheri; 1557 int okay; 1558 1559 if (tmp->tm_isdst > 1) 1560 tmp->tm_isdst = 1; 1561 t = time2(tmp, funcp, offset, &okay); 1562#ifdef PCTS 1563 /* 1564 ** PCTS code courtesy Grant Sullivan (grant@osf.org). 1565 */ 1566 if (okay) 1567 return t; 1568 if (tmp->tm_isdst < 0) 1569 tmp->tm_isdst = 0; /* reset to std and try again */ 1570#endif /* defined PCTS */ 1571#ifndef PCTS 1572 if (okay || tmp->tm_isdst < 0) 1573 return t; 1574#endif /* !defined PCTS */ 1575 /* 1576 ** We're supposed to assume that somebody took a time of one type 1577 ** and did some math on it that yielded a "struct tm" that's bad. 1578 ** We try to divine the type they started from and adjust to the 1579 ** type they need. 1580 */ 1581 /* 1582 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's. 1583 */ 1584 sp = (const struct state *) (((void *) funcp == (void *) localsub) ? 1585 lclptr : gmtptr); 1586#ifdef ALL_STATE 1587 if (sp == NULL) 1588 return WRONG; 1589#endif /* defined ALL_STATE */ 1590 for (samei = sp->typecnt - 1; samei >= 0; --samei) { 1591 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) 1592 continue; 1593 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) { 1594 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) 1595 continue; 1596 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - 1597 sp->ttis[samei].tt_gmtoff; 1598 tmp->tm_isdst = !tmp->tm_isdst; 1599 t = time2(tmp, funcp, offset, &okay); 1600 if (okay) 1601 return t; 1602 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - 1603 sp->ttis[samei].tt_gmtoff; 1604 tmp->tm_isdst = !tmp->tm_isdst; 1605 } 1606 } 1607 return WRONG; 1608} 1609 1610time_t 1611mktime(tmp) 1612struct tm * const tmp; 1613{ 1614 time_t mktime_return_value; 1615#ifdef _THREAD_SAFE 1616 pthread_mutex_lock(&lcl_mutex); 1617#endif 1618 tzset(); 1619 mktime_return_value = time1(tmp, localsub, 0L); 1620#ifdef _THREAD_SAFE 1621 pthread_mutex_unlock(&lcl_mutex); 1622#endif 1623 return(mktime_return_value); 1624} 1625 1626#ifdef STD_INSPIRED 1627 1628time_t 1629timelocal(tmp) 1630struct tm * const tmp; 1631{ 1632 tmp->tm_isdst = -1; /* in case it wasn't initialized */ 1633 return mktime(tmp); 1634} 1635 1636time_t 1637timegm(tmp) 1638struct tm * const tmp; 1639{ 1640 tmp->tm_isdst = 0; 1641 return time1(tmp, gmtsub, 0L); 1642} 1643 1644time_t 1645timeoff(tmp, offset) 1646struct tm * const tmp; 1647const long offset; 1648{ 1649 tmp->tm_isdst = 0; 1650 return time1(tmp, gmtsub, offset); 1651} 1652 1653#endif /* defined STD_INSPIRED */ 1654 1655#ifdef CMUCS 1656 1657/* 1658** The following is supplied for compatibility with 1659** previous versions of the CMUCS runtime library. 1660*/ 1661 1662long 1663gtime(tmp) 1664struct tm * const tmp; 1665{ 1666 const time_t t = mktime(tmp); 1667 1668 if (t == WRONG) 1669 return -1; 1670 return t; 1671} 1672 1673#endif /* defined CMUCS */ 1674 1675/* 1676** XXX--is the below the right way to conditionalize?? 1677*/ 1678 1679#ifdef STD_INSPIRED 1680 1681/* 1682** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 1683** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which 1684** is not the case if we are accounting for leap seconds. 1685** So, we provide the following conversion routines for use 1686** when exchanging timestamps with POSIX conforming systems. 1687*/ 1688 1689static long 1690leapcorr(timep) 1691time_t * timep; 1692{ 1693 register struct state * sp; 1694 register struct lsinfo * lp; 1695 register int i; 1696 1697 sp = lclptr; 1698 i = sp->leapcnt; 1699 while (--i >= 0) { 1700 lp = &sp->lsis[i]; 1701 if (*timep >= lp->ls_trans) 1702 return lp->ls_corr; 1703 } 1704 return 0; 1705} 1706 1707time_t 1708time2posix(t) 1709time_t t; 1710{ 1711 tzset(); 1712 return t - leapcorr(&t); 1713} 1714 1715time_t 1716posix2time(t) 1717time_t t; 1718{ 1719 time_t x; 1720 time_t y; 1721 1722 tzset(); 1723 /* 1724 ** For a positive leap second hit, the result 1725 ** is not unique. For a negative leap second 1726 ** hit, the corresponding time doesn't exist, 1727 ** so we return an adjacent second. 1728 */ 1729 x = t + leapcorr(&t); 1730 y = x - leapcorr(&x); 1731 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 } else if (y > t) { 1739 do { 1740 --x; 1741 y = x - leapcorr(&x); 1742 } while (y > t); 1743 if (t != y) 1744 return x + 1; 1745 } 1746 return x; 1747} 1748 1749#endif /* defined STD_INSPIRED */ 1750