1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
|
/*
Copyright (c) 1990-2001 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2000-Apr-09 or later
(the contents of which are also included in zip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/* Replacement time library functions, based on platform independent public
* domain timezone code from ftp://elsie.nci.nih.gov/pub, with mktime and
* mkgmtime from our own mktime.c in Zip.
*
* Contains: tzset()
* __tzset()
* gmtime()
* localtime()
* mktime()
* mkgmtime()
* GetPlatformLocalTimezone() [different versions]
*/
/* HISTORY/CHANGES
* 17 Jun 00, Paul Kienitz, added the PD-based tzset(), localtime(), and so on
* to amiga/filedate.c, replacing GNU-based functions which had
* replaced time_lib.c, both having been rejected for licensing
* reasons. Support for timezone files and leap seconds was removed.
*
* 23 Aug 00, Paul Kienitz, split into separate timezone.c file, made platform
* independent, copied in mktime() and mkgmtime() from Zip, renamed
* locale_TZ as GetPlatformLocalTimezone(), for use as a generic
* hook by other platforms.
*/
#ifndef __timezone_c
#define __timezone_c
#include "zip.h"
#include "timezone.h"
#include <ctype.h>
#include <errno.h>
#ifdef IZTZ_DEFINESTDGLOBALS
long timezone = 0;
int daylight = 0;
char *tzname[2];
#endif
#ifndef IZTZ_GETLOCALETZINFO
# define IZTZ_GETLOCALETZINFO(ptzstruct, pgenrulefunct) (FALSE)
#endif
int real_timezone_is_set = FALSE; /* set by tzset() */
#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
#define TZDEFAULT "EST5EDT"
#define SECSPERMIN 60
#define MINSPERHOUR 60
#define HOURSPERDAY 24
#define DAYSPERWEEK 7
#define DAYSPERNYEAR 365
#define DAYSPERLYEAR 366
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
#define MONSPERYEAR 12
#define EPOCH_WDAY 4 /* Jan 1, 1970 was thursday */
#define EPOCH_YEAR 1970
#define TM_YEAR_BASE 1900
#define FIRST_GOOD_YEAR ((time_t) -1 < (time_t) 1 ? EPOCH_YEAR-68 : EPOCH_YEAR)
#define LAST_GOOD_YEAR (EPOCH_YEAR + ((time_t) -1 < (time_t) 1 ? 67 : 135))
#define YDAYS(month, year) yr_days[leap(year)][month]
/* Nonzero if `y' is a leap year, else zero. */
#define leap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
/* Number of leap years from EPOCH_YEAR to `y' (not including `y' itself). */
#define _P4 ((EPOCH_YEAR / 4) * 4 + 1)
#define _P100 ((EPOCH_YEAR / 100) * 100 + 1)
#define _P400 ((EPOCH_YEAR / 400) * 400 + 1)
#define nleap(y) (((y) - _P4) / 4 - ((y) - _P100) / 100 + ((y) - _P400) / 400)
/* Length of month `m' (0 .. 11) */
#define monthlen(m, y) (yr_days[0][(m)+1] - yr_days[0][m] + \
((m) == 1 && leap(y)))
/* internal module-level constants */
#ifndef IZ_MKTIME_ONLY
static ZCONST char gmt[] = "GMT";
static ZCONST int mon_lengths[2][MONSPERYEAR] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
#endif /* !IZ_MKTIME_ONLY */
static ZCONST int yr_days[2][MONSPERYEAR+1] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
};
#ifndef IZ_MKTIME_ONLY
static ZCONST int year_lengths[2] = {
DAYSPERNYEAR, DAYSPERLYEAR
};
/* internal variables */
static struct state statism;
/* prototypes of static functions */
static time_t transtime OF((ZCONST time_t janfirst, ZCONST int year,
ZCONST struct rule * ZCONST rulep,
ZCONST long offset));
static void generate_transitions OF((register struct state * ZCONST sp,
ZCONST struct rule * ZCONST start,
ZCONST struct rule * ZCONST end));
static ZCONST char *getzname OF((ZCONST char *strp));
static ZCONST char *getnum OF((ZCONST char *strp, int * ZCONST nump,
ZCONST int min, ZCONST int max));
static ZCONST char *getsecs OF((ZCONST char *strp, long * ZCONST secsp));
static ZCONST char *getoffset OF((ZCONST char *strp, long * ZCONST offsetp));
static ZCONST char *getrule OF((ZCONST char *strp, struct rule * ZCONST rulep));
static int Parse_TZ OF((ZCONST char *name, register struct state * ZCONST sp));
static time_t transtime(janfirst, year, rulep, offset)
ZCONST time_t janfirst;
ZCONST int year;
ZCONST struct rule * ZCONST rulep;
ZCONST long offset;
{
register int leapyear;
register time_t value;
register int i;
int d, m1, yy0, yy1, yy2, dow;
value = 0;
leapyear = leap(year);
switch (rulep->r_type) {
case JULIAN_DAY:
/*
** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
** years.
** In non-leap years, or if the day number is 59 or less, just
** add SECSPERDAY times the day number-1 to the time of
** January 1, midnight, to get the day.
*/
value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
if (leapyear && rulep->r_day >= 60)
value += SECSPERDAY;
break;
case DAY_OF_YEAR:
/*
** n - day of year.
** Just add SECSPERDAY times the day number to the time of
** January 1, midnight, to get the day.
*/
value = janfirst + rulep->r_day * SECSPERDAY;
break;
case MONTH_NTH_DAY_OF_WEEK:
/*
** Mm.n.d - nth "dth day" of month m.
*/
value = janfirst;
/*
for (i = 0; i < rulep->r_mon - 1; ++i)
value += mon_lengths[leapyear][i] * SECSPERDAY;
*/
value += yr_days[leapyear][rulep->r_mon - 1] * SECSPERDAY;
/*
** Use Zeller's Congruence to get day-of-week of first day of
** month.
*/
m1 = (rulep->r_mon + 9) % 12 + 1;
yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
yy1 = yy0 / 100;
yy2 = yy0 % 100;
dow = ((26 * m1 - 2) / 10 +
1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
if (dow < 0)
dow += DAYSPERWEEK;
/*
** "dow" is the day-of-week of the first day of the month. Get
** the day-of-month (zero-origin) of the first "dow" day of the
** month.
*/
d = rulep->r_day - dow;
if (d < 0)
d += DAYSPERWEEK;
for (i = 1; i < rulep->r_week; ++i) {
if (d + DAYSPERWEEK >= mon_lengths[leapyear][rulep->r_mon - 1])
break;
d += DAYSPERWEEK;
}
/*
** "d" is the day-of-month (zero-origin) of the day we want.
*/
value += d * SECSPERDAY;
break;
}
/*
** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
** question. To get the Epoch-relative time of the specified local
** time on that day, add the transition time and the current offset
** from UTC.
*/
return value + rulep->r_time + offset;
}
static void generate_transitions(sp, start, end)
register struct state * ZCONST sp;
ZCONST struct rule * ZCONST start;
ZCONST struct rule * ZCONST end;
{
register int year;
register time_t janfirst;
time_t starttime;
time_t endtime;
long stdoffset = -sp->ttis[0].tt_gmtoff;
long dstoffset = -sp->ttis[1].tt_gmtoff;
register time_t * atp;
register unsigned char * typep;
/*
** Two transitions per year, from EPOCH_YEAR to LAST_GOOD_YEAR.
*/
sp->timecnt = 2 * (LAST_GOOD_YEAR - EPOCH_YEAR + 1);
atp = sp->ats;
typep = sp->types;
janfirst = 0;
for (year = EPOCH_YEAR; year <= LAST_GOOD_YEAR; ++year) {
starttime = transtime(janfirst, year, start, stdoffset);
endtime = transtime(janfirst, year, end, dstoffset);
if (starttime > endtime) {
*atp++ = endtime;
*typep++ = 0; /* DST ends */
*atp++ = starttime;
*typep++ = 1; /* DST begins */
} else {
*atp++ = starttime;
*typep++ = 1; /* DST begins */
*atp++ = endtime;
*typep++ = 0; /* DST ends */
}
janfirst += year_lengths[leap(year)] * SECSPERDAY;
}
}
static ZCONST char *getzname(strp)
ZCONST char *strp;
{
register char c;
while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
c != '+')
++strp;
return strp;
}
static ZCONST char *getnum(strp, nump, min, max)
ZCONST char *strp;
int * ZCONST nump;
ZCONST int min;
ZCONST int max;
{
register char c;
register int num;
if (strp == NULL || !isdigit(c = *strp))
return NULL;
num = 0;
do {
num = num * 10 + (c - '0');
if (num > max)
return NULL; /* illegal value */
c = *++strp;
} while (isdigit(c));
if (num < min)
return NULL; /* illegal value */
*nump = num;
return strp;
}
static ZCONST char *getsecs(strp, secsp)
ZCONST char *strp;
long * ZCONST secsp;
{
int num;
/*
** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
** "M10.4.6/26", which does not conform to Posix,
** but which specifies the equivalent of
** ``02:00 on the first Sunday on or after 23 Oct''.
*/
strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
if (strp == NULL)
return NULL;
*secsp = num * (long) SECSPERHOUR;
if (*strp == ':') {
++strp;
strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
if (strp == NULL)
return NULL;
*secsp += num * SECSPERMIN;
if (*strp == ':') {
++strp;
/* `SECSPERMIN' allows for leap seconds. */
strp = getnum(strp, &num, 0, SECSPERMIN);
if (strp == NULL)
return NULL;
*secsp += num;
}
}
return strp;
}
static ZCONST char *getoffset(strp, offsetp)
ZCONST char *strp;
long * ZCONST offsetp;
{
register int neg = 0;
if (*strp == '-') {
neg = 1;
++strp;
} else if (*strp == '+')
++strp;
strp = getsecs(strp, offsetp);
if (strp == NULL)
return NULL; /* illegal time */
if (neg)
*offsetp = -*offsetp;
return strp;
}
static ZCONST char *getrule(strp, rulep)
ZCONST char *strp;
struct rule * ZCONST rulep;
{
if (*strp == 'J') {
/*
** Julian day.
*/
rulep->r_type = JULIAN_DAY;
++strp;
strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
} else if (*strp == 'M') {
/*
** Month, week, day.
*/
rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
++strp;
strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
if (strp == NULL)
return NULL;
if (*strp++ != '.')
return NULL;
strp = getnum(strp, &rulep->r_week, 1, 5);
if (strp == NULL)
return NULL;
if (*strp++ != '.')
return NULL;
strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
} else if (isdigit(*strp)) {
/*
** Day of year.
*/
rulep->r_type = DAY_OF_YEAR;
strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
} else return NULL; /* invalid format */
if (strp == NULL)
return NULL;
if (*strp == '/') {
/*
** Time specified.
*/
++strp;
strp = getsecs(strp, &rulep->r_time);
} else
rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
return strp;
}
static int Parse_TZ(name, sp)
ZCONST char *name;
register struct state * ZCONST sp;
{
ZCONST char * stdname;
ZCONST char * dstname;
size_t stdlen;
size_t dstlen;
long stdoffset;
long dstoffset;
register char * cp;
dstname = NULL;
stdname = name;
name = getzname(name);
stdlen = name - stdname;
if (stdlen < 3)
return -1;
if (*name == '\0')
return -1;
name = getoffset(name, &stdoffset);
if (name == NULL)
return -1;
if (*name != '\0') {
dstname = name;
name = getzname(name);
dstlen = name - dstname; /* length of DST zone name */
if (dstlen < 3)
return -1;
if (*name != '\0' && *name != ',' && *name != ';') {
name = getoffset(name, &dstoffset);
if (name == NULL)
return -1;
} else
dstoffset = stdoffset - SECSPERHOUR;
if (*name == '\0')
name = TZDEFRULESTRING;
if (*name == ',' || *name == ';') {
struct rule start;
struct rule end;
++name;
if ((name = getrule(name, &start)) == NULL)
return -1;
if (*name++ != ',')
return -1;
if ((name = getrule(name, &end)) == NULL)
return -1;
if (*name != '\0')
return -1;
sp->typecnt = 2; /* standard time and DST */
sp->ttis[0].tt_gmtoff = -stdoffset;
sp->ttis[0].tt_isdst = 0;
sp->ttis[0].tt_abbrind = 0;
sp->ttis[1].tt_gmtoff = -dstoffset;
sp->ttis[1].tt_isdst = 1;
sp->ttis[1].tt_abbrind = stdlen + 1;
generate_transitions(sp, &start, &end);
}
} else {
dstlen = 0;
sp->typecnt = 1; /* only standard time */
sp->timecnt = 0;
sp->ttis[0].tt_gmtoff = -stdoffset;
sp->ttis[0].tt_isdst = 0;
sp->ttis[0].tt_abbrind = 0;
}
sp->charcnt = stdlen + 1;
if (dstlen != 0)
sp->charcnt += dstlen + 1;
if ((size_t) sp->charcnt > sizeof(sp->chars))
return -1;
cp = sp->chars;
(void) strncpy(cp, stdname, stdlen);
cp += stdlen;
*cp++ = '\0';
if (dstlen != 0) {
(void) strncpy(cp, dstname, dstlen);
*(cp + dstlen) = '\0';
}
return 0;
}
void tzset()
{
char *TZstring;
int dstfirst;
static char *old_TZstring = NULL;
TZstring = getenv("TZ"); /* read TZ envvar */
if (old_TZstring && TZstring && !strcmp(old_TZstring, TZstring))
/* do not repeatedly parse an unchanged TZ specification */
return;
if ((TZstring && TZstring[0] && Parse_TZ(TZstring, &statism) == 0)
|| IZTZ_GETLOCALETZINFO(&statism, generate_transitions)
|| Parse_TZ(gmt, &statism) == 0) {
daylight = statism.typecnt > 1;
dstfirst = daylight && statism.ttis[0].tt_isdst && !statism.ttis[1].tt_isdst;
timezone = -statism.ttis[dstfirst].tt_gmtoff;
tzname[0] = statism.chars + statism.ttis[dstfirst].tt_abbrind;
tzname[1] = statism.chars + statism.ttis[!dstfirst].tt_abbrind;
real_timezone_is_set = TRUE;
if (TZstring) {
if (old_TZstring)
old_TZstring = realloc(old_TZstring, strlen(TZstring) + 1);
else
old_TZstring = malloc(strlen(TZstring) + 1);
if (old_TZstring)
strcpy(old_TZstring, TZstring);
}
} else {
timezone = 0; /* default is GMT0 which means no offsets */
daylight = 0; /* from local system time */
real_timezone_is_set = FALSE;
if (old_TZstring) {
free(old_TZstring);
old_TZstring = NULL;
}
}
#ifdef IZTZ_SETLOCALTZINFO
/* Some SAS/C library functions, e.g. stat(), call library */
/* __tzset() themselves. So envvar TZ *must* exist in order to */
/* to get the right offset from GMT. XXX TRY HARD to fix this! */
set_TZ(timezone, daylight);
#endif /* IZTZ_SETLOCALTZINFO */
}
/* XXX Does this also help SAS/C library work? */
void __tzset()
{
if (!real_timezone_is_set) tzset();
}
static struct tm _tmbuf;
struct tm *gmtime(when)
ZCONST time_t *when;
{
long days = *when / SECSPERDAY;
long secs = *when % SECSPERDAY;
int isleap;
memset(&_tmbuf, 0, sizeof(_tmbuf)); /* get any nonstandard fields */
_tmbuf.tm_wday = (days + EPOCH_WDAY) % 7;
_tmbuf.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
isleap = leap(_tmbuf.tm_year + TM_YEAR_BASE);
while (days >= year_lengths[isleap]) {
days -= year_lengths[isleap];
_tmbuf.tm_year++;
isleap = leap(_tmbuf.tm_year + TM_YEAR_BASE);
}
_tmbuf.tm_mon = 0;
_tmbuf.tm_yday = days;
while (days >= mon_lengths[isleap][_tmbuf.tm_mon])
days -= mon_lengths[isleap][_tmbuf.tm_mon++];
_tmbuf.tm_mday = days + 1;
_tmbuf.tm_isdst = 0;
_tmbuf.tm_sec = secs % SECSPERMIN;
_tmbuf.tm_min = (secs / SECSPERMIN) % SECSPERMIN;
_tmbuf.tm_hour = secs / SECSPERHOUR;
return &_tmbuf;
}
struct tm *localtime(when)
ZCONST time_t *when;
{
time_t localwhen = *when;
int timetype;
struct tm *ret;
__tzset();
if (statism.timecnt == 0 || localwhen < statism.ats[0])
timetype = statism.ttis[0].tt_isdst && statism.typecnt > 1 &&
!statism.ttis[1].tt_isdst;
else {
for (timetype = 1; timetype < statism.timecnt; ++timetype)
if (localwhen < statism.ats[timetype])
break;
timetype = statism.types[timetype - 1];
}
localwhen += statism.ttis[timetype].tt_gmtoff;
ret = gmtime(&localwhen);
ret->tm_isdst = statism.ttis[timetype].tt_isdst;
return ret;
}
#ifdef NEED__ISINDST
int _isindst(tb)
struct tm *tb;
{
time_t localt; /* time_t equivalent of given tm struct */
time_t univt; /* assumed UTC value of given time */
long tzoffset_adj; /* timezone-adjustment `remainder' */
int bailout_cnt; /* counter of tries for tz correction */
int timetype;
__tzset();
/* when DST is unsupported in current timezone, DST is always off */
if (statism.typecnt <= 1) return FALSE;
localt = mkgmtime(tb);
if (localt == (time_t)-1)
/* specified time is out-of-range, default to FALSE */
return FALSE;
univt = localt - statism.ttis[0].tt_gmtoff;
bailout_cnt = 3;
do {
if (statism.timecnt == 0 || univt < statism.ats[0])
timetype = statism.ttis[0].tt_isdst && statism.typecnt > 1 &&
!statism.ttis[1].tt_isdst;
else {
for (timetype = 1; timetype < statism.timecnt; ++timetype)
if (univt < statism.ats[timetype])
break;
timetype = statism.types[timetype - 1];
}
if ((tzoffset_adj = localt - univt - statism.ttis[timetype].tt_gmtoff)
== 0L)
break;
univt += tzoffset_adj;
} while (--bailout_cnt > 0);
/* return TRUE when DST is active at given time */
return (statism.ttis[timetype].tt_isdst);
}
#endif /* NEED__ISINDST */
#endif /* !IZ_MKTIME_ONLY */
/* Return the equivalent in seconds past 12:00:00 a.m. Jan 1, 1970 GMT
of the local time and date in the exploded time structure `tm',
adjust out of range fields in `tm' and set `tm->tm_yday', `tm->tm_wday'.
If `tm->tm_isdst < 0' was passed to mktime(), the correct setting of
tm_isdst is determined and returned. Otherwise, mktime() assumes this
field as valid; its information is used when converting local time
to UTC.
Return -1 if time in `tm' cannot be represented as time_t value. */
time_t mktime(tm)
struct tm *tm;
{
struct tm *ltm; /* Local time. */
time_t loctime; /* The time_t value of local time. */
time_t then; /* The time to return. */
long tzoffset_adj; /* timezone-adjustment `remainder' */
int bailout_cnt; /* counter of tries for tz correction */
int save_isdst; /* Copy of the tm->isdst input value */
save_isdst = tm->tm_isdst;
loctime = mkgmtime(tm);
if (loctime == -1) {
tm->tm_isdst = save_isdst;
return (time_t)-1;
}
/* Correct for the timezone and any daylight savings time.
The correction is verified and repeated when not correct, to
take into account the rare case that a change to or from daylight
savings time occurs between when it is the time in `tm' locally
and when it is that time in Greenwich. After the second correction,
the "timezone & daylight" offset should be correct in all cases. To
be sure, we allow a third try, but then the loop is stopped. */
bailout_cnt = 3;
then = loctime;
do {
ltm = localtime(&then);
if (ltm == (struct tm *)NULL ||
(tzoffset_adj = loctime - mkgmtime(ltm)) == 0L)
break;
then += tzoffset_adj;
} while (--bailout_cnt > 0);
if (ltm == (struct tm *)NULL || tzoffset_adj != 0L) {
/* Signal failure if timezone adjustment did not converge. */
tm->tm_isdst = save_isdst;
return (time_t)-1;
}
if (save_isdst >= 0) {
if (ltm->tm_isdst && !save_isdst)
{
if (then + 3600 < then)
then = (time_t)-1;
else
then += 3600;
}
else if (!ltm->tm_isdst && save_isdst)
{
if (then - 3600 > then)
then = (time_t)-1;
else
then -= 3600;
}
ltm->tm_isdst = save_isdst;
}
if (tm != ltm) /* `tm' may already point to localtime's internal storage */
*tm = *ltm;
return then;
}
#ifndef NO_TIME_T_MAX
/* Provide default values for the upper limit of the time_t range.
These are the result of the decomposition into a `struct tm' for
the time value 0xFFFFFFFEL ( = (time_t)-2 ).
Note: `(time_t)-1' is reserved for "invalid time"! */
# ifndef TM_YEAR_MAX
# define TM_YEAR_MAX 2106
# endif
# ifndef TM_MON_MAX
# define TM_MON_MAX 1 /* February */
# endif
# ifndef TM_MDAY_MAX
# define TM_MDAY_MAX 7
# endif
# ifndef TM_HOUR_MAX
# define TM_HOUR_MAX 6
# endif
# ifndef TM_MIN_MAX
# define TM_MIN_MAX 28
# endif
# ifndef TM_SEC_MAX
# define TM_SEC_MAX 14
# endif
#endif /* NO_TIME_T_MAX */
/* Adjusts out-of-range values for `tm' field `tm_member'. */
#define ADJUST_TM(tm_member, tm_carry, modulus) \
if ((tm_member) < 0) { \
tm_carry -= (1 - ((tm_member)+1) / (modulus)); \
tm_member = (modulus-1) + (((tm_member)+1) % (modulus)); \
} else if ((tm_member) >= (modulus)) { \
tm_carry += (tm_member) / (modulus); \
tm_member = (tm_member) % (modulus); \
}
/* Return the equivalent in seconds past 12:00:00 a.m. Jan 1, 1970 GMT
of the Greenwich Mean time and date in the exploded time structure `tm'.
This function does always put back normalized values into the `tm' struct,
parameter, including the calculated numbers for `tm->tm_yday',
`tm->tm_wday', and `tm->tm_isdst'.
Returns -1 if the time in the `tm' parameter cannot be represented
as valid `time_t' number. */
time_t mkgmtime(tm)
struct tm *tm;
{
int years, months, days, hours, minutes, seconds;
years = tm->tm_year + TM_YEAR_BASE; /* year - 1900 -> year */
months = tm->tm_mon; /* 0..11 */
days = tm->tm_mday - 1; /* 1..31 -> 0..30 */
hours = tm->tm_hour; /* 0..23 */
minutes = tm->tm_min; /* 0..59 */
seconds = tm->tm_sec; /* 0..61 in ANSI C. */
ADJUST_TM(seconds, minutes, 60)
ADJUST_TM(minutes, hours, 60)
ADJUST_TM(hours, days, 24)
ADJUST_TM(months, years, 12)
if (days < 0)
do {
if (--months < 0) {
--years;
months = 11;
}
days += monthlen(months, years);
} while (days < 0);
else
while (days >= monthlen(months, years)) {
days -= monthlen(months, years);
if (++months >= 12) {
++years;
months = 0;
}
}
/* Restore adjusted values in tm structure */
tm->tm_year = years - TM_YEAR_BASE;
tm->tm_mon = months;
tm->tm_mday = days + 1;
tm->tm_hour = hours;
tm->tm_min = minutes;
tm->tm_sec = seconds;
/* Set `days' to the number of days into the year. */
days += YDAYS(months, years);
tm->tm_yday = days;
/* Now calculate `days' to the number of days since Jan 1, 1970. */
days = (unsigned)days + 365 * (unsigned)(years - EPOCH_YEAR) +
(unsigned)(nleap (years));
tm->tm_wday = ((unsigned)days + EPOCH_WDAY) % 7;
tm->tm_isdst = 0;
if (years < EPOCH_YEAR)
return (time_t)-1;
#if (defined(TM_YEAR_MAX) && defined(TM_MON_MAX) && defined(TM_MDAY_MAX))
#if (defined(TM_HOUR_MAX) && defined(TM_MIN_MAX) && defined(TM_SEC_MAX))
if (years > TM_YEAR_MAX ||
(years == TM_YEAR_MAX &&
(tm->tm_yday > (YDAYS(TM_MON_MAX, TM_YEAR_MAX) + (TM_MDAY_MAX - 1)) ||
(tm->tm_yday == (YDAYS(TM_MON_MAX, TM_YEAR_MAX) + (TM_MDAY_MAX - 1)) &&
(hours > TM_HOUR_MAX ||
(hours == TM_HOUR_MAX &&
(minutes > TM_MIN_MAX ||
(minutes == TM_MIN_MAX && seconds > TM_SEC_MAX) )))))))
return (time_t)-1;
#endif
#endif
return (time_t)(SECSPERDAY * (unsigned long)(unsigned)days +
SECSPERHOUR * (unsigned long)hours +
(unsigned long)(SECSPERMIN * minutes + seconds));
}
#endif /* __timezone_c */
|