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