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