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