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