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