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