localtime.c revision 1.1.1.13 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.78";
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
1059 struct tm *
1060 localtime_r(timep, tm)
1061 const time_t * const timep;
1062 struct tm * tm;
1063 {
1064 localsub(timep, 0L, tm);
1065 return tm;
1066 }
1067
1068 /*
1069 ** gmtsub is to gmtime as localsub is to localtime.
1070 */
1071
1072 static void
1073 gmtsub(timep, offset, tmp)
1074 const time_t * const timep;
1075 const long offset;
1076 struct tm * const tmp;
1077 {
1078 if (!gmt_is_set) {
1079 gmt_is_set = TRUE;
1080 #ifdef ALL_STATE
1081 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1082 if (gmtptr != NULL)
1083 #endif /* defined ALL_STATE */
1084 gmtload(gmtptr);
1085 }
1086 timesub(timep, offset, gmtptr, tmp);
1087 #ifdef TM_ZONE
1088 /*
1089 ** Could get fancy here and deliver something such as
1090 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1091 ** but this is no time for a treasure hunt.
1092 */
1093 if (offset != 0)
1094 tmp->TM_ZONE = wildabbr;
1095 else {
1096 #ifdef ALL_STATE
1097 if (gmtptr == NULL)
1098 tmp->TM_ZONE = gmt;
1099 else tmp->TM_ZONE = gmtptr->chars;
1100 #endif /* defined ALL_STATE */
1101 #ifndef ALL_STATE
1102 tmp->TM_ZONE = gmtptr->chars;
1103 #endif /* State Farm */
1104 }
1105 #endif /* defined TM_ZONE */
1106 }
1107
1108 struct tm *
1109 gmtime(timep)
1110 const time_t * const timep;
1111 {
1112 gmtsub(timep, 0L, &tm);
1113 return &tm;
1114 }
1115
1116 /*
1117 * Re-entrant version of gmtime.
1118 */
1119
1120 struct tm *
1121 gmtime_r(timep, tm)
1122 const time_t * const timep;
1123 struct tm * tm;
1124 {
1125 gmtsub(timep, 0L, tm);
1126 return tm;
1127 }
1128
1129 #ifdef STD_INSPIRED
1130
1131 struct tm *
1132 offtime(timep, offset)
1133 const time_t * const timep;
1134 const long offset;
1135 {
1136 gmtsub(timep, offset, &tm);
1137 return &tm;
1138 }
1139
1140 #endif /* defined STD_INSPIRED */
1141
1142 static void
1143 timesub(timep, offset, sp, tmp)
1144 const time_t * const timep;
1145 const long offset;
1146 register const struct state * const sp;
1147 register struct tm * const tmp;
1148 {
1149 register const struct lsinfo * lp;
1150 register long days;
1151 register long rem;
1152 register int y;
1153 register int yleap;
1154 register const int * ip;
1155 register long corr;
1156 register int hit;
1157 register int i;
1158
1159 corr = 0;
1160 hit = 0;
1161 #ifdef ALL_STATE
1162 i = (sp == NULL) ? 0 : sp->leapcnt;
1163 #endif /* defined ALL_STATE */
1164 #ifndef ALL_STATE
1165 i = sp->leapcnt;
1166 #endif /* State Farm */
1167 while (--i >= 0) {
1168 lp = &sp->lsis[i];
1169 if (*timep >= lp->ls_trans) {
1170 if (*timep == lp->ls_trans) {
1171 hit = ((i == 0 && lp->ls_corr > 0) ||
1172 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1173 if (hit)
1174 while (i > 0 &&
1175 sp->lsis[i].ls_trans ==
1176 sp->lsis[i - 1].ls_trans + 1 &&
1177 sp->lsis[i].ls_corr ==
1178 sp->lsis[i - 1].ls_corr + 1) {
1179 ++hit;
1180 --i;
1181 }
1182 }
1183 corr = lp->ls_corr;
1184 break;
1185 }
1186 }
1187 days = *timep / SECSPERDAY;
1188 rem = *timep % SECSPERDAY;
1189 #ifdef mc68k
1190 if (*timep == 0x80000000) {
1191 /*
1192 ** A 3B1 muffs the division on the most negative number.
1193 */
1194 days = -24855;
1195 rem = -11648;
1196 }
1197 #endif /* defined mc68k */
1198 rem += (offset - corr);
1199 while (rem < 0) {
1200 rem += SECSPERDAY;
1201 --days;
1202 }
1203 while (rem >= SECSPERDAY) {
1204 rem -= SECSPERDAY;
1205 ++days;
1206 }
1207 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1208 rem = rem % SECSPERHOUR;
1209 tmp->tm_min = (int) (rem / SECSPERMIN);
1210 /*
1211 ** A positive leap second requires a special
1212 ** representation. This uses "... ??:59:60" et seq.
1213 */
1214 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1215 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1216 if (tmp->tm_wday < 0)
1217 tmp->tm_wday += DAYSPERWEEK;
1218 y = EPOCH_YEAR;
1219 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
1220 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1221 register int newy;
1222
1223 newy = y + days / DAYSPERNYEAR;
1224 if (days < 0)
1225 --newy;
1226 days -= (newy - y) * DAYSPERNYEAR +
1227 LEAPS_THRU_END_OF(newy - 1) -
1228 LEAPS_THRU_END_OF(y - 1);
1229 y = newy;
1230 }
1231 tmp->tm_year = y - TM_YEAR_BASE;
1232 tmp->tm_yday = (int) days;
1233 ip = mon_lengths[yleap];
1234 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1235 days = days - (long) ip[tmp->tm_mon];
1236 tmp->tm_mday = (int) (days + 1);
1237 tmp->tm_isdst = 0;
1238 #ifdef TM_GMTOFF
1239 tmp->TM_GMTOFF = offset;
1240 #endif /* defined TM_GMTOFF */
1241 }
1242
1243 char *
1244 ctime(timep)
1245 const time_t * const timep;
1246 {
1247 /*
1248 ** Section 4.12.3.2 of X3.159-1989 requires that
1249 ** The ctime function converts the calendar time pointed to by timer
1250 ** to local time in the form of a string. It is equivalent to
1251 ** asctime(localtime(timer))
1252 */
1253 return asctime(localtime(timep));
1254 }
1255
1256 char *
1257 ctime_r(timep, buf)
1258 const time_t * const timep;
1259 char * buf;
1260 {
1261 struct tm tm;
1262
1263 return asctime_r(localtime_r(timep, &tm), buf);
1264 }
1265
1266 /*
1267 ** Adapted from code provided by Robert Elz, who writes:
1268 ** The "best" way to do mktime I think is based on an idea of Bob
1269 ** Kridle's (so its said...) from a long time ago.
1270 ** [kridle (at) xinet.com as of 1996-01-16.]
1271 ** It does a binary search of the time_t space. Since time_t's are
1272 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
1273 ** would still be very reasonable).
1274 */
1275
1276 #ifndef WRONG
1277 #define WRONG (-1)
1278 #endif /* !defined WRONG */
1279
1280 /*
1281 ** Simplified normalize logic courtesy Paul Eggert (eggert (at) twinsun.com).
1282 */
1283
1284 static int
1285 increment_overflow(number, delta)
1286 int * number;
1287 int delta;
1288 {
1289 int number0;
1290
1291 number0 = *number;
1292 *number += delta;
1293 return (*number < number0) != (delta < 0);
1294 }
1295
1296 static int
1297 normalize_overflow(tensptr, unitsptr, base)
1298 int * const tensptr;
1299 int * const unitsptr;
1300 const int base;
1301 {
1302 register int tensdelta;
1303
1304 tensdelta = (*unitsptr >= 0) ?
1305 (*unitsptr / base) :
1306 (-1 - (-1 - *unitsptr) / base);
1307 *unitsptr -= tensdelta * base;
1308 return increment_overflow(tensptr, tensdelta);
1309 }
1310
1311 static int
1312 tmcomp(atmp, btmp)
1313 register const struct tm * const atmp;
1314 register const struct tm * const btmp;
1315 {
1316 register int result;
1317
1318 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1319 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1320 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1321 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1322 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1323 result = atmp->tm_sec - btmp->tm_sec;
1324 return result;
1325 }
1326
1327 static time_t
1328 time2sub(tmp, funcp, offset, okayp, do_norm_secs)
1329 struct tm * const tmp;
1330 void (* const funcp) P((const time_t*, long, struct tm*));
1331 const long offset;
1332 int * const okayp;
1333 const int do_norm_secs;
1334 {
1335 register const struct state * sp;
1336 register int dir;
1337 register int bits;
1338 register int i, j ;
1339 register int saved_seconds;
1340 time_t newt;
1341 time_t t;
1342 struct tm yourtm, mytm;
1343
1344 *okayp = FALSE;
1345 yourtm = *tmp;
1346 if (do_norm_secs) {
1347 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1348 SECSPERMIN))
1349 return WRONG;
1350 }
1351 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1352 return WRONG;
1353 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1354 return WRONG;
1355 if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1356 return WRONG;
1357 /*
1358 ** Turn yourtm.tm_year into an actual year number for now.
1359 ** It is converted back to an offset from TM_YEAR_BASE later.
1360 */
1361 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1362 return WRONG;
1363 while (yourtm.tm_mday <= 0) {
1364 if (increment_overflow(&yourtm.tm_year, -1))
1365 return WRONG;
1366 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1367 yourtm.tm_mday += year_lengths[isleap(i)];
1368 }
1369 while (yourtm.tm_mday > DAYSPERLYEAR) {
1370 i = yourtm.tm_year + (1 < yourtm.tm_mon);
1371 yourtm.tm_mday -= year_lengths[isleap(i)];
1372 if (increment_overflow(&yourtm.tm_year, 1))
1373 return WRONG;
1374 }
1375 for ( ; ; ) {
1376 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1377 if (yourtm.tm_mday <= i)
1378 break;
1379 yourtm.tm_mday -= i;
1380 if (++yourtm.tm_mon >= MONSPERYEAR) {
1381 yourtm.tm_mon = 0;
1382 if (increment_overflow(&yourtm.tm_year, 1))
1383 return WRONG;
1384 }
1385 }
1386 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1387 return WRONG;
1388 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1389 saved_seconds = 0;
1390 else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1391 /*
1392 ** We can't set tm_sec to 0, because that might push the
1393 ** time below the minimum representable time.
1394 ** Set tm_sec to 59 instead.
1395 ** This assumes that the minimum representable time is
1396 ** not in the same minute that a leap second was deleted from,
1397 ** which is a safer assumption than using 58 would be.
1398 */
1399 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1400 return WRONG;
1401 saved_seconds = yourtm.tm_sec;
1402 yourtm.tm_sec = SECSPERMIN - 1;
1403 } else {
1404 saved_seconds = yourtm.tm_sec;
1405 yourtm.tm_sec = 0;
1406 }
1407 /*
1408 ** Divide the search space in half
1409 ** (this works whether time_t is signed or unsigned).
1410 */
1411 bits = TYPE_BIT(time_t) - 1;
1412 /*
1413 ** If time_t is signed, then 0 is just above the median,
1414 ** assuming two's complement arithmetic.
1415 ** If time_t is unsigned, then (1 << bits) is just above the median.
1416 */
1417 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1418 for ( ; ; ) {
1419 (*funcp)(&t, offset, &mytm);
1420 dir = tmcomp(&mytm, &yourtm);
1421 if (dir != 0) {
1422 if (bits-- < 0)
1423 return WRONG;
1424 if (bits < 0)
1425 --t; /* may be needed if new t is minimal */
1426 else if (dir > 0)
1427 t -= ((time_t) 1) << bits;
1428 else t += ((time_t) 1) << bits;
1429 continue;
1430 }
1431 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1432 break;
1433 /*
1434 ** Right time, wrong type.
1435 ** Hunt for right time, right type.
1436 ** It's okay to guess wrong since the guess
1437 ** gets checked.
1438 */
1439 /*
1440 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1441 */
1442 sp = (const struct state *)
1443 (((void *) funcp == (void *) localsub) ?
1444 lclptr : gmtptr);
1445 #ifdef ALL_STATE
1446 if (sp == NULL)
1447 return WRONG;
1448 #endif /* defined ALL_STATE */
1449 for (i = sp->typecnt - 1; i >= 0; --i) {
1450 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1451 continue;
1452 for (j = sp->typecnt - 1; j >= 0; --j) {
1453 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1454 continue;
1455 newt = t + sp->ttis[j].tt_gmtoff -
1456 sp->ttis[i].tt_gmtoff;
1457 (*funcp)(&newt, offset, &mytm);
1458 if (tmcomp(&mytm, &yourtm) != 0)
1459 continue;
1460 if (mytm.tm_isdst != yourtm.tm_isdst)
1461 continue;
1462 /*
1463 ** We have a match.
1464 */
1465 t = newt;
1466 goto label;
1467 }
1468 }
1469 return WRONG;
1470 }
1471 label:
1472 newt = t + saved_seconds;
1473 if ((newt < t) != (saved_seconds < 0))
1474 return WRONG;
1475 t = newt;
1476 (*funcp)(&t, offset, tmp);
1477 *okayp = TRUE;
1478 return t;
1479 }
1480
1481 static time_t
1482 time2(tmp, funcp, offset, okayp)
1483 struct tm * const tmp;
1484 void (* const funcp) P((const time_t*, long, struct tm*));
1485 const long offset;
1486 int * const okayp;
1487 {
1488 time_t t;
1489
1490 /*
1491 ** First try without normalization of seconds
1492 ** (in case tm_sec contains a value associated with a leap second).
1493 ** If that fails, try with normalization of seconds.
1494 */
1495 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1496 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1497 }
1498
1499 static time_t
1500 time1(tmp, funcp, offset)
1501 struct tm * const tmp;
1502 void (* const funcp) P((const time_t *, long, struct tm *));
1503 const long offset;
1504 {
1505 register time_t t;
1506 register const struct state * sp;
1507 register int samei, otheri;
1508 register int sameind, otherind;
1509 register int i;
1510 register int nseen;
1511 int seen[TZ_MAX_TYPES];
1512 int types[TZ_MAX_TYPES];
1513 int okay;
1514
1515 if (tmp->tm_isdst > 1)
1516 tmp->tm_isdst = 1;
1517 t = time2(tmp, funcp, offset, &okay);
1518 #ifdef PCTS
1519 /*
1520 ** PCTS code courtesy Grant Sullivan (grant (at) osf.org).
1521 */
1522 if (okay)
1523 return t;
1524 if (tmp->tm_isdst < 0)
1525 tmp->tm_isdst = 0; /* reset to std and try again */
1526 #endif /* defined PCTS */
1527 #ifndef PCTS
1528 if (okay || tmp->tm_isdst < 0)
1529 return t;
1530 #endif /* !defined PCTS */
1531 /*
1532 ** We're supposed to assume that somebody took a time of one type
1533 ** and did some math on it that yielded a "struct tm" that's bad.
1534 ** We try to divine the type they started from and adjust to the
1535 ** type they need.
1536 */
1537 /*
1538 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1539 */
1540 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1541 lclptr : gmtptr);
1542 #ifdef ALL_STATE
1543 if (sp == NULL)
1544 return WRONG;
1545 #endif /* defined ALL_STATE */
1546 for (i = 0; i < sp->typecnt; ++i)
1547 seen[i] = FALSE;
1548 nseen = 0;
1549 for (i = sp->timecnt - 1; i >= 0; --i)
1550 if (!seen[sp->types[i]]) {
1551 seen[sp->types[i]] = TRUE;
1552 types[nseen++] = sp->types[i];
1553 }
1554 for (sameind = 0; sameind < nseen; ++sameind) {
1555 samei = types[sameind];
1556 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1557 continue;
1558 for (otherind = 0; otherind < nseen; ++otherind) {
1559 otheri = types[otherind];
1560 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1561 continue;
1562 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1563 sp->ttis[samei].tt_gmtoff;
1564 tmp->tm_isdst = !tmp->tm_isdst;
1565 t = time2(tmp, funcp, offset, &okay);
1566 if (okay)
1567 return t;
1568 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1569 sp->ttis[samei].tt_gmtoff;
1570 tmp->tm_isdst = !tmp->tm_isdst;
1571 }
1572 }
1573 return WRONG;
1574 }
1575
1576 time_t
1577 mktime(tmp)
1578 struct tm * const tmp;
1579 {
1580 tzset();
1581 return time1(tmp, localsub, 0L);
1582 }
1583
1584 #ifdef STD_INSPIRED
1585
1586 time_t
1587 timelocal(tmp)
1588 struct tm * const tmp;
1589 {
1590 tmp->tm_isdst = -1; /* in case it wasn't initialized */
1591 return mktime(tmp);
1592 }
1593
1594 time_t
1595 timegm(tmp)
1596 struct tm * const tmp;
1597 {
1598 tmp->tm_isdst = 0;
1599 return time1(tmp, gmtsub, 0L);
1600 }
1601
1602 time_t
1603 timeoff(tmp, offset)
1604 struct tm * const tmp;
1605 const long offset;
1606 {
1607 tmp->tm_isdst = 0;
1608 return time1(tmp, gmtsub, offset);
1609 }
1610
1611 #endif /* defined STD_INSPIRED */
1612
1613 #ifdef CMUCS
1614
1615 /*
1616 ** The following is supplied for compatibility with
1617 ** previous versions of the CMUCS runtime library.
1618 */
1619
1620 long
1621 gtime(tmp)
1622 struct tm * const tmp;
1623 {
1624 const time_t t = mktime(tmp);
1625
1626 if (t == WRONG)
1627 return -1;
1628 return t;
1629 }
1630
1631 #endif /* defined CMUCS */
1632
1633 /*
1634 ** XXX--is the below the right way to conditionalize??
1635 */
1636
1637 #ifdef STD_INSPIRED
1638
1639 /*
1640 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1641 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
1642 ** is not the case if we are accounting for leap seconds.
1643 ** So, we provide the following conversion routines for use
1644 ** when exchanging timestamps with POSIX conforming systems.
1645 */
1646
1647 static long
1648 leapcorr(timep)
1649 time_t * timep;
1650 {
1651 register struct state * sp;
1652 register struct lsinfo * lp;
1653 register int i;
1654
1655 sp = lclptr;
1656 i = sp->leapcnt;
1657 while (--i >= 0) {
1658 lp = &sp->lsis[i];
1659 if (*timep >= lp->ls_trans)
1660 return lp->ls_corr;
1661 }
1662 return 0;
1663 }
1664
1665 time_t
1666 time2posix(t)
1667 time_t t;
1668 {
1669 tzset();
1670 return t - leapcorr(&t);
1671 }
1672
1673 time_t
1674 posix2time(t)
1675 time_t t;
1676 {
1677 time_t x;
1678 time_t y;
1679
1680 tzset();
1681 /*
1682 ** For a positive leap second hit, the result
1683 ** is not unique. For a negative leap second
1684 ** hit, the corresponding time doesn't exist,
1685 ** so we return an adjacent second.
1686 */
1687 x = t + leapcorr(&t);
1688 y = x - leapcorr(&x);
1689 if (y < t) {
1690 do {
1691 x++;
1692 y = x - leapcorr(&x);
1693 } while (y < t);
1694 if (t != y)
1695 return x - 1;
1696 } else if (y > t) {
1697 do {
1698 --x;
1699 y = x - leapcorr(&x);
1700 } while (y > t);
1701 if (t != y)
1702 return x + 1;
1703 }
1704 return x;
1705 }
1706
1707 #endif /* defined STD_INSPIRED */
1708