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