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