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