localtime.c revision 1.1.1.12 1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson (arthur_david_olson (at) nih.gov).
4 */
5
6 #ifndef lint
7 #ifndef NOID
8 static char elsieid[] = "@(#)localtime.c 7.76";
9 #endif /* !defined NOID */
10 #endif /* !defined lint */
11
12 /*
13 ** Leap second handling from Bradley White (bww (at) k.gp.cs.cmu.edu).
14 ** POSIX-style TZ environment variable handling from Guy Harris
15 ** (guy (at) auspex.com).
16 */
17
18 /*LINTLIBRARY*/
19
20 #include "private.h"
21 #include "tzfile.h"
22 #include "fcntl.h"
23
24 /*
25 ** SunOS 4.1.1 headers lack O_BINARY.
26 */
27
28 #ifdef O_BINARY
29 #define OPEN_MODE (O_RDONLY | O_BINARY)
30 #endif /* defined O_BINARY */
31 #ifndef O_BINARY
32 #define OPEN_MODE O_RDONLY
33 #endif /* !defined O_BINARY */
34
35 #ifndef WILDABBR
36 /*
37 ** Someone might make incorrect use of a time zone abbreviation:
38 ** 1. They might reference tzname[0] before calling tzset (explicitly
39 ** or implicitly).
40 ** 2. They might reference tzname[1] before calling tzset (explicitly
41 ** or implicitly).
42 ** 3. They might reference tzname[1] after setting to a time zone
43 ** in which Daylight Saving Time is never observed.
44 ** 4. They might reference tzname[0] after setting to a time zone
45 ** in which Standard Time is never observed.
46 ** 5. They might reference tm.TM_ZONE after calling offtime.
47 ** What's best to do in the above cases is open to debate;
48 ** for now, we just set things up so that in any of the five cases
49 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
50 ** string "tzname[0] used before set", and similarly for the other cases.
51 ** And another: initialize tzname[0] to "ERA", with an explanation in the
52 ** manual page of what this "time zone abbreviation" means (doing this so
53 ** that tzname[0] has the "normal" length of three characters).
54 */
55 #define WILDABBR " "
56 #endif /* !defined WILDABBR */
57
58 static char wildabbr[] = "WILDABBR";
59
60 static const char gmt[] = "GMT";
61
62 /*
63 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
64 ** We default to US rules as of 1999-08-17.
65 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
66 ** implementation dependent; for historical reasons, US rules are a
67 ** common default.
68 */
69 #ifndef TZDEFRULESTRING
70 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
71 #endif /* !defined TZDEFDST */
72
73 struct ttinfo { /* time type information */
74 long tt_gmtoff; /* UTC offset in seconds */
75 int tt_isdst; /* used to set tm_isdst */
76 int tt_abbrind; /* abbreviation list index */
77 int tt_ttisstd; /* TRUE if transition is std time */
78 int tt_ttisgmt; /* TRUE if transition is UTC */
79 };
80
81 struct lsinfo { /* leap second information */
82 time_t ls_trans; /* transition time */
83 long ls_corr; /* correction to apply */
84 };
85
86 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
87
88 #ifdef TZNAME_MAX
89 #define MY_TZNAME_MAX TZNAME_MAX
90 #endif /* defined TZNAME_MAX */
91 #ifndef TZNAME_MAX
92 #define MY_TZNAME_MAX 255
93 #endif /* !defined TZNAME_MAX */
94
95 struct state {
96 int leapcnt;
97 int timecnt;
98 int typecnt;
99 int charcnt;
100 time_t ats[TZ_MAX_TIMES];
101 unsigned char types[TZ_MAX_TIMES];
102 struct ttinfo ttis[TZ_MAX_TYPES];
103 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
104 (2 * (MY_TZNAME_MAX + 1)))];
105 struct lsinfo lsis[TZ_MAX_LEAPS];
106 };
107
108 struct rule {
109 int r_type; /* type of rule--see below */
110 int r_day; /* day number of rule */
111 int r_week; /* week number of rule */
112 int r_mon; /* month number of rule */
113 long r_time; /* transition time of rule */
114 };
115
116 #define JULIAN_DAY 0 /* Jn - Julian day */
117 #define DAY_OF_YEAR 1 /* n - day of year */
118 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
119
120 /*
121 ** Prototypes for static functions.
122 */
123
124 static long detzcode P((const char * codep));
125 static const char * getzname P((const char * strp));
126 static const char * getnum P((const char * strp, int * nump, int min,
127 int max));
128 static const char * getsecs P((const char * strp, long * secsp));
129 static const char * getoffset P((const char * strp, long * offsetp));
130 static const char * getrule P((const char * strp, struct rule * rulep));
131 static void gmtload P((struct state * sp));
132 static void gmtsub P((const time_t * timep, long offset,
133 struct tm * tmp));
134 static void localsub P((const time_t * timep, long offset,
135 struct tm * tmp));
136 static int increment_overflow P((int * number, int delta));
137 static int normalize_overflow P((int * tensptr, int * unitsptr,
138 int base));
139 static void settzname P((void));
140 static time_t time1 P((struct tm * tmp,
141 void(*funcp) P((const time_t *,
142 long, struct tm *)),
143 long offset));
144 static time_t time2 P((struct tm *tmp,
145 void(*funcp) P((const time_t *,
146 long, struct tm*)),
147 long offset, int * okayp));
148 static time_t time2sub P((struct tm *tmp,
149 void(*funcp) P((const time_t *,
150 long, struct tm*)),
151 long offset, int * okayp, int do_norm_secs));
152 static void timesub P((const time_t * timep, long offset,
153 const struct state * sp, struct tm * tmp));
154 static int tmcomp P((const struct tm * atmp,
155 const struct tm * btmp));
156 static time_t transtime P((time_t janfirst, int year,
157 const struct rule * rulep, long offset));
158 static int tzload P((const char * name, struct state * sp));
159 static int tzparse P((const char * name, struct state * sp,
160 int lastditch));
161
162 #ifdef ALL_STATE
163 static struct state * lclptr;
164 static struct state * gmtptr;
165 #endif /* defined ALL_STATE */
166
167 #ifndef ALL_STATE
168 static struct state lclmem;
169 static struct state gmtmem;
170 #define lclptr (&lclmem)
171 #define gmtptr (&gmtmem)
172 #endif /* State Farm */
173
174 #ifndef TZ_STRLEN_MAX
175 #define TZ_STRLEN_MAX 255
176 #endif /* !defined TZ_STRLEN_MAX */
177
178 static char lcl_TZname[TZ_STRLEN_MAX + 1];
179 static int lcl_is_set;
180 static int gmt_is_set;
181
182 char * tzname[2] = {
183 wildabbr,
184 wildabbr
185 };
186
187 /*
188 ** Section 4.12.3 of X3.159-1989 requires that
189 ** Except for the strftime function, these functions [asctime,
190 ** ctime, gmtime, localtime] return values in one of two static
191 ** objects: a broken-down time structure and an array of char.
192 ** Thanks to Paul Eggert (eggert (at) twinsun.com) for noting this.
193 */
194
195 static struct tm tm;
196
197 #ifdef USG_COMPAT
198 time_t timezone = 0;
199 int daylight = 0;
200 #endif /* defined USG_COMPAT */
201
202 #ifdef ALTZONE
203 time_t altzone = 0;
204 #endif /* defined ALTZONE */
205
206 static long
207 detzcode(codep)
208 const char * const codep;
209 {
210 register long result;
211 register int i;
212
213 result = (codep[0] & 0x80) ? ~0L : 0L;
214 for (i = 0; i < 4; ++i)
215 result = (result << 8) | (codep[i] & 0xff);
216 return result;
217 }
218
219 static void
220 settzname P((void))
221 {
222 register struct state * const sp = lclptr;
223 register int i;
224
225 tzname[0] = wildabbr;
226 tzname[1] = wildabbr;
227 #ifdef USG_COMPAT
228 daylight = 0;
229 timezone = 0;
230 #endif /* defined USG_COMPAT */
231 #ifdef ALTZONE
232 altzone = 0;
233 #endif /* defined ALTZONE */
234 #ifdef ALL_STATE
235 if (sp == NULL) {
236 tzname[0] = tzname[1] = gmt;
237 return;
238 }
239 #endif /* defined ALL_STATE */
240 for (i = 0; i < sp->typecnt; ++i) {
241 register const struct ttinfo * const ttisp = &sp->ttis[i];
242
243 tzname[ttisp->tt_isdst] =
244 &sp->chars[ttisp->tt_abbrind];
245 #ifdef USG_COMPAT
246 if (ttisp->tt_isdst)
247 daylight = 1;
248 if (i == 0 || !ttisp->tt_isdst)
249 timezone = -(ttisp->tt_gmtoff);
250 #endif /* defined USG_COMPAT */
251 #ifdef ALTZONE
252 if (i == 0 || ttisp->tt_isdst)
253 altzone = -(ttisp->tt_gmtoff);
254 #endif /* defined ALTZONE */
255 }
256 /*
257 ** And to get the latest zone names into tzname. . .
258 */
259 for (i = 0; i < sp->timecnt; ++i) {
260 register const struct ttinfo * const ttisp =
261 &sp->ttis[
262 sp->types[i]];
263
264 tzname[ttisp->tt_isdst] =
265 &sp->chars[ttisp->tt_abbrind];
266 }
267 }
268
269 static int
270 tzload(name, sp)
271 register const char * name;
272 register struct state * const sp;
273 {
274 register const char * p;
275 register int i;
276 register int fid;
277
278 if (name == NULL && (name = TZDEFAULT) == NULL)
279 return -1;
280 {
281 register int doaccess;
282 /*
283 ** Section 4.9.1 of the C standard says that
284 ** "FILENAME_MAX expands to an integral constant expression
285 ** that is the size needed for an array of char large enough
286 ** to hold the longest file name string that the implementation
287 ** guarantees can be opened."
288 */
289 char fullname[FILENAME_MAX + 1];
290
291 if (name[0] == ':')
292 ++name;
293 doaccess = name[0] == '/';
294 if (!doaccess) {
295 if ((p = TZDIR) == NULL)
296 return -1;
297 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
298 return -1;
299 (void) strcpy(fullname, p);
300 (void) strcat(fullname, "/");
301 (void) strcat(fullname, name);
302 /*
303 ** Set doaccess if '.' (as in "../") shows up in name.
304 */
305 if (strchr(name, '.') != NULL)
306 doaccess = TRUE;
307 name = fullname;
308 }
309 if (doaccess && access(name, R_OK) != 0)
310 return -1;
311 if ((fid = open(name, OPEN_MODE)) == -1)
312 return -1;
313 }
314 {
315 struct tzhead * tzhp;
316 union {
317 struct tzhead tzhead;
318 char buf[sizeof *sp + sizeof *tzhp];
319 } u;
320 int ttisstdcnt;
321 int ttisgmtcnt;
322
323 i = read(fid, u.buf, sizeof u.buf);
324 if (close(fid) != 0)
325 return -1;
326 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
327 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
328 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
329 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
330 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
331 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
332 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
333 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
334 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
335 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
336 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
337 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
338 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
339 return -1;
340 if (i - (p - u.buf) < sp->timecnt * 4 + /* ats */
341 sp->timecnt + /* types */
342 sp->typecnt * (4 + 2) + /* ttinfos */
343 sp->charcnt + /* chars */
344 sp->leapcnt * (4 + 4) + /* lsinfos */
345 ttisstdcnt + /* ttisstds */
346 ttisgmtcnt) /* ttisgmts */
347 return -1;
348 for (i = 0; i < sp->timecnt; ++i) {
349 sp->ats[i] = detzcode(p);
350 p += 4;
351 }
352 for (i = 0; i < sp->timecnt; ++i) {
353 sp->types[i] = (unsigned char) *p++;
354 if (sp->types[i] >= sp->typecnt)
355 return -1;
356 }
357 for (i = 0; i < sp->typecnt; ++i) {
358 register struct ttinfo * ttisp;
359
360 ttisp = &sp->ttis[i];
361 ttisp->tt_gmtoff = detzcode(p);
362 p += 4;
363 ttisp->tt_isdst = (unsigned char) *p++;
364 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
365 return -1;
366 ttisp->tt_abbrind = (unsigned char) *p++;
367 if (ttisp->tt_abbrind < 0 ||
368 ttisp->tt_abbrind > sp->charcnt)
369 return -1;
370 }
371 for (i = 0; i < sp->charcnt; ++i)
372 sp->chars[i] = *p++;
373 sp->chars[i] = '\0'; /* ensure '\0' at end */
374 for (i = 0; i < sp->leapcnt; ++i) {
375 register struct lsinfo * lsisp;
376
377 lsisp = &sp->lsis[i];
378 lsisp->ls_trans = detzcode(p);
379 p += 4;
380 lsisp->ls_corr = detzcode(p);
381 p += 4;
382 }
383 for (i = 0; i < sp->typecnt; ++i) {
384 register struct ttinfo * ttisp;
385
386 ttisp = &sp->ttis[i];
387 if (ttisstdcnt == 0)
388 ttisp->tt_ttisstd = FALSE;
389 else {
390 ttisp->tt_ttisstd = *p++;
391 if (ttisp->tt_ttisstd != TRUE &&
392 ttisp->tt_ttisstd != FALSE)
393 return -1;
394 }
395 }
396 for (i = 0; i < sp->typecnt; ++i) {
397 register struct ttinfo * ttisp;
398
399 ttisp = &sp->ttis[i];
400 if (ttisgmtcnt == 0)
401 ttisp->tt_ttisgmt = FALSE;
402 else {
403 ttisp->tt_ttisgmt = *p++;
404 if (ttisp->tt_ttisgmt != TRUE &&
405 ttisp->tt_ttisgmt != FALSE)
406 return -1;
407 }
408 }
409 }
410 return 0;
411 }
412
413 static const int mon_lengths[2][MONSPERYEAR] = {
414 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
415 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
416 };
417
418 static const int year_lengths[2] = {
419 DAYSPERNYEAR, DAYSPERLYEAR
420 };
421
422 /*
423 ** Given a pointer into a time zone string, scan until a character that is not
424 ** a valid character in a zone name is found. Return a pointer to that
425 ** character.
426 */
427
428 static const char *
429 getzname(strp)
430 register const char * strp;
431 {
432 register char c;
433
434 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
435 c != '+')
436 ++strp;
437 return strp;
438 }
439
440 /*
441 ** Given a pointer into a time zone string, extract a number from that string.
442 ** Check that the number is within a specified range; if it is not, return
443 ** NULL.
444 ** Otherwise, return a pointer to the first character not part of the number.
445 */
446
447 static const char *
448 getnum(strp, nump, min, max)
449 register const char * strp;
450 int * const nump;
451 const int min;
452 const int max;
453 {
454 register char c;
455 register int num;
456
457 if (strp == NULL || !is_digit(c = *strp))
458 return NULL;
459 num = 0;
460 do {
461 num = num * 10 + (c - '0');
462 if (num > max)
463 return NULL; /* illegal value */
464 c = *++strp;
465 } while (is_digit(c));
466 if (num < min)
467 return NULL; /* illegal value */
468 *nump = num;
469 return strp;
470 }
471
472 /*
473 ** Given a pointer into a time zone string, extract a number of seconds,
474 ** in hh[:mm[:ss]] form, from the string.
475 ** If any error occurs, return NULL.
476 ** Otherwise, return a pointer to the first character not part of the number
477 ** of seconds.
478 */
479
480 static const char *
481 getsecs(strp, secsp)
482 register const char * strp;
483 long * const secsp;
484 {
485 int num;
486
487 /*
488 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
489 ** "M10.4.6/26", which does not conform to Posix,
490 ** but which specifies the equivalent of
491 ** ``02:00 on the first Sunday on or after 23 Oct''.
492 */
493 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
494 if (strp == NULL)
495 return NULL;
496 *secsp = num * (long) SECSPERHOUR;
497 if (*strp == ':') {
498 ++strp;
499 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
500 if (strp == NULL)
501 return NULL;
502 *secsp += num * SECSPERMIN;
503 if (*strp == ':') {
504 ++strp;
505 /* `SECSPERMIN' allows for leap seconds. */
506 strp = getnum(strp, &num, 0, SECSPERMIN);
507 if (strp == NULL)
508 return NULL;
509 *secsp += num;
510 }
511 }
512 return strp;
513 }
514
515 /*
516 ** Given a pointer into a time zone string, extract an offset, in
517 ** [+-]hh[:mm[:ss]] form, from the string.
518 ** If any error occurs, return NULL.
519 ** Otherwise, return a pointer to the first character not part of the time.
520 */
521
522 static const char *
523 getoffset(strp, offsetp)
524 register const char * strp;
525 long * const offsetp;
526 {
527 register int neg = 0;
528
529 if (*strp == '-') {
530 neg = 1;
531 ++strp;
532 } else if (*strp == '+')
533 ++strp;
534 strp = getsecs(strp, offsetp);
535 if (strp == NULL)
536 return NULL; /* illegal time */
537 if (neg)
538 *offsetp = -*offsetp;
539 return strp;
540 }
541
542 /*
543 ** Given a pointer into a time zone string, extract a rule in the form
544 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
545 ** If a valid rule is not found, return NULL.
546 ** Otherwise, return a pointer to the first character not part of the rule.
547 */
548
549 static const char *
550 getrule(strp, rulep)
551 const char * strp;
552 register struct rule * const rulep;
553 {
554 if (*strp == 'J') {
555 /*
556 ** Julian day.
557 */
558 rulep->r_type = JULIAN_DAY;
559 ++strp;
560 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
561 } else if (*strp == 'M') {
562 /*
563 ** Month, week, day.
564 */
565 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
566 ++strp;
567 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
568 if (strp == NULL)
569 return NULL;
570 if (*strp++ != '.')
571 return NULL;
572 strp = getnum(strp, &rulep->r_week, 1, 5);
573 if (strp == NULL)
574 return NULL;
575 if (*strp++ != '.')
576 return NULL;
577 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
578 } else if (is_digit(*strp)) {
579 /*
580 ** Day of year.
581 */
582 rulep->r_type = DAY_OF_YEAR;
583 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
584 } else return NULL; /* invalid format */
585 if (strp == NULL)
586 return NULL;
587 if (*strp == '/') {
588 /*
589 ** Time specified.
590 */
591 ++strp;
592 strp = getsecs(strp, &rulep->r_time);
593 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
594 return strp;
595 }
596
597 /*
598 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
599 ** year, a rule, and the offset from UTC at the time that rule takes effect,
600 ** calculate the Epoch-relative time that rule takes effect.
601 */
602
603 static time_t
604 transtime(janfirst, year, rulep, offset)
605 const time_t janfirst;
606 const int year;
607 register const struct rule * const rulep;
608 const long offset;
609 {
610 register int leapyear;
611 register time_t value;
612 register int i;
613 int d, m1, yy0, yy1, yy2, dow;
614
615 INITIALIZE(value);
616 leapyear = isleap(year);
617 switch (rulep->r_type) {
618
619 case JULIAN_DAY:
620 /*
621 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
622 ** years.
623 ** In non-leap years, or if the day number is 59 or less, just
624 ** add SECSPERDAY times the day number-1 to the time of
625 ** January 1, midnight, to get the day.
626 */
627 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
628 if (leapyear && rulep->r_day >= 60)
629 value += SECSPERDAY;
630 break;
631
632 case DAY_OF_YEAR:
633 /*
634 ** n - day of year.
635 ** Just add SECSPERDAY times the day number to the time of
636 ** January 1, midnight, to get the day.
637 */
638 value = janfirst + rulep->r_day * SECSPERDAY;
639 break;
640
641 case MONTH_NTH_DAY_OF_WEEK:
642 /*
643 ** Mm.n.d - nth "dth day" of month m.
644 */
645 value = janfirst;
646 for (i = 0; i < rulep->r_mon - 1; ++i)
647 value += mon_lengths[leapyear][i] * SECSPERDAY;
648
649 /*
650 ** Use Zeller's Congruence to get day-of-week of first day of
651 ** month.
652 */
653 m1 = (rulep->r_mon + 9) % 12 + 1;
654 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
655 yy1 = yy0 / 100;
656 yy2 = yy0 % 100;
657 dow = ((26 * m1 - 2) / 10 +
658 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
659 if (dow < 0)
660 dow += DAYSPERWEEK;
661
662 /*
663 ** "dow" is the day-of-week of the first day of the month. Get
664 ** the day-of-month (zero-origin) of the first "dow" day of the
665 ** month.
666 */
667 d = rulep->r_day - dow;
668 if (d < 0)
669 d += DAYSPERWEEK;
670 for (i = 1; i < rulep->r_week; ++i) {
671 if (d + DAYSPERWEEK >=
672 mon_lengths[leapyear][rulep->r_mon - 1])
673 break;
674 d += DAYSPERWEEK;
675 }
676
677 /*
678 ** "d" is the day-of-month (zero-origin) of the day we want.
679 */
680 value += d * SECSPERDAY;
681 break;
682 }
683
684 /*
685 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
686 ** question. To get the Epoch-relative time of the specified local
687 ** time on that day, add the transition time and the current offset
688 ** from UTC.
689 */
690 return value + rulep->r_time + offset;
691 }
692
693 /*
694 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
695 ** appropriate.
696 */
697
698 static int
699 tzparse(name, sp, lastditch)
700 const char * name;
701 register struct state * const sp;
702 const int lastditch;
703 {
704 const char * stdname;
705 const char * dstname;
706 size_t stdlen;
707 size_t dstlen;
708 long stdoffset;
709 long dstoffset;
710 register time_t * atp;
711 register unsigned char * typep;
712 register char * cp;
713 register int load_result;
714
715 INITIALIZE(dstname);
716 stdname = name;
717 if (lastditch) {
718 stdlen = strlen(name); /* length of standard zone name */
719 name += stdlen;
720 if (stdlen >= sizeof sp->chars)
721 stdlen = (sizeof sp->chars) - 1;
722 stdoffset = 0;
723 } else {
724 name = getzname(name);
725 stdlen = name - stdname;
726 if (stdlen < 3)
727 return -1;
728 if (*name == '\0')
729 return -1;
730 name = getoffset(name, &stdoffset);
731 if (name == NULL)
732 return -1;
733 }
734 load_result = tzload(TZDEFRULES, sp);
735 if (load_result != 0)
736 sp->leapcnt = 0; /* so, we're off a little */
737 if (*name != '\0') {
738 dstname = name;
739 name = getzname(name);
740 dstlen = name - dstname; /* length of DST zone name */
741 if (dstlen < 3)
742 return -1;
743 if (*name != '\0' && *name != ',' && *name != ';') {
744 name = getoffset(name, &dstoffset);
745 if (name == NULL)
746 return -1;
747 } else dstoffset = stdoffset - SECSPERHOUR;
748 if (*name == '\0' && load_result != 0)
749 name = TZDEFRULESTRING;
750 if (*name == ',' || *name == ';') {
751 struct rule start;
752 struct rule end;
753 register int year;
754 register time_t janfirst;
755 time_t starttime;
756 time_t endtime;
757
758 ++name;
759 if ((name = getrule(name, &start)) == NULL)
760 return -1;
761 if (*name++ != ',')
762 return -1;
763 if ((name = getrule(name, &end)) == NULL)
764 return -1;
765 if (*name != '\0')
766 return -1;
767 sp->typecnt = 2; /* standard time and DST */
768 /*
769 ** Two transitions per year, from EPOCH_YEAR to 2037.
770 */
771 sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
772 if (sp->timecnt > TZ_MAX_TIMES)
773 return -1;
774 sp->ttis[0].tt_gmtoff = -dstoffset;
775 sp->ttis[0].tt_isdst = 1;
776 sp->ttis[0].tt_abbrind = stdlen + 1;
777 sp->ttis[1].tt_gmtoff = -stdoffset;
778 sp->ttis[1].tt_isdst = 0;
779 sp->ttis[1].tt_abbrind = 0;
780 atp = sp->ats;
781 typep = sp->types;
782 janfirst = 0;
783 for (year = EPOCH_YEAR; year <= 2037; ++year) {
784 starttime = transtime(janfirst, year, &start,
785 stdoffset);
786 endtime = transtime(janfirst, year, &end,
787 dstoffset);
788 if (starttime > endtime) {
789 *atp++ = endtime;
790 *typep++ = 1; /* DST ends */
791 *atp++ = starttime;
792 *typep++ = 0; /* DST begins */
793 } else {
794 *atp++ = starttime;
795 *typep++ = 0; /* DST begins */
796 *atp++ = endtime;
797 *typep++ = 1; /* DST ends */
798 }
799 janfirst += year_lengths[isleap(year)] *
800 SECSPERDAY;
801 }
802 } else {
803 register long theirstdoffset;
804 register long theirdstoffset;
805 register long theiroffset;
806 register int isdst;
807 register int i;
808 register int j;
809
810 if (*name != '\0')
811 return -1;
812 /*
813 ** Initial values of theirstdoffset and theirdstoffset.
814 */
815 theirstdoffset = 0;
816 for (i = 0; i < sp->timecnt; ++i) {
817 j = sp->types[i];
818 if (!sp->ttis[j].tt_isdst) {
819 theirstdoffset =
820 -sp->ttis[j].tt_gmtoff;
821 break;
822 }
823 }
824 theirdstoffset = 0;
825 for (i = 0; i < sp->timecnt; ++i) {
826 j = sp->types[i];
827 if (sp->ttis[j].tt_isdst) {
828 theirdstoffset =
829 -sp->ttis[j].tt_gmtoff;
830 break;
831 }
832 }
833 /*
834 ** Initially we're assumed to be in standard time.
835 */
836 isdst = FALSE;
837 theiroffset = theirstdoffset;
838 /*
839 ** Now juggle transition times and types
840 ** tracking offsets as you do.
841 */
842 for (i = 0; i < sp->timecnt; ++i) {
843 j = sp->types[i];
844 sp->types[i] = sp->ttis[j].tt_isdst;
845 if (sp->ttis[j].tt_ttisgmt) {
846 /* No adjustment to transition time */
847 } else {
848 /*
849 ** If summer time is in effect, and the
850 ** transition time was not specified as
851 ** standard time, add the summer time
852 ** offset to the transition time;
853 ** otherwise, add the standard time
854 ** offset to the transition time.
855 */
856 /*
857 ** Transitions from DST to DDST
858 ** will effectively disappear since
859 ** POSIX provides for only one DST
860 ** offset.
861 */
862 if (isdst && !sp->ttis[j].tt_ttisstd) {
863 sp->ats[i] += dstoffset -
864 theirdstoffset;
865 } else {
866 sp->ats[i] += stdoffset -
867 theirstdoffset;
868 }
869 }
870 theiroffset = -sp->ttis[j].tt_gmtoff;
871 if (sp->ttis[j].tt_isdst)
872 theirdstoffset = theiroffset;
873 else theirstdoffset = theiroffset;
874 }
875 /*
876 ** Finally, fill in ttis.
877 ** ttisstd and ttisgmt need not be handled.
878 */
879 sp->ttis[0].tt_gmtoff = -stdoffset;
880 sp->ttis[0].tt_isdst = FALSE;
881 sp->ttis[0].tt_abbrind = 0;
882 sp->ttis[1].tt_gmtoff = -dstoffset;
883 sp->ttis[1].tt_isdst = TRUE;
884 sp->ttis[1].tt_abbrind = stdlen + 1;
885 sp->typecnt = 2;
886 }
887 } else {
888 dstlen = 0;
889 sp->typecnt = 1; /* only standard time */
890 sp->timecnt = 0;
891 sp->ttis[0].tt_gmtoff = -stdoffset;
892 sp->ttis[0].tt_isdst = 0;
893 sp->ttis[0].tt_abbrind = 0;
894 }
895 sp->charcnt = stdlen + 1;
896 if (dstlen != 0)
897 sp->charcnt += dstlen + 1;
898 if ((size_t) sp->charcnt > sizeof sp->chars)
899 return -1;
900 cp = sp->chars;
901 (void) strncpy(cp, stdname, stdlen);
902 cp += stdlen;
903 *cp++ = '\0';
904 if (dstlen != 0) {
905 (void) strncpy(cp, dstname, dstlen);
906 *(cp + dstlen) = '\0';
907 }
908 return 0;
909 }
910
911 static void
912 gmtload(sp)
913 struct state * const sp;
914 {
915 if (tzload(gmt, sp) != 0)
916 (void) tzparse(gmt, sp, TRUE);
917 }
918
919 #ifndef STD_INSPIRED
920 /*
921 ** A non-static declaration of tzsetwall in a system header file
922 ** may cause a warning about this upcoming static declaration...
923 */
924 static
925 #endif /* !defined STD_INSPIRED */
926 void
927 tzsetwall P((void))
928 {
929 if (lcl_is_set < 0)
930 return;
931 lcl_is_set = -1;
932
933 #ifdef ALL_STATE
934 if (lclptr == NULL) {
935 lclptr = (struct state *) malloc(sizeof *lclptr);
936 if (lclptr == NULL) {
937 settzname(); /* all we can do */
938 return;
939 }
940 }
941 #endif /* defined ALL_STATE */
942 if (tzload((char *) NULL, lclptr) != 0)
943 gmtload(lclptr);
944 settzname();
945 }
946
947 void
948 tzset P((void))
949 {
950 register const char * name;
951
952 name = getenv("TZ");
953 if (name == NULL) {
954 tzsetwall();
955 return;
956 }
957
958 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
959 return;
960 lcl_is_set = strlen(name) < sizeof lcl_TZname;
961 if (lcl_is_set)
962 (void) strcpy(lcl_TZname, name);
963
964 #ifdef ALL_STATE
965 if (lclptr == NULL) {
966 lclptr = (struct state *) malloc(sizeof *lclptr);
967 if (lclptr == NULL) {
968 settzname(); /* all we can do */
969 return;
970 }
971 }
972 #endif /* defined ALL_STATE */
973 if (*name == '\0') {
974 /*
975 ** User wants it fast rather than right.
976 */
977 lclptr->leapcnt = 0; /* so, we're off a little */
978 lclptr->timecnt = 0;
979 lclptr->typecnt = 0;
980 lclptr->ttis[0].tt_isdst = 0;
981 lclptr->ttis[0].tt_gmtoff = 0;
982 lclptr->ttis[0].tt_abbrind = 0;
983 (void) strcpy(lclptr->chars, gmt);
984 } else if (tzload(name, lclptr) != 0)
985 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
986 (void) gmtload(lclptr);
987 settzname();
988 }
989
990 /*
991 ** The easy way to behave "as if no library function calls" localtime
992 ** is to not call it--so we drop its guts into "localsub", which can be
993 ** freely called. (And no, the PANS doesn't require the above behavior--
994 ** but it *is* desirable.)
995 **
996 ** The unused offset argument is for the benefit of mktime variants.
997 */
998
999 /*ARGSUSED*/
1000 static void
1001 localsub(timep, offset, tmp)
1002 const time_t * const timep;
1003 const long offset;
1004 struct tm * const tmp;
1005 {
1006 register struct state * sp;
1007 register const struct ttinfo * ttisp;
1008 register int i;
1009 const time_t t = *timep;
1010
1011 sp = lclptr;
1012 #ifdef ALL_STATE
1013 if (sp == NULL) {
1014 gmtsub(timep, offset, tmp);
1015 return;
1016 }
1017 #endif /* defined ALL_STATE */
1018 if (sp->timecnt == 0 || t < sp->ats[0]) {
1019 i = 0;
1020 while (sp->ttis[i].tt_isdst)
1021 if (++i >= sp->typecnt) {
1022 i = 0;
1023 break;
1024 }
1025 } else {
1026 for (i = 1; i < sp->timecnt; ++i)
1027 if (t < sp->ats[i])
1028 break;
1029 i = sp->types[i - 1];
1030 }
1031 ttisp = &sp->ttis[i];
1032 /*
1033 ** To get (wrong) behavior that's compatible with System V Release 2.0
1034 ** you'd replace the statement below with
1035 ** t += ttisp->tt_gmtoff;
1036 ** timesub(&t, 0L, sp, tmp);
1037 */
1038 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1039 tmp->tm_isdst = ttisp->tt_isdst;
1040 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1041 #ifdef TM_ZONE
1042 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1043 #endif /* defined TM_ZONE */
1044 }
1045
1046 struct tm *
1047 localtime(timep)
1048 const time_t * const timep;
1049 {
1050 tzset();
1051 localsub(timep, 0L, &tm);
1052 return &tm;
1053 }
1054
1055 /*
1056 * Re-entrant version of localtime
1057 */
1058 struct tm *
1059 localtime_r(timep, tm)
1060 const time_t * const timep;
1061 struct tm * tm;
1062 {
1063 localsub(timep, 0L, tm);
1064 return tm;
1065 }
1066
1067 /*
1068 ** gmtsub is to gmtime as localsub is to localtime.
1069 */
1070
1071 static void
1072 gmtsub(timep, offset, tmp)
1073 const time_t * const timep;
1074 const long offset;
1075 struct tm * const tmp;
1076 {
1077 if (!gmt_is_set) {
1078 gmt_is_set = TRUE;
1079 #ifdef ALL_STATE
1080 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1081 if (gmtptr != NULL)
1082 #endif /* defined ALL_STATE */
1083 gmtload(gmtptr);
1084 }
1085 timesub(timep, offset, gmtptr, tmp);
1086 #ifdef TM_ZONE
1087 /*
1088 ** Could get fancy here and deliver something such as
1089 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1090 ** but this is no time for a treasure hunt.
1091 */
1092 if (offset != 0)
1093 tmp->TM_ZONE = wildabbr;
1094 else {
1095 #ifdef ALL_STATE
1096 if (gmtptr == NULL)
1097 tmp->TM_ZONE = gmt;
1098 else tmp->TM_ZONE = gmtptr->chars;
1099 #endif /* defined ALL_STATE */
1100 #ifndef ALL_STATE
1101 tmp->TM_ZONE = gmtptr->chars;
1102 #endif /* State Farm */
1103 }
1104 #endif /* defined TM_ZONE */
1105 }
1106
1107 struct tm *
1108 gmtime(timep)
1109 const time_t * const timep;
1110 {
1111 gmtsub(timep, 0L, &tm);
1112 return &tm;
1113 }
1114
1115 /*
1116 * Re-entrant version of gmtime
1117 */
1118 struct tm *
1119 gmtime_r(timep, tm)
1120 const time_t * const timep;
1121 struct tm * tm;
1122 {
1123 gmtsub(timep, 0L, tm);
1124 return tm;
1125 }
1126
1127 #ifdef STD_INSPIRED
1128
1129 struct tm *
1130 offtime(timep, offset)
1131 const time_t * const timep;
1132 const long offset;
1133 {
1134 gmtsub(timep, offset, &tm);
1135 return &tm;
1136 }
1137
1138 #endif /* defined STD_INSPIRED */
1139
1140 static void
1141 timesub(timep, offset, sp, tmp)
1142 const time_t * const timep;
1143 const long offset;
1144 register const struct state * const sp;
1145 register struct tm * const tmp;
1146 {
1147 register const struct lsinfo * lp;
1148 register long days;
1149 register long rem;
1150 register int y;
1151 register int yleap;
1152 register const int * ip;
1153 register long corr;
1154 register int hit;
1155 register int i;
1156
1157 corr = 0;
1158 hit = 0;
1159 #ifdef ALL_STATE
1160 i = (sp == NULL) ? 0 : sp->leapcnt;
1161 #endif /* defined ALL_STATE */
1162 #ifndef ALL_STATE
1163 i = sp->leapcnt;
1164 #endif /* State Farm */
1165 while (--i >= 0) {
1166 lp = &sp->lsis[i];
1167 if (*timep >= lp->ls_trans) {
1168 if (*timep == lp->ls_trans) {
1169 hit = ((i == 0 && lp->ls_corr > 0) ||
1170 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1171 if (hit)
1172 while (i > 0 &&
1173 sp->lsis[i].ls_trans ==
1174 sp->lsis[i - 1].ls_trans + 1 &&
1175 sp->lsis[i].ls_corr ==
1176 sp->lsis[i - 1].ls_corr + 1) {
1177 ++hit;
1178 --i;
1179 }
1180 }
1181 corr = lp->ls_corr;
1182 break;
1183 }
1184 }
1185 days = *timep / SECSPERDAY;
1186 rem = *timep % SECSPERDAY;
1187 #ifdef mc68k
1188 if (*timep == 0x80000000) {
1189 /*
1190 ** A 3B1 muffs the division on the most negative number.
1191 */
1192 days = -24855;
1193 rem = -11648;
1194 }
1195 #endif /* defined mc68k */
1196 rem += (offset - corr);
1197 while (rem < 0) {
1198 rem += SECSPERDAY;
1199 --days;
1200 }
1201 while (rem >= SECSPERDAY) {
1202 rem -= SECSPERDAY;
1203 ++days;
1204 }
1205 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1206 rem = rem % SECSPERHOUR;
1207 tmp->tm_min = (int) (rem / SECSPERMIN);
1208 /*
1209 ** A positive leap second requires a special
1210 ** representation. This uses "... ??:59:60" et seq.
1211 */
1212 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1213 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1214 if (tmp->tm_wday < 0)
1215 tmp->tm_wday += DAYSPERWEEK;
1216 y = EPOCH_YEAR;
1217 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1218 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1219 register int newy;
1220
1221 newy = y + days / DAYSPERNYEAR;
1222 if (days < 0)
1223 --newy;
1224 days -= (newy - y) * DAYSPERNYEAR +
1225 LEAPS_THRU_END_OF(newy - 1) -
1226 LEAPS_THRU_END_OF(y - 1);
1227 y = newy;
1228 }
1229 tmp->tm_year = y - TM_YEAR_BASE;
1230 tmp->tm_yday = (int) days;
1231 ip = mon_lengths[yleap];
1232 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1233 days = days - (long) ip[tmp->tm_mon];
1234 tmp->tm_mday = (int) (days + 1);
1235 tmp->tm_isdst = 0;
1236 #ifdef TM_GMTOFF
1237 tmp->TM_GMTOFF = offset;
1238 #endif /* defined TM_GMTOFF */
1239 }
1240
1241 char *
1242 ctime(timep)
1243 const time_t * const timep;
1244 {
1245 /*
1246 ** Section 4.12.3.2 of X3.159-1989 requires that
1247 ** The ctime function converts the calendar time pointed to by timer
1248 ** to local time in the form of a string. It is equivalent to
1249 ** asctime(localtime(timer))
1250 */
1251 return asctime(localtime(timep));
1252 }
1253
1254 char *
1255 ctime_r(timep, buf)
1256 const time_t * const timep;
1257 char * buf;
1258 {
1259 struct tm tm;
1260
1261 return asctime_r(localtime_r(timep, &tm), buf);
1262 }
1263
1264 /*
1265 ** Adapted from code provided by Robert Elz, who writes:
1266 ** The "best" way to do mktime I think is based on an idea of Bob
1267 ** Kridle's (so its said...) from a long time ago.
1268 ** [kridle (at) xinet.com as of 1996-01-16.]
1269 ** It does a binary search of the time_t space. Since time_t's are
1270 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1271 ** would still be very reasonable).
1272 */
1273
1274 #ifndef WRONG
1275 #define WRONG (-1)
1276 #endif /* !defined WRONG */
1277
1278 /*
1279 ** Simplified normalize logic courtesy Paul Eggert (eggert (at) twinsun.com).
1280 */
1281
1282 static int
1283 increment_overflow(number, delta)
1284 int * number;
1285 int delta;
1286 {
1287 int number0;
1288
1289 number0 = *number;
1290 *number += delta;
1291 return (*number < number0) != (delta < 0);
1292 }
1293
1294 static int
1295 normalize_overflow(tensptr, unitsptr, base)
1296 int * const tensptr;
1297 int * const unitsptr;
1298 const int base;
1299 {
1300 register int tensdelta;
1301
1302 tensdelta = (*unitsptr >= 0) ?
1303 (*unitsptr / base) :
1304 (-1 - (-1 - *unitsptr) / base);
1305 *unitsptr -= tensdelta * base;
1306 return increment_overflow(tensptr, tensdelta);
1307 }
1308
1309 static int
1310 tmcomp(atmp, btmp)
1311 register const struct tm * const atmp;
1312 register const struct tm * const btmp;
1313 {
1314 register int result;
1315
1316 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1317 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1318 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1319 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1320 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1321 result = atmp->tm_sec - btmp->tm_sec;
1322 return result;
1323 }
1324
1325 static time_t
1326 time2sub(tmp, funcp, offset, okayp, do_norm_secs)
1327 struct tm * const tmp;
1328 void (* const funcp) P((const time_t*, long, struct tm*));
1329 const long offset;
1330 int * const okayp;
1331 const int do_norm_secs;
1332 {
1333 register const struct state * sp;
1334 register int dir;
1335 register int bits;
1336 register int i, j ;
1337 register int saved_seconds;
1338 time_t newt;
1339 time_t t;
1340 struct tm yourtm, mytm;
1341
1342 *okayp = FALSE;
1343 yourtm = *tmp;
1344 if (do_norm_secs) {
1345 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1346 SECSPERMIN))
1347 return WRONG;
1348 }
1349 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1350 return WRONG;
1351 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1352 return WRONG;
1353 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1354 return WRONG;
1355 /*
1356 ** Turn yourtm.tm_year into an actual year number for now.
1357 ** It is converted back to an offset from TM_YEAR_BASE later.
1358 */
1359 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1360 return WRONG;
1361 while (yourtm.tm_mday <= 0) {
1362 if (increment_overflow(&yourtm.tm_year, -1))
1363 return WRONG;
1364 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1365 yourtm.tm_mday += year_lengths[isleap(i)];
1366 }
1367 while (yourtm.tm_mday > DAYSPERLYEAR) {
1368 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1369 yourtm.tm_mday -= year_lengths[isleap(i)];
1370 if (increment_overflow(&yourtm.tm_year, 1))
1371 return WRONG;
1372 }
1373 for ( ; ; ) {
1374 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1375 if (yourtm.tm_mday <= i)
1376 break;
1377 yourtm.tm_mday -= i;
1378 if (++yourtm.tm_mon >= MONSPERYEAR) {
1379 yourtm.tm_mon = 0;
1380 if (increment_overflow(&yourtm.tm_year, 1))
1381 return WRONG;
1382 }
1383 }
1384 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1385 return WRONG;
1386 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1387 saved_seconds = 0;
1388 else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1389 /*
1390 ** We can't set tm_sec to 0, because that might push the
1391 ** time below the minimum representable time.
1392 ** Set tm_sec to 59 instead.
1393 ** This assumes that the minimum representable time is
1394 ** not in the same minute that a leap second was deleted from,
1395 ** which is a safer assumption than using 58 would be.
1396 */
1397 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1398 return WRONG;
1399 saved_seconds = yourtm.tm_sec;
1400 yourtm.tm_sec = SECSPERMIN - 1;
1401 } else {
1402 saved_seconds = yourtm.tm_sec;
1403 yourtm.tm_sec = 0;
1404 }
1405 /*
1406 ** Divide the search space in half
1407 ** (this works whether time_t is signed or unsigned).
1408 */
1409 bits = TYPE_BIT(time_t) - 1;
1410 /*
1411 ** If time_t is signed, then 0 is just above the median,
1412 ** assuming two's complement arithmetic.
1413 ** If time_t is unsigned, then (1 << bits) is just above the median.
1414 */
1415 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1416 for ( ; ; ) {
1417 (*funcp)(&t, offset, &mytm);
1418 dir = tmcomp(&mytm, &yourtm);
1419 if (dir != 0) {
1420 if (bits-- < 0)
1421 return WRONG;
1422 if (bits < 0)
1423 --t; /* may be needed if new t is minimal */
1424 else if (dir > 0)
1425 t -= ((time_t) 1) << bits;
1426 else t += ((time_t) 1) << bits;
1427 continue;
1428 }
1429 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1430 break;
1431 /*
1432 ** Right time, wrong type.
1433 ** Hunt for right time, right type.
1434 ** It's okay to guess wrong since the guess
1435 ** gets checked.
1436 */
1437 /*
1438 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1439 */
1440 sp = (const struct state *)
1441 (((void *) funcp == (void *) localsub) ?
1442 lclptr : gmtptr);
1443 #ifdef ALL_STATE
1444 if (sp == NULL)
1445 return WRONG;
1446 #endif /* defined ALL_STATE */
1447 for (i = sp->typecnt - 1; i >= 0; --i) {
1448 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1449 continue;
1450 for (j = sp->typecnt - 1; j >= 0; --j) {
1451 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1452 continue;
1453 newt = t + sp->ttis[j].tt_gmtoff -
1454 sp->ttis[i].tt_gmtoff;
1455 (*funcp)(&newt, offset, &mytm);
1456 if (tmcomp(&mytm, &yourtm) != 0)
1457 continue;
1458 if (mytm.tm_isdst != yourtm.tm_isdst)
1459 continue;
1460 /*
1461 ** We have a match.
1462 */
1463 t = newt;
1464 goto label;
1465 }
1466 }
1467 return WRONG;
1468 }
1469 label:
1470 newt = t + saved_seconds;
1471 if ((newt < t) != (saved_seconds < 0))
1472 return WRONG;
1473 t = newt;
1474 (*funcp)(&t, offset, tmp);
1475 *okayp = TRUE;
1476 return t;
1477 }
1478
1479 static time_t
1480 time2(tmp, funcp, offset, okayp)
1481 struct tm * const tmp;
1482 void (* const funcp) P((const time_t*, long, struct tm*));
1483 const long offset;
1484 int * const okayp;
1485 {
1486 time_t t;
1487
1488 /*
1489 ** First try without normalization of seconds
1490 ** (in case tm_sec contains a value associated with a leap second).
1491 ** If that fails, try with normalization of seconds.
1492 */
1493 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1494 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1495 }
1496
1497 static time_t
1498 time1(tmp, funcp, offset)
1499 struct tm * const tmp;
1500 void (* const funcp) P((const time_t *, long, struct tm *));
1501 const long offset;
1502 {
1503 register time_t t;
1504 register const struct state * sp;
1505 register int samei, otheri;
1506 int okay;
1507
1508 if (tmp->tm_isdst > 1)
1509 tmp->tm_isdst = 1;
1510 t = time2(tmp, funcp, offset, &okay);
1511 #ifdef PCTS
1512 /*
1513 ** PCTS code courtesy Grant Sullivan (grant (at) osf.org).
1514 */
1515 if (okay)
1516 return t;
1517 if (tmp->tm_isdst < 0)
1518 tmp->tm_isdst = 0; /* reset to std and try again */
1519 #endif /* defined PCTS */
1520 #ifndef PCTS
1521 if (okay || tmp->tm_isdst < 0)
1522 return t;
1523 #endif /* !defined PCTS */
1524 /*
1525 ** We're supposed to assume that somebody took a time of one type
1526 ** and did some math on it that yielded a "struct tm" that's bad.
1527 ** We try to divine the type they started from and adjust to the
1528 ** type they need.
1529 */
1530 /*
1531 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1532 */
1533 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1534 lclptr : gmtptr);
1535 #ifdef ALL_STATE
1536 if (sp == NULL)
1537 return WRONG;
1538 #endif /* defined ALL_STATE */
1539 for (samei = sp->typecnt - 1; samei >= 0; --samei) {
1540 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1541 continue;
1542 for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
1543 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1544 continue;
1545 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1546 sp->ttis[samei].tt_gmtoff;
1547 tmp->tm_isdst = !tmp->tm_isdst;
1548 t = time2(tmp, funcp, offset, &okay);
1549 if (okay)
1550 return t;
1551 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1552 sp->ttis[samei].tt_gmtoff;
1553 tmp->tm_isdst = !tmp->tm_isdst;
1554 }
1555 }
1556 return WRONG;
1557 }
1558
1559 time_t
1560 mktime(tmp)
1561 struct tm * const tmp;
1562 {
1563 tzset();
1564 return time1(tmp, localsub, 0L);
1565 }
1566
1567 #ifdef STD_INSPIRED
1568
1569 time_t
1570 timelocal(tmp)
1571 struct tm * const tmp;
1572 {
1573 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1574 return mktime(tmp);
1575 }
1576
1577 time_t
1578 timegm(tmp)
1579 struct tm * const tmp;
1580 {
1581 tmp->tm_isdst = 0;
1582 return time1(tmp, gmtsub, 0L);
1583 }
1584
1585 time_t
1586 timeoff(tmp, offset)
1587 struct tm * const tmp;
1588 const long offset;
1589 {
1590 tmp->tm_isdst = 0;
1591 return time1(tmp, gmtsub, offset);
1592 }
1593
1594 #endif /* defined STD_INSPIRED */
1595
1596 #ifdef CMUCS
1597
1598 /*
1599 ** The following is supplied for compatibility with
1600 ** previous versions of the CMUCS runtime library.
1601 */
1602
1603 long
1604 gtime(tmp)
1605 struct tm * const tmp;
1606 {
1607 const time_t t = mktime(tmp);
1608
1609 if (t == WRONG)
1610 return -1;
1611 return t;
1612 }
1613
1614 #endif /* defined CMUCS */
1615
1616 /*
1617 ** XXX--is the below the right way to conditionalize??
1618 */
1619
1620 #ifdef STD_INSPIRED
1621
1622 /*
1623 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1624 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
1625 ** is not the case if we are accounting for leap seconds.
1626 ** So, we provide the following conversion routines for use
1627 ** when exchanging timestamps with POSIX conforming systems.
1628 */
1629
1630 static long
1631 leapcorr(timep)
1632 time_t * timep;
1633 {
1634 register struct state * sp;
1635 register struct lsinfo * lp;
1636 register int i;
1637
1638 sp = lclptr;
1639 i = sp->leapcnt;
1640 while (--i >= 0) {
1641 lp = &sp->lsis[i];
1642 if (*timep >= lp->ls_trans)
1643 return lp->ls_corr;
1644 }
1645 return 0;
1646 }
1647
1648 time_t
1649 time2posix(t)
1650 time_t t;
1651 {
1652 tzset();
1653 return t - leapcorr(&t);
1654 }
1655
1656 time_t
1657 posix2time(t)
1658 time_t t;
1659 {
1660 time_t x;
1661 time_t y;
1662
1663 tzset();
1664 /*
1665 ** For a positive leap second hit, the result
1666 ** is not unique. For a negative leap second
1667 ** hit, the corresponding time doesn't exist,
1668 ** so we return an adjacent second.
1669 */
1670 x = t + leapcorr(&t);
1671 y = x - leapcorr(&x);
1672 if (y < t) {
1673 do {
1674 x++;
1675 y = x - leapcorr(&x);
1676 } while (y < t);
1677 if (t != y)
1678 return x - 1;
1679 } else if (y > t) {
1680 do {
1681 --x;
1682 y = x - leapcorr(&x);
1683 } while (y > t);
1684 if (t != y)
1685 return x + 1;
1686 }
1687 return x;
1688 }
1689
1690 #endif /* defined STD_INSPIRED */
1691