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