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