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