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