1 /* 2 ** This file is in the public domain, so clarified as of 3 ** 1996-06-05 by Arthur David Olson. 4 */ 5 6 #include <sys/cdefs.h> 7 #ifndef lint 8 #ifndef NOID 9 static char elsieid[] __unused = "@(#)localtime.c 8.14"; 10 #endif /* !defined NOID */ 11 #endif /* !defined lint */ 12 __FBSDID("$FreeBSD$"); 13 14 /* 15 ** Leap second handling from Bradley White. 16 ** POSIX-style TZ environment variable handling from Guy Harris. 17 */ 18 19 /*LINTLIBRARY*/ 20 21 #include "namespace.h" 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <pthread.h> 27 #include "private.h" 28 #include "un-namespace.h" 29 30 #include "tzfile.h" 31 #include "float.h" /* for FLT_MAX and DBL_MAX */ 32 33 #ifndef TZ_ABBR_MAX_LEN 34 #define TZ_ABBR_MAX_LEN 16 35 #endif /* !defined TZ_ABBR_MAX_LEN */ 36 37 #ifndef TZ_ABBR_CHAR_SET 38 #define TZ_ABBR_CHAR_SET \ 39 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" 40 #endif /* !defined TZ_ABBR_CHAR_SET */ 41 42 #ifndef TZ_ABBR_ERR_CHAR 43 #define TZ_ABBR_ERR_CHAR '_' 44 #endif /* !defined TZ_ABBR_ERR_CHAR */ 45 46 #include "libc_private.h" 47 48 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x) 49 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x) 50 51 #define _RWLOCK_RDLOCK(x) \ 52 do { \ 53 if (__isthreaded) _pthread_rwlock_rdlock(x); \ 54 } while (0) 55 56 #define _RWLOCK_WRLOCK(x) \ 57 do { \ 58 if (__isthreaded) _pthread_rwlock_wrlock(x); \ 59 } while (0) 60 61 #define _RWLOCK_UNLOCK(x) \ 62 do { \ 63 if (__isthreaded) _pthread_rwlock_unlock(x); \ 64 } while (0) 65 66 /* 67 ** SunOS 4.1.1 headers lack O_BINARY. 68 */ 69 70 #ifdef O_BINARY 71 #define OPEN_MODE (O_RDONLY | O_BINARY) 72 #endif /* defined O_BINARY */ 73 #ifndef O_BINARY 74 #define OPEN_MODE O_RDONLY 75 #endif /* !defined O_BINARY */ 76 77 #ifndef WILDABBR 78 /* 79 ** Someone might make incorrect use of a time zone abbreviation: 80 ** 1. They might reference tzname[0] before calling tzset (explicitly 81 ** or implicitly). 82 ** 2. They might reference tzname[1] before calling tzset (explicitly 83 ** or implicitly). 84 ** 3. They might reference tzname[1] after setting to a time zone 85 ** in which Daylight Saving Time is never observed. 86 ** 4. They might reference tzname[0] after setting to a time zone 87 ** in which Standard Time is never observed. 88 ** 5. They might reference tm.TM_ZONE after calling offtime. 89 ** What's best to do in the above cases is open to debate; 90 ** for now, we just set things up so that in any of the five cases 91 ** WILDABBR is used. Another possibility: initialize tzname[0] to the 92 ** string "tzname[0] used before set", and similarly for the other cases. 93 ** And another: initialize tzname[0] to "ERA", with an explanation in the 94 ** manual page of what this "time zone abbreviation" means (doing this so 95 ** that tzname[0] has the "normal" length of three characters). 96 */ 97 #define WILDABBR " " 98 #endif /* !defined WILDABBR */ 99 100 static char wildabbr[] = WILDABBR; 101 102 /* 103 * In June 2004 it was decided UTC was a more appropriate default time 104 * zone than GMT. 105 */ 106 107 static const char gmt[] = "UTC"; 108 109 /* 110 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES. 111 ** We default to US rules as of 1999-08-17. 112 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are 113 ** implementation dependent; for historical reasons, US rules are a 114 ** common default. 115 */ 116 #ifndef TZDEFRULESTRING 117 #define TZDEFRULESTRING ",M4.1.0,M10.5.0" 118 #endif /* !defined TZDEFDST */ 119 120 struct ttinfo { /* time type information */ 121 long tt_gmtoff; /* UTC offset in seconds */ 122 int tt_isdst; /* used to set tm_isdst */ 123 int tt_abbrind; /* abbreviation list index */ 124 int tt_ttisstd; /* TRUE if transition is std time */ 125 int tt_ttisgmt; /* TRUE if transition is UTC */ 126 }; 127 128 struct lsinfo { /* leap second information */ 129 time_t ls_trans; /* transition time */ 130 long ls_corr; /* correction to apply */ 131 }; 132 133 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) 134 135 #ifdef TZNAME_MAX 136 #define MY_TZNAME_MAX TZNAME_MAX 137 #endif /* defined TZNAME_MAX */ 138 #ifndef TZNAME_MAX 139 #define MY_TZNAME_MAX 255 140 #endif /* !defined TZNAME_MAX */ 141 142 struct state { 143 int leapcnt; 144 int timecnt; 145 int typecnt; 146 int charcnt; 147 int goback; 148 int goahead; 149 time_t ats[TZ_MAX_TIMES]; 150 unsigned char types[TZ_MAX_TIMES]; 151 struct ttinfo ttis[TZ_MAX_TYPES]; 152 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), 153 (2 * (MY_TZNAME_MAX + 1)))]; 154 struct lsinfo lsis[TZ_MAX_LEAPS]; 155 }; 156 157 struct rule { 158 int r_type; /* type of rule--see below */ 159 int r_day; /* day number of rule */ 160 int r_week; /* week number of rule */ 161 int r_mon; /* month number of rule */ 162 long r_time; /* transition time of rule */ 163 }; 164 165 #define JULIAN_DAY 0 /* Jn - Julian day */ 166 #define DAY_OF_YEAR 1 /* n - day of year */ 167 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ 168 169 /* 170 ** Prototypes for static functions. 171 */ 172 173 static long detzcode(const char * codep); 174 static time_t detzcode64(const char * codep); 175 static int differ_by_repeat(time_t t1, time_t t0); 176 static const char * getzname(const char * strp) ATTRIBUTE_PURE; 177 static const char * getqzname(const char * strp, const int delim) 178 ATTRIBUTE_PURE; 179 static const char * getnum(const char * strp, int * nump, int min, 180 int max); 181 static const char * getsecs(const char * strp, long * secsp); 182 static const char * getoffset(const char * strp, long * offsetp); 183 static const char * getrule(const char * strp, struct rule * rulep); 184 static void gmtload(struct state * sp); 185 static struct tm * gmtsub(const time_t * timep, long offset, 186 struct tm * tmp); 187 static struct tm * localsub(const time_t * timep, long offset, 188 struct tm * tmp); 189 static int increment_overflow(int * number, int delta); 190 static int leaps_thru_end_of(int y) ATTRIBUTE_PURE; 191 static int long_increment_overflow(long * number, int delta); 192 static int long_normalize_overflow(long * tensptr, 193 int * unitsptr, int base); 194 static int normalize_overflow(int * tensptr, int * unitsptr, 195 int base); 196 static void settzname(void); 197 static time_t time1(struct tm * tmp, 198 struct tm * (*funcp)(const time_t *, 199 long, struct tm *), 200 long offset); 201 static time_t time2(struct tm *tmp, 202 struct tm * (*funcp)(const time_t *, 203 long, struct tm*), 204 long offset, int * okayp); 205 static time_t time2sub(struct tm *tmp, 206 struct tm * (*funcp)(const time_t *, 207 long, struct tm*), 208 long offset, int * okayp, int do_norm_secs); 209 static struct tm * timesub(const time_t * timep, long offset, 210 const struct state * sp, struct tm * tmp); 211 static int tmcomp(const struct tm * atmp, 212 const struct tm * btmp); 213 static time_t transtime(time_t janfirst, int year, 214 const struct rule * rulep, long offset) 215 ATTRIBUTE_PURE; 216 static int typesequiv(const struct state * sp, int a, int b); 217 static int tzload(const char * name, struct state * sp, 218 int doextend); 219 static int tzparse(const char * name, struct state * sp, 220 int lastditch); 221 222 #ifdef ALL_STATE 223 static struct state * lclptr; 224 static struct state * gmtptr; 225 #endif /* defined ALL_STATE */ 226 227 #ifndef ALL_STATE 228 static struct state lclmem; 229 static struct state gmtmem; 230 #define lclptr (&lclmem) 231 #define gmtptr (&gmtmem) 232 #endif /* State Farm */ 233 234 #ifndef TZ_STRLEN_MAX 235 #define TZ_STRLEN_MAX 255 236 #endif /* !defined TZ_STRLEN_MAX */ 237 238 static char lcl_TZname[TZ_STRLEN_MAX + 1]; 239 static int lcl_is_set; 240 static pthread_once_t gmt_once = PTHREAD_ONCE_INIT; 241 static pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER; 242 static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT; 243 static pthread_key_t gmtime_key; 244 static int gmtime_key_error; 245 static pthread_once_t localtime_once = PTHREAD_ONCE_INIT; 246 static pthread_key_t localtime_key; 247 static int localtime_key_error; 248 249 char * tzname[2] = { 250 wildabbr, 251 wildabbr 252 }; 253 254 /* 255 ** Section 4.12.3 of X3.159-1989 requires that 256 ** Except for the strftime function, these functions [asctime, 257 ** ctime, gmtime, localtime] return values in one of two static 258 ** objects: a broken-down time structure and an array of char. 259 ** Thanks to Paul Eggert for noting this. 260 */ 261 262 static struct tm tm; 263 264 #ifdef USG_COMPAT 265 time_t timezone = 0; 266 int daylight = 0; 267 #endif /* defined USG_COMPAT */ 268 269 #ifdef ALTZONE 270 time_t altzone = 0; 271 #endif /* defined ALTZONE */ 272 273 static long 274 detzcode(const char *const codep) 275 { 276 long result; 277 int i; 278 279 result = (codep[0] & 0x80) ? ~0L : 0; 280 for (i = 0; i < 4; ++i) 281 result = (result << 8) | (codep[i] & 0xff); 282 return result; 283 } 284 285 static time_t 286 detzcode64(const char *const codep) 287 { 288 register time_t result; 289 register int i; 290 291 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0; 292 for (i = 0; i < 8; ++i) 293 result = result * 256 + (codep[i] & 0xff); 294 return result; 295 } 296 297 static void 298 settzname(void) 299 { 300 struct state * sp = lclptr; 301 int i; 302 303 tzname[0] = wildabbr; 304 tzname[1] = wildabbr; 305 #ifdef USG_COMPAT 306 daylight = 0; 307 timezone = 0; 308 #endif /* defined USG_COMPAT */ 309 #ifdef ALTZONE 310 altzone = 0; 311 #endif /* defined ALTZONE */ 312 #ifdef ALL_STATE 313 if (sp == NULL) { 314 tzname[0] = tzname[1] = gmt; 315 return; 316 } 317 #endif /* defined ALL_STATE */ 318 /* 319 ** And to get the latest zone names into tzname. . . 320 */ 321 for (i = 0; i < sp->typecnt; ++i) { 322 const struct ttinfo * const ttisp = &sp->ttis[sp->types[i]]; 323 324 tzname[ttisp->tt_isdst] = 325 &sp->chars[ttisp->tt_abbrind]; 326 #ifdef USG_COMPAT 327 if (ttisp->tt_isdst) 328 daylight = 1; 329 if (!ttisp->tt_isdst) 330 timezone = -(ttisp->tt_gmtoff); 331 #endif /* defined USG_COMPAT */ 332 #ifdef ALTZONE 333 if (ttisp->tt_isdst) 334 altzone = -(ttisp->tt_gmtoff); 335 #endif /* defined ALTZONE */ 336 } 337 /* 338 ** Finally, scrub the abbreviations. 339 ** First, replace bogus characters. 340 */ 341 for (i = 0; i < sp->charcnt; ++i) 342 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) 343 sp->chars[i] = TZ_ABBR_ERR_CHAR; 344 /* 345 ** Second, truncate long abbreviations. 346 */ 347 for (i = 0; i < sp->typecnt; ++i) { 348 register const struct ttinfo * const ttisp = &sp->ttis[i]; 349 register char * cp = &sp->chars[ttisp->tt_abbrind]; 350 351 if (strlen(cp) > TZ_ABBR_MAX_LEN && 352 strcmp(cp, GRANDPARENTED) != 0) 353 *(cp + TZ_ABBR_MAX_LEN) = '\0'; 354 } 355 } 356 357 static int 358 differ_by_repeat(const time_t t1, const time_t t0) 359 { 360 int_fast64_t _t0 = t0; 361 int_fast64_t _t1 = t1; 362 363 if (TYPE_INTEGRAL(time_t) && 364 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) 365 return 0; 366 //turn ((int_fast64_t)(t1 - t0) == SECSPERREPEAT); 367 return _t1 - _t0 == SECSPERREPEAT; 368 } 369 370 static int 371 tzload(name, sp, doextend) 372 const char * name; 373 struct state * const sp; 374 register const int doextend; 375 { 376 const char * p; 377 int i; 378 int fid; 379 int stored; 380 int nread; 381 int res; 382 union { 383 struct tzhead tzhead; 384 char buf[2 * sizeof(struct tzhead) + 385 2 * sizeof *sp + 386 4 * TZ_MAX_TIMES]; 387 } *u; 388 389 u = NULL; 390 res = -1; 391 sp->goback = sp->goahead = FALSE; 392 393 if (name != NULL && issetugid() != 0) 394 if ((name[0] == ':' && name[1] == '/') || 395 name[0] == '/' || strchr(name, '.')) 396 name = NULL; 397 if (name == NULL && (name = TZDEFAULT) == NULL) 398 return -1; 399 { 400 struct stat stab; 401 /* 402 ** Section 4.9.1 of the C standard says that 403 ** "FILENAME_MAX expands to an integral constant expression 404 ** that is the size needed for an array of char large enough 405 ** to hold the longest file name string that the implementation 406 ** guarantees can be opened." 407 */ 408 char *fullname; 409 410 fullname = malloc(FILENAME_MAX + 1); 411 if (fullname == NULL) 412 goto out; 413 414 if (name[0] == ':') 415 ++name; 416 if (name[0] != '/') { 417 if ((p = TZDIR) == NULL) { 418 free(fullname); 419 return -1; 420 } 421 if (strlen(p) + 1 + strlen(name) >= FILENAME_MAX) { 422 free(fullname); 423 return -1; 424 } 425 (void) strcpy(fullname, p); 426 (void) strcat(fullname, "/"); 427 (void) strcat(fullname, name); 428 name = fullname; 429 } 430 if ((fid = _open(name, OPEN_MODE)) == -1) { 431 free(fullname); 432 return -1; 433 } 434 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) { 435 free(fullname); 436 _close(fid); 437 return -1; 438 } 439 free(fullname); 440 } 441 u = malloc(sizeof(*u)); 442 if (u == NULL) 443 goto out; 444 nread = _read(fid, u->buf, sizeof u->buf); 445 if (_close(fid) < 0 || nread <= 0) 446 goto out; 447 for (stored = 4; stored <= 8; stored *= 2) { 448 int ttisstdcnt; 449 int ttisgmtcnt; 450 451 ttisstdcnt = (int) detzcode(u->tzhead.tzh_ttisstdcnt); 452 ttisgmtcnt = (int) detzcode(u->tzhead.tzh_ttisgmtcnt); 453 sp->leapcnt = (int) detzcode(u->tzhead.tzh_leapcnt); 454 sp->timecnt = (int) detzcode(u->tzhead.tzh_timecnt); 455 sp->typecnt = (int) detzcode(u->tzhead.tzh_typecnt); 456 sp->charcnt = (int) detzcode(u->tzhead.tzh_charcnt); 457 p = u->tzhead.tzh_charcnt + sizeof u->tzhead.tzh_charcnt; 458 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 459 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 460 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 461 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 462 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || 463 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) 464 goto out; 465 if (nread - (p - u->buf) < 466 sp->timecnt * stored + /* ats */ 467 sp->timecnt + /* types */ 468 sp->typecnt * 6 + /* ttinfos */ 469 sp->charcnt + /* chars */ 470 sp->leapcnt * (stored + 4) + /* lsinfos */ 471 ttisstdcnt + /* ttisstds */ 472 ttisgmtcnt) /* ttisgmts */ 473 goto out; 474 for (i = 0; i < sp->timecnt; ++i) { 475 sp->ats[i] = (stored == 4) ? 476 detzcode(p) : detzcode64(p); 477 p += stored; 478 } 479 for (i = 0; i < sp->timecnt; ++i) { 480 sp->types[i] = (unsigned char) *p++; 481 if (sp->types[i] >= sp->typecnt) 482 goto out; 483 } 484 for (i = 0; i < sp->typecnt; ++i) { 485 struct ttinfo * ttisp; 486 487 ttisp = &sp->ttis[i]; 488 ttisp->tt_gmtoff = detzcode(p); 489 p += 4; 490 ttisp->tt_isdst = (unsigned char) *p++; 491 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) 492 goto out; 493 ttisp->tt_abbrind = (unsigned char) *p++; 494 if (ttisp->tt_abbrind < 0 || 495 ttisp->tt_abbrind > sp->charcnt) 496 goto out; 497 } 498 for (i = 0; i < sp->charcnt; ++i) 499 sp->chars[i] = *p++; 500 sp->chars[i] = '\0'; /* ensure '\0' at end */ 501 for (i = 0; i < sp->leapcnt; ++i) { 502 struct lsinfo * lsisp; 503 504 lsisp = &sp->lsis[i]; 505 lsisp->ls_trans = (stored == 4) ? 506 detzcode(p) : detzcode64(p); 507 p += stored; 508 lsisp->ls_corr = detzcode(p); 509 p += 4; 510 } 511 for (i = 0; i < sp->typecnt; ++i) { 512 struct ttinfo * ttisp; 513 514 ttisp = &sp->ttis[i]; 515 if (ttisstdcnt == 0) 516 ttisp->tt_ttisstd = FALSE; 517 else { 518 ttisp->tt_ttisstd = *p++; 519 if (ttisp->tt_ttisstd != TRUE && 520 ttisp->tt_ttisstd != FALSE) 521 goto out; 522 } 523 } 524 for (i = 0; i < sp->typecnt; ++i) { 525 struct ttinfo * ttisp; 526 527 ttisp = &sp->ttis[i]; 528 if (ttisgmtcnt == 0) 529 ttisp->tt_ttisgmt = FALSE; 530 else { 531 ttisp->tt_ttisgmt = *p++; 532 if (ttisp->tt_ttisgmt != TRUE && 533 ttisp->tt_ttisgmt != FALSE) 534 goto out; 535 } 536 } 537 /* 538 ** Out-of-sort ats should mean we're running on a 539 ** signed time_t system but using a data file with 540 ** unsigned values (or vice versa). 541 */ 542 for (i = 0; i < sp->timecnt - 2; ++i) 543 if (sp->ats[i] > sp->ats[i + 1]) { 544 ++i; 545 if (TYPE_SIGNED(time_t)) { 546 /* 547 ** Ignore the end (easy). 548 */ 549 sp->timecnt = i; 550 } else { 551 /* 552 ** Ignore the beginning (harder). 553 */ 554 register int j; 555 556 for (j = 0; j + i < sp->timecnt; ++j) { 557 sp->ats[j] = sp->ats[j + i]; 558 sp->types[j] = sp->types[j + i]; 559 } 560 sp->timecnt = j; 561 } 562 break; 563 } 564 /* 565 ** If this is an old file, we're done. 566 */ 567 if (u->tzhead.tzh_version[0] == '\0') 568 break; 569 nread -= p - u->buf; 570 for (i = 0; i < nread; ++i) 571 u->buf[i] = p[i]; 572 /* 573 ** If this is a narrow integer time_t system, we're done. 574 */ 575 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t)) 576 break; 577 } 578 if (doextend && nread > 2 && 579 u->buf[0] == '\n' && u->buf[nread - 1] == '\n' && 580 sp->typecnt + 2 <= TZ_MAX_TYPES) { 581 struct state *ts; 582 register int result; 583 584 ts = malloc(sizeof(*ts)); 585 if (ts == NULL) 586 goto out; 587 u->buf[nread - 1] = '\0'; 588 result = tzparse(&u->buf[1], ts, FALSE); 589 if (result == 0 && ts->typecnt == 2 && 590 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) { 591 for (i = 0; i < 2; ++i) 592 ts->ttis[i].tt_abbrind += 593 sp->charcnt; 594 for (i = 0; i < ts->charcnt; ++i) 595 sp->chars[sp->charcnt++] = 596 ts->chars[i]; 597 i = 0; 598 while (i < ts->timecnt && 599 ts->ats[i] <= 600 sp->ats[sp->timecnt - 1]) 601 ++i; 602 while (i < ts->timecnt && 603 sp->timecnt < TZ_MAX_TIMES) { 604 sp->ats[sp->timecnt] = 605 ts->ats[i]; 606 sp->types[sp->timecnt] = 607 sp->typecnt + 608 ts->types[i]; 609 ++sp->timecnt; 610 ++i; 611 } 612 sp->ttis[sp->typecnt++] = ts->ttis[0]; 613 sp->ttis[sp->typecnt++] = ts->ttis[1]; 614 } 615 free(ts); 616 } 617 if (sp->timecnt > 1) { 618 for (i = 1; i < sp->timecnt; ++i) 619 if (typesequiv(sp, sp->types[i], sp->types[0]) && 620 differ_by_repeat(sp->ats[i], sp->ats[0])) { 621 sp->goback = TRUE; 622 break; 623 } 624 for (i = sp->timecnt - 2; i >= 0; --i) 625 if (typesequiv(sp, sp->types[sp->timecnt - 1], 626 sp->types[i]) && 627 differ_by_repeat(sp->ats[sp->timecnt - 1], 628 sp->ats[i])) { 629 sp->goahead = TRUE; 630 break; 631 } 632 } 633 res = 0; 634 out: 635 free(u); 636 return (res); 637 } 638 639 static int 640 typesequiv(sp, a, b) 641 const struct state * const sp; 642 const int a; 643 const int b; 644 { 645 register int result; 646 647 if (sp == NULL || 648 a < 0 || a >= sp->typecnt || 649 b < 0 || b >= sp->typecnt) 650 result = FALSE; 651 else { 652 register const struct ttinfo * ap = &sp->ttis[a]; 653 register const struct ttinfo * bp = &sp->ttis[b]; 654 result = ap->tt_gmtoff == bp->tt_gmtoff && 655 ap->tt_isdst == bp->tt_isdst && 656 ap->tt_ttisstd == bp->tt_ttisstd && 657 ap->tt_ttisgmt == bp->tt_ttisgmt && 658 strcmp(&sp->chars[ap->tt_abbrind], 659 &sp->chars[bp->tt_abbrind]) == 0; 660 } 661 return result; 662 } 663 664 static const int mon_lengths[2][MONSPERYEAR] = { 665 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 666 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } 667 }; 668 669 static const int year_lengths[2] = { 670 DAYSPERNYEAR, DAYSPERLYEAR 671 }; 672 673 /* 674 ** Given a pointer into a time zone string, scan until a character that is not 675 ** a valid character in a zone name is found. Return a pointer to that 676 ** character. 677 */ 678 679 static const char * 680 getzname(strp) 681 const char * strp; 682 { 683 char c; 684 685 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && 686 c != '+') 687 ++strp; 688 return strp; 689 } 690 691 /* 692 ** Given a pointer into an extended time zone string, scan until the ending 693 ** delimiter of the zone name is located. Return a pointer to the delimiter. 694 ** 695 ** As with getzname above, the legal character set is actually quite 696 ** restricted, with other characters producing undefined results. 697 ** We don't do any checking here; checking is done later in common-case code. 698 */ 699 700 static const char * 701 getqzname(register const char *strp, const int delim) 702 { 703 register int c; 704 705 while ((c = *strp) != '\0' && c != delim) 706 ++strp; 707 return strp; 708 } 709 710 /* 711 ** Given a pointer into a time zone string, extract a number from that string. 712 ** Check that the number is within a specified range; if it is not, return 713 ** NULL. 714 ** Otherwise, return a pointer to the first character not part of the number. 715 */ 716 717 static const char * 718 getnum(strp, nump, min, max) 719 const char * strp; 720 int * const nump; 721 const int min; 722 const int max; 723 { 724 char c; 725 int num; 726 727 if (strp == NULL || !is_digit(c = *strp)) 728 return NULL; 729 num = 0; 730 do { 731 num = num * 10 + (c - '0'); 732 if (num > max) 733 return NULL; /* illegal value */ 734 c = *++strp; 735 } while (is_digit(c)); 736 if (num < min) 737 return NULL; /* illegal value */ 738 *nump = num; 739 return strp; 740 } 741 742 /* 743 ** Given a pointer into a time zone string, extract a number of seconds, 744 ** in hh[:mm[:ss]] form, from the string. 745 ** If any error occurs, return NULL. 746 ** Otherwise, return a pointer to the first character not part of the number 747 ** of seconds. 748 */ 749 750 static const char * 751 getsecs(strp, secsp) 752 const char * strp; 753 long * const secsp; 754 { 755 int num; 756 757 /* 758 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like 759 ** "M10.4.6/26", which does not conform to Posix, 760 ** but which specifies the equivalent of 761 ** ``02:00 on the first Sunday on or after 23 Oct''. 762 */ 763 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); 764 if (strp == NULL) 765 return NULL; 766 *secsp = num * (long) SECSPERHOUR; 767 if (*strp == ':') { 768 ++strp; 769 strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 770 if (strp == NULL) 771 return NULL; 772 *secsp += num * SECSPERMIN; 773 if (*strp == ':') { 774 ++strp; 775 /* `SECSPERMIN' allows for leap seconds. */ 776 strp = getnum(strp, &num, 0, SECSPERMIN); 777 if (strp == NULL) 778 return NULL; 779 *secsp += num; 780 } 781 } 782 return strp; 783 } 784 785 /* 786 ** Given a pointer into a time zone string, extract an offset, in 787 ** [+-]hh[:mm[:ss]] form, from the string. 788 ** If any error occurs, return NULL. 789 ** Otherwise, return a pointer to the first character not part of the time. 790 */ 791 792 static const char * 793 getoffset(strp, offsetp) 794 const char * strp; 795 long * const offsetp; 796 { 797 int neg = 0; 798 799 if (*strp == '-') { 800 neg = 1; 801 ++strp; 802 } else if (*strp == '+') 803 ++strp; 804 strp = getsecs(strp, offsetp); 805 if (strp == NULL) 806 return NULL; /* illegal time */ 807 if (neg) 808 *offsetp = -*offsetp; 809 return strp; 810 } 811 812 /* 813 ** Given a pointer into a time zone string, extract a rule in the form 814 ** date[/time]. See POSIX section 8 for the format of "date" and "time". 815 ** If a valid rule is not found, return NULL. 816 ** Otherwise, return a pointer to the first character not part of the rule. 817 */ 818 819 static const char * 820 getrule(strp, rulep) 821 const char * strp; 822 struct rule * const rulep; 823 { 824 if (*strp == 'J') { 825 /* 826 ** Julian day. 827 */ 828 rulep->r_type = JULIAN_DAY; 829 ++strp; 830 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 831 } else if (*strp == 'M') { 832 /* 833 ** Month, week, day. 834 */ 835 rulep->r_type = MONTH_NTH_DAY_OF_WEEK; 836 ++strp; 837 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 838 if (strp == NULL) 839 return NULL; 840 if (*strp++ != '.') 841 return NULL; 842 strp = getnum(strp, &rulep->r_week, 1, 5); 843 if (strp == NULL) 844 return NULL; 845 if (*strp++ != '.') 846 return NULL; 847 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 848 } else if (is_digit(*strp)) { 849 /* 850 ** Day of year. 851 */ 852 rulep->r_type = DAY_OF_YEAR; 853 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 854 } else return NULL; /* invalid format */ 855 if (strp == NULL) 856 return NULL; 857 if (*strp == '/') { 858 /* 859 ** Time specified. 860 */ 861 ++strp; 862 strp = getsecs(strp, &rulep->r_time); 863 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 864 return strp; 865 } 866 867 /* 868 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the 869 ** year, a rule, and the offset from UTC at the time that rule takes effect, 870 ** calculate the Epoch-relative time that rule takes effect. 871 */ 872 873 static time_t 874 transtime(janfirst, year, rulep, offset) 875 const time_t janfirst; 876 const int year; 877 const struct rule * const rulep; 878 const long offset; 879 { 880 int leapyear; 881 time_t value; 882 int i; 883 int d, m1, yy0, yy1, yy2, dow; 884 885 INITIALIZE(value); 886 leapyear = isleap(year); 887 switch (rulep->r_type) { 888 889 case JULIAN_DAY: 890 /* 891 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap 892 ** years. 893 ** In non-leap years, or if the day number is 59 or less, just 894 ** add SECSPERDAY times the day number-1 to the time of 895 ** January 1, midnight, to get the day. 896 */ 897 value = janfirst + (rulep->r_day - 1) * SECSPERDAY; 898 if (leapyear && rulep->r_day >= 60) 899 value += SECSPERDAY; 900 break; 901 902 case DAY_OF_YEAR: 903 /* 904 ** n - day of year. 905 ** Just add SECSPERDAY times the day number to the time of 906 ** January 1, midnight, to get the day. 907 */ 908 value = janfirst + rulep->r_day * SECSPERDAY; 909 break; 910 911 case MONTH_NTH_DAY_OF_WEEK: 912 /* 913 ** Mm.n.d - nth "dth day" of month m. 914 */ 915 value = janfirst; 916 for (i = 0; i < rulep->r_mon - 1; ++i) 917 value += mon_lengths[leapyear][i] * SECSPERDAY; 918 919 /* 920 ** Use Zeller's Congruence to get day-of-week of first day of 921 ** month. 922 */ 923 m1 = (rulep->r_mon + 9) % 12 + 1; 924 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 925 yy1 = yy0 / 100; 926 yy2 = yy0 % 100; 927 dow = ((26 * m1 - 2) / 10 + 928 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 929 if (dow < 0) 930 dow += DAYSPERWEEK; 931 932 /* 933 ** "dow" is the day-of-week of the first day of the month. Get 934 ** the day-of-month (zero-origin) of the first "dow" day of the 935 ** month. 936 */ 937 d = rulep->r_day - dow; 938 if (d < 0) 939 d += DAYSPERWEEK; 940 for (i = 1; i < rulep->r_week; ++i) { 941 if (d + DAYSPERWEEK >= 942 mon_lengths[leapyear][rulep->r_mon - 1]) 943 break; 944 d += DAYSPERWEEK; 945 } 946 947 /* 948 ** "d" is the day-of-month (zero-origin) of the day we want. 949 */ 950 value += d * SECSPERDAY; 951 break; 952 } 953 954 /* 955 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in 956 ** question. To get the Epoch-relative time of the specified local 957 ** time on that day, add the transition time and the current offset 958 ** from UTC. 959 */ 960 return value + rulep->r_time + offset; 961 } 962 963 /* 964 ** Given a POSIX section 8-style TZ string, fill in the rule tables as 965 ** appropriate. 966 */ 967 968 static int 969 tzparse(name, sp, lastditch) 970 const char * name; 971 struct state * const sp; 972 const int lastditch; 973 { 974 const char * stdname; 975 const char * dstname; 976 size_t stdlen; 977 size_t dstlen; 978 long stdoffset; 979 long dstoffset; 980 time_t * atp; 981 unsigned char * typep; 982 char * cp; 983 int load_result; 984 985 INITIALIZE(dstname); 986 stdname = name; 987 if (lastditch) { 988 stdlen = strlen(name); /* length of standard zone name */ 989 name += stdlen; 990 if (stdlen >= sizeof sp->chars) 991 stdlen = (sizeof sp->chars) - 1; 992 stdoffset = 0; 993 } else { 994 if (*name == '<') { 995 name++; 996 stdname = name; 997 name = getqzname(name, '>'); 998 if (*name != '>') 999 return (-1); 1000 stdlen = name - stdname; 1001 name++; 1002 } else { 1003 name = getzname(name); 1004 stdlen = name - stdname; 1005 } 1006 if (*name == '\0') 1007 return -1; /* was "stdoffset = 0;" */ 1008 else { 1009 name = getoffset(name, &stdoffset); 1010 if (name == NULL) 1011 return -1; 1012 } 1013 } 1014 load_result = tzload(TZDEFRULES, sp, FALSE); 1015 if (load_result != 0) 1016 sp->leapcnt = 0; /* so, we're off a little */ 1017 if (*name != '\0') { 1018 if (*name == '<') { 1019 dstname = ++name; 1020 name = getqzname(name, '>'); 1021 if (*name != '>') 1022 return -1; 1023 dstlen = name - dstname; 1024 name++; 1025 } else { 1026 dstname = name; 1027 name = getzname(name); 1028 dstlen = name - dstname; /* length of DST zone name */ 1029 } 1030 if (*name != '\0' && *name != ',' && *name != ';') { 1031 name = getoffset(name, &dstoffset); 1032 if (name == NULL) 1033 return -1; 1034 } else dstoffset = stdoffset - SECSPERHOUR; 1035 if (*name == '\0' && load_result != 0) 1036 name = TZDEFRULESTRING; 1037 if (*name == ',' || *name == ';') { 1038 struct rule start; 1039 struct rule end; 1040 int year; 1041 time_t janfirst; 1042 time_t starttime; 1043 time_t endtime; 1044 1045 ++name; 1046 if ((name = getrule(name, &start)) == NULL) 1047 return -1; 1048 if (*name++ != ',') 1049 return -1; 1050 if ((name = getrule(name, &end)) == NULL) 1051 return -1; 1052 if (*name != '\0') 1053 return -1; 1054 sp->typecnt = 2; /* standard time and DST */ 1055 /* 1056 ** Two transitions per year, from EPOCH_YEAR forward. 1057 */ 1058 sp->ttis[0].tt_gmtoff = -dstoffset; 1059 sp->ttis[0].tt_isdst = 1; 1060 sp->ttis[0].tt_abbrind = stdlen + 1; 1061 sp->ttis[1].tt_gmtoff = -stdoffset; 1062 sp->ttis[1].tt_isdst = 0; 1063 sp->ttis[1].tt_abbrind = 0; 1064 atp = sp->ats; 1065 typep = sp->types; 1066 janfirst = 0; 1067 sp->timecnt = 0; 1068 for (year = EPOCH_YEAR; 1069 sp->timecnt + 2 <= TZ_MAX_TIMES; 1070 ++year) { 1071 time_t newfirst; 1072 1073 starttime = transtime(janfirst, year, &start, 1074 stdoffset); 1075 endtime = transtime(janfirst, year, &end, 1076 dstoffset); 1077 if (starttime > endtime) { 1078 *atp++ = endtime; 1079 *typep++ = 1; /* DST ends */ 1080 *atp++ = starttime; 1081 *typep++ = 0; /* DST begins */ 1082 } else { 1083 *atp++ = starttime; 1084 *typep++ = 0; /* DST begins */ 1085 *atp++ = endtime; 1086 *typep++ = 1; /* DST ends */ 1087 } 1088 sp->timecnt += 2; 1089 newfirst = janfirst; 1090 newfirst += year_lengths[isleap(year)] * 1091 SECSPERDAY; 1092 if (newfirst <= janfirst) 1093 break; 1094 janfirst = newfirst; 1095 } 1096 } else { 1097 long theirstdoffset; 1098 long theirdstoffset; 1099 long theiroffset; 1100 int isdst; 1101 int i; 1102 int j; 1103 1104 if (*name != '\0') 1105 return -1; 1106 /* 1107 ** Initial values of theirstdoffset and theirdstoffset. 1108 */ 1109 theirstdoffset = 0; 1110 for (i = 0; i < sp->timecnt; ++i) { 1111 j = sp->types[i]; 1112 if (!sp->ttis[j].tt_isdst) { 1113 theirstdoffset = 1114 -sp->ttis[j].tt_gmtoff; 1115 break; 1116 } 1117 } 1118 theirdstoffset = 0; 1119 for (i = 0; i < sp->timecnt; ++i) { 1120 j = sp->types[i]; 1121 if (sp->ttis[j].tt_isdst) { 1122 theirdstoffset = 1123 -sp->ttis[j].tt_gmtoff; 1124 break; 1125 } 1126 } 1127 /* 1128 ** Initially we're assumed to be in standard time. 1129 */ 1130 isdst = FALSE; 1131 theiroffset = theirstdoffset; 1132 /* 1133 ** Now juggle transition times and types 1134 ** tracking offsets as you do. 1135 */ 1136 for (i = 0; i < sp->timecnt; ++i) { 1137 j = sp->types[i]; 1138 sp->types[i] = sp->ttis[j].tt_isdst; 1139 if (sp->ttis[j].tt_ttisgmt) { 1140 /* No adjustment to transition time */ 1141 } else { 1142 /* 1143 ** If summer time is in effect, and the 1144 ** transition time was not specified as 1145 ** standard time, add the summer time 1146 ** offset to the transition time; 1147 ** otherwise, add the standard time 1148 ** offset to the transition time. 1149 */ 1150 /* 1151 ** Transitions from DST to DDST 1152 ** will effectively disappear since 1153 ** POSIX provides for only one DST 1154 ** offset. 1155 */ 1156 if (isdst && !sp->ttis[j].tt_ttisstd) { 1157 sp->ats[i] += dstoffset - 1158 theirdstoffset; 1159 } else { 1160 sp->ats[i] += stdoffset - 1161 theirstdoffset; 1162 } 1163 } 1164 theiroffset = -sp->ttis[j].tt_gmtoff; 1165 if (sp->ttis[j].tt_isdst) 1166 theirdstoffset = theiroffset; 1167 else theirstdoffset = theiroffset; 1168 } 1169 /* 1170 ** Finally, fill in ttis. 1171 ** ttisstd and ttisgmt need not be handled. 1172 */ 1173 sp->ttis[0].tt_gmtoff = -stdoffset; 1174 sp->ttis[0].tt_isdst = FALSE; 1175 sp->ttis[0].tt_abbrind = 0; 1176 sp->ttis[1].tt_gmtoff = -dstoffset; 1177 sp->ttis[1].tt_isdst = TRUE; 1178 sp->ttis[1].tt_abbrind = stdlen + 1; 1179 sp->typecnt = 2; 1180 } 1181 } else { 1182 dstlen = 0; 1183 sp->typecnt = 1; /* only standard time */ 1184 sp->timecnt = 0; 1185 sp->ttis[0].tt_gmtoff = -stdoffset; 1186 sp->ttis[0].tt_isdst = 0; 1187 sp->ttis[0].tt_abbrind = 0; 1188 } 1189 sp->charcnt = stdlen + 1; 1190 if (dstlen != 0) 1191 sp->charcnt += dstlen + 1; 1192 if ((size_t) sp->charcnt > sizeof sp->chars) 1193 return -1; 1194 cp = sp->chars; 1195 (void) strncpy(cp, stdname, stdlen); 1196 cp += stdlen; 1197 *cp++ = '\0'; 1198 if (dstlen != 0) { 1199 (void) strncpy(cp, dstname, dstlen); 1200 *(cp + dstlen) = '\0'; 1201 } 1202 return 0; 1203 } 1204 1205 static void 1206 gmtload(struct state *const sp) 1207 { 1208 if (tzload(gmt, sp, TRUE) != 0) 1209 (void) tzparse(gmt, sp, TRUE); 1210 } 1211 1212 static void 1213 tzsetwall_basic(int rdlocked) 1214 { 1215 if (!rdlocked) 1216 _RWLOCK_RDLOCK(&lcl_rwlock); 1217 if (lcl_is_set < 0) { 1218 if (!rdlocked) 1219 _RWLOCK_UNLOCK(&lcl_rwlock); 1220 return; 1221 } 1222 _RWLOCK_UNLOCK(&lcl_rwlock); 1223 1224 _RWLOCK_WRLOCK(&lcl_rwlock); 1225 lcl_is_set = -1; 1226 1227 #ifdef ALL_STATE 1228 if (lclptr == NULL) { 1229 lclptr = calloc(1, sizeof *lclptr); 1230 if (lclptr == NULL) { 1231 settzname(); /* all we can do */ 1232 _RWLOCK_UNLOCK(&lcl_rwlock); 1233 if (rdlocked) 1234 _RWLOCK_RDLOCK(&lcl_rwlock); 1235 return; 1236 } 1237 } 1238 #endif /* defined ALL_STATE */ 1239 if (tzload((char *) NULL, lclptr, TRUE) != 0) 1240 gmtload(lclptr); 1241 settzname(); 1242 _RWLOCK_UNLOCK(&lcl_rwlock); 1243 1244 if (rdlocked) 1245 _RWLOCK_RDLOCK(&lcl_rwlock); 1246 } 1247 1248 void 1249 tzsetwall(void) 1250 { 1251 tzsetwall_basic(0); 1252 } 1253 1254 static void 1255 tzset_basic(int rdlocked) 1256 { 1257 const char * name; 1258 1259 name = getenv("TZ"); 1260 if (name == NULL) { 1261 tzsetwall_basic(rdlocked); 1262 return; 1263 } 1264 1265 if (!rdlocked) 1266 _RWLOCK_RDLOCK(&lcl_rwlock); 1267 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) { 1268 if (!rdlocked) 1269 _RWLOCK_UNLOCK(&lcl_rwlock); 1270 return; 1271 } 1272 _RWLOCK_UNLOCK(&lcl_rwlock); 1273 1274 _RWLOCK_WRLOCK(&lcl_rwlock); 1275 lcl_is_set = strlen(name) < sizeof lcl_TZname; 1276 if (lcl_is_set) 1277 (void) strcpy(lcl_TZname, name); 1278 1279 #ifdef ALL_STATE 1280 if (lclptr == NULL) { 1281 lclptr = (struct state *) calloc(1, sizeof *lclptr); 1282 if (lclptr == NULL) { 1283 settzname(); /* all we can do */ 1284 _RWLOCK_UNLOCK(&lcl_rwlock); 1285 if (rdlocked) 1286 _RWLOCK_RDLOCK(&lcl_rwlock); 1287 return; 1288 } 1289 } 1290 #endif /* defined ALL_STATE */ 1291 if (*name == '\0') { 1292 /* 1293 ** User wants it fast rather than right. 1294 */ 1295 lclptr->leapcnt = 0; /* so, we're off a little */ 1296 lclptr->timecnt = 0; 1297 lclptr->typecnt = 0; 1298 lclptr->ttis[0].tt_isdst = 0; 1299 lclptr->ttis[0].tt_gmtoff = 0; 1300 lclptr->ttis[0].tt_abbrind = 0; 1301 (void) strcpy(lclptr->chars, gmt); 1302 } else if (tzload(name, lclptr, TRUE) != 0) 1303 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) 1304 (void) gmtload(lclptr); 1305 settzname(); 1306 _RWLOCK_UNLOCK(&lcl_rwlock); 1307 1308 if (rdlocked) 1309 _RWLOCK_RDLOCK(&lcl_rwlock); 1310 } 1311 1312 void 1313 tzset(void) 1314 { 1315 tzset_basic(0); 1316 } 1317 1318 /* 1319 ** The easy way to behave "as if no library function calls" localtime 1320 ** is to not call it--so we drop its guts into "localsub", which can be 1321 ** freely called. (And no, the PANS doesn't require the above behavior-- 1322 ** but it *is* desirable.) 1323 ** 1324 ** The unused offset argument is for the benefit of mktime variants. 1325 */ 1326 1327 /*ARGSUSED*/ 1328 static struct tm * 1329 localsub(const time_t *const timep, const long offset, struct tm *const tmp) 1330 { 1331 struct state * sp; 1332 const struct ttinfo * ttisp; 1333 int i; 1334 struct tm * result; 1335 const time_t t = *timep; 1336 1337 sp = lclptr; 1338 #ifdef ALL_STATE 1339 if (sp == NULL) 1340 return gmtsub(timep, offset, tmp); 1341 #endif /* defined ALL_STATE */ 1342 if ((sp->goback && t < sp->ats[0]) || 1343 (sp->goahead && t > sp->ats[sp->timecnt - 1])) { 1344 time_t newt = t; 1345 register time_t seconds; 1346 register time_t tcycles; 1347 register int_fast64_t icycles; 1348 1349 if (t < sp->ats[0]) 1350 seconds = sp->ats[0] - t; 1351 else seconds = t - sp->ats[sp->timecnt - 1]; 1352 --seconds; 1353 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR; 1354 ++tcycles; 1355 icycles = tcycles; 1356 if (tcycles - icycles >= 1 || icycles - tcycles >= 1) 1357 return NULL; 1358 seconds = icycles; 1359 seconds *= YEARSPERREPEAT; 1360 seconds *= AVGSECSPERYEAR; 1361 if (t < sp->ats[0]) 1362 newt += seconds; 1363 else newt -= seconds; 1364 if (newt < sp->ats[0] || 1365 newt > sp->ats[sp->timecnt - 1]) 1366 return NULL; /* "cannot happen" */ 1367 result = localsub(&newt, offset, tmp); 1368 if (result == tmp) { 1369 register time_t newy; 1370 1371 newy = tmp->tm_year; 1372 if (t < sp->ats[0]) 1373 newy -= icycles * YEARSPERREPEAT; 1374 else newy += icycles * YEARSPERREPEAT; 1375 tmp->tm_year = newy; 1376 if (tmp->tm_year != newy) 1377 return NULL; 1378 } 1379 return result; 1380 } 1381 if (sp->timecnt == 0 || t < sp->ats[0]) { 1382 i = 0; 1383 while (sp->ttis[i].tt_isdst) 1384 if (++i >= sp->typecnt) { 1385 i = 0; 1386 break; 1387 } 1388 } else { 1389 register int lo = 1; 1390 register int hi = sp->timecnt; 1391 1392 while (lo < hi) { 1393 register int mid = (lo + hi) >> 1; 1394 1395 if (t < sp->ats[mid]) 1396 hi = mid; 1397 else lo = mid + 1; 1398 } 1399 i = (int) sp->types[lo - 1]; 1400 } 1401 ttisp = &sp->ttis[i]; 1402 /* 1403 ** To get (wrong) behavior that's compatible with System V Release 2.0 1404 ** you'd replace the statement below with 1405 ** t += ttisp->tt_gmtoff; 1406 ** timesub(&t, 0L, sp, tmp); 1407 */ 1408 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); 1409 tmp->tm_isdst = ttisp->tt_isdst; 1410 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; 1411 #ifdef TM_ZONE 1412 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; 1413 #endif /* defined TM_ZONE */ 1414 return result; 1415 } 1416 1417 static void 1418 localtime_key_init(void) 1419 { 1420 1421 localtime_key_error = _pthread_key_create(&localtime_key, free); 1422 } 1423 1424 struct tm * 1425 localtime(const time_t *const timep) 1426 { 1427 struct tm *p_tm; 1428 1429 if (__isthreaded != 0) { 1430 _pthread_once(&localtime_once, localtime_key_init); 1431 if (localtime_key_error != 0) { 1432 errno = localtime_key_error; 1433 return(NULL); 1434 } 1435 p_tm = _pthread_getspecific(localtime_key); 1436 if (p_tm == NULL) { 1437 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) 1438 == NULL) 1439 return(NULL); 1440 _pthread_setspecific(localtime_key, p_tm); 1441 } 1442 _RWLOCK_RDLOCK(&lcl_rwlock); 1443 tzset_basic(1); 1444 p_tm = localsub(timep, 0L, p_tm); 1445 _RWLOCK_UNLOCK(&lcl_rwlock); 1446 } else { 1447 tzset_basic(0); 1448 p_tm = localsub(timep, 0L, &tm); 1449 } 1450 return(p_tm); 1451 } 1452 1453 /* 1454 ** Re-entrant version of localtime. 1455 */ 1456 1457 struct tm * 1458 localtime_r(const time_t *const timep, struct tm *tmp) 1459 { 1460 _RWLOCK_RDLOCK(&lcl_rwlock); 1461 tzset_basic(1); 1462 tmp = localsub(timep, 0L, tmp); 1463 _RWLOCK_UNLOCK(&lcl_rwlock); 1464 return tmp; 1465 } 1466 1467 static void 1468 gmt_init(void) 1469 { 1470 1471 #ifdef ALL_STATE 1472 gmtptr = (struct state *) calloc(1, sizeof *gmtptr); 1473 if (gmtptr != NULL) 1474 #endif /* defined ALL_STATE */ 1475 gmtload(gmtptr); 1476 } 1477 1478 /* 1479 ** gmtsub is to gmtime as localsub is to localtime. 1480 */ 1481 1482 static struct tm * 1483 gmtsub(timep, offset, tmp) 1484 const time_t * const timep; 1485 const long offset; 1486 struct tm * const tmp; 1487 { 1488 register struct tm * result; 1489 1490 _once(&gmt_once, gmt_init); 1491 result = timesub(timep, offset, gmtptr, tmp); 1492 #ifdef TM_ZONE 1493 /* 1494 ** Could get fancy here and deliver something such as 1495 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero, 1496 ** but this is no time for a treasure hunt. 1497 */ 1498 if (offset != 0) 1499 tmp->TM_ZONE = wildabbr; 1500 else { 1501 #ifdef ALL_STATE 1502 if (gmtptr == NULL) 1503 tmp->TM_ZONE = gmt; 1504 else tmp->TM_ZONE = gmtptr->chars; 1505 #endif /* defined ALL_STATE */ 1506 #ifndef ALL_STATE 1507 tmp->TM_ZONE = gmtptr->chars; 1508 #endif /* State Farm */ 1509 } 1510 #endif /* defined TM_ZONE */ 1511 return result; 1512 } 1513 1514 static void 1515 gmtime_key_init(void) 1516 { 1517 1518 gmtime_key_error = _pthread_key_create(&gmtime_key, free); 1519 } 1520 1521 struct tm * 1522 gmtime(const time_t *const timep) 1523 { 1524 struct tm *p_tm; 1525 1526 if (__isthreaded != 0) { 1527 _pthread_once(&gmtime_once, gmtime_key_init); 1528 if (gmtime_key_error != 0) { 1529 errno = gmtime_key_error; 1530 return(NULL); 1531 } 1532 /* 1533 * Changed to follow POSIX.1 threads standard, which 1534 * is what BSD currently has. 1535 */ 1536 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) { 1537 if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) 1538 == NULL) { 1539 return(NULL); 1540 } 1541 _pthread_setspecific(gmtime_key, p_tm); 1542 } 1543 gmtsub(timep, 0L, p_tm); 1544 return(p_tm); 1545 } 1546 else { 1547 gmtsub(timep, 0L, &tm); 1548 return(&tm); 1549 } 1550 } 1551 1552 /* 1553 * Re-entrant version of gmtime. 1554 */ 1555 1556 struct tm * 1557 gmtime_r(const time_t *const timep, struct tm *tmp) 1558 { 1559 return gmtsub(timep, 0L, tmp); 1560 } 1561 1562 #ifdef STD_INSPIRED 1563 1564 struct tm * 1565 offtime(const time_t *const timep, const long offset) 1566 { 1567 return gmtsub(timep, offset, &tm); 1568 } 1569 1570 #endif /* defined STD_INSPIRED */ 1571 1572 /* 1573 ** Return the number of leap years through the end of the given year 1574 ** where, to make the math easy, the answer for year zero is defined as zero. 1575 */ 1576 1577 static int 1578 leaps_thru_end_of(y) 1579 register const int y; 1580 { 1581 return (y >= 0) ? (y / 4 - y / 100 + y / 400) : 1582 -(leaps_thru_end_of(-(y + 1)) + 1); 1583 } 1584 1585 static struct tm * 1586 timesub(timep, offset, sp, tmp) 1587 const time_t * const timep; 1588 const long offset; 1589 const struct state * const sp; 1590 struct tm * const tmp; 1591 { 1592 const struct lsinfo * lp; 1593 time_t tdays; 1594 int idays; /* unsigned would be so 2003 */ 1595 long rem; 1596 int y; 1597 const int * ip; 1598 long corr; 1599 int hit; 1600 int i; 1601 1602 corr = 0; 1603 hit = 0; 1604 #ifdef ALL_STATE 1605 i = (sp == NULL) ? 0 : sp->leapcnt; 1606 #endif /* defined ALL_STATE */ 1607 #ifndef ALL_STATE 1608 i = sp->leapcnt; 1609 #endif /* State Farm */ 1610 while (--i >= 0) { 1611 lp = &sp->lsis[i]; 1612 if (*timep >= lp->ls_trans) { 1613 if (*timep == lp->ls_trans) { 1614 hit = ((i == 0 && lp->ls_corr > 0) || 1615 lp->ls_corr > sp->lsis[i - 1].ls_corr); 1616 if (hit) 1617 while (i > 0 && 1618 sp->lsis[i].ls_trans == 1619 sp->lsis[i - 1].ls_trans + 1 && 1620 sp->lsis[i].ls_corr == 1621 sp->lsis[i - 1].ls_corr + 1) { 1622 ++hit; 1623 --i; 1624 } 1625 } 1626 corr = lp->ls_corr; 1627 break; 1628 } 1629 } 1630 y = EPOCH_YEAR; 1631 tdays = *timep / SECSPERDAY; 1632 rem = *timep - tdays * SECSPERDAY; 1633 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { 1634 int newy; 1635 register time_t tdelta; 1636 register int idelta; 1637 register int leapdays; 1638 1639 tdelta = tdays / DAYSPERLYEAR; 1640 idelta = tdelta; 1641 if (tdelta - idelta >= 1 || idelta - tdelta >= 1) 1642 return NULL; 1643 if (idelta == 0) 1644 idelta = (tdays < 0) ? -1 : 1; 1645 newy = y; 1646 if (increment_overflow(&newy, idelta)) 1647 return NULL; 1648 leapdays = leaps_thru_end_of(newy - 1) - 1649 leaps_thru_end_of(y - 1); 1650 tdays -= ((time_t) newy - y) * DAYSPERNYEAR; 1651 tdays -= leapdays; 1652 y = newy; 1653 } 1654 { 1655 register long seconds; 1656 1657 seconds = tdays * SECSPERDAY + 0.5; 1658 tdays = seconds / SECSPERDAY; 1659 rem += seconds - tdays * SECSPERDAY; 1660 } 1661 /* 1662 ** Given the range, we can now fearlessly cast... 1663 */ 1664 idays = tdays; 1665 rem += offset - corr; 1666 while (rem < 0) { 1667 rem += SECSPERDAY; 1668 --idays; 1669 } 1670 while (rem >= SECSPERDAY) { 1671 rem -= SECSPERDAY; 1672 ++idays; 1673 } 1674 while (idays < 0) { 1675 if (increment_overflow(&y, -1)) 1676 return NULL; 1677 idays += year_lengths[isleap(y)]; 1678 } 1679 while (idays >= year_lengths[isleap(y)]) { 1680 idays -= year_lengths[isleap(y)]; 1681 if (increment_overflow(&y, 1)) 1682 return NULL; 1683 } 1684 tmp->tm_year = y; 1685 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE)) 1686 return NULL; 1687 tmp->tm_yday = idays; 1688 /* 1689 ** The "extra" mods below avoid overflow problems. 1690 */ 1691 tmp->tm_wday = EPOCH_WDAY + 1692 ((y - EPOCH_YEAR) % DAYSPERWEEK) * 1693 (DAYSPERNYEAR % DAYSPERWEEK) + 1694 leaps_thru_end_of(y - 1) - 1695 leaps_thru_end_of(EPOCH_YEAR - 1) + 1696 idays; 1697 tmp->tm_wday %= DAYSPERWEEK; 1698 if (tmp->tm_wday < 0) 1699 tmp->tm_wday += DAYSPERWEEK; 1700 tmp->tm_hour = (int) (rem / SECSPERHOUR); 1701 rem %= SECSPERHOUR; 1702 tmp->tm_min = (int) (rem / SECSPERMIN); 1703 /* 1704 ** A positive leap second requires a special 1705 ** representation. This uses "... ??:59:60" et seq. 1706 */ 1707 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; 1708 ip = mon_lengths[isleap(y)]; 1709 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon)) 1710 idays -= ip[tmp->tm_mon]; 1711 tmp->tm_mday = (int) (idays + 1); 1712 tmp->tm_isdst = 0; 1713 #ifdef TM_GMTOFF 1714 tmp->TM_GMTOFF = offset; 1715 #endif /* defined TM_GMTOFF */ 1716 return tmp; 1717 } 1718 1719 char * 1720 ctime(const time_t *const timep) 1721 { 1722 /* 1723 ** Section 4.12.3.2 of X3.159-1989 requires that 1724 ** The ctime function converts the calendar time pointed to by timer 1725 ** to local time in the form of a string. It is equivalent to 1726 ** asctime(localtime(timer)) 1727 */ 1728 return asctime(localtime(timep)); 1729 } 1730 1731 char * 1732 ctime_r(const time_t *const timep, char *buf) 1733 { 1734 struct tm mytm; 1735 1736 return asctime_r(localtime_r(timep, &mytm), buf); 1737 } 1738 1739 /* 1740 ** Adapted from code provided by Robert Elz, who writes: 1741 ** The "best" way to do mktime I think is based on an idea of Bob 1742 ** Kridle's (so its said...) from a long time ago. 1743 ** It does a binary search of the time_t space. Since time_t's are 1744 ** just 32 bits, its a max of 32 iterations (even at 64 bits it 1745 ** would still be very reasonable). 1746 */ 1747 1748 #ifndef WRONG 1749 #define WRONG (-1) 1750 #endif /* !defined WRONG */ 1751 1752 /* 1753 ** Simplified normalize logic courtesy Paul Eggert. 1754 */ 1755 1756 static int 1757 increment_overflow(number, delta) 1758 int * number; 1759 int delta; 1760 { 1761 int number0; 1762 1763 number0 = *number; 1764 *number += delta; 1765 return (*number < number0) != (delta < 0); 1766 } 1767 1768 static int 1769 long_increment_overflow(number, delta) 1770 long * number; 1771 int delta; 1772 { 1773 long number0; 1774 1775 number0 = *number; 1776 *number += delta; 1777 return (*number < number0) != (delta < 0); 1778 } 1779 1780 static int 1781 normalize_overflow(int *const tensptr, int *const unitsptr, const int base) 1782 { 1783 int tensdelta; 1784 1785 tensdelta = (*unitsptr >= 0) ? 1786 (*unitsptr / base) : 1787 (-1 - (-1 - *unitsptr) / base); 1788 *unitsptr -= tensdelta * base; 1789 return increment_overflow(tensptr, tensdelta); 1790 } 1791 1792 static int 1793 long_normalize_overflow(long *const tensptr, int *const unitsptr, const int base) 1794 { 1795 register int tensdelta; 1796 1797 tensdelta = (*unitsptr >= 0) ? 1798 (*unitsptr / base) : 1799 (-1 - (-1 - *unitsptr) / base); 1800 *unitsptr -= tensdelta * base; 1801 return long_increment_overflow(tensptr, tensdelta); 1802 } 1803 1804 static int 1805 tmcomp(atmp, btmp) 1806 const struct tm * const atmp; 1807 const struct tm * const btmp; 1808 { 1809 int result; 1810 1811 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && 1812 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && 1813 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && 1814 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && 1815 (result = (atmp->tm_min - btmp->tm_min)) == 0) 1816 result = atmp->tm_sec - btmp->tm_sec; 1817 return result; 1818 } 1819 1820 static time_t 1821 time2sub(struct tm *const tmp, 1822 struct tm *(*const funcp)(const time_t *, long, struct tm *), 1823 const long offset, 1824 int *const okayp, 1825 const int do_norm_secs) 1826 { 1827 const struct state * sp; 1828 int dir; 1829 int i, j; 1830 int saved_seconds; 1831 long li; 1832 time_t lo; 1833 time_t hi; 1834 long y; 1835 time_t newt; 1836 time_t t; 1837 struct tm yourtm, mytm; 1838 1839 *okayp = FALSE; 1840 yourtm = *tmp; 1841 if (do_norm_secs) { 1842 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, 1843 SECSPERMIN)) 1844 return WRONG; 1845 } 1846 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) 1847 return WRONG; 1848 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) 1849 return WRONG; 1850 y = yourtm.tm_year; 1851 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR)) 1852 return WRONG; 1853 /* 1854 ** Turn y into an actual year number for now. 1855 ** It is converted back to an offset from TM_YEAR_BASE later. 1856 */ 1857 if (long_increment_overflow(&y, TM_YEAR_BASE)) 1858 return WRONG; 1859 while (yourtm.tm_mday <= 0) { 1860 if (long_increment_overflow(&y, -1)) 1861 return WRONG; 1862 li = y + (1 < yourtm.tm_mon); 1863 yourtm.tm_mday += year_lengths[isleap(li)]; 1864 } 1865 while (yourtm.tm_mday > DAYSPERLYEAR) { 1866 li = y + (1 < yourtm.tm_mon); 1867 yourtm.tm_mday -= year_lengths[isleap(li)]; 1868 if (long_increment_overflow(&y, 1)) 1869 return WRONG; 1870 } 1871 for ( ; ; ) { 1872 i = mon_lengths[isleap(y)][yourtm.tm_mon]; 1873 if (yourtm.tm_mday <= i) 1874 break; 1875 yourtm.tm_mday -= i; 1876 if (++yourtm.tm_mon >= MONSPERYEAR) { 1877 yourtm.tm_mon = 0; 1878 if (long_increment_overflow(&y, 1)) 1879 return WRONG; 1880 } 1881 } 1882 if (long_increment_overflow(&y, -TM_YEAR_BASE)) 1883 return WRONG; 1884 yourtm.tm_year = y; 1885 if (yourtm.tm_year != y) 1886 return WRONG; 1887 /* Don't go below 1900 for POLA */ 1888 if (yourtm.tm_year < 0) 1889 return WRONG; 1890 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) 1891 saved_seconds = 0; 1892 else if (y + TM_YEAR_BASE < EPOCH_YEAR) { 1893 /* 1894 ** We can't set tm_sec to 0, because that might push the 1895 ** time below the minimum representable time. 1896 ** Set tm_sec to 59 instead. 1897 ** This assumes that the minimum representable time is 1898 ** not in the same minute that a leap second was deleted from, 1899 ** which is a safer assumption than using 58 would be. 1900 */ 1901 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) 1902 return WRONG; 1903 saved_seconds = yourtm.tm_sec; 1904 yourtm.tm_sec = SECSPERMIN - 1; 1905 } else { 1906 saved_seconds = yourtm.tm_sec; 1907 yourtm.tm_sec = 0; 1908 } 1909 /* 1910 ** Do a binary search (this works whatever time_t's type is). 1911 */ 1912 if (!TYPE_SIGNED(time_t)) { 1913 lo = 0; 1914 hi = lo - 1; 1915 } else if (!TYPE_INTEGRAL(time_t)) { 1916 if (sizeof(time_t) > sizeof(float)) 1917 hi = (time_t) DBL_MAX; 1918 else hi = (time_t) FLT_MAX; 1919 lo = -hi; 1920 } else { 1921 lo = 1; 1922 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) 1923 lo *= 2; 1924 hi = -(lo + 1); 1925 } 1926 for ( ; ; ) { 1927 t = lo / 2 + hi / 2; 1928 if (t < lo) 1929 t = lo; 1930 else if (t > hi) 1931 t = hi; 1932 if ((*funcp)(&t, offset, &mytm) == NULL) { 1933 /* 1934 ** Assume that t is too extreme to be represented in 1935 ** a struct tm; arrange things so that it is less 1936 ** extreme on the next pass. 1937 */ 1938 dir = (t > 0) ? 1 : -1; 1939 } else dir = tmcomp(&mytm, &yourtm); 1940 if (dir != 0) { 1941 if (t == lo) { 1942 ++t; 1943 if (t <= lo) 1944 return WRONG; 1945 ++lo; 1946 } else if (t == hi) { 1947 --t; 1948 if (t >= hi) 1949 return WRONG; 1950 --hi; 1951 } 1952 if (lo > hi) 1953 return WRONG; 1954 if (dir > 0) 1955 hi = t; 1956 else lo = t; 1957 continue; 1958 } 1959 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) 1960 break; 1961 /* 1962 ** Right time, wrong type. 1963 ** Hunt for right time, right type. 1964 ** It's okay to guess wrong since the guess 1965 ** gets checked. 1966 */ 1967 sp = (const struct state *) 1968 ((funcp == localsub) ? lclptr : gmtptr); 1969 #ifdef ALL_STATE 1970 if (sp == NULL) 1971 return WRONG; 1972 #endif /* defined ALL_STATE */ 1973 for (i = sp->typecnt - 1; i >= 0; --i) { 1974 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) 1975 continue; 1976 for (j = sp->typecnt - 1; j >= 0; --j) { 1977 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) 1978 continue; 1979 newt = t + sp->ttis[j].tt_gmtoff - 1980 sp->ttis[i].tt_gmtoff; 1981 if ((*funcp)(&newt, offset, &mytm) == NULL) 1982 continue; 1983 if (tmcomp(&mytm, &yourtm) != 0) 1984 continue; 1985 if (mytm.tm_isdst != yourtm.tm_isdst) 1986 continue; 1987 /* 1988 ** We have a match. 1989 */ 1990 t = newt; 1991 goto label; 1992 } 1993 } 1994 return WRONG; 1995 } 1996 label: 1997 newt = t + saved_seconds; 1998 if ((newt < t) != (saved_seconds < 0)) 1999 return WRONG; 2000 t = newt; 2001 if ((*funcp)(&t, offset, tmp)) 2002 *okayp = TRUE; 2003 return t; 2004 } 2005 2006 static time_t 2007 time2(struct tm * const tmp, 2008 struct tm * (*const funcp)(const time_t *, long, struct tm *), 2009 const long offset, 2010 int *const okayp) 2011 { 2012 time_t t; 2013 2014 /* 2015 ** First try without normalization of seconds 2016 ** (in case tm_sec contains a value associated with a leap second). 2017 ** If that fails, try with normalization of seconds. 2018 */ 2019 t = time2sub(tmp, funcp, offset, okayp, FALSE); 2020 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE); 2021 } 2022 2023 static time_t 2024 time1(tmp, funcp, offset) 2025 struct tm * const tmp; 2026 struct tm * (* const funcp)(const time_t *, long, struct tm *); 2027 const long offset; 2028 { 2029 time_t t; 2030 const struct state * sp; 2031 int samei, otheri; 2032 int sameind, otherind; 2033 int i; 2034 int nseen; 2035 int seen[TZ_MAX_TYPES]; 2036 int types[TZ_MAX_TYPES]; 2037 int okay; 2038 2039 if (tmp == NULL) { 2040 errno = EINVAL; 2041 return WRONG; 2042 } 2043 2044 if (tmp->tm_isdst > 1) 2045 tmp->tm_isdst = 1; 2046 t = time2(tmp, funcp, offset, &okay); 2047 #ifdef PCTS 2048 /* 2049 ** PCTS code courtesy Grant Sullivan. 2050 */ 2051 if (okay) 2052 return t; 2053 if (tmp->tm_isdst < 0) 2054 tmp->tm_isdst = 0; /* reset to std and try again */ 2055 #endif /* defined PCTS */ 2056 #ifndef PCTS 2057 if (okay || tmp->tm_isdst < 0) 2058 return t; 2059 #endif /* !defined PCTS */ 2060 /* 2061 ** We're supposed to assume that somebody took a time of one type 2062 ** and did some math on it that yielded a "struct tm" that's bad. 2063 ** We try to divine the type they started from and adjust to the 2064 ** type they need. 2065 */ 2066 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); 2067 #ifdef ALL_STATE 2068 if (sp == NULL) 2069 return WRONG; 2070 #endif /* defined ALL_STATE */ 2071 for (i = 0; i < sp->typecnt; ++i) 2072 seen[i] = FALSE; 2073 nseen = 0; 2074 for (i = sp->timecnt - 1; i >= 0; --i) 2075 if (!seen[sp->types[i]]) { 2076 seen[sp->types[i]] = TRUE; 2077 types[nseen++] = sp->types[i]; 2078 } 2079 for (sameind = 0; sameind < nseen; ++sameind) { 2080 samei = types[sameind]; 2081 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) 2082 continue; 2083 for (otherind = 0; otherind < nseen; ++otherind) { 2084 otheri = types[otherind]; 2085 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) 2086 continue; 2087 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - 2088 sp->ttis[samei].tt_gmtoff; 2089 tmp->tm_isdst = !tmp->tm_isdst; 2090 t = time2(tmp, funcp, offset, &okay); 2091 if (okay) 2092 return t; 2093 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - 2094 sp->ttis[samei].tt_gmtoff; 2095 tmp->tm_isdst = !tmp->tm_isdst; 2096 } 2097 } 2098 return WRONG; 2099 } 2100 2101 time_t 2102 mktime(struct tm *const tmp) 2103 { 2104 time_t mktime_return_value; 2105 _RWLOCK_RDLOCK(&lcl_rwlock); 2106 tzset_basic(1); 2107 mktime_return_value = time1(tmp, localsub, 0L); 2108 _RWLOCK_UNLOCK(&lcl_rwlock); 2109 return(mktime_return_value); 2110 } 2111 2112 #ifdef STD_INSPIRED 2113 2114 time_t 2115 timelocal(struct tm *const tmp) 2116 { 2117 if (tmp != NULL) 2118 tmp->tm_isdst = -1; /* in case it wasn't initialized */ 2119 return mktime(tmp); 2120 } 2121 2122 time_t 2123 timegm(struct tm *const tmp) 2124 { 2125 if (tmp != NULL) 2126 tmp->tm_isdst = 0; 2127 return time1(tmp, gmtsub, 0L); 2128 } 2129 2130 time_t 2131 timeoff(struct tm *const tmp, const long offset) 2132 { 2133 if (tmp != NULL) 2134 tmp->tm_isdst = 0; 2135 return time1(tmp, gmtsub, offset); 2136 } 2137 2138 #endif /* defined STD_INSPIRED */ 2139 2140 #ifdef CMUCS 2141 2142 /* 2143 ** The following is supplied for compatibility with 2144 ** previous versions of the CMUCS runtime library. 2145 */ 2146 2147 long 2148 gtime(struct tm *const tmp) 2149 { 2150 const time_t t = mktime(tmp); 2151 2152 if (t == WRONG) 2153 return -1; 2154 return t; 2155 } 2156 2157 #endif /* defined CMUCS */ 2158 2159 /* 2160 ** XXX--is the below the right way to conditionalize?? 2161 */ 2162 2163 #ifdef STD_INSPIRED 2164 2165 /* 2166 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 2167 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which 2168 ** is not the case if we are accounting for leap seconds. 2169 ** So, we provide the following conversion routines for use 2170 ** when exchanging timestamps with POSIX conforming systems. 2171 */ 2172 2173 static long 2174 leapcorr(time_t *timep) 2175 { 2176 struct state * sp; 2177 struct lsinfo * lp; 2178 int i; 2179 2180 sp = lclptr; 2181 i = sp->leapcnt; 2182 while (--i >= 0) { 2183 lp = &sp->lsis[i]; 2184 if (*timep >= lp->ls_trans) 2185 return lp->ls_corr; 2186 } 2187 return 0; 2188 } 2189 2190 time_t 2191 time2posix(time_t t) 2192 { 2193 tzset(); 2194 return t - leapcorr(&t); 2195 } 2196 2197 time_t 2198 posix2time(time_t t) 2199 { 2200 time_t x; 2201 time_t y; 2202 2203 tzset(); 2204 /* 2205 ** For a positive leap second hit, the result 2206 ** is not unique. For a negative leap second 2207 ** hit, the corresponding time doesn't exist, 2208 ** so we return an adjacent second. 2209 */ 2210 x = t + leapcorr(&t); 2211 y = x - leapcorr(&x); 2212 if (y < t) { 2213 do { 2214 x++; 2215 y = x - leapcorr(&x); 2216 } while (y < t); 2217 if (t != y) 2218 return x - 1; 2219 } else if (y > t) { 2220 do { 2221 --x; 2222 y = x - leapcorr(&x); 2223 } while (y > t); 2224 if (t != y) 2225 return x + 1; 2226 } 2227 return x; 2228 } 2229 2230 #endif /* defined STD_INSPIRED */ 2231