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