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