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