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