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