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