blob: 79c4a9a59c7fa62f11fb5a95e0f9a6c407e7ab17 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2** This file is in the public domain, so clarified as of
3** 1996-06-05 by Arthur David Olson.
4*/
5
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08006/*
7** Leap second handling from Bradley White.
8** POSIX-style TZ environment variable handling from Guy Harris.
9*/
10
11/*LINTLIBRARY*/
12
Elliott Hughes9fb22a32015-10-07 17:13:40 -070013#define LOCALTIME_IMPLEMENTATION
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080014#include "private.h"
Elliott Hughes9fb22a32015-10-07 17:13:40 -070015
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016#include "tzfile.h"
17#include "fcntl.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080018
Elliott Hughes9fb22a32015-10-07 17:13:40 -070019#if THREAD_SAFE
20# include <pthread.h>
21static pthread_mutex_t locallock = PTHREAD_MUTEX_INITIALIZER;
22static int lock(void) { return pthread_mutex_lock(&locallock); }
23static void unlock(void) { pthread_mutex_unlock(&locallock); }
24#else
25static int lock(void) { return 0; }
26static void unlock(void) { }
27#endif
28
29/* NETBSD_INSPIRED_EXTERN functions are exported to callers if
30 NETBSD_INSPIRED is defined, and are private otherwise. */
31#if NETBSD_INSPIRED
32# define NETBSD_INSPIRED_EXTERN
33#else
34# define NETBSD_INSPIRED_EXTERN static
35#endif
36
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#ifndef TZ_ABBR_MAX_LEN
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070038#define TZ_ABBR_MAX_LEN 16
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#endif /* !defined TZ_ABBR_MAX_LEN */
40
41#ifndef TZ_ABBR_CHAR_SET
42#define TZ_ABBR_CHAR_SET \
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070043 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#endif /* !defined TZ_ABBR_CHAR_SET */
45
46#ifndef TZ_ABBR_ERR_CHAR
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070047#define TZ_ABBR_ERR_CHAR '_'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048#endif /* !defined TZ_ABBR_ERR_CHAR */
49
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050/*
51** SunOS 4.1.1 headers lack O_BINARY.
52*/
53
54#ifdef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070055#define OPEN_MODE (O_RDONLY | O_BINARY)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#endif /* defined O_BINARY */
57#ifndef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070058#define OPEN_MODE O_RDONLY
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#endif /* !defined O_BINARY */
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061#ifndef WILDABBR
62/*
63** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070064** 1. They might reference tzname[0] before calling tzset (explicitly
65** or implicitly).
66** 2. They might reference tzname[1] before calling tzset (explicitly
67** or implicitly).
68** 3. They might reference tzname[1] after setting to a time zone
69** in which Daylight Saving Time is never observed.
70** 4. They might reference tzname[0] after setting to a time zone
71** in which Standard Time is never observed.
72** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073** What's best to do in the above cases is open to debate;
74** for now, we just set things up so that in any of the five cases
75** WILDABBR is used. Another possibility: initialize tzname[0] to the
76** string "tzname[0] used before set", and similarly for the other cases.
77** And another: initialize tzname[0] to "ERA", with an explanation in the
78** manual page of what this "time zone abbreviation" means (doing this so
79** that tzname[0] has the "normal" length of three characters).
80*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070081#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082#endif /* !defined WILDABBR */
83
Elliott Hughes906eb992014-06-18 19:46:25 -070084static const char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Calin Juravled8928922014-02-28 12:18:53 +000086static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88/*
89** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
90** We default to US rules as of 1999-08-17.
91** POSIX 1003.1 section 8.1.1 says that the default DST rules are
92** implementation dependent; for historical reasons, US rules are a
93** common default.
94*/
95#ifndef TZDEFRULESTRING
96#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
97#endif /* !defined TZDEFDST */
98
Calin Juravled8928922014-02-28 12:18:53 +000099struct ttinfo { /* time type information */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700100 int_fast32_t tt_gmtoff; /* UT offset in seconds */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700101 bool tt_isdst; /* used to set tm_isdst */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700102 int tt_abbrind; /* abbreviation list index */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700103 bool tt_ttisstd; /* transition is std time */
104 bool tt_ttisgmt; /* transition is UT */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105};
106
Calin Juravled8928922014-02-28 12:18:53 +0000107struct lsinfo { /* leap second information */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700108 time_t ls_trans; /* transition time */
109 int_fast64_t ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110};
111
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700112#define SMALLEST(a, b) (((a) < (b)) ? (a) : (b))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700113#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
115#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700116#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#endif /* defined TZNAME_MAX */
118#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700119#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120#endif /* !defined TZNAME_MAX */
121
122struct state {
Calin Juravled8928922014-02-28 12:18:53 +0000123 int leapcnt;
124 int timecnt;
125 int typecnt;
126 int charcnt;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700127 bool goback;
128 bool goahead;
Calin Juravled8928922014-02-28 12:18:53 +0000129 time_t ats[TZ_MAX_TIMES];
130 unsigned char types[TZ_MAX_TIMES];
131 struct ttinfo ttis[TZ_MAX_TYPES];
132 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
133 (2 * (MY_TZNAME_MAX + 1)))];
134 struct lsinfo lsis[TZ_MAX_LEAPS];
135 int defaulttype; /* for early times or if no transitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136};
137
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700138enum r_type {
139 JULIAN_DAY, /* Jn = Julian day */
140 DAY_OF_YEAR, /* n = day of year */
141 MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
142};
143
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144struct rule {
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700145 enum r_type r_type; /* type of rule */
Calin Juravled8928922014-02-28 12:18:53 +0000146 int r_day; /* day number of rule */
147 int r_week; /* week number of rule */
148 int r_mon; /* month number of rule */
149 int_fast32_t r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150};
151
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700152static struct tm *gmtsub(struct state const *, time_t const *, int_fast32_t,
153 struct tm *);
154static bool increment_overflow(int *, int);
155static bool increment_overflow_time(time_t *, int_fast32_t);
156static bool normalize_overflow32(int_fast32_t *, int *, int);
157static struct tm *timesub(time_t const *, int_fast32_t, struct state const *,
158 struct tm *);
159static bool typesequiv(struct state const *, int, int);
160static bool tzparse(char const *, struct state *, bool);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
162#ifdef ALL_STATE
Calin Juravled8928922014-02-28 12:18:53 +0000163static struct state * lclptr;
164static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165#endif /* defined ALL_STATE */
166
167#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700168static struct state lclmem;
169static struct state gmtmem;
170#define lclptr (&lclmem)
171#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172#endif /* State Farm */
173
174#ifndef TZ_STRLEN_MAX
175#define TZ_STRLEN_MAX 255
176#endif /* !defined TZ_STRLEN_MAX */
177
Calin Juravled8928922014-02-28 12:18:53 +0000178static char lcl_TZname[TZ_STRLEN_MAX + 1];
179static int lcl_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Calin Juravled8928922014-02-28 12:18:53 +0000181char * tzname[2] = {
Elliott Hughes906eb992014-06-18 19:46:25 -0700182 (char *) wildabbr,
183 (char *) wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184};
185
186/*
187** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700188** Except for the strftime function, these functions [asctime,
189** ctime, gmtime, localtime] return values in one of two static
190** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191** Thanks to Paul Eggert for noting this.
192*/
193
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700194static struct tm tm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
196#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700197long timezone;
198int daylight;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199#endif /* defined USG_COMPAT */
200
201#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700202long altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203#endif /* defined ALTZONE */
204
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700205/* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND. */
206static void
207init_ttinfo(struct ttinfo *s, int_fast32_t gmtoff, bool isdst, int abbrind)
208{
209 s->tt_gmtoff = gmtoff;
210 s->tt_isdst = isdst;
211 s->tt_abbrind = abbrind;
212 s->tt_ttisstd = false;
213 s->tt_ttisgmt = false;
214}
215
Elliott Hughesce4783c2013-07-12 17:31:11 -0700216static int_fast32_t
217detzcode(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700219 register int_fast32_t result;
220 register int i;
221 int_fast32_t one = 1;
222 int_fast32_t halfmaxval = one << (32 - 2);
223 int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
224 int_fast32_t minval = -1 - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700226 result = codep[0] & 0x7f;
227 for (i = 1; i < 4; ++i)
228 result = (result << 8) | (codep[i] & 0xff);
229
230 if (codep[0] & 0x80) {
231 /* Do two's-complement negation even on non-two's-complement machines.
232 If the result would be minval - 1, return minval. */
233 result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
234 result += minval;
235 }
236 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237}
238
Calin Juravle627d37c2014-02-28 11:46:03 +0000239static int_fast64_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700240detzcode64(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700242 register uint_fast64_t result;
243 register int i;
244 int_fast64_t one = 1;
245 int_fast64_t halfmaxval = one << (64 - 2);
246 int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
247 int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700249 result = codep[0] & 0x7f;
250 for (i = 1; i < 8; ++i)
251 result = (result << 8) | (codep[i] & 0xff);
252
253 if (codep[0] & 0x80) {
254 /* Do two's-complement negation even on non-two's-complement machines.
255 If the result would be minval - 1, return minval. */
256 result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
257 result += minval;
258 }
259 return result;
260}
261
262static void
263update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
264{
265 tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
266#ifdef USG_COMPAT
267 if (!ttisp->tt_isdst)
268 timezone = - ttisp->tt_gmtoff;
269#endif
270#ifdef ALTZONE
271 if (ttisp->tt_isdst)
272 altzone = - ttisp->tt_gmtoff;
273#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274}
275
276static void
Elliott Hughesce4783c2013-07-12 17:31:11 -0700277settzname(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700279 register struct state * const sp = lclptr;
280 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700282 tzname[0] = tzname[1] = (char *) wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700284 daylight = 0;
285 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286#endif /* defined USG_COMPAT */
287#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700288 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289#endif /* defined ALTZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700290 if (sp == NULL) {
291 tzname[0] = tzname[1] = (char *) gmt;
292 return;
293 }
294 /*
295 ** And to get the latest zone names into tzname. . .
296 */
297 for (i = 0; i < sp->typecnt; ++i) {
298 register const struct ttinfo * const ttisp = &sp->ttis[i];
299 update_tzname_etc(sp, ttisp);
300 }
301 for (i = 0; i < sp->timecnt; ++i) {
302 register const struct ttinfo * const ttisp =
303 &sp->ttis[
304 sp->types[i]];
305 update_tzname_etc(sp, ttisp);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700306#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700307 if (ttisp->tt_isdst)
308 daylight = 1;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700309#endif /* defined USG_COMPAT */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700310 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311}
312
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700313static void
314scrub_abbrs(struct state *sp)
315{
316 int i;
317 /*
318 ** First, replace bogus characters.
319 */
320 for (i = 0; i < sp->charcnt; ++i)
321 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
322 sp->chars[i] = TZ_ABBR_ERR_CHAR;
323 /*
324 ** Second, truncate long abbreviations.
325 */
326 for (i = 0; i < sp->typecnt; ++i) {
327 register const struct ttinfo * const ttisp = &sp->ttis[i];
328 register char * cp = &sp->chars[ttisp->tt_abbrind];
329
330 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
331 strcmp(cp, GRANDPARENTED) != 0)
332 *(cp + TZ_ABBR_MAX_LEN) = '\0';
333 }
334}
335
336static bool
337differ_by_repeat(const time_t t1, const time_t t0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338{
Calin Juravled8928922014-02-28 12:18:53 +0000339 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
340 return 0;
Elliott Hughes5204a9e2014-06-11 17:15:56 -0700341#if defined(__LP64__) // 32-bit Android only has a signed 32-bit time_t; 64-bit Android is fixed.
Calin Juravled8928922014-02-28 12:18:53 +0000342 return t1 - t0 == SECSPERREPEAT;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700343#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344}
345
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700346/* Input buffer for data read from a compiled tz file. */
347union input_buffer {
348 /* The first part of the buffer, interpreted as a header. */
349 struct tzhead tzhead;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700351 /* The entire buffer. */
352 char buf[2 * sizeof(struct tzhead) + 2 * sizeof (struct state)
353 + 4 * TZ_MAX_TIMES];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354};
355
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700356/* Local storage needed for 'tzloadbody'. */
357union local_storage {
358 /* The file name to be opened. */
359 char fullname[FILENAME_MAX + 1];
360
361 /* The results of analyzing the file's contents after it is opened. */
362 struct {
363 /* The input buffer. */
364 union input_buffer u;
365
366 /* A temporary state used for parsing a TZ string in the file. */
367 struct state st;
368 } u;
369};
370
371static int __bionic_open_tzdata(const char*);
372
373/* Load tz data from the file named NAME into *SP. Read extended
374 format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
375 success, an errno value on failure. */
376static int
377tzloadbody(char const *name, struct state *sp, bool doextend,
378 union local_storage *lsp)
379{
380 register int i;
381 register int fid;
382 register int stored;
383 register ssize_t nread;
384#if !defined(__ANDROID__)
385 register bool doaccess;
386 register char *fullname = lsp->fullname;
387#endif
388 register union input_buffer *up = &lsp->u.u;
389 register int tzheadsize = sizeof (struct tzhead);
390
391 sp->goback = sp->goahead = false;
392
393 if (! name) {
394 name = TZDEFAULT;
395 if (! name)
396 return EINVAL;
397 }
398
399#if defined(__ANDROID__)
400 fid = __bionic_open_tzdata(name);
401#else
402 if (name[0] == ':')
403 ++name;
404 doaccess = name[0] == '/';
405 if (!doaccess) {
406 char const *p = TZDIR;
407 if (! p)
408 return EINVAL;
409 if (sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
410 return ENAMETOOLONG;
411 strcpy(fullname, p);
412 strcat(fullname, "/");
413 strcat(fullname, name);
414 /* Set doaccess if '.' (as in "../") shows up in name. */
415 if (strchr(name, '.'))
416 doaccess = true;
417 name = fullname;
418 }
419 if (doaccess && access(name, R_OK) != 0)
420 return errno;
421 fid = open(name, OPEN_MODE);
422#endif
423 if (fid < 0)
424 return errno;
425
426 nread = read(fid, up->buf, sizeof up->buf);
427 if (nread < tzheadsize) {
428 int err = nread < 0 ? errno : EINVAL;
429 close(fid);
430 return err;
431 }
432 if (close(fid) < 0)
433 return errno;
434 for (stored = 4; stored <= 8; stored *= 2) {
435 int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
436 int_fast32_t ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
437 int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
438 int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
439 int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
440 int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
441 char const *p = up->buf + tzheadsize;
442 if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
443 && 0 < typecnt && typecnt < TZ_MAX_TYPES
444 && 0 <= timecnt && timecnt < TZ_MAX_TIMES
445 && 0 <= charcnt && charcnt < TZ_MAX_CHARS
446 && (ttisstdcnt == typecnt || ttisstdcnt == 0)
447 && (ttisgmtcnt == typecnt || ttisgmtcnt == 0)))
448 return EINVAL;
449 if (nread
450 < (tzheadsize /* struct tzhead */
451 + timecnt * stored /* ats */
452 + timecnt /* types */
453 + typecnt * 6 /* ttinfos */
454 + charcnt /* chars */
455 + leapcnt * (stored + 4) /* lsinfos */
456 + ttisstdcnt /* ttisstds */
457 + ttisgmtcnt)) /* ttisgmts */
458 return EINVAL;
459 sp->leapcnt = leapcnt;
460 sp->timecnt = timecnt;
461 sp->typecnt = typecnt;
462 sp->charcnt = charcnt;
463
464 /* Read transitions, discarding those out of time_t range.
465 But pretend the last transition before time_t_min
466 occurred at time_t_min. */
467 timecnt = 0;
468 for (i = 0; i < sp->timecnt; ++i) {
469 int_fast64_t at
470 = stored == 4 ? detzcode(p) : detzcode64(p);
471 sp->types[i] = at <= time_t_max;
472 if (sp->types[i]) {
473 time_t attime
474 = ((TYPE_SIGNED(time_t) ? at < time_t_min : at < 0)
475 ? time_t_min : at);
476 if (timecnt && attime <= sp->ats[timecnt - 1]) {
477 if (attime < sp->ats[timecnt - 1])
478 return EINVAL;
479 sp->types[i - 1] = 0;
480 timecnt--;
481 }
482 sp->ats[timecnt++] = attime;
483 }
484 p += stored;
485 }
486
487 timecnt = 0;
488 for (i = 0; i < sp->timecnt; ++i) {
489 unsigned char typ = *p++;
490 if (sp->typecnt <= typ)
491 return EINVAL;
492 if (sp->types[i])
493 sp->types[timecnt++] = typ;
494 }
495 sp->timecnt = timecnt;
496 for (i = 0; i < sp->typecnt; ++i) {
497 register struct ttinfo * ttisp;
498 unsigned char isdst, abbrind;
499
500 ttisp = &sp->ttis[i];
501 ttisp->tt_gmtoff = detzcode(p);
502 p += 4;
503 isdst = *p++;
504 if (! (isdst < 2))
505 return EINVAL;
506 ttisp->tt_isdst = isdst;
507 abbrind = *p++;
508 if (! (abbrind < sp->charcnt))
509 return EINVAL;
510 ttisp->tt_abbrind = abbrind;
511 }
512 for (i = 0; i < sp->charcnt; ++i)
513 sp->chars[i] = *p++;
514 sp->chars[i] = '\0'; /* ensure '\0' at end */
515
516 /* Read leap seconds, discarding those out of time_t range. */
517 leapcnt = 0;
518 for (i = 0; i < sp->leapcnt; ++i) {
519 int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
520 int_fast32_t corr = detzcode(p + stored);
521 p += stored + 4;
522 if (tr <= time_t_max) {
523 time_t trans
524 = ((TYPE_SIGNED(time_t) ? tr < time_t_min : tr < 0)
525 ? time_t_min : tr);
526 if (leapcnt && trans <= sp->lsis[leapcnt - 1].ls_trans) {
527 if (trans < sp->lsis[leapcnt - 1].ls_trans)
528 return EINVAL;
529 leapcnt--;
530 }
531 sp->lsis[leapcnt].ls_trans = trans;
532 sp->lsis[leapcnt].ls_corr = corr;
533 leapcnt++;
534 }
535 }
536 sp->leapcnt = leapcnt;
537
538 for (i = 0; i < sp->typecnt; ++i) {
539 register struct ttinfo * ttisp;
540
541 ttisp = &sp->ttis[i];
542 if (ttisstdcnt == 0)
543 ttisp->tt_ttisstd = false;
544 else {
545 if (*p != true && *p != false)
546 return EINVAL;
547 ttisp->tt_ttisstd = *p++;
548 }
549 }
550 for (i = 0; i < sp->typecnt; ++i) {
551 register struct ttinfo * ttisp;
552
553 ttisp = &sp->ttis[i];
554 if (ttisgmtcnt == 0)
555 ttisp->tt_ttisgmt = false;
556 else {
557 if (*p != true && *p != false)
558 return EINVAL;
559 ttisp->tt_ttisgmt = *p++;
560 }
561 }
562 /*
563 ** If this is an old file, we're done.
564 */
565 if (up->tzhead.tzh_version[0] == '\0')
566 break;
567 nread -= p - up->buf;
568 memmove(up->buf, p, nread);
569 }
570 if (doextend && nread > 2 &&
571 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
572 sp->typecnt + 2 <= TZ_MAX_TYPES) {
573 struct state *ts = &lsp->u.st;
574
575 up->buf[nread - 1] = '\0';
576 if (tzparse(&up->buf[1], ts, false)
577 && ts->typecnt == 2) {
578
579 /* Attempt to reuse existing abbreviations.
580 Without this, America/Anchorage would stop
581 working after 2037 when TZ_MAX_CHARS is 50, as
582 sp->charcnt equals 42 (for LMT CAT CAWT CAPT AHST
583 AHDT YST AKDT AKST) and ts->charcnt equals 10
584 (for AKST AKDT). Reusing means sp->charcnt can
585 stay 42 in this example. */
586 int gotabbr = 0;
587 int charcnt = sp->charcnt;
588 for (i = 0; i < 2; i++) {
589 char *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
590 int j;
591 for (j = 0; j < charcnt; j++)
592 if (strcmp(sp->chars + j, tsabbr) == 0) {
593 ts->ttis[i].tt_abbrind = j;
594 gotabbr++;
595 break;
596 }
597 if (! (j < charcnt)) {
598 int tsabbrlen = strlen(tsabbr);
599 if (j + tsabbrlen < TZ_MAX_CHARS) {
600 strcpy(sp->chars + j, tsabbr);
601 charcnt = j + tsabbrlen + 1;
602 ts->ttis[i].tt_abbrind = j;
603 gotabbr++;
604 }
605 }
606 }
607 if (gotabbr == 2) {
608 sp->charcnt = charcnt;
609 for (i = 0; i < ts->timecnt; i++)
610 if (sp->ats[sp->timecnt - 1] < ts->ats[i])
611 break;
612 while (i < ts->timecnt
613 && sp->timecnt < TZ_MAX_TIMES) {
614 sp->ats[sp->timecnt] = ts->ats[i];
615 sp->types[sp->timecnt] = (sp->typecnt
616 + ts->types[i]);
617 sp->timecnt++;
618 i++;
619 }
620 sp->ttis[sp->typecnt++] = ts->ttis[0];
621 sp->ttis[sp->typecnt++] = ts->ttis[1];
622 }
623 }
624 }
625 if (sp->timecnt > 1) {
626 for (i = 1; i < sp->timecnt; ++i)
627 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
628 differ_by_repeat(sp->ats[i], sp->ats[0])) {
629 sp->goback = true;
630 break;
631 }
632 for (i = sp->timecnt - 2; i >= 0; --i)
633 if (typesequiv(sp, sp->types[sp->timecnt - 1],
634 sp->types[i]) &&
635 differ_by_repeat(sp->ats[sp->timecnt - 1],
636 sp->ats[i])) {
637 sp->goahead = true;
638 break;
639 }
640 }
641 /*
642 ** If type 0 is is unused in transitions,
643 ** it's the type to use for early times.
644 */
645 for (i = 0; i < sp->timecnt; ++i)
646 if (sp->types[i] == 0)
647 break;
648 i = i < sp->timecnt ? -1 : 0;
649 /*
650 ** Absent the above,
651 ** if there are transition times
652 ** and the first transition is to a daylight time
653 ** find the standard type less than and closest to
654 ** the type of the first transition.
655 */
656 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
657 i = sp->types[0];
658 while (--i >= 0)
659 if (!sp->ttis[i].tt_isdst)
660 break;
661 }
662 /*
663 ** If no result yet, find the first standard type.
664 ** If there is none, punt to type zero.
665 */
666 if (i < 0) {
667 i = 0;
668 while (sp->ttis[i].tt_isdst)
669 if (++i >= sp->typecnt) {
670 i = 0;
671 break;
672 }
673 }
674 sp->defaulttype = i;
675 return 0;
676}
677
678/* Load tz data from the file named NAME into *SP. Read extended
679 format if DOEXTEND. Return 0 on success, an errno value on failure. */
680static int
681tzload(char const *name, struct state *sp, bool doextend)
682{
683#ifdef ALL_STATE
684 union local_storage *lsp = malloc(sizeof *lsp);
685 if (!lsp)
686 return errno;
687 else {
688 int err = tzloadbody(name, sp, doextend, lsp);
689 free(lsp);
690 return err;
691 }
692#else
693 union local_storage ls;
694 return tzloadbody(name, sp, doextend, &ls);
695#endif
696}
697
698static bool
699typesequiv(const struct state *sp, int a, int b)
700{
701 register bool result;
702
703 if (sp == NULL ||
704 a < 0 || a >= sp->typecnt ||
705 b < 0 || b >= sp->typecnt)
706 result = false;
707 else {
708 register const struct ttinfo * ap = &sp->ttis[a];
709 register const struct ttinfo * bp = &sp->ttis[b];
710 result = ap->tt_gmtoff == bp->tt_gmtoff &&
711 ap->tt_isdst == bp->tt_isdst &&
712 ap->tt_ttisstd == bp->tt_ttisstd &&
713 ap->tt_ttisgmt == bp->tt_ttisgmt &&
714 strcmp(&sp->chars[ap->tt_abbrind],
715 &sp->chars[bp->tt_abbrind]) == 0;
716 }
717 return result;
718}
719
720static const int mon_lengths[2][MONSPERYEAR] = {
721 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
722 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
723};
724
725static const int year_lengths[2] = {
726 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727};
728
729/*
730** Given a pointer into a time zone string, scan until a character that is not
731** a valid character in a zone name is found. Return a pointer to that
732** character.
733*/
734
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700735static const char * ATTRIBUTE_PURE
736getzname(register const char *strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700738 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700740 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
741 c != '+')
742 ++strp;
743 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744}
745
746/*
747** Given a pointer into an extended time zone string, scan until the ending
748** delimiter of the zone name is located. Return a pointer to the delimiter.
749**
750** As with getzname above, the legal character set is actually quite
751** restricted, with other characters producing undefined results.
752** We don't do any checking here; checking is done later in common-case code.
753*/
754
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700755static const char * ATTRIBUTE_PURE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756getqzname(register const char *strp, const int delim)
757{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700758 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700760 while ((c = *strp) != '\0' && c != delim)
761 ++strp;
762 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763}
764
765/*
766** Given a pointer into a time zone string, extract a number from that string.
767** Check that the number is within a specified range; if it is not, return
768** NULL.
769** Otherwise, return a pointer to the first character not part of the number.
770*/
771
772static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700773getnum(register const char *strp, int *const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800774{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700775 register char c;
776 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700778 if (strp == NULL || !is_digit(c = *strp))
779 return NULL;
780 num = 0;
781 do {
782 num = num * 10 + (c - '0');
783 if (num > max)
784 return NULL; /* illegal value */
785 c = *++strp;
786 } while (is_digit(c));
787 if (num < min)
788 return NULL; /* illegal value */
789 *nump = num;
790 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791}
792
793/*
794** Given a pointer into a time zone string, extract a number of seconds,
795** in hh[:mm[:ss]] form, from the string.
796** If any error occurs, return NULL.
797** Otherwise, return a pointer to the first character not part of the number
798** of seconds.
799*/
800
801static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700802getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700804 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800805
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700806 /*
807 ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
808 ** "M10.4.6/26", which does not conform to Posix,
809 ** but which specifies the equivalent of
810 ** "02:00 on the first Sunday on or after 23 Oct".
811 */
812 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
813 if (strp == NULL)
814 return NULL;
815 *secsp = num * (int_fast32_t) SECSPERHOUR;
816 if (*strp == ':') {
817 ++strp;
818 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
819 if (strp == NULL)
820 return NULL;
821 *secsp += num * SECSPERMIN;
822 if (*strp == ':') {
823 ++strp;
824 /* 'SECSPERMIN' allows for leap seconds. */
825 strp = getnum(strp, &num, 0, SECSPERMIN);
826 if (strp == NULL)
827 return NULL;
828 *secsp += num;
829 }
830 }
831 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832}
833
834/*
835** Given a pointer into a time zone string, extract an offset, in
836** [+-]hh[:mm[:ss]] form, from the string.
837** If any error occurs, return NULL.
838** Otherwise, return a pointer to the first character not part of the time.
839*/
840
841static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700842getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700844 register bool neg = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700846 if (*strp == '-') {
847 neg = true;
848 ++strp;
849 } else if (*strp == '+')
850 ++strp;
851 strp = getsecs(strp, offsetp);
852 if (strp == NULL)
853 return NULL; /* illegal time */
854 if (neg)
855 *offsetp = -*offsetp;
856 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800857}
858
859/*
860** Given a pointer into a time zone string, extract a rule in the form
861** date[/time]. See POSIX section 8 for the format of "date" and "time".
862** If a valid rule is not found, return NULL.
863** Otherwise, return a pointer to the first character not part of the rule.
864*/
865
866static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700867getrule(const char *strp, register struct rule *const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700869 if (*strp == 'J') {
870 /*
871 ** Julian day.
872 */
873 rulep->r_type = JULIAN_DAY;
874 ++strp;
875 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
876 } else if (*strp == 'M') {
877 /*
878 ** Month, week, day.
879 */
880 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
881 ++strp;
882 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
883 if (strp == NULL)
884 return NULL;
885 if (*strp++ != '.')
886 return NULL;
887 strp = getnum(strp, &rulep->r_week, 1, 5);
888 if (strp == NULL)
889 return NULL;
890 if (*strp++ != '.')
891 return NULL;
892 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
893 } else if (is_digit(*strp)) {
894 /*
895 ** Day of year.
896 */
897 rulep->r_type = DAY_OF_YEAR;
898 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
899 } else return NULL; /* invalid format */
900 if (strp == NULL)
901 return NULL;
902 if (*strp == '/') {
903 /*
904 ** Time specified.
905 */
906 ++strp;
907 strp = getoffset(strp, &rulep->r_time);
908 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
909 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910}
911
912/*
Calin Juravle627d37c2014-02-28 11:46:03 +0000913** Given a year, a rule, and the offset from UT at the time that rule takes
914** effect, calculate the year-relative time that rule takes effect.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915*/
916
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700917static int_fast32_t ATTRIBUTE_PURE
Calin Juravle627d37c2014-02-28 11:46:03 +0000918transtime(const int year, register const struct rule *const rulep,
Calin Juravled8928922014-02-28 12:18:53 +0000919 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800920{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700921 register bool leapyear;
Calin Juravled8928922014-02-28 12:18:53 +0000922 register int_fast32_t value;
923 register int i;
924 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800925
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700926 INITIALIZE(value);
927 leapyear = isleap(year);
928 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700930 case JULIAN_DAY:
931 /*
932 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
933 ** years.
934 ** In non-leap years, or if the day number is 59 or less, just
935 ** add SECSPERDAY times the day number-1 to the time of
936 ** January 1, midnight, to get the day.
937 */
Calin Juravled8928922014-02-28 12:18:53 +0000938 value = (rulep->r_day - 1) * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700939 if (leapyear && rulep->r_day >= 60)
940 value += SECSPERDAY;
941 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700943 case DAY_OF_YEAR:
944 /*
945 ** n - day of year.
946 ** Just add SECSPERDAY times the day number to the time of
947 ** January 1, midnight, to get the day.
948 */
Calin Juravled8928922014-02-28 12:18:53 +0000949 value = rulep->r_day * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700950 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800951
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700952 case MONTH_NTH_DAY_OF_WEEK:
953 /*
954 ** Mm.n.d - nth "dth day" of month m.
955 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800956
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700957 /*
958 ** Use Zeller's Congruence to get day-of-week of first day of
959 ** month.
960 */
961 m1 = (rulep->r_mon + 9) % 12 + 1;
962 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
963 yy1 = yy0 / 100;
964 yy2 = yy0 % 100;
965 dow = ((26 * m1 - 2) / 10 +
966 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
967 if (dow < 0)
968 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700970 /*
971 ** "dow" is the day-of-week of the first day of the month. Get
972 ** the day-of-month (zero-origin) of the first "dow" day of the
973 ** month.
974 */
975 d = rulep->r_day - dow;
976 if (d < 0)
977 d += DAYSPERWEEK;
978 for (i = 1; i < rulep->r_week; ++i) {
979 if (d + DAYSPERWEEK >=
980 mon_lengths[leapyear][rulep->r_mon - 1])
981 break;
982 d += DAYSPERWEEK;
983 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700985 /*
986 ** "d" is the day-of-month (zero-origin) of the day we want.
987 */
Calin Juravled8928922014-02-28 12:18:53 +0000988 value = d * SECSPERDAY;
989 for (i = 0; i < rulep->r_mon - 1; ++i)
990 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700991 break;
992 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800993
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700994 /*
Calin Juravled8928922014-02-28 12:18:53 +0000995 ** "value" is the year-relative time of 00:00:00 UT on the day in
996 ** question. To get the year-relative time of the specified local
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700997 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -0700998 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700999 */
1000 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001001}
1002
1003/*
1004** Given a POSIX section 8-style TZ string, fill in the rule tables as
1005** appropriate.
1006*/
1007
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001008static bool
1009tzparse(const char *name, struct state *sp, bool lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001010{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001011 const char * stdname;
1012 const char * dstname;
1013 size_t stdlen;
1014 size_t dstlen;
1015 size_t charcnt;
1016 int_fast32_t stdoffset;
1017 int_fast32_t dstoffset;
1018 register char * cp;
1019 register bool load_ok;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001020
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001021 stdname = name;
1022 if (lastditch) {
1023 stdlen = sizeof gmt - 1;
1024 name += stdlen;
1025 stdoffset = 0;
1026 } else {
1027 if (*name == '<') {
1028 name++;
1029 stdname = name;
1030 name = getqzname(name, '>');
1031 if (*name != '>')
1032 return false;
1033 stdlen = name - stdname;
1034 name++;
1035 } else {
1036 name = getzname(name);
1037 stdlen = name - stdname;
1038 }
1039 if (!stdlen)
1040 return false;
1041 name = getoffset(name, &stdoffset);
1042 if (name == NULL)
1043 return false;
1044 }
1045 charcnt = stdlen + 1;
1046 if (sizeof sp->chars < charcnt)
1047 return false;
1048 load_ok = tzload(TZDEFRULES, sp, false) == 0;
1049 if (!load_ok)
1050 sp->leapcnt = 0; /* so, we're off a little */
1051 if (*name != '\0') {
1052 if (*name == '<') {
1053 dstname = ++name;
1054 name = getqzname(name, '>');
1055 if (*name != '>')
1056 return false;
1057 dstlen = name - dstname;
1058 name++;
1059 } else {
1060 dstname = name;
1061 name = getzname(name);
1062 dstlen = name - dstname; /* length of DST zone name */
1063 }
1064 if (!dstlen)
1065 return false;
1066 charcnt += dstlen + 1;
1067 if (sizeof sp->chars < charcnt)
1068 return false;
1069 if (*name != '\0' && *name != ',' && *name != ';') {
1070 name = getoffset(name, &dstoffset);
1071 if (name == NULL)
1072 return false;
1073 } else dstoffset = stdoffset - SECSPERHOUR;
1074 if (*name == '\0' && !load_ok)
1075 name = TZDEFRULESTRING;
1076 if (*name == ',' || *name == ';') {
1077 struct rule start;
1078 struct rule end;
1079 register int year;
1080 register int yearlim;
1081 register int timecnt;
1082 time_t janfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001084 ++name;
1085 if ((name = getrule(name, &start)) == NULL)
1086 return false;
1087 if (*name++ != ',')
1088 return false;
1089 if ((name = getrule(name, &end)) == NULL)
1090 return false;
1091 if (*name != '\0')
1092 return false;
1093 sp->typecnt = 2; /* standard time and DST */
1094 /*
1095 ** Two transitions per year, from EPOCH_YEAR forward.
1096 */
1097 init_ttinfo(&sp->ttis[0], -dstoffset, true, stdlen + 1);
1098 init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
1099 sp->defaulttype = 0;
1100 timecnt = 0;
1101 janfirst = 0;
1102 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1103 for (year = EPOCH_YEAR; year < yearlim; year++) {
1104 int_fast32_t
1105 starttime = transtime(year, &start, stdoffset),
1106 endtime = transtime(year, &end, dstoffset);
1107 int_fast32_t
1108 yearsecs = (year_lengths[isleap(year)]
1109 * SECSPERDAY);
1110 bool reversed = endtime < starttime;
1111 if (reversed) {
1112 int_fast32_t swap = starttime;
1113 starttime = endtime;
1114 endtime = swap;
1115 }
1116 if (reversed
1117 || (starttime < endtime
1118 && (endtime - starttime
1119 < (yearsecs
1120 + (stdoffset - dstoffset))))) {
1121 if (TZ_MAX_TIMES - 2 < timecnt)
1122 break;
1123 yearlim = year + YEARSPERREPEAT + 1;
1124 sp->ats[timecnt] = janfirst;
1125 if (increment_overflow_time
1126 (&sp->ats[timecnt], starttime))
1127 break;
1128 sp->types[timecnt++] = reversed;
1129 sp->ats[timecnt] = janfirst;
1130 if (increment_overflow_time
1131 (&sp->ats[timecnt], endtime))
1132 break;
1133 sp->types[timecnt++] = !reversed;
1134 }
1135 if (increment_overflow_time(&janfirst, yearsecs))
1136 break;
1137 }
1138 sp->timecnt = timecnt;
1139 if (!timecnt)
1140 sp->typecnt = 1; /* Perpetual DST. */
1141 } else {
1142 register int_fast32_t theirstdoffset;
1143 register int_fast32_t theirdstoffset;
1144 register int_fast32_t theiroffset;
1145 register bool isdst;
1146 register int i;
1147 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001148
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001149 if (*name != '\0')
1150 return false;
1151 /*
1152 ** Initial values of theirstdoffset and theirdstoffset.
1153 */
1154 theirstdoffset = 0;
1155 for (i = 0; i < sp->timecnt; ++i) {
1156 j = sp->types[i];
1157 if (!sp->ttis[j].tt_isdst) {
1158 theirstdoffset =
1159 -sp->ttis[j].tt_gmtoff;
1160 break;
1161 }
1162 }
1163 theirdstoffset = 0;
1164 for (i = 0; i < sp->timecnt; ++i) {
1165 j = sp->types[i];
1166 if (sp->ttis[j].tt_isdst) {
1167 theirdstoffset =
1168 -sp->ttis[j].tt_gmtoff;
1169 break;
1170 }
1171 }
1172 /*
1173 ** Initially we're assumed to be in standard time.
1174 */
1175 isdst = false;
1176 theiroffset = theirstdoffset;
1177 /*
1178 ** Now juggle transition times and types
1179 ** tracking offsets as you do.
1180 */
1181 for (i = 0; i < sp->timecnt; ++i) {
1182 j = sp->types[i];
1183 sp->types[i] = sp->ttis[j].tt_isdst;
1184 if (sp->ttis[j].tt_ttisgmt) {
1185 /* No adjustment to transition time */
1186 } else {
1187 /*
1188 ** If summer time is in effect, and the
1189 ** transition time was not specified as
1190 ** standard time, add the summer time
1191 ** offset to the transition time;
1192 ** otherwise, add the standard time
1193 ** offset to the transition time.
1194 */
1195 /*
1196 ** Transitions from DST to DDST
1197 ** will effectively disappear since
1198 ** POSIX provides for only one DST
1199 ** offset.
1200 */
1201 if (isdst && !sp->ttis[j].tt_ttisstd) {
1202 sp->ats[i] += dstoffset -
1203 theirdstoffset;
1204 } else {
1205 sp->ats[i] += stdoffset -
1206 theirstdoffset;
1207 }
1208 }
1209 theiroffset = -sp->ttis[j].tt_gmtoff;
1210 if (sp->ttis[j].tt_isdst)
1211 theirdstoffset = theiroffset;
1212 else theirstdoffset = theiroffset;
1213 }
1214 /*
1215 ** Finally, fill in ttis.
1216 */
1217 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1218 init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1219 sp->typecnt = 2;
1220 sp->defaulttype = 0;
1221 }
1222 } else {
1223 dstlen = 0;
1224 sp->typecnt = 1; /* only standard time */
1225 sp->timecnt = 0;
1226 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1227 sp->defaulttype = 0;
1228 }
1229 sp->charcnt = charcnt;
1230 cp = sp->chars;
1231 memcpy(cp, stdname, stdlen);
1232 cp += stdlen;
1233 *cp++ = '\0';
1234 if (dstlen != 0) {
1235 memcpy(cp, dstname, dstlen);
1236 *(cp + dstlen) = '\0';
1237 }
1238 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001239}
1240
1241static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001242gmtload(struct state *const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001244 if (tzload(gmt, sp, true) != 0)
1245 tzparse(gmt, sp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001246}
1247
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001248/* Initialize *SP to a value appropriate for the TZ setting NAME.
1249 Return 0 on success, an errno value on failure. */
1250static int
1251zoneinit(struct state *sp, char const *name)
1252{
1253 if (name && ! name[0]) {
1254 /*
1255 ** User wants it fast rather than right.
1256 */
1257 sp->leapcnt = 0; /* so, we're off a little */
1258 sp->timecnt = 0;
1259 sp->typecnt = 0;
1260 sp->charcnt = 0;
1261 sp->goback = sp->goahead = false;
1262 init_ttinfo(&sp->ttis[0], 0, false, 0);
1263 strcpy(sp->chars, gmt);
1264 sp->defaulttype = 0;
1265 return 0;
1266 } else {
1267 int err = tzload(name, sp, true);
1268 if (err != 0 && name && name[0] != ':' && tzparse(name, sp, false))
1269 err = 0;
1270 if (err == 0)
1271 scrub_abbrs(sp);
1272 return err;
1273 }
1274}
1275
1276static void
1277tzsetlcl(char const *name)
1278{
1279 struct state *sp = lclptr;
1280 int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1281 if (lcl < 0
1282 ? lcl_is_set < 0
1283 : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1284 return;
1285#ifdef ALL_STATE
1286 if (! sp)
1287 lclptr = sp = malloc(sizeof *lclptr);
1288#endif /* defined ALL_STATE */
1289 if (sp) {
1290 if (zoneinit(sp, name) != 0)
1291 zoneinit(sp, "");
1292 if (0 < lcl)
1293 strcpy(lcl_TZname, name);
1294 }
1295 settzname();
1296 lcl_is_set = lcl;
1297}
1298
1299#ifdef STD_INSPIRED
Elliott Hughesce4783c2013-07-12 17:31:11 -07001300void
1301tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001303 if (lock() != 0)
1304 return;
1305 tzsetlcl(NULL);
1306 unlock();
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001307}
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001308#endif
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001309
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001310#if defined(__ANDROID__)
Mark Salyzynd0578942015-10-02 13:15:07 -07001311#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
1312#include <sys/_system_properties.h> // For __system_property_serial.
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001313#endif
Elliott Hughesce4783c2013-07-12 17:31:11 -07001314
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001315static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001316tzset_unlocked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001317{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001318#if defined(__ANDROID__)
1319 const char * name = getenv("TZ");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001320
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001321 // Try the "persist.sys.timezone" system property.
1322 if (name == NULL) {
1323 static const prop_info *pi;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001324
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001325 if (!pi) {
1326 pi = __system_property_find("persist.sys.timezone");
Elliott Hughesce4783c2013-07-12 17:31:11 -07001327 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001328 if (pi) {
1329 static char buf[PROP_VALUE_MAX];
1330 static uint32_t s = -1;
1331 static bool ok = false;
1332 uint32_t serial = __system_property_serial(pi);
1333 if (serial != s) {
1334 ok = __system_property_read(pi, 0, buf) > 0;
1335 s = serial;
1336 }
1337 if (ok) {
1338 name = buf;
1339 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001340 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001341 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001342
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001343 tzsetlcl(name);
1344#else
1345 tzsetlcl(getenv("TZ"));
1346#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347}
1348
1349void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001350tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001351{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001352 if (lock() != 0)
1353 return;
1354 tzset_unlocked();
1355 unlock();
1356}
1357
1358static void
1359gmtcheck(void)
1360{
1361 static bool gmt_is_set;
1362 if (lock() != 0)
1363 return;
1364 if (! gmt_is_set) {
1365#ifdef ALL_STATE
1366 gmtptr = malloc(sizeof *gmtptr);
1367#endif
1368 if (gmtptr)
1369 gmtload(gmtptr);
1370 gmt_is_set = true;
1371 }
1372 unlock();
1373}
1374
1375#if NETBSD_INSPIRED
1376
1377timezone_t
1378tzalloc(char const *name)
1379{
1380 timezone_t sp = malloc(sizeof *sp);
1381 if (sp) {
1382 int err = zoneinit(sp, name);
1383 if (err != 0) {
1384 free(sp);
1385 errno = err;
1386 return NULL;
1387 }
1388 }
1389 return sp;
1390}
1391
1392void
1393tzfree(timezone_t sp)
1394{
1395 free(sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396}
1397
1398/*
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001399** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1400** ctime_r are obsolescent and have potential security problems that
1401** ctime_rz would share. Callers can instead use localtime_rz + strftime.
1402**
1403** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1404** in zones with three or more time zone abbreviations.
1405** Callers can instead use localtime_rz + strftime.
1406*/
1407
1408#endif
1409
1410/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001411** The easy way to behave "as if no library function calls" localtime
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001412** is to not call it, so we drop its guts into "localsub", which can be
1413** freely called. (And no, the PANS doesn't require the above behavior,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001414** but it *is* desirable.)
1415**
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001416** If successful and SETNAME is nonzero,
1417** set the applicable parts of tzname, timezone and altzone;
1418** however, it's OK to omit this step if the time zone is POSIX-compatible,
1419** since in that case tzset should have already done this step correctly.
1420** SETNAME's type is intfast32_t for compatibility with gmtsub,
1421** but it is actually a boolean and its value should be 0 or 1.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422*/
1423
1424/*ARGSUSED*/
1425static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001426localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
1427 struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001429 register const struct ttinfo * ttisp;
1430 register int i;
1431 register struct tm * result;
1432 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001433
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001434 if (sp == NULL) {
1435 /* Don't bother to set tzname etc.; tzset has already done it. */
1436 return gmtsub(gmtptr, timep, 0, tmp);
1437 }
1438 if ((sp->goback && t < sp->ats[0]) ||
1439 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1440 time_t newt = t;
1441 register time_t seconds;
1442 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001444 if (t < sp->ats[0])
1445 seconds = sp->ats[0] - t;
1446 else seconds = t - sp->ats[sp->timecnt - 1];
1447 --seconds;
1448 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1449 seconds = years * AVGSECSPERYEAR;
1450 if (t < sp->ats[0])
1451 newt += seconds;
1452 else newt -= seconds;
1453 if (newt < sp->ats[0] ||
1454 newt > sp->ats[sp->timecnt - 1])
1455 return NULL; /* "cannot happen" */
1456 result = localsub(sp, &newt, setname, tmp);
1457 if (result) {
1458 register int_fast64_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001460 newy = result->tm_year;
1461 if (t < sp->ats[0])
1462 newy -= years;
1463 else newy += years;
1464 if (! (INT_MIN <= newy && newy <= INT_MAX))
1465 return NULL;
1466 result->tm_year = newy;
1467 }
1468 return result;
1469 }
1470 if (sp->timecnt == 0 || t < sp->ats[0]) {
1471 i = sp->defaulttype;
1472 } else {
1473 register int lo = 1;
1474 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001475
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001476 while (lo < hi) {
1477 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001478
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001479 if (t < sp->ats[mid])
1480 hi = mid;
1481 else lo = mid + 1;
1482 }
1483 i = (int) sp->types[lo - 1];
1484 }
1485 ttisp = &sp->ttis[i];
1486 /*
1487 ** To get (wrong) behavior that's compatible with System V Release 2.0
1488 ** you'd replace the statement below with
1489 ** t += ttisp->tt_gmtoff;
1490 ** timesub(&t, 0L, sp, tmp);
1491 */
1492 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1493 if (result) {
1494 result->tm_isdst = ttisp->tt_isdst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001496 result->TM_ZONE = (char *) &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001498 if (setname)
1499 update_tzname_etc(sp, ttisp);
1500 }
1501 return result;
1502}
1503
1504#if NETBSD_INSPIRED
1505
1506struct tm *
1507localtime_rz(struct state *sp, time_t const *timep, struct tm *tmp)
1508{
1509 return localsub(sp, timep, 0, tmp);
1510}
1511
1512#endif
1513
1514static struct tm *
1515localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1516{
1517 int err = lock();
1518 if (err) {
1519 errno = err;
1520 return NULL;
1521 }
1522 if (setname || !lcl_is_set)
1523 tzset_unlocked();
1524 tmp = localsub(lclptr, timep, setname, tmp);
1525 unlock();
1526 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001527}
1528
1529struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001530localtime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001532 return localtime_tzset(timep, &tm, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001533}
1534
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001535struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001536localtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001537{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001538 return localtime_tzset(timep, tmp, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539}
1540
1541/*
1542** gmtsub is to gmtime as localsub is to localtime.
1543*/
1544
1545static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001546gmtsub(struct state const *sp, time_t const *timep, int_fast32_t offset,
1547 struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001548{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001549 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001551 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001552#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001553 /*
1554 ** Could get fancy here and deliver something such as
1555 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
1556 ** but this is no time for a treasure hunt.
1557 */
1558 tmp->TM_ZONE = ((char *)
1559 (offset ? wildabbr : gmtptr ? gmtptr->chars : gmt));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001560#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001561 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001562}
1563
1564/*
1565* Re-entrant version of gmtime.
1566*/
1567
1568struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001569gmtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001571 gmtcheck();
1572 return gmtsub(gmtptr, timep, 0, tmp);
1573}
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001574
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001575struct tm *
1576gmtime(const time_t *timep)
1577{
1578 return gmtime_r(timep, &tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001579}
1580
Elliott Hughes906eb992014-06-18 19:46:25 -07001581#ifdef STD_INSPIRED
1582
1583struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001584offtime(const time_t *timep, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07001585{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001586 gmtcheck();
1587 return gmtsub(gmtptr, timep, offset, &tm);
Elliott Hughes906eb992014-06-18 19:46:25 -07001588}
1589
1590#endif /* defined STD_INSPIRED */
1591
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001592/*
1593** Return the number of leap years through the end of the given year
1594** where, to make the math easy, the answer for year zero is defined as zero.
1595*/
1596
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001597static int ATTRIBUTE_PURE
Elliott Hughesce4783c2013-07-12 17:31:11 -07001598leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001600 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1601 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001602}
1603
1604static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001605timesub(const time_t *timep, int_fast32_t offset,
1606 const struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001607{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001608 register const struct lsinfo * lp;
1609 register time_t tdays;
1610 register int idays; /* unsigned would be so 2003 */
1611 register int_fast64_t rem;
1612 int y;
1613 register const int * ip;
1614 register int_fast64_t corr;
1615 register bool hit;
1616 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001617
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001618 corr = 0;
1619 hit = false;
1620 i = (sp == NULL) ? 0 : sp->leapcnt;
1621 while (--i >= 0) {
1622 lp = &sp->lsis[i];
1623 if (*timep >= lp->ls_trans) {
1624 if (*timep == lp->ls_trans) {
1625 hit = ((i == 0 && lp->ls_corr > 0) ||
1626 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1627 if (hit)
1628 while (i > 0 &&
1629 sp->lsis[i].ls_trans ==
1630 sp->lsis[i - 1].ls_trans + 1 &&
1631 sp->lsis[i].ls_corr ==
1632 sp->lsis[i - 1].ls_corr + 1) {
1633 ++hit;
1634 --i;
1635 }
1636 }
1637 corr = lp->ls_corr;
1638 break;
1639 }
1640 }
1641 y = EPOCH_YEAR;
1642 tdays = *timep / SECSPERDAY;
1643 rem = *timep % SECSPERDAY;
1644 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1645 int newy;
1646 register time_t tdelta;
1647 register int idelta;
1648 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001649
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001650 tdelta = tdays / DAYSPERLYEAR;
1651 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1652 && tdelta <= INT_MAX))
1653 goto out_of_range;
1654 idelta = tdelta;
1655 if (idelta == 0)
1656 idelta = (tdays < 0) ? -1 : 1;
1657 newy = y;
1658 if (increment_overflow(&newy, idelta))
1659 goto out_of_range;
1660 leapdays = leaps_thru_end_of(newy - 1) -
1661 leaps_thru_end_of(y - 1);
1662 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1663 tdays -= leapdays;
1664 y = newy;
1665 }
1666 /*
1667 ** Given the range, we can now fearlessly cast...
1668 */
1669 idays = tdays;
1670 rem += offset - corr;
1671 while (rem < 0) {
1672 rem += SECSPERDAY;
1673 --idays;
1674 }
1675 while (rem >= SECSPERDAY) {
1676 rem -= SECSPERDAY;
1677 ++idays;
1678 }
1679 while (idays < 0) {
1680 if (increment_overflow(&y, -1))
1681 goto out_of_range;
1682 idays += year_lengths[isleap(y)];
1683 }
1684 while (idays >= year_lengths[isleap(y)]) {
1685 idays -= year_lengths[isleap(y)];
1686 if (increment_overflow(&y, 1))
1687 goto out_of_range;
1688 }
1689 tmp->tm_year = y;
1690 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1691 goto out_of_range;
1692 tmp->tm_yday = idays;
1693 /*
1694 ** The "extra" mods below avoid overflow problems.
1695 */
1696 tmp->tm_wday = EPOCH_WDAY +
1697 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1698 (DAYSPERNYEAR % DAYSPERWEEK) +
1699 leaps_thru_end_of(y - 1) -
1700 leaps_thru_end_of(EPOCH_YEAR - 1) +
1701 idays;
1702 tmp->tm_wday %= DAYSPERWEEK;
1703 if (tmp->tm_wday < 0)
1704 tmp->tm_wday += DAYSPERWEEK;
1705 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1706 rem %= SECSPERHOUR;
1707 tmp->tm_min = (int) (rem / SECSPERMIN);
1708 /*
1709 ** A positive leap second requires a special
1710 ** representation. This uses "... ??:59:60" et seq.
1711 */
1712 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1713 ip = mon_lengths[isleap(y)];
1714 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1715 idays -= ip[tmp->tm_mon];
1716 tmp->tm_mday = (int) (idays + 1);
1717 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001718#ifdef TM_GMTOFF
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001719 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001720#endif /* defined TM_GMTOFF */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001721 return tmp;
1722
1723 out_of_range:
1724 errno = EOVERFLOW;
1725 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001726}
1727
1728char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001729ctime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001730{
1731/*
1732** Section 4.12.3.2 of X3.159-1989 requires that
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001733** The ctime function converts the calendar time pointed to by timer
1734** to local time in the form of a string. It is equivalent to
1735** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001736*/
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001737 struct tm *tmp = localtime(timep);
1738 return tmp ? asctime(tmp) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001739}
1740
1741char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001742ctime_r(const time_t *timep, char *buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001744 struct tm mytm;
1745 struct tm *tmp = localtime_r(timep, &mytm);
1746 return tmp ? asctime_r(tmp, buf) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001747}
1748
1749/*
1750** Adapted from code provided by Robert Elz, who writes:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001751** The "best" way to do mktime I think is based on an idea of Bob
1752** Kridle's (so its said...) from a long time ago.
1753** It does a binary search of the time_t space. Since time_t's are
1754** just 32 bits, its a max of 32 iterations (even at 64 bits it
1755** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001756*/
1757
1758#ifndef WRONG
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001759#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001760#endif /* !defined WRONG */
1761
1762/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001763** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001764*/
1765
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001766static bool
1767increment_overflow(int *ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001768{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001769 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001770
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001771 /*
1772 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1773 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1774 ** If i < 0 there can only be overflow if i + j < INT_MIN
1775 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1776 */
1777 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1778 return true;
1779 *ip += j;
1780 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001781}
1782
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001783static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001784increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001785{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001786 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001787
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001788 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1789 return true;
1790 *lp += m;
1791 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001792}
1793
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001794static bool
Calin Juravle627d37c2014-02-28 11:46:03 +00001795increment_overflow_time(time_t *tp, int_fast32_t j)
1796{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001797 /*
1798 ** This is like
1799 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1800 ** except that it does the right thing even if *tp + j would overflow.
1801 */
1802 if (! (j < 0
1803 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1804 : *tp <= time_t_max - j))
1805 return true;
1806 *tp += j;
1807 return false;
Calin Juravle627d37c2014-02-28 11:46:03 +00001808}
1809
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001810static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001811normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001813 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001814
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001815 tensdelta = (*unitsptr >= 0) ?
1816 (*unitsptr / base) :
1817 (-1 - (-1 - *unitsptr) / base);
1818 *unitsptr -= tensdelta * base;
1819 return increment_overflow(tensptr, tensdelta);
1820}
1821
1822static bool
1823normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
1824{
1825 register int tensdelta;
1826
1827 tensdelta = (*unitsptr >= 0) ?
1828 (*unitsptr / base) :
1829 (-1 - (-1 - *unitsptr) / base);
1830 *unitsptr -= tensdelta * base;
1831 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832}
1833
1834static int
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001835tmcomp(register const struct tm *const atmp,
1836 register const struct tm *const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001837{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001838 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001839
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001840 if (atmp->tm_year != btmp->tm_year)
1841 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1842 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1843 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1844 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1845 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1846 result = atmp->tm_sec - btmp->tm_sec;
1847 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001848}
1849
1850static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001851time2sub(struct tm *const tmp,
1852 struct tm *(*funcp)(struct state const *, time_t const *,
1853 int_fast32_t, struct tm *),
1854 struct state const *sp,
1855 const int_fast32_t offset,
1856 bool *okayp,
1857 bool do_norm_secs)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001858{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001859 register int dir;
1860 register int i, j;
1861 register int saved_seconds;
1862 register int_fast32_t li;
1863 register time_t lo;
1864 register time_t hi;
1865 int_fast32_t y;
1866 time_t newt;
1867 time_t t;
1868 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001869
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001870 *okayp = false;
1871 yourtm = *tmp;
1872 if (do_norm_secs) {
1873 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1874 SECSPERMIN))
1875 return WRONG;
1876 }
1877 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1878 return WRONG;
1879 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1880 return WRONG;
1881 y = yourtm.tm_year;
1882 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1883 return WRONG;
1884 /*
1885 ** Turn y into an actual year number for now.
1886 ** It is converted back to an offset from TM_YEAR_BASE later.
1887 */
1888 if (increment_overflow32(&y, TM_YEAR_BASE))
1889 return WRONG;
1890 while (yourtm.tm_mday <= 0) {
1891 if (increment_overflow32(&y, -1))
1892 return WRONG;
1893 li = y + (1 < yourtm.tm_mon);
1894 yourtm.tm_mday += year_lengths[isleap(li)];
1895 }
1896 while (yourtm.tm_mday > DAYSPERLYEAR) {
1897 li = y + (1 < yourtm.tm_mon);
1898 yourtm.tm_mday -= year_lengths[isleap(li)];
1899 if (increment_overflow32(&y, 1))
1900 return WRONG;
1901 }
1902 for ( ; ; ) {
1903 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1904 if (yourtm.tm_mday <= i)
1905 break;
1906 yourtm.tm_mday -= i;
1907 if (++yourtm.tm_mon >= MONSPERYEAR) {
1908 yourtm.tm_mon = 0;
1909 if (increment_overflow32(&y, 1))
1910 return WRONG;
1911 }
1912 }
1913 if (increment_overflow32(&y, -TM_YEAR_BASE))
1914 return WRONG;
1915 if (! (INT_MIN <= y && y <= INT_MAX))
1916 return WRONG;
1917 yourtm.tm_year = y;
1918 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1919 saved_seconds = 0;
1920 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1921 /*
1922 ** We can't set tm_sec to 0, because that might push the
1923 ** time below the minimum representable time.
1924 ** Set tm_sec to 59 instead.
1925 ** This assumes that the minimum representable time is
1926 ** not in the same minute that a leap second was deleted from,
1927 ** which is a safer assumption than using 58 would be.
1928 */
1929 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1930 return WRONG;
1931 saved_seconds = yourtm.tm_sec;
1932 yourtm.tm_sec = SECSPERMIN - 1;
1933 } else {
1934 saved_seconds = yourtm.tm_sec;
1935 yourtm.tm_sec = 0;
1936 }
1937 /*
1938 ** Do a binary search (this works whatever time_t's type is).
1939 */
1940 lo = time_t_min;
1941 hi = time_t_max;
1942 for ( ; ; ) {
1943 t = lo / 2 + hi / 2;
1944 if (t < lo)
1945 t = lo;
1946 else if (t > hi)
1947 t = hi;
1948 if (! funcp(sp, &t, offset, &mytm)) {
1949 /*
1950 ** Assume that t is too extreme to be represented in
1951 ** a struct tm; arrange things so that it is less
1952 ** extreme on the next pass.
1953 */
1954 dir = (t > 0) ? 1 : -1;
1955 } else dir = tmcomp(&mytm, &yourtm);
1956 if (dir != 0) {
1957 if (t == lo) {
1958 if (t == time_t_max)
1959 return WRONG;
1960 ++t;
1961 ++lo;
1962 } else if (t == hi) {
1963 if (t == time_t_min)
1964 return WRONG;
1965 --t;
1966 --hi;
1967 }
1968 if (lo > hi)
1969 return WRONG;
1970 if (dir > 0)
1971 hi = t;
1972 else lo = t;
1973 continue;
1974 }
1975#if defined TM_GMTOFF && ! UNINIT_TRAP
1976 if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
1977 && (yourtm.TM_GMTOFF < 0
1978 ? (-SECSPERDAY <= yourtm.TM_GMTOFF
1979 && (mytm.TM_GMTOFF <=
1980 (SMALLEST (INT_FAST32_MAX, LONG_MAX)
1981 + yourtm.TM_GMTOFF)))
1982 : (yourtm.TM_GMTOFF <= SECSPERDAY
1983 && ((BIGGEST (INT_FAST32_MIN, LONG_MIN)
1984 + yourtm.TM_GMTOFF)
1985 <= mytm.TM_GMTOFF)))) {
1986 /* MYTM matches YOURTM except with the wrong UTC offset.
1987 YOURTM.TM_GMTOFF is plausible, so try it instead.
1988 It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
1989 since the guess gets checked. */
1990 time_t altt = t;
1991 int_fast32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
1992 if (!increment_overflow_time(&altt, diff)) {
1993 struct tm alttm;
1994 if (funcp(sp, &altt, offset, &alttm)
1995 && alttm.tm_isdst == mytm.tm_isdst
1996 && alttm.TM_GMTOFF == yourtm.TM_GMTOFF
1997 && tmcomp(&alttm, &yourtm) == 0) {
1998 t = altt;
1999 mytm = alttm;
2000 }
2001 }
2002 }
2003#endif
2004 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
2005 break;
2006 /*
2007 ** Right time, wrong type.
2008 ** Hunt for right time, right type.
2009 ** It's okay to guess wrong since the guess
2010 ** gets checked.
2011 */
2012 if (sp == NULL)
2013 return WRONG;
2014 for (i = sp->typecnt - 1; i >= 0; --i) {
2015 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2016 continue;
2017 for (j = sp->typecnt - 1; j >= 0; --j) {
2018 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2019 continue;
2020 newt = t + sp->ttis[j].tt_gmtoff -
2021 sp->ttis[i].tt_gmtoff;
2022 if (! funcp(sp, &newt, offset, &mytm))
2023 continue;
2024 if (tmcomp(&mytm, &yourtm) != 0)
2025 continue;
2026 if (mytm.tm_isdst != yourtm.tm_isdst)
2027 continue;
2028 /*
2029 ** We have a match.
2030 */
2031 t = newt;
2032 goto label;
2033 }
2034 }
2035 return WRONG;
2036 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037label:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002038 newt = t + saved_seconds;
2039 if ((newt < t) != (saved_seconds < 0))
2040 return WRONG;
2041 t = newt;
2042 if (funcp(sp, &t, offset, tmp))
2043 *okayp = true;
2044 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045}
2046
2047static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002048time2(struct tm * const tmp,
2049 struct tm *(*funcp)(struct state const *, time_t const *,
2050 int_fast32_t, struct tm *),
2051 struct state const *sp,
Elliott Hughesce4783c2013-07-12 17:31:11 -07002052 const int_fast32_t offset,
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002053 bool *okayp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002054{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002055 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002056
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002057 /*
2058 ** First try without normalization of seconds
2059 ** (in case tm_sec contains a value associated with a leap second).
2060 ** If that fails, try with normalization of seconds.
2061 */
2062 t = time2sub(tmp, funcp, sp, offset, okayp, false);
2063 return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002064}
2065
2066static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002067time1(struct tm *const tmp,
2068 struct tm *(*funcp) (struct state const *, time_t const *,
2069 int_fast32_t, struct tm *),
2070 struct state const *sp,
2071 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002072{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002073 register time_t t;
2074 register int samei, otheri;
2075 register int sameind, otherind;
2076 register int i;
2077 register int nseen;
2078 char seen[TZ_MAX_TYPES];
2079 unsigned char types[TZ_MAX_TYPES];
2080 bool okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002081
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002082 if (tmp == NULL) {
2083 errno = EINVAL;
2084 return WRONG;
2085 }
2086 if (tmp->tm_isdst > 1)
2087 tmp->tm_isdst = 1;
2088 t = time2(tmp, funcp, sp, offset, &okay);
2089 if (okay)
2090 return t;
2091 if (tmp->tm_isdst < 0)
Elliott Hughes906eb992014-06-18 19:46:25 -07002092#ifdef PCTS
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002093 /*
2094 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
2095 */
2096 tmp->tm_isdst = 0; /* reset to std and try again */
Elliott Hughes906eb992014-06-18 19:46:25 -07002097#else
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002098 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002099#endif /* !defined PCTS */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002100 /*
2101 ** We're supposed to assume that somebody took a time of one type
2102 ** and did some math on it that yielded a "struct tm" that's bad.
2103 ** We try to divine the type they started from and adjust to the
2104 ** type they need.
2105 */
2106 if (sp == NULL)
2107 return WRONG;
2108 for (i = 0; i < sp->typecnt; ++i)
2109 seen[i] = false;
2110 nseen = 0;
2111 for (i = sp->timecnt - 1; i >= 0; --i)
2112 if (!seen[sp->types[i]]) {
2113 seen[sp->types[i]] = true;
2114 types[nseen++] = sp->types[i];
2115 }
2116 for (sameind = 0; sameind < nseen; ++sameind) {
2117 samei = types[sameind];
2118 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2119 continue;
2120 for (otherind = 0; otherind < nseen; ++otherind) {
2121 otheri = types[otherind];
2122 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2123 continue;
2124 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2125 sp->ttis[samei].tt_gmtoff;
2126 tmp->tm_isdst = !tmp->tm_isdst;
2127 t = time2(tmp, funcp, sp, offset, &okay);
2128 if (okay)
2129 return t;
2130 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2131 sp->ttis[samei].tt_gmtoff;
2132 tmp->tm_isdst = !tmp->tm_isdst;
2133 }
2134 }
2135 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002136}
2137
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002138static time_t
2139mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
2140{
2141 if (sp)
2142 return time1(tmp, localsub, sp, setname);
2143 else {
2144 gmtcheck();
2145 return time1(tmp, gmtsub, gmtptr, 0);
2146 }
2147}
2148
2149#if NETBSD_INSPIRED
2150
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002151time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002152mktime_z(struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002153{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002154 return mktime_tzname(sp, tmp, false);
2155}
2156
2157#endif
2158
2159time_t
2160mktime(struct tm *tmp)
2161{
2162 time_t t;
2163 int err = lock();
2164 if (err) {
2165 errno = err;
2166 return -1;
2167 }
2168 tzset_unlocked();
2169 t = mktime_tzname(lclptr, tmp, true);
2170 unlock();
2171 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002172}
2173
2174#ifdef STD_INSPIRED
2175
2176time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002177timelocal(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002178{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002179 if (tmp != NULL)
2180 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2181 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002182}
2183
2184time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002185timegm(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002186{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002187 return timeoff(tmp, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002188}
2189
Elliott Hughes906eb992014-06-18 19:46:25 -07002190time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002191timeoff(struct tm *tmp, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07002192{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002193 if (tmp)
2194 tmp->tm_isdst = 0;
2195 gmtcheck();
2196 return time1(tmp, gmtsub, gmtptr, offset);
Elliott Hughes906eb992014-06-18 19:46:25 -07002197}
2198
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002199#endif /* defined STD_INSPIRED */
2200
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002201/*
2202** XXX--is the below the right way to conditionalize??
2203*/
2204
2205#ifdef STD_INSPIRED
2206
2207/*
2208** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2209** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2210** is not the case if we are accounting for leap seconds.
2211** So, we provide the following conversion routines for use
2212** when exchanging timestamps with POSIX conforming systems.
2213*/
2214
Elliott Hughesce4783c2013-07-12 17:31:11 -07002215static int_fast64_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002216leapcorr(struct state const *sp, time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002217{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002218 register struct lsinfo const * lp;
2219 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002220
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002221 i = sp->leapcnt;
2222 while (--i >= 0) {
2223 lp = &sp->lsis[i];
2224 if (t >= lp->ls_trans)
2225 return lp->ls_corr;
2226 }
2227 return 0;
2228}
2229
2230NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2231time2posix_z(struct state *sp, time_t t)
2232{
2233 return t - leapcorr(sp, t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002234}
2235
2236time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002237time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002238{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002239 int err = lock();
2240 if (err) {
2241 errno = err;
2242 return -1;
2243 }
2244 if (!lcl_is_set)
2245 tzset_unlocked();
2246 if (lclptr)
2247 t = time2posix_z(lclptr, t);
2248 unlock();
2249 return t;
2250}
2251
2252NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2253posix2time_z(struct state *sp, time_t t)
2254{
2255 time_t x;
2256 time_t y;
2257 /*
2258 ** For a positive leap second hit, the result
2259 ** is not unique. For a negative leap second
2260 ** hit, the corresponding time doesn't exist,
2261 ** so we return an adjacent second.
2262 */
2263 x = t + leapcorr(sp, t);
2264 y = x - leapcorr(sp, x);
2265 if (y < t) {
2266 do {
2267 x++;
2268 y = x - leapcorr(sp, x);
2269 } while (y < t);
2270 x -= y != t;
2271 } else if (y > t) {
2272 do {
2273 --x;
2274 y = x - leapcorr(sp, x);
2275 } while (y > t);
2276 x += y != t;
2277 }
2278 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002279}
2280
2281time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002282posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002283{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002284 int err = lock();
2285 if (err) {
2286 errno = err;
2287 return -1;
2288 }
2289 if (!lcl_is_set)
2290 tzset_unlocked();
2291 if (lclptr)
2292 t = posix2time_z(lclptr, t);
2293 unlock();
2294 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002295}
2296
2297#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002298
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002299#ifdef time_tz
2300
2301/* Convert from the underlying system's time_t to the ersatz time_tz,
2302 which is called 'time_t' in this file. */
2303
2304time_t
2305time(time_t *p)
2306{
2307 time_t r = sys_time(0);
2308 if (p)
2309 *p = r;
2310 return r;
2311}
2312
2313#endif
2314
Elliott Hughesce4783c2013-07-12 17:31:11 -07002315// BEGIN android-added
2316
Elliott Hughes1c295722012-10-19 18:13:15 -07002317#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002318#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002319#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002320
Elliott Hughescf178bf2013-09-18 19:25:28 -07002321static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002322 const char* olson_id) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002323 const char* path_prefix = getenv(path_prefix_variable);
2324 if (path_prefix == NULL) {
2325 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2326 return -1;
2327 }
Elliott Hughes329103d2014-04-25 16:55:04 -07002328 size_t path_length = strlen(path_prefix) + 1 + strlen(path_suffix) + 1;
2329 char* path = malloc(path_length);
2330 if (path == NULL) {
2331 fprintf(stderr, "%s: couldn't allocate %zu-byte path\n", __FUNCTION__, path_length);
2332 return -1;
2333 }
2334 snprintf(path, path_length, "%s/%s", path_prefix, path_suffix);
Elliott Hughes1c295722012-10-19 18:13:15 -07002335 int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
Elliott Hughesd23af232012-10-17 16:30:47 -07002336 if (fd == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002337 free(path);
Elliott Hughes1c295722012-10-19 18:13:15 -07002338 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002339 }
2340
2341 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002342 // int index_offset
2343 // int data_offset
2344 // int zonetab_offset
2345 struct bionic_tzdata_header {
2346 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002347 int32_t index_offset;
2348 int32_t data_offset;
2349 int32_t zonetab_offset;
2350 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002351 memset(&header, 0, sizeof(header));
2352 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2353 if (bytes_read != sizeof(header)) {
2354 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2355 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002356 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002357 close(fd);
2358 return -1;
2359 }
2360
2361 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002362 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2363 __FUNCTION__, path, header.tzdata_version);
Elliott Hughes329103d2014-04-25 16:55:04 -07002364 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002365 close(fd);
2366 return -1;
2367 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002368
2369#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002370 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002371 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2372 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2373 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2374#endif
2375
2376 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002377 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2378 __FUNCTION__, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002379 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002380 close(fd);
2381 return -1;
2382 }
2383
2384 off_t specific_zone_offset = -1;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002385 ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
2386 char* index = malloc(index_size);
Elliott Hughes329103d2014-04-25 16:55:04 -07002387 if (index == NULL) {
2388 fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n",
2389 __FUNCTION__, index_size, path);
2390 free(path);
2391 close(fd);
2392 return -1;
2393 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002394 if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) {
2395 fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
2396 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002397 free(path);
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002398 free(index);
2399 close(fd);
2400 return -1;
2401 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002402
Elliott Hughes1c295722012-10-19 18:13:15 -07002403 static const size_t NAME_LENGTH = 40;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002404 struct index_entry_t {
2405 char buf[NAME_LENGTH];
2406 int32_t start;
2407 int32_t length;
Elliott Hughes13bab432014-08-06 15:23:11 -07002408 int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L).
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002409 };
Elliott Hughese0175ca2013-03-14 14:38:08 -07002410
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002411 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(struct index_entry_t);
2412 struct index_entry_t* entry = (struct index_entry_t*) index;
Elliott Hughese0175ca2013-03-14 14:38:08 -07002413 for (size_t i = 0; i < id_count; ++i) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002414 char this_id[NAME_LENGTH + 1];
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002415 memcpy(this_id, entry->buf, NAME_LENGTH);
Elliott Hughes1c295722012-10-19 18:13:15 -07002416 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002417
2418 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002419 specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset);
Elliott Hughesd23af232012-10-17 16:30:47 -07002420 break;
2421 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002422
2423 ++entry;
Elliott Hughesd23af232012-10-17 16:30:47 -07002424 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002425 free(index);
Elliott Hughesd23af232012-10-17 16:30:47 -07002426
2427 if (specific_zone_offset == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002428 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002429 close(fd);
2430 return -1;
2431 }
2432
2433 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002434 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2435 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002436 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002437 close(fd);
2438 return -1;
2439 }
2440
Elliott Hughese7aaad82013-04-25 14:02:59 -07002441 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2442
Elliott Hughes329103d2014-04-25 16:55:04 -07002443 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002444 return fd;
2445}
Elliott Hughes1c295722012-10-19 18:13:15 -07002446
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002447static int __bionic_open_tzdata(const char* olson_id) {
2448 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata", olson_id);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002449 if (fd < 0) {
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002450 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002451 if (fd == -2) {
2452 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2453 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2454 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
2455 }
Elliott Hughes1c295722012-10-19 18:13:15 -07002456 }
2457 return fd;
2458}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002459
2460// Caches the most recent timezone (http://b/8270865).
2461static int __bionic_tzload_cached(const char* name, struct state* const sp, const int doextend) {
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002462 lock();
Elliott Hughesce4783c2013-07-12 17:31:11 -07002463
2464 // Our single-item cache.
Elliott Hughes1728b232014-05-14 10:02:03 -07002465 static char* g_cached_time_zone_name;
2466 static struct state g_cached_time_zone;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002467
2468 // Do we already have this timezone cached?
Elliott Hughes1728b232014-05-14 10:02:03 -07002469 if (g_cached_time_zone_name != NULL && strcmp(name, g_cached_time_zone_name) == 0) {
2470 *sp = g_cached_time_zone;
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002471 unlock();
Elliott Hughesce4783c2013-07-12 17:31:11 -07002472 return 0;
2473 }
2474
2475 // Can we load it?
2476 int rc = tzload(name, sp, doextend);
2477 if (rc == 0) {
2478 // Update the cache.
Elliott Hughes1728b232014-05-14 10:02:03 -07002479 free(g_cached_time_zone_name);
2480 g_cached_time_zone_name = strdup(name);
2481 g_cached_time_zone = *sp;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002482 }
2483
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002484 unlock();
Elliott Hughesce4783c2013-07-12 17:31:11 -07002485 return rc;
2486}
2487
2488// Non-standard API: mktime(3) but with an explicit timezone parameter.
Elliott Hughes905e6d52014-07-25 11:55:59 -07002489// This can't actually be hidden/removed until we fix MtpUtils.cpp
2490__attribute__((visibility("default"))) time_t mktime_tz(struct tm* const tmp, const char* tz) {
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002491 struct state* st = malloc(sizeof(*st));
2492 time_t return_value;
2493
2494 if (st == NULL)
2495 return 0;
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002496 if (__bionic_tzload_cached(tz, st, true) != 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07002497 // TODO: not sure what's best here, but for now, we fall back to gmt.
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002498 gmtload(st);
Elliott Hughesce4783c2013-07-12 17:31:11 -07002499 }
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002500
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002501 return_value = time1(tmp, localsub, st, 0L);
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002502 free(st);
2503 return return_value;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002504}
2505
Elliott Hughesce4783c2013-07-12 17:31:11 -07002506// END android-added