blob: 74411f767322b24e461b4833fbefffb90494e5f5 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181/*
182** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700183** Except for the strftime function, these functions [asctime,
184** ctime, gmtime, localtime] return values in one of two static
185** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186** Thanks to Paul Eggert for noting this.
187*/
188
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700189static struct tm tm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Elliott Hughes0a610d02016-07-29 14:04:17 -0700191#if !HAVE_POSIX_DECLS
192char * tzname[2] = {
193 (char *) wildabbr,
194 (char *) wildabbr
195};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700197long timezone;
198int daylight;
Elliott Hughes0a610d02016-07-29 14:04:17 -0700199# endif
200#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
202#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700203long altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204#endif /* defined ALTZONE */
205
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700206/* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND. */
207static void
208init_ttinfo(struct ttinfo *s, int_fast32_t gmtoff, bool isdst, int abbrind)
209{
210 s->tt_gmtoff = gmtoff;
211 s->tt_isdst = isdst;
212 s->tt_abbrind = abbrind;
213 s->tt_ttisstd = false;
214 s->tt_ttisgmt = false;
215}
216
Elliott Hughesce4783c2013-07-12 17:31:11 -0700217static int_fast32_t
218detzcode(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700220 register int_fast32_t result;
221 register int i;
222 int_fast32_t one = 1;
223 int_fast32_t halfmaxval = one << (32 - 2);
224 int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
225 int_fast32_t minval = -1 - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700227 result = codep[0] & 0x7f;
228 for (i = 1; i < 4; ++i)
229 result = (result << 8) | (codep[i] & 0xff);
230
231 if (codep[0] & 0x80) {
232 /* Do two's-complement negation even on non-two's-complement machines.
233 If the result would be minval - 1, return minval. */
234 result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
235 result += minval;
236 }
237 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238}
239
Calin Juravle627d37c2014-02-28 11:46:03 +0000240static int_fast64_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700241detzcode64(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700243 register uint_fast64_t result;
244 register int i;
245 int_fast64_t one = 1;
246 int_fast64_t halfmaxval = one << (64 - 2);
247 int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
248 int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800249
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700250 result = codep[0] & 0x7f;
251 for (i = 1; i < 8; ++i)
252 result = (result << 8) | (codep[i] & 0xff);
253
254 if (codep[0] & 0x80) {
255 /* Do two's-complement negation even on non-two's-complement machines.
256 If the result would be minval - 1, return minval. */
257 result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
258 result += minval;
259 }
260 return result;
261}
262
263static void
264update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
265{
266 tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
267#ifdef USG_COMPAT
268 if (!ttisp->tt_isdst)
269 timezone = - ttisp->tt_gmtoff;
270#endif
271#ifdef ALTZONE
272 if (ttisp->tt_isdst)
273 altzone = - ttisp->tt_gmtoff;
274#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275}
276
277static void
Elliott Hughesce4783c2013-07-12 17:31:11 -0700278settzname(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700280 register struct state * const sp = lclptr;
281 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700283 tzname[0] = tzname[1] = (char *) wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700285 daylight = 0;
286 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287#endif /* defined USG_COMPAT */
288#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700289 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290#endif /* defined ALTZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700291 if (sp == NULL) {
292 tzname[0] = tzname[1] = (char *) gmt;
293 return;
294 }
295 /*
296 ** And to get the latest zone names into tzname. . .
297 */
298 for (i = 0; i < sp->typecnt; ++i) {
299 register const struct ttinfo * const ttisp = &sp->ttis[i];
300 update_tzname_etc(sp, ttisp);
301 }
302 for (i = 0; i < sp->timecnt; ++i) {
303 register const struct ttinfo * const ttisp =
304 &sp->ttis[
305 sp->types[i]];
306 update_tzname_etc(sp, ttisp);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700307#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700308 if (ttisp->tt_isdst)
309 daylight = 1;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700310#endif /* defined USG_COMPAT */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700311 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800312}
313
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700314static void
315scrub_abbrs(struct state *sp)
316{
317 int i;
318 /*
319 ** First, replace bogus characters.
320 */
321 for (i = 0; i < sp->charcnt; ++i)
322 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
323 sp->chars[i] = TZ_ABBR_ERR_CHAR;
324 /*
325 ** Second, truncate long abbreviations.
326 */
327 for (i = 0; i < sp->typecnt; ++i) {
328 register const struct ttinfo * const ttisp = &sp->ttis[i];
329 register char * cp = &sp->chars[ttisp->tt_abbrind];
330
331 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
332 strcmp(cp, GRANDPARENTED) != 0)
333 *(cp + TZ_ABBR_MAX_LEN) = '\0';
334 }
335}
336
337static bool
338differ_by_repeat(const time_t t1, const time_t t0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339{
Calin Juravled8928922014-02-28 12:18:53 +0000340 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
341 return 0;
Christopher Ferris384ffe32015-11-02 12:56:37 -0800342#if defined(__LP64__) // 32-bit Android/glibc has a signed 32-bit time_t; 64-bit doesn't.
Calin Juravled8928922014-02-28 12:18:53 +0000343 return t1 - t0 == SECSPERREPEAT;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700344#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345}
346
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700347/* Input buffer for data read from a compiled tz file. */
348union input_buffer {
349 /* The first part of the buffer, interpreted as a header. */
350 struct tzhead tzhead;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700352 /* The entire buffer. */
353 char buf[2 * sizeof(struct tzhead) + 2 * sizeof (struct state)
354 + 4 * TZ_MAX_TIMES];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355};
356
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700357/* Local storage needed for 'tzloadbody'. */
358union local_storage {
359 /* The file name to be opened. */
360 char fullname[FILENAME_MAX + 1];
361
362 /* The results of analyzing the file's contents after it is opened. */
363 struct {
364 /* The input buffer. */
365 union input_buffer u;
366
367 /* A temporary state used for parsing a TZ string in the file. */
368 struct state st;
369 } u;
370};
371
Elliott Hughes81c46fc2016-10-03 12:29:30 -0700372static int __bionic_open_tzdata(const char*, int32_t*);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700373
374/* Load tz data from the file named NAME into *SP. Read extended
375 format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
376 success, an errno value on failure. */
377static int
378tzloadbody(char const *name, struct state *sp, bool doextend,
379 union local_storage *lsp)
380{
381 register int i;
382 register int fid;
383 register int stored;
384 register ssize_t nread;
Elliott Hughesa9209d72016-09-16 18:16:47 -0700385#if !defined(__BIONIC__)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700386 register bool doaccess;
387 register char *fullname = lsp->fullname;
388#endif
389 register union input_buffer *up = &lsp->u.u;
390 register int tzheadsize = sizeof (struct tzhead);
391
392 sp->goback = sp->goahead = false;
393
394 if (! name) {
395 name = TZDEFAULT;
396 if (! name)
397 return EINVAL;
398 }
399
Elliott Hughesa9209d72016-09-16 18:16:47 -0700400#if defined(__BIONIC__)
Elliott Hughes81c46fc2016-10-03 12:29:30 -0700401 int32_t entry_length;
402 fid = __bionic_open_tzdata(name, &entry_length);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700403#else
404 if (name[0] == ':')
405 ++name;
406 doaccess = name[0] == '/';
407 if (!doaccess) {
408 char const *p = TZDIR;
409 if (! p)
410 return EINVAL;
411 if (sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
412 return ENAMETOOLONG;
413 strcpy(fullname, p);
414 strcat(fullname, "/");
415 strcat(fullname, name);
416 /* Set doaccess if '.' (as in "../") shows up in name. */
417 if (strchr(name, '.'))
418 doaccess = true;
419 name = fullname;
420 }
421 if (doaccess && access(name, R_OK) != 0)
422 return errno;
423 fid = open(name, OPEN_MODE);
424#endif
425 if (fid < 0)
426 return errno;
427
Elliott Hughes81c46fc2016-10-03 12:29:30 -0700428#if defined(__BIONIC__)
Elliott Hughes4edd6512016-10-03 16:46:33 -0700429 nread = TEMP_FAILURE_RETRY(read(fid, up->buf, entry_length));
Elliott Hughes81c46fc2016-10-03 12:29:30 -0700430#else
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700431 nread = read(fid, up->buf, sizeof up->buf);
Elliott Hughes81c46fc2016-10-03 12:29:30 -0700432#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700433 if (nread < tzheadsize) {
434 int err = nread < 0 ? errno : EINVAL;
435 close(fid);
436 return err;
437 }
438 if (close(fid) < 0)
439 return errno;
440 for (stored = 4; stored <= 8; stored *= 2) {
441 int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
442 int_fast32_t ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
443 int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
444 int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
445 int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
446 int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
447 char const *p = up->buf + tzheadsize;
448 if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
449 && 0 < typecnt && typecnt < TZ_MAX_TYPES
450 && 0 <= timecnt && timecnt < TZ_MAX_TIMES
451 && 0 <= charcnt && charcnt < TZ_MAX_CHARS
452 && (ttisstdcnt == typecnt || ttisstdcnt == 0)
453 && (ttisgmtcnt == typecnt || ttisgmtcnt == 0)))
454 return EINVAL;
455 if (nread
456 < (tzheadsize /* struct tzhead */
457 + timecnt * stored /* ats */
458 + timecnt /* types */
459 + typecnt * 6 /* ttinfos */
460 + charcnt /* chars */
461 + leapcnt * (stored + 4) /* lsinfos */
462 + ttisstdcnt /* ttisstds */
463 + ttisgmtcnt)) /* ttisgmts */
464 return EINVAL;
465 sp->leapcnt = leapcnt;
466 sp->timecnt = timecnt;
467 sp->typecnt = typecnt;
468 sp->charcnt = charcnt;
469
470 /* Read transitions, discarding those out of time_t range.
471 But pretend the last transition before time_t_min
472 occurred at time_t_min. */
473 timecnt = 0;
474 for (i = 0; i < sp->timecnt; ++i) {
475 int_fast64_t at
476 = stored == 4 ? detzcode(p) : detzcode64(p);
477 sp->types[i] = at <= time_t_max;
478 if (sp->types[i]) {
479 time_t attime
480 = ((TYPE_SIGNED(time_t) ? at < time_t_min : at < 0)
481 ? time_t_min : at);
482 if (timecnt && attime <= sp->ats[timecnt - 1]) {
483 if (attime < sp->ats[timecnt - 1])
484 return EINVAL;
485 sp->types[i - 1] = 0;
486 timecnt--;
487 }
488 sp->ats[timecnt++] = attime;
489 }
490 p += stored;
491 }
492
493 timecnt = 0;
494 for (i = 0; i < sp->timecnt; ++i) {
495 unsigned char typ = *p++;
496 if (sp->typecnt <= typ)
497 return EINVAL;
498 if (sp->types[i])
499 sp->types[timecnt++] = typ;
500 }
501 sp->timecnt = timecnt;
502 for (i = 0; i < sp->typecnt; ++i) {
503 register struct ttinfo * ttisp;
504 unsigned char isdst, abbrind;
505
506 ttisp = &sp->ttis[i];
507 ttisp->tt_gmtoff = detzcode(p);
508 p += 4;
509 isdst = *p++;
510 if (! (isdst < 2))
511 return EINVAL;
512 ttisp->tt_isdst = isdst;
513 abbrind = *p++;
514 if (! (abbrind < sp->charcnt))
515 return EINVAL;
516 ttisp->tt_abbrind = abbrind;
517 }
518 for (i = 0; i < sp->charcnt; ++i)
519 sp->chars[i] = *p++;
520 sp->chars[i] = '\0'; /* ensure '\0' at end */
521
522 /* Read leap seconds, discarding those out of time_t range. */
523 leapcnt = 0;
524 for (i = 0; i < sp->leapcnt; ++i) {
525 int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
526 int_fast32_t corr = detzcode(p + stored);
527 p += stored + 4;
528 if (tr <= time_t_max) {
529 time_t trans
530 = ((TYPE_SIGNED(time_t) ? tr < time_t_min : tr < 0)
531 ? time_t_min : tr);
532 if (leapcnt && trans <= sp->lsis[leapcnt - 1].ls_trans) {
533 if (trans < sp->lsis[leapcnt - 1].ls_trans)
534 return EINVAL;
535 leapcnt--;
536 }
537 sp->lsis[leapcnt].ls_trans = trans;
538 sp->lsis[leapcnt].ls_corr = corr;
539 leapcnt++;
540 }
541 }
542 sp->leapcnt = leapcnt;
543
544 for (i = 0; i < sp->typecnt; ++i) {
545 register struct ttinfo * ttisp;
546
547 ttisp = &sp->ttis[i];
548 if (ttisstdcnt == 0)
549 ttisp->tt_ttisstd = false;
550 else {
551 if (*p != true && *p != false)
552 return EINVAL;
553 ttisp->tt_ttisstd = *p++;
554 }
555 }
556 for (i = 0; i < sp->typecnt; ++i) {
557 register struct ttinfo * ttisp;
558
559 ttisp = &sp->ttis[i];
560 if (ttisgmtcnt == 0)
561 ttisp->tt_ttisgmt = false;
562 else {
563 if (*p != true && *p != false)
564 return EINVAL;
565 ttisp->tt_ttisgmt = *p++;
566 }
567 }
568 /*
569 ** If this is an old file, we're done.
570 */
571 if (up->tzhead.tzh_version[0] == '\0')
572 break;
573 nread -= p - up->buf;
574 memmove(up->buf, p, nread);
575 }
576 if (doextend && nread > 2 &&
577 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
578 sp->typecnt + 2 <= TZ_MAX_TYPES) {
579 struct state *ts = &lsp->u.st;
580
581 up->buf[nread - 1] = '\0';
582 if (tzparse(&up->buf[1], ts, false)
583 && ts->typecnt == 2) {
584
585 /* Attempt to reuse existing abbreviations.
586 Without this, America/Anchorage would stop
587 working after 2037 when TZ_MAX_CHARS is 50, as
588 sp->charcnt equals 42 (for LMT CAT CAWT CAPT AHST
589 AHDT YST AKDT AKST) and ts->charcnt equals 10
590 (for AKST AKDT). Reusing means sp->charcnt can
591 stay 42 in this example. */
592 int gotabbr = 0;
593 int charcnt = sp->charcnt;
594 for (i = 0; i < 2; i++) {
595 char *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
596 int j;
597 for (j = 0; j < charcnt; j++)
598 if (strcmp(sp->chars + j, tsabbr) == 0) {
599 ts->ttis[i].tt_abbrind = j;
600 gotabbr++;
601 break;
602 }
603 if (! (j < charcnt)) {
604 int tsabbrlen = strlen(tsabbr);
605 if (j + tsabbrlen < TZ_MAX_CHARS) {
606 strcpy(sp->chars + j, tsabbr);
607 charcnt = j + tsabbrlen + 1;
608 ts->ttis[i].tt_abbrind = j;
609 gotabbr++;
610 }
611 }
612 }
613 if (gotabbr == 2) {
614 sp->charcnt = charcnt;
615 for (i = 0; i < ts->timecnt; i++)
616 if (sp->ats[sp->timecnt - 1] < ts->ats[i])
617 break;
618 while (i < ts->timecnt
619 && sp->timecnt < TZ_MAX_TIMES) {
620 sp->ats[sp->timecnt] = ts->ats[i];
621 sp->types[sp->timecnt] = (sp->typecnt
622 + ts->types[i]);
623 sp->timecnt++;
624 i++;
625 }
626 sp->ttis[sp->typecnt++] = ts->ttis[0];
627 sp->ttis[sp->typecnt++] = ts->ttis[1];
628 }
629 }
630 }
631 if (sp->timecnt > 1) {
632 for (i = 1; i < sp->timecnt; ++i)
633 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
634 differ_by_repeat(sp->ats[i], sp->ats[0])) {
635 sp->goback = true;
636 break;
637 }
638 for (i = sp->timecnt - 2; i >= 0; --i)
639 if (typesequiv(sp, sp->types[sp->timecnt - 1],
640 sp->types[i]) &&
641 differ_by_repeat(sp->ats[sp->timecnt - 1],
642 sp->ats[i])) {
643 sp->goahead = true;
644 break;
645 }
646 }
647 /*
648 ** If type 0 is is unused in transitions,
649 ** it's the type to use for early times.
650 */
651 for (i = 0; i < sp->timecnt; ++i)
652 if (sp->types[i] == 0)
653 break;
654 i = i < sp->timecnt ? -1 : 0;
655 /*
656 ** Absent the above,
657 ** if there are transition times
658 ** and the first transition is to a daylight time
659 ** find the standard type less than and closest to
660 ** the type of the first transition.
661 */
662 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
663 i = sp->types[0];
664 while (--i >= 0)
665 if (!sp->ttis[i].tt_isdst)
666 break;
667 }
668 /*
669 ** If no result yet, find the first standard type.
670 ** If there is none, punt to type zero.
671 */
672 if (i < 0) {
673 i = 0;
674 while (sp->ttis[i].tt_isdst)
675 if (++i >= sp->typecnt) {
676 i = 0;
677 break;
678 }
679 }
680 sp->defaulttype = i;
681 return 0;
682}
683
684/* Load tz data from the file named NAME into *SP. Read extended
685 format if DOEXTEND. Return 0 on success, an errno value on failure. */
686static int
687tzload(char const *name, struct state *sp, bool doextend)
688{
689#ifdef ALL_STATE
690 union local_storage *lsp = malloc(sizeof *lsp);
691 if (!lsp)
692 return errno;
693 else {
694 int err = tzloadbody(name, sp, doextend, lsp);
695 free(lsp);
696 return err;
697 }
698#else
699 union local_storage ls;
700 return tzloadbody(name, sp, doextend, &ls);
701#endif
702}
703
704static bool
705typesequiv(const struct state *sp, int a, int b)
706{
707 register bool result;
708
709 if (sp == NULL ||
710 a < 0 || a >= sp->typecnt ||
711 b < 0 || b >= sp->typecnt)
712 result = false;
713 else {
714 register const struct ttinfo * ap = &sp->ttis[a];
715 register const struct ttinfo * bp = &sp->ttis[b];
716 result = ap->tt_gmtoff == bp->tt_gmtoff &&
717 ap->tt_isdst == bp->tt_isdst &&
718 ap->tt_ttisstd == bp->tt_ttisstd &&
719 ap->tt_ttisgmt == bp->tt_ttisgmt &&
720 strcmp(&sp->chars[ap->tt_abbrind],
721 &sp->chars[bp->tt_abbrind]) == 0;
722 }
723 return result;
724}
725
726static const int mon_lengths[2][MONSPERYEAR] = {
727 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
728 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
729};
730
731static const int year_lengths[2] = {
732 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733};
734
735/*
736** Given a pointer into a time zone string, scan until a character that is not
737** a valid character in a zone name is found. Return a pointer to that
738** character.
739*/
740
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700741static const char * ATTRIBUTE_PURE
742getzname(register const char *strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800743{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700744 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800745
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700746 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
747 c != '+')
748 ++strp;
749 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750}
751
752/*
753** Given a pointer into an extended time zone string, scan until the ending
754** delimiter of the zone name is located. Return a pointer to the delimiter.
755**
756** As with getzname above, the legal character set is actually quite
757** restricted, with other characters producing undefined results.
758** We don't do any checking here; checking is done later in common-case code.
759*/
760
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700761static const char * ATTRIBUTE_PURE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762getqzname(register const char *strp, const int delim)
763{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700764 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800765
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700766 while ((c = *strp) != '\0' && c != delim)
767 ++strp;
768 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800769}
770
771/*
772** Given a pointer into a time zone string, extract a number from that string.
773** Check that the number is within a specified range; if it is not, return
774** NULL.
775** Otherwise, return a pointer to the first character not part of the number.
776*/
777
778static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700779getnum(register const char *strp, int *const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700781 register char c;
782 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700784 if (strp == NULL || !is_digit(c = *strp))
785 return NULL;
786 num = 0;
787 do {
788 num = num * 10 + (c - '0');
789 if (num > max)
790 return NULL; /* illegal value */
791 c = *++strp;
792 } while (is_digit(c));
793 if (num < min)
794 return NULL; /* illegal value */
795 *nump = num;
796 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800797}
798
799/*
800** Given a pointer into a time zone string, extract a number of seconds,
801** in hh[:mm[:ss]] form, from the string.
802** If any error occurs, return NULL.
803** Otherwise, return a pointer to the first character not part of the number
804** of seconds.
805*/
806
807static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700808getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800809{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700810 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700812 /*
813 ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
814 ** "M10.4.6/26", which does not conform to Posix,
815 ** but which specifies the equivalent of
816 ** "02:00 on the first Sunday on or after 23 Oct".
817 */
818 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
819 if (strp == NULL)
820 return NULL;
821 *secsp = num * (int_fast32_t) SECSPERHOUR;
822 if (*strp == ':') {
823 ++strp;
824 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
825 if (strp == NULL)
826 return NULL;
827 *secsp += num * SECSPERMIN;
828 if (*strp == ':') {
829 ++strp;
830 /* 'SECSPERMIN' allows for leap seconds. */
831 strp = getnum(strp, &num, 0, SECSPERMIN);
832 if (strp == NULL)
833 return NULL;
834 *secsp += num;
835 }
836 }
837 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838}
839
840/*
841** Given a pointer into a time zone string, extract an offset, in
842** [+-]hh[:mm[:ss]] form, from the string.
843** If any error occurs, return NULL.
844** Otherwise, return a pointer to the first character not part of the time.
845*/
846
847static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700848getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700850 register bool neg = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700852 if (*strp == '-') {
853 neg = true;
854 ++strp;
855 } else if (*strp == '+')
856 ++strp;
857 strp = getsecs(strp, offsetp);
858 if (strp == NULL)
859 return NULL; /* illegal time */
860 if (neg)
861 *offsetp = -*offsetp;
862 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800863}
864
865/*
866** Given a pointer into a time zone string, extract a rule in the form
867** date[/time]. See POSIX section 8 for the format of "date" and "time".
868** If a valid rule is not found, return NULL.
869** Otherwise, return a pointer to the first character not part of the rule.
870*/
871
872static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700873getrule(const char *strp, register struct rule *const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800874{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700875 if (*strp == 'J') {
876 /*
877 ** Julian day.
878 */
879 rulep->r_type = JULIAN_DAY;
880 ++strp;
881 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
882 } else if (*strp == 'M') {
883 /*
884 ** Month, week, day.
885 */
886 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
887 ++strp;
888 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
889 if (strp == NULL)
890 return NULL;
891 if (*strp++ != '.')
892 return NULL;
893 strp = getnum(strp, &rulep->r_week, 1, 5);
894 if (strp == NULL)
895 return NULL;
896 if (*strp++ != '.')
897 return NULL;
898 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
899 } else if (is_digit(*strp)) {
900 /*
901 ** Day of year.
902 */
903 rulep->r_type = DAY_OF_YEAR;
904 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
905 } else return NULL; /* invalid format */
906 if (strp == NULL)
907 return NULL;
908 if (*strp == '/') {
909 /*
910 ** Time specified.
911 */
912 ++strp;
913 strp = getoffset(strp, &rulep->r_time);
914 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
915 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800916}
917
918/*
Calin Juravle627d37c2014-02-28 11:46:03 +0000919** Given a year, a rule, and the offset from UT at the time that rule takes
920** effect, calculate the year-relative time that rule takes effect.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800921*/
922
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700923static int_fast32_t ATTRIBUTE_PURE
Calin Juravle627d37c2014-02-28 11:46:03 +0000924transtime(const int year, register const struct rule *const rulep,
Calin Juravled8928922014-02-28 12:18:53 +0000925 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800926{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700927 register bool leapyear;
Calin Juravled8928922014-02-28 12:18:53 +0000928 register int_fast32_t value;
929 register int i;
930 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700932 INITIALIZE(value);
933 leapyear = isleap(year);
934 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800935
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700936 case JULIAN_DAY:
937 /*
938 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
939 ** years.
940 ** In non-leap years, or if the day number is 59 or less, just
941 ** add SECSPERDAY times the day number-1 to the time of
942 ** January 1, midnight, to get the day.
943 */
Calin Juravled8928922014-02-28 12:18:53 +0000944 value = (rulep->r_day - 1) * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700945 if (leapyear && rulep->r_day >= 60)
946 value += SECSPERDAY;
947 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800948
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700949 case DAY_OF_YEAR:
950 /*
951 ** n - day of year.
952 ** Just add SECSPERDAY times the day number to the time of
953 ** January 1, midnight, to get the day.
954 */
Calin Juravled8928922014-02-28 12:18:53 +0000955 value = rulep->r_day * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700956 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800957
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700958 case MONTH_NTH_DAY_OF_WEEK:
959 /*
960 ** Mm.n.d - nth "dth day" of month m.
961 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800962
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700963 /*
964 ** Use Zeller's Congruence to get day-of-week of first day of
965 ** month.
966 */
967 m1 = (rulep->r_mon + 9) % 12 + 1;
968 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
969 yy1 = yy0 / 100;
970 yy2 = yy0 % 100;
971 dow = ((26 * m1 - 2) / 10 +
972 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
973 if (dow < 0)
974 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800975
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700976 /*
977 ** "dow" is the day-of-week of the first day of the month. Get
978 ** the day-of-month (zero-origin) of the first "dow" day of the
979 ** month.
980 */
981 d = rulep->r_day - dow;
982 if (d < 0)
983 d += DAYSPERWEEK;
984 for (i = 1; i < rulep->r_week; ++i) {
985 if (d + DAYSPERWEEK >=
986 mon_lengths[leapyear][rulep->r_mon - 1])
987 break;
988 d += DAYSPERWEEK;
989 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800990
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700991 /*
992 ** "d" is the day-of-month (zero-origin) of the day we want.
993 */
Calin Juravled8928922014-02-28 12:18:53 +0000994 value = d * SECSPERDAY;
995 for (i = 0; i < rulep->r_mon - 1; ++i)
996 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700997 break;
998 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800999
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001000 /*
Calin Juravled8928922014-02-28 12:18:53 +00001001 ** "value" is the year-relative time of 00:00:00 UT on the day in
1002 ** question. To get the year-relative time of the specified local
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001003 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -07001004 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001005 */
1006 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001007}
1008
1009/*
1010** Given a POSIX section 8-style TZ string, fill in the rule tables as
1011** appropriate.
1012*/
1013
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001014static bool
1015tzparse(const char *name, struct state *sp, bool lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001016{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001017 const char * stdname;
1018 const char * dstname;
1019 size_t stdlen;
1020 size_t dstlen;
1021 size_t charcnt;
1022 int_fast32_t stdoffset;
1023 int_fast32_t dstoffset;
1024 register char * cp;
1025 register bool load_ok;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001027 stdname = name;
1028 if (lastditch) {
1029 stdlen = sizeof gmt - 1;
1030 name += stdlen;
1031 stdoffset = 0;
1032 } else {
1033 if (*name == '<') {
1034 name++;
1035 stdname = name;
1036 name = getqzname(name, '>');
1037 if (*name != '>')
1038 return false;
1039 stdlen = name - stdname;
1040 name++;
1041 } else {
1042 name = getzname(name);
1043 stdlen = name - stdname;
1044 }
1045 if (!stdlen)
1046 return false;
1047 name = getoffset(name, &stdoffset);
1048 if (name == NULL)
1049 return false;
1050 }
1051 charcnt = stdlen + 1;
1052 if (sizeof sp->chars < charcnt)
1053 return false;
1054 load_ok = tzload(TZDEFRULES, sp, false) == 0;
1055 if (!load_ok)
1056 sp->leapcnt = 0; /* so, we're off a little */
1057 if (*name != '\0') {
1058 if (*name == '<') {
1059 dstname = ++name;
1060 name = getqzname(name, '>');
1061 if (*name != '>')
1062 return false;
1063 dstlen = name - dstname;
1064 name++;
1065 } else {
1066 dstname = name;
1067 name = getzname(name);
1068 dstlen = name - dstname; /* length of DST zone name */
1069 }
1070 if (!dstlen)
1071 return false;
1072 charcnt += dstlen + 1;
1073 if (sizeof sp->chars < charcnt)
1074 return false;
1075 if (*name != '\0' && *name != ',' && *name != ';') {
1076 name = getoffset(name, &dstoffset);
1077 if (name == NULL)
1078 return false;
1079 } else dstoffset = stdoffset - SECSPERHOUR;
1080 if (*name == '\0' && !load_ok)
1081 name = TZDEFRULESTRING;
1082 if (*name == ',' || *name == ';') {
1083 struct rule start;
1084 struct rule end;
1085 register int year;
1086 register int yearlim;
1087 register int timecnt;
1088 time_t janfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001090 ++name;
1091 if ((name = getrule(name, &start)) == NULL)
1092 return false;
1093 if (*name++ != ',')
1094 return false;
1095 if ((name = getrule(name, &end)) == NULL)
1096 return false;
1097 if (*name != '\0')
1098 return false;
1099 sp->typecnt = 2; /* standard time and DST */
1100 /*
1101 ** Two transitions per year, from EPOCH_YEAR forward.
1102 */
1103 init_ttinfo(&sp->ttis[0], -dstoffset, true, stdlen + 1);
1104 init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
1105 sp->defaulttype = 0;
1106 timecnt = 0;
1107 janfirst = 0;
1108 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1109 for (year = EPOCH_YEAR; year < yearlim; year++) {
1110 int_fast32_t
1111 starttime = transtime(year, &start, stdoffset),
1112 endtime = transtime(year, &end, dstoffset);
1113 int_fast32_t
1114 yearsecs = (year_lengths[isleap(year)]
1115 * SECSPERDAY);
1116 bool reversed = endtime < starttime;
1117 if (reversed) {
1118 int_fast32_t swap = starttime;
1119 starttime = endtime;
1120 endtime = swap;
1121 }
1122 if (reversed
1123 || (starttime < endtime
1124 && (endtime - starttime
1125 < (yearsecs
1126 + (stdoffset - dstoffset))))) {
1127 if (TZ_MAX_TIMES - 2 < timecnt)
1128 break;
1129 yearlim = year + YEARSPERREPEAT + 1;
1130 sp->ats[timecnt] = janfirst;
1131 if (increment_overflow_time
1132 (&sp->ats[timecnt], starttime))
1133 break;
1134 sp->types[timecnt++] = reversed;
1135 sp->ats[timecnt] = janfirst;
1136 if (increment_overflow_time
1137 (&sp->ats[timecnt], endtime))
1138 break;
1139 sp->types[timecnt++] = !reversed;
1140 }
1141 if (increment_overflow_time(&janfirst, yearsecs))
1142 break;
1143 }
1144 sp->timecnt = timecnt;
1145 if (!timecnt)
1146 sp->typecnt = 1; /* Perpetual DST. */
1147 } else {
1148 register int_fast32_t theirstdoffset;
1149 register int_fast32_t theirdstoffset;
1150 register int_fast32_t theiroffset;
1151 register bool isdst;
1152 register int i;
1153 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001154
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001155 if (*name != '\0')
1156 return false;
1157 /*
1158 ** Initial values of theirstdoffset and theirdstoffset.
1159 */
1160 theirstdoffset = 0;
1161 for (i = 0; i < sp->timecnt; ++i) {
1162 j = sp->types[i];
1163 if (!sp->ttis[j].tt_isdst) {
1164 theirstdoffset =
1165 -sp->ttis[j].tt_gmtoff;
1166 break;
1167 }
1168 }
1169 theirdstoffset = 0;
1170 for (i = 0; i < sp->timecnt; ++i) {
1171 j = sp->types[i];
1172 if (sp->ttis[j].tt_isdst) {
1173 theirdstoffset =
1174 -sp->ttis[j].tt_gmtoff;
1175 break;
1176 }
1177 }
1178 /*
1179 ** Initially we're assumed to be in standard time.
1180 */
1181 isdst = false;
1182 theiroffset = theirstdoffset;
1183 /*
1184 ** Now juggle transition times and types
1185 ** tracking offsets as you do.
1186 */
1187 for (i = 0; i < sp->timecnt; ++i) {
1188 j = sp->types[i];
1189 sp->types[i] = sp->ttis[j].tt_isdst;
1190 if (sp->ttis[j].tt_ttisgmt) {
1191 /* No adjustment to transition time */
1192 } else {
1193 /*
1194 ** If summer time is in effect, and the
1195 ** transition time was not specified as
1196 ** standard time, add the summer time
1197 ** offset to the transition time;
1198 ** otherwise, add the standard time
1199 ** offset to the transition time.
1200 */
1201 /*
1202 ** Transitions from DST to DDST
1203 ** will effectively disappear since
1204 ** POSIX provides for only one DST
1205 ** offset.
1206 */
1207 if (isdst && !sp->ttis[j].tt_ttisstd) {
1208 sp->ats[i] += dstoffset -
1209 theirdstoffset;
1210 } else {
1211 sp->ats[i] += stdoffset -
1212 theirstdoffset;
1213 }
1214 }
1215 theiroffset = -sp->ttis[j].tt_gmtoff;
1216 if (sp->ttis[j].tt_isdst)
1217 theirdstoffset = theiroffset;
1218 else theirstdoffset = theiroffset;
1219 }
1220 /*
1221 ** Finally, fill in ttis.
1222 */
1223 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1224 init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1225 sp->typecnt = 2;
1226 sp->defaulttype = 0;
1227 }
1228 } else {
1229 dstlen = 0;
1230 sp->typecnt = 1; /* only standard time */
1231 sp->timecnt = 0;
1232 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1233 sp->defaulttype = 0;
1234 }
1235 sp->charcnt = charcnt;
1236 cp = sp->chars;
1237 memcpy(cp, stdname, stdlen);
1238 cp += stdlen;
1239 *cp++ = '\0';
1240 if (dstlen != 0) {
1241 memcpy(cp, dstname, dstlen);
1242 *(cp + dstlen) = '\0';
1243 }
1244 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001245}
1246
1247static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001248gmtload(struct state *const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001249{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001250 if (tzload(gmt, sp, true) != 0)
1251 tzparse(gmt, sp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252}
1253
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001254/* Initialize *SP to a value appropriate for the TZ setting NAME.
1255 Return 0 on success, an errno value on failure. */
1256static int
1257zoneinit(struct state *sp, char const *name)
1258{
1259 if (name && ! name[0]) {
1260 /*
1261 ** User wants it fast rather than right.
1262 */
1263 sp->leapcnt = 0; /* so, we're off a little */
1264 sp->timecnt = 0;
1265 sp->typecnt = 0;
1266 sp->charcnt = 0;
1267 sp->goback = sp->goahead = false;
1268 init_ttinfo(&sp->ttis[0], 0, false, 0);
1269 strcpy(sp->chars, gmt);
1270 sp->defaulttype = 0;
1271 return 0;
1272 } else {
1273 int err = tzload(name, sp, true);
1274 if (err != 0 && name && name[0] != ':' && tzparse(name, sp, false))
1275 err = 0;
1276 if (err == 0)
1277 scrub_abbrs(sp);
1278 return err;
1279 }
1280}
1281
1282static void
1283tzsetlcl(char const *name)
1284{
1285 struct state *sp = lclptr;
1286 int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1287 if (lcl < 0
1288 ? lcl_is_set < 0
1289 : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1290 return;
1291#ifdef ALL_STATE
1292 if (! sp)
1293 lclptr = sp = malloc(sizeof *lclptr);
1294#endif /* defined ALL_STATE */
1295 if (sp) {
1296 if (zoneinit(sp, name) != 0)
1297 zoneinit(sp, "");
1298 if (0 < lcl)
1299 strcpy(lcl_TZname, name);
1300 }
1301 settzname();
1302 lcl_is_set = lcl;
1303}
1304
1305#ifdef STD_INSPIRED
Elliott Hughesce4783c2013-07-12 17:31:11 -07001306void
1307tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001308{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001309 if (lock() != 0)
1310 return;
1311 tzsetlcl(NULL);
1312 unlock();
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001313}
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001314#endif
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001315
Elliott Hughesa9209d72016-09-16 18:16:47 -07001316#if defined(__BIONIC__)
Mark Salyzynd0578942015-10-02 13:15:07 -07001317#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
1318#include <sys/_system_properties.h> // For __system_property_serial.
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001319#endif
Elliott Hughesce4783c2013-07-12 17:31:11 -07001320
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001321static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001322tzset_unlocked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001323{
Elliott Hughesa9209d72016-09-16 18:16:47 -07001324#if defined(__BIONIC__)
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001325 // The TZ environment variable is meant to override the system-wide setting.
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001326 const char* name = getenv("TZ");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001327
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001328 // If that's not set, look at the "persist.sys.timezone" system property.
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001329 if (name == NULL) {
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001330 static const prop_info* pi;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001331
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001332 if (!pi) {
1333 pi = __system_property_find("persist.sys.timezone");
Elliott Hughesce4783c2013-07-12 17:31:11 -07001334 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001335 if (pi) {
1336 static char buf[PROP_VALUE_MAX];
1337 static uint32_t s = -1;
1338 static bool ok = false;
1339 uint32_t serial = __system_property_serial(pi);
1340 if (serial != s) {
1341 ok = __system_property_read(pi, 0, buf) > 0;
1342 s = serial;
1343 }
1344 if (ok) {
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001345 // POSIX and Java disagree about the sign in a timezone string. For POSIX, "GMT+3" means
1346 // "3 hours west/behind", but for Java it means "3 hours east/ahead". Since (a) Java is
1347 // the one that matches human expectations and (b) this system property is used directly
1348 // by Java, we flip the sign here to translate from Java to POSIX. http://b/25463955.
1349 if (buf[3] == '-') {
1350 buf[3] = '+';
1351 } else if (buf[3] == '+') {
1352 buf[3] = '-';
1353 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001354 name = buf;
1355 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001356 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001357 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001358
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001359 // If the system property is also not available (because you're running AOSP on a WiFi-only
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001360 // device, say), fall back to GMT.
1361 if (name == NULL) name = gmt;
1362
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001363 tzsetlcl(name);
1364#else
1365 tzsetlcl(getenv("TZ"));
1366#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367}
1368
1369void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001370tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001371{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001372 if (lock() != 0)
1373 return;
1374 tzset_unlocked();
1375 unlock();
1376}
1377
1378static void
1379gmtcheck(void)
1380{
1381 static bool gmt_is_set;
1382 if (lock() != 0)
1383 return;
1384 if (! gmt_is_set) {
1385#ifdef ALL_STATE
1386 gmtptr = malloc(sizeof *gmtptr);
1387#endif
1388 if (gmtptr)
1389 gmtload(gmtptr);
1390 gmt_is_set = true;
1391 }
1392 unlock();
1393}
1394
1395#if NETBSD_INSPIRED
1396
1397timezone_t
1398tzalloc(char const *name)
1399{
1400 timezone_t sp = malloc(sizeof *sp);
1401 if (sp) {
1402 int err = zoneinit(sp, name);
1403 if (err != 0) {
1404 free(sp);
1405 errno = err;
1406 return NULL;
1407 }
1408 }
1409 return sp;
1410}
1411
1412void
1413tzfree(timezone_t sp)
1414{
1415 free(sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416}
1417
1418/*
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001419** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1420** ctime_r are obsolescent and have potential security problems that
1421** ctime_rz would share. Callers can instead use localtime_rz + strftime.
1422**
1423** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1424** in zones with three or more time zone abbreviations.
1425** Callers can instead use localtime_rz + strftime.
1426*/
1427
1428#endif
1429
1430/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431** The easy way to behave "as if no library function calls" localtime
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001432** is to not call it, so we drop its guts into "localsub", which can be
1433** freely called. (And no, the PANS doesn't require the above behavior,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001434** but it *is* desirable.)
1435**
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001436** If successful and SETNAME is nonzero,
1437** set the applicable parts of tzname, timezone and altzone;
1438** however, it's OK to omit this step if the time zone is POSIX-compatible,
1439** since in that case tzset should have already done this step correctly.
1440** SETNAME's type is intfast32_t for compatibility with gmtsub,
1441** but it is actually a boolean and its value should be 0 or 1.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001442*/
1443
1444/*ARGSUSED*/
1445static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001446localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
1447 struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001449 register const struct ttinfo * ttisp;
1450 register int i;
1451 register struct tm * result;
1452 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001454 if (sp == NULL) {
1455 /* Don't bother to set tzname etc.; tzset has already done it. */
1456 return gmtsub(gmtptr, timep, 0, tmp);
1457 }
1458 if ((sp->goback && t < sp->ats[0]) ||
1459 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1460 time_t newt = t;
1461 register time_t seconds;
1462 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001464 if (t < sp->ats[0])
1465 seconds = sp->ats[0] - t;
1466 else seconds = t - sp->ats[sp->timecnt - 1];
1467 --seconds;
1468 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1469 seconds = years * AVGSECSPERYEAR;
1470 if (t < sp->ats[0])
1471 newt += seconds;
1472 else newt -= seconds;
1473 if (newt < sp->ats[0] ||
1474 newt > sp->ats[sp->timecnt - 1])
1475 return NULL; /* "cannot happen" */
1476 result = localsub(sp, &newt, setname, tmp);
1477 if (result) {
1478 register int_fast64_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001480 newy = result->tm_year;
1481 if (t < sp->ats[0])
1482 newy -= years;
1483 else newy += years;
1484 if (! (INT_MIN <= newy && newy <= INT_MAX))
1485 return NULL;
1486 result->tm_year = newy;
1487 }
1488 return result;
1489 }
1490 if (sp->timecnt == 0 || t < sp->ats[0]) {
1491 i = sp->defaulttype;
1492 } else {
1493 register int lo = 1;
1494 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001496 while (lo < hi) {
1497 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001499 if (t < sp->ats[mid])
1500 hi = mid;
1501 else lo = mid + 1;
1502 }
1503 i = (int) sp->types[lo - 1];
1504 }
1505 ttisp = &sp->ttis[i];
1506 /*
1507 ** To get (wrong) behavior that's compatible with System V Release 2.0
1508 ** you'd replace the statement below with
1509 ** t += ttisp->tt_gmtoff;
1510 ** timesub(&t, 0L, sp, tmp);
1511 */
1512 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1513 if (result) {
1514 result->tm_isdst = ttisp->tt_isdst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001515#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001516 result->TM_ZONE = (char *) &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001518 if (setname)
1519 update_tzname_etc(sp, ttisp);
1520 }
1521 return result;
1522}
1523
1524#if NETBSD_INSPIRED
1525
1526struct tm *
1527localtime_rz(struct state *sp, time_t const *timep, struct tm *tmp)
1528{
1529 return localsub(sp, timep, 0, tmp);
1530}
1531
1532#endif
1533
1534static struct tm *
1535localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1536{
1537 int err = lock();
1538 if (err) {
1539 errno = err;
1540 return NULL;
1541 }
1542 if (setname || !lcl_is_set)
1543 tzset_unlocked();
1544 tmp = localsub(lclptr, timep, setname, tmp);
1545 unlock();
1546 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547}
1548
1549struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001550localtime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001551{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001552 return localtime_tzset(timep, &tm, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553}
1554
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001555struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001556localtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001557{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001558 return localtime_tzset(timep, tmp, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001559}
1560
1561/*
1562** gmtsub is to gmtime as localsub is to localtime.
1563*/
1564
1565static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001566gmtsub(struct state const *sp, time_t const *timep, int_fast32_t offset,
1567 struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001569 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001571 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001573 /*
1574 ** Could get fancy here and deliver something such as
1575 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
1576 ** but this is no time for a treasure hunt.
1577 */
1578 tmp->TM_ZONE = ((char *)
1579 (offset ? wildabbr : gmtptr ? gmtptr->chars : gmt));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001580#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001581 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001582}
1583
1584/*
1585* Re-entrant version of gmtime.
1586*/
1587
1588struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001589gmtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001591 gmtcheck();
1592 return gmtsub(gmtptr, timep, 0, tmp);
1593}
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001594
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001595struct tm *
1596gmtime(const time_t *timep)
1597{
1598 return gmtime_r(timep, &tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599}
1600
Elliott Hughes906eb992014-06-18 19:46:25 -07001601#ifdef STD_INSPIRED
1602
1603struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001604offtime(const time_t *timep, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07001605{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001606 gmtcheck();
1607 return gmtsub(gmtptr, timep, offset, &tm);
Elliott Hughes906eb992014-06-18 19:46:25 -07001608}
1609
1610#endif /* defined STD_INSPIRED */
1611
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001612/*
1613** Return the number of leap years through the end of the given year
1614** where, to make the math easy, the answer for year zero is defined as zero.
1615*/
1616
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001617static int ATTRIBUTE_PURE
Elliott Hughesce4783c2013-07-12 17:31:11 -07001618leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001619{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001620 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1621 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001622}
1623
1624static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001625timesub(const time_t *timep, int_fast32_t offset,
1626 const struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001627{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001628 register const struct lsinfo * lp;
1629 register time_t tdays;
1630 register int idays; /* unsigned would be so 2003 */
1631 register int_fast64_t rem;
1632 int y;
1633 register const int * ip;
1634 register int_fast64_t corr;
1635 register bool hit;
1636 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001638 corr = 0;
1639 hit = false;
1640 i = (sp == NULL) ? 0 : sp->leapcnt;
1641 while (--i >= 0) {
1642 lp = &sp->lsis[i];
1643 if (*timep >= lp->ls_trans) {
1644 if (*timep == lp->ls_trans) {
1645 hit = ((i == 0 && lp->ls_corr > 0) ||
1646 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1647 if (hit)
1648 while (i > 0 &&
1649 sp->lsis[i].ls_trans ==
1650 sp->lsis[i - 1].ls_trans + 1 &&
1651 sp->lsis[i].ls_corr ==
1652 sp->lsis[i - 1].ls_corr + 1) {
1653 ++hit;
1654 --i;
1655 }
1656 }
1657 corr = lp->ls_corr;
1658 break;
1659 }
1660 }
1661 y = EPOCH_YEAR;
1662 tdays = *timep / SECSPERDAY;
1663 rem = *timep % SECSPERDAY;
1664 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1665 int newy;
1666 register time_t tdelta;
1667 register int idelta;
1668 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001669
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001670 tdelta = tdays / DAYSPERLYEAR;
1671 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1672 && tdelta <= INT_MAX))
1673 goto out_of_range;
1674 idelta = tdelta;
1675 if (idelta == 0)
1676 idelta = (tdays < 0) ? -1 : 1;
1677 newy = y;
1678 if (increment_overflow(&newy, idelta))
1679 goto out_of_range;
1680 leapdays = leaps_thru_end_of(newy - 1) -
1681 leaps_thru_end_of(y - 1);
1682 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1683 tdays -= leapdays;
1684 y = newy;
1685 }
1686 /*
1687 ** Given the range, we can now fearlessly cast...
1688 */
1689 idays = tdays;
1690 rem += offset - corr;
1691 while (rem < 0) {
1692 rem += SECSPERDAY;
1693 --idays;
1694 }
1695 while (rem >= SECSPERDAY) {
1696 rem -= SECSPERDAY;
1697 ++idays;
1698 }
1699 while (idays < 0) {
1700 if (increment_overflow(&y, -1))
1701 goto out_of_range;
1702 idays += year_lengths[isleap(y)];
1703 }
1704 while (idays >= year_lengths[isleap(y)]) {
1705 idays -= year_lengths[isleap(y)];
1706 if (increment_overflow(&y, 1))
1707 goto out_of_range;
1708 }
1709 tmp->tm_year = y;
1710 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1711 goto out_of_range;
1712 tmp->tm_yday = idays;
1713 /*
1714 ** The "extra" mods below avoid overflow problems.
1715 */
1716 tmp->tm_wday = EPOCH_WDAY +
1717 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1718 (DAYSPERNYEAR % DAYSPERWEEK) +
1719 leaps_thru_end_of(y - 1) -
1720 leaps_thru_end_of(EPOCH_YEAR - 1) +
1721 idays;
1722 tmp->tm_wday %= DAYSPERWEEK;
1723 if (tmp->tm_wday < 0)
1724 tmp->tm_wday += DAYSPERWEEK;
1725 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1726 rem %= SECSPERHOUR;
1727 tmp->tm_min = (int) (rem / SECSPERMIN);
1728 /*
1729 ** A positive leap second requires a special
1730 ** representation. This uses "... ??:59:60" et seq.
1731 */
1732 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1733 ip = mon_lengths[isleap(y)];
1734 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1735 idays -= ip[tmp->tm_mon];
1736 tmp->tm_mday = (int) (idays + 1);
1737 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001738#ifdef TM_GMTOFF
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001739 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740#endif /* defined TM_GMTOFF */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001741 return tmp;
1742
1743 out_of_range:
1744 errno = EOVERFLOW;
1745 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001746}
1747
1748char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001749ctime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001750{
1751/*
1752** Section 4.12.3.2 of X3.159-1989 requires that
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001753** The ctime function converts the calendar time pointed to by timer
1754** to local time in the form of a string. It is equivalent to
1755** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001756*/
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001757 struct tm *tmp = localtime(timep);
1758 return tmp ? asctime(tmp) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001759}
1760
1761char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001762ctime_r(const time_t *timep, char *buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001763{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001764 struct tm mytm;
1765 struct tm *tmp = localtime_r(timep, &mytm);
1766 return tmp ? asctime_r(tmp, buf) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001767}
1768
1769/*
1770** Adapted from code provided by Robert Elz, who writes:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001771** The "best" way to do mktime I think is based on an idea of Bob
1772** Kridle's (so its said...) from a long time ago.
1773** It does a binary search of the time_t space. Since time_t's are
1774** just 32 bits, its a max of 32 iterations (even at 64 bits it
1775** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776*/
1777
1778#ifndef WRONG
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001779#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001780#endif /* !defined WRONG */
1781
1782/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001783** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784*/
1785
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001786static bool
1787increment_overflow(int *ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001788{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001789 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001790
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001791 /*
1792 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1793 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1794 ** If i < 0 there can only be overflow if i + j < INT_MIN
1795 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1796 */
1797 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1798 return true;
1799 *ip += j;
1800 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001801}
1802
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001803static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001804increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001805{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001806 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001807
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001808 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1809 return true;
1810 *lp += m;
1811 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001812}
1813
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001814static bool
Calin Juravle627d37c2014-02-28 11:46:03 +00001815increment_overflow_time(time_t *tp, int_fast32_t j)
1816{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001817 /*
1818 ** This is like
1819 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1820 ** except that it does the right thing even if *tp + j would overflow.
1821 */
1822 if (! (j < 0
1823 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1824 : *tp <= time_t_max - j))
1825 return true;
1826 *tp += j;
1827 return false;
Calin Juravle627d37c2014-02-28 11:46:03 +00001828}
1829
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001830static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001831normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001832{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001833 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001834
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001835 tensdelta = (*unitsptr >= 0) ?
1836 (*unitsptr / base) :
1837 (-1 - (-1 - *unitsptr) / base);
1838 *unitsptr -= tensdelta * base;
1839 return increment_overflow(tensptr, tensdelta);
1840}
1841
1842static bool
1843normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
1844{
1845 register int tensdelta;
1846
1847 tensdelta = (*unitsptr >= 0) ?
1848 (*unitsptr / base) :
1849 (-1 - (-1 - *unitsptr) / base);
1850 *unitsptr -= tensdelta * base;
1851 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001852}
1853
1854static int
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001855tmcomp(register const struct tm *const atmp,
1856 register const struct tm *const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001857{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001858 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001859
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001860 if (atmp->tm_year != btmp->tm_year)
1861 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1862 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1863 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1864 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1865 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1866 result = atmp->tm_sec - btmp->tm_sec;
1867 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001868}
1869
1870static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001871time2sub(struct tm *const tmp,
1872 struct tm *(*funcp)(struct state const *, time_t const *,
1873 int_fast32_t, struct tm *),
1874 struct state const *sp,
1875 const int_fast32_t offset,
1876 bool *okayp,
1877 bool do_norm_secs)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001878{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001879 register int dir;
1880 register int i, j;
1881 register int saved_seconds;
1882 register int_fast32_t li;
1883 register time_t lo;
1884 register time_t hi;
1885 int_fast32_t y;
1886 time_t newt;
1887 time_t t;
1888 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001889
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001890 *okayp = false;
1891 yourtm = *tmp;
1892 if (do_norm_secs) {
1893 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1894 SECSPERMIN))
1895 return WRONG;
1896 }
1897 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1898 return WRONG;
1899 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1900 return WRONG;
1901 y = yourtm.tm_year;
1902 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1903 return WRONG;
1904 /*
1905 ** Turn y into an actual year number for now.
1906 ** It is converted back to an offset from TM_YEAR_BASE later.
1907 */
1908 if (increment_overflow32(&y, TM_YEAR_BASE))
1909 return WRONG;
1910 while (yourtm.tm_mday <= 0) {
1911 if (increment_overflow32(&y, -1))
1912 return WRONG;
1913 li = y + (1 < yourtm.tm_mon);
1914 yourtm.tm_mday += year_lengths[isleap(li)];
1915 }
1916 while (yourtm.tm_mday > DAYSPERLYEAR) {
1917 li = y + (1 < yourtm.tm_mon);
1918 yourtm.tm_mday -= year_lengths[isleap(li)];
1919 if (increment_overflow32(&y, 1))
1920 return WRONG;
1921 }
1922 for ( ; ; ) {
1923 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1924 if (yourtm.tm_mday <= i)
1925 break;
1926 yourtm.tm_mday -= i;
1927 if (++yourtm.tm_mon >= MONSPERYEAR) {
1928 yourtm.tm_mon = 0;
1929 if (increment_overflow32(&y, 1))
1930 return WRONG;
1931 }
1932 }
1933 if (increment_overflow32(&y, -TM_YEAR_BASE))
1934 return WRONG;
1935 if (! (INT_MIN <= y && y <= INT_MAX))
1936 return WRONG;
1937 yourtm.tm_year = y;
1938 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1939 saved_seconds = 0;
1940 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1941 /*
1942 ** We can't set tm_sec to 0, because that might push the
1943 ** time below the minimum representable time.
1944 ** Set tm_sec to 59 instead.
1945 ** This assumes that the minimum representable time is
1946 ** not in the same minute that a leap second was deleted from,
1947 ** which is a safer assumption than using 58 would be.
1948 */
1949 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1950 return WRONG;
1951 saved_seconds = yourtm.tm_sec;
1952 yourtm.tm_sec = SECSPERMIN - 1;
1953 } else {
1954 saved_seconds = yourtm.tm_sec;
1955 yourtm.tm_sec = 0;
1956 }
1957 /*
1958 ** Do a binary search (this works whatever time_t's type is).
1959 */
1960 lo = time_t_min;
1961 hi = time_t_max;
1962 for ( ; ; ) {
1963 t = lo / 2 + hi / 2;
1964 if (t < lo)
1965 t = lo;
1966 else if (t > hi)
1967 t = hi;
1968 if (! funcp(sp, &t, offset, &mytm)) {
1969 /*
1970 ** Assume that t is too extreme to be represented in
1971 ** a struct tm; arrange things so that it is less
1972 ** extreme on the next pass.
1973 */
1974 dir = (t > 0) ? 1 : -1;
1975 } else dir = tmcomp(&mytm, &yourtm);
1976 if (dir != 0) {
1977 if (t == lo) {
1978 if (t == time_t_max)
1979 return WRONG;
1980 ++t;
1981 ++lo;
1982 } else if (t == hi) {
1983 if (t == time_t_min)
1984 return WRONG;
1985 --t;
1986 --hi;
1987 }
1988 if (lo > hi)
1989 return WRONG;
1990 if (dir > 0)
1991 hi = t;
1992 else lo = t;
1993 continue;
1994 }
1995#if defined TM_GMTOFF && ! UNINIT_TRAP
1996 if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
1997 && (yourtm.TM_GMTOFF < 0
1998 ? (-SECSPERDAY <= yourtm.TM_GMTOFF
1999 && (mytm.TM_GMTOFF <=
2000 (SMALLEST (INT_FAST32_MAX, LONG_MAX)
2001 + yourtm.TM_GMTOFF)))
2002 : (yourtm.TM_GMTOFF <= SECSPERDAY
2003 && ((BIGGEST (INT_FAST32_MIN, LONG_MIN)
2004 + yourtm.TM_GMTOFF)
2005 <= mytm.TM_GMTOFF)))) {
2006 /* MYTM matches YOURTM except with the wrong UTC offset.
2007 YOURTM.TM_GMTOFF is plausible, so try it instead.
2008 It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
2009 since the guess gets checked. */
2010 time_t altt = t;
2011 int_fast32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
2012 if (!increment_overflow_time(&altt, diff)) {
2013 struct tm alttm;
2014 if (funcp(sp, &altt, offset, &alttm)
2015 && alttm.tm_isdst == mytm.tm_isdst
2016 && alttm.TM_GMTOFF == yourtm.TM_GMTOFF
2017 && tmcomp(&alttm, &yourtm) == 0) {
2018 t = altt;
2019 mytm = alttm;
2020 }
2021 }
2022 }
2023#endif
2024 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
2025 break;
2026 /*
2027 ** Right time, wrong type.
2028 ** Hunt for right time, right type.
2029 ** It's okay to guess wrong since the guess
2030 ** gets checked.
2031 */
2032 if (sp == NULL)
2033 return WRONG;
2034 for (i = sp->typecnt - 1; i >= 0; --i) {
2035 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2036 continue;
2037 for (j = sp->typecnt - 1; j >= 0; --j) {
2038 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2039 continue;
2040 newt = t + sp->ttis[j].tt_gmtoff -
2041 sp->ttis[i].tt_gmtoff;
2042 if (! funcp(sp, &newt, offset, &mytm))
2043 continue;
2044 if (tmcomp(&mytm, &yourtm) != 0)
2045 continue;
2046 if (mytm.tm_isdst != yourtm.tm_isdst)
2047 continue;
2048 /*
2049 ** We have a match.
2050 */
2051 t = newt;
2052 goto label;
2053 }
2054 }
2055 return WRONG;
2056 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002057label:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002058 newt = t + saved_seconds;
2059 if ((newt < t) != (saved_seconds < 0))
2060 return WRONG;
2061 t = newt;
2062 if (funcp(sp, &t, offset, tmp))
2063 *okayp = true;
2064 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002065}
2066
2067static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002068time2(struct tm * const tmp,
2069 struct tm *(*funcp)(struct state const *, time_t const *,
2070 int_fast32_t, struct tm *),
2071 struct state const *sp,
Elliott Hughesce4783c2013-07-12 17:31:11 -07002072 const int_fast32_t offset,
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002073 bool *okayp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002075 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002076
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002077 /*
2078 ** First try without normalization of seconds
2079 ** (in case tm_sec contains a value associated with a leap second).
2080 ** If that fails, try with normalization of seconds.
2081 */
2082 t = time2sub(tmp, funcp, sp, offset, okayp, false);
2083 return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002084}
2085
2086static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002087time1(struct tm *const tmp,
2088 struct tm *(*funcp) (struct state const *, time_t const *,
2089 int_fast32_t, struct tm *),
2090 struct state const *sp,
2091 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002092{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002093 register time_t t;
2094 register int samei, otheri;
2095 register int sameind, otherind;
2096 register int i;
2097 register int nseen;
2098 char seen[TZ_MAX_TYPES];
2099 unsigned char types[TZ_MAX_TYPES];
2100 bool okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002101
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002102 if (tmp == NULL) {
2103 errno = EINVAL;
2104 return WRONG;
2105 }
2106 if (tmp->tm_isdst > 1)
2107 tmp->tm_isdst = 1;
2108 t = time2(tmp, funcp, sp, offset, &okay);
2109 if (okay)
2110 return t;
2111 if (tmp->tm_isdst < 0)
Elliott Hughes906eb992014-06-18 19:46:25 -07002112#ifdef PCTS
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002113 /*
2114 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
2115 */
2116 tmp->tm_isdst = 0; /* reset to std and try again */
Elliott Hughes906eb992014-06-18 19:46:25 -07002117#else
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002118 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002119#endif /* !defined PCTS */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002120 /*
2121 ** We're supposed to assume that somebody took a time of one type
2122 ** and did some math on it that yielded a "struct tm" that's bad.
2123 ** We try to divine the type they started from and adjust to the
2124 ** type they need.
2125 */
2126 if (sp == NULL)
2127 return WRONG;
2128 for (i = 0; i < sp->typecnt; ++i)
2129 seen[i] = false;
2130 nseen = 0;
2131 for (i = sp->timecnt - 1; i >= 0; --i)
2132 if (!seen[sp->types[i]]) {
2133 seen[sp->types[i]] = true;
2134 types[nseen++] = sp->types[i];
2135 }
2136 for (sameind = 0; sameind < nseen; ++sameind) {
2137 samei = types[sameind];
2138 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2139 continue;
2140 for (otherind = 0; otherind < nseen; ++otherind) {
2141 otheri = types[otherind];
2142 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2143 continue;
2144 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2145 sp->ttis[samei].tt_gmtoff;
2146 tmp->tm_isdst = !tmp->tm_isdst;
2147 t = time2(tmp, funcp, sp, offset, &okay);
2148 if (okay)
2149 return t;
2150 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2151 sp->ttis[samei].tt_gmtoff;
2152 tmp->tm_isdst = !tmp->tm_isdst;
2153 }
2154 }
2155 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002156}
2157
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002158static time_t
2159mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
2160{
2161 if (sp)
2162 return time1(tmp, localsub, sp, setname);
2163 else {
2164 gmtcheck();
2165 return time1(tmp, gmtsub, gmtptr, 0);
2166 }
2167}
2168
2169#if NETBSD_INSPIRED
2170
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002171time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002172mktime_z(struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002173{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002174 return mktime_tzname(sp, tmp, false);
2175}
2176
2177#endif
2178
2179time_t
2180mktime(struct tm *tmp)
2181{
Elliott Hughesa9209d72016-09-16 18:16:47 -07002182#if defined(__BIONIC__)
Elliott Hughesf8ebaa42016-08-12 16:28:36 -07002183 int saved_errno = errno;
2184#endif
2185
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002186 time_t t;
2187 int err = lock();
2188 if (err) {
2189 errno = err;
2190 return -1;
2191 }
2192 tzset_unlocked();
2193 t = mktime_tzname(lclptr, tmp, true);
2194 unlock();
Elliott Hughesf8ebaa42016-08-12 16:28:36 -07002195
Elliott Hughesa9209d72016-09-16 18:16:47 -07002196#if defined(__BIONIC__)
Elliott Hughesf8ebaa42016-08-12 16:28:36 -07002197 errno = (t == -1) ? EOVERFLOW : saved_errno;
2198#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002199 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002200}
2201
2202#ifdef STD_INSPIRED
2203
2204time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002205timelocal(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002206{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002207 if (tmp != NULL)
2208 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2209 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002210}
2211
2212time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002213timegm(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002214{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002215 return timeoff(tmp, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002216}
2217
Elliott Hughes906eb992014-06-18 19:46:25 -07002218time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002219timeoff(struct tm *tmp, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07002220{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002221 if (tmp)
2222 tmp->tm_isdst = 0;
2223 gmtcheck();
2224 return time1(tmp, gmtsub, gmtptr, offset);
Elliott Hughes906eb992014-06-18 19:46:25 -07002225}
2226
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002227#endif /* defined STD_INSPIRED */
2228
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002229/*
2230** XXX--is the below the right way to conditionalize??
2231*/
2232
2233#ifdef STD_INSPIRED
2234
2235/*
2236** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2237** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2238** is not the case if we are accounting for leap seconds.
2239** So, we provide the following conversion routines for use
2240** when exchanging timestamps with POSIX conforming systems.
2241*/
2242
Elliott Hughesce4783c2013-07-12 17:31:11 -07002243static int_fast64_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002244leapcorr(struct state const *sp, time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002245{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002246 register struct lsinfo const * lp;
2247 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002248
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002249 i = sp->leapcnt;
2250 while (--i >= 0) {
2251 lp = &sp->lsis[i];
2252 if (t >= lp->ls_trans)
2253 return lp->ls_corr;
2254 }
2255 return 0;
2256}
2257
2258NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2259time2posix_z(struct state *sp, time_t t)
2260{
2261 return t - leapcorr(sp, t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002262}
2263
2264time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002265time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002266{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002267 int err = lock();
2268 if (err) {
2269 errno = err;
2270 return -1;
2271 }
2272 if (!lcl_is_set)
2273 tzset_unlocked();
2274 if (lclptr)
2275 t = time2posix_z(lclptr, t);
2276 unlock();
2277 return t;
2278}
2279
2280NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2281posix2time_z(struct state *sp, time_t t)
2282{
2283 time_t x;
2284 time_t y;
2285 /*
2286 ** For a positive leap second hit, the result
2287 ** is not unique. For a negative leap second
2288 ** hit, the corresponding time doesn't exist,
2289 ** so we return an adjacent second.
2290 */
2291 x = t + leapcorr(sp, t);
2292 y = x - leapcorr(sp, x);
2293 if (y < t) {
2294 do {
2295 x++;
2296 y = x - leapcorr(sp, x);
2297 } while (y < t);
2298 x -= y != t;
2299 } else if (y > t) {
2300 do {
2301 --x;
2302 y = x - leapcorr(sp, x);
2303 } while (y > t);
2304 x += y != t;
2305 }
2306 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002307}
2308
2309time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002310posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002311{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002312 int err = lock();
2313 if (err) {
2314 errno = err;
2315 return -1;
2316 }
2317 if (!lcl_is_set)
2318 tzset_unlocked();
2319 if (lclptr)
2320 t = posix2time_z(lclptr, t);
2321 unlock();
2322 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002323}
2324
2325#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002326
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002327#ifdef time_tz
2328
2329/* Convert from the underlying system's time_t to the ersatz time_tz,
2330 which is called 'time_t' in this file. */
2331
2332time_t
2333time(time_t *p)
2334{
2335 time_t r = sys_time(0);
2336 if (p)
2337 *p = r;
2338 return r;
2339}
2340
2341#endif
2342
Elliott Hughesce4783c2013-07-12 17:31:11 -07002343// BEGIN android-added
2344
Elliott Hughes1c295722012-10-19 18:13:15 -07002345#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002346#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002347#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002348
Elliott Hughescf178bf2013-09-18 19:25:28 -07002349static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
Elliott Hughes81c46fc2016-10-03 12:29:30 -07002350 const char* olson_id,
2351 int32_t* entry_length) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002352 const char* path_prefix = getenv(path_prefix_variable);
2353 if (path_prefix == NULL) {
2354 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2355 return -1;
2356 }
Elliott Hughes329103d2014-04-25 16:55:04 -07002357 size_t path_length = strlen(path_prefix) + 1 + strlen(path_suffix) + 1;
2358 char* path = malloc(path_length);
2359 if (path == NULL) {
2360 fprintf(stderr, "%s: couldn't allocate %zu-byte path\n", __FUNCTION__, path_length);
2361 return -1;
2362 }
2363 snprintf(path, path_length, "%s/%s", path_prefix, path_suffix);
Elliott Hughes4edd6512016-10-03 16:46:33 -07002364 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Elliott Hughesd23af232012-10-17 16:30:47 -07002365 if (fd == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002366 free(path);
Elliott Hughes1c295722012-10-19 18:13:15 -07002367 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002368 }
2369
2370 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002371 // int index_offset
2372 // int data_offset
2373 // int zonetab_offset
2374 struct bionic_tzdata_header {
2375 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002376 int32_t index_offset;
2377 int32_t data_offset;
2378 int32_t zonetab_offset;
2379 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002380 memset(&header, 0, sizeof(header));
2381 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2382 if (bytes_read != sizeof(header)) {
2383 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2384 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002385 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002386 close(fd);
2387 return -1;
2388 }
2389
2390 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002391 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2392 __FUNCTION__, path, header.tzdata_version);
Elliott Hughes329103d2014-04-25 16:55:04 -07002393 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002394 close(fd);
2395 return -1;
2396 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002397
2398#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002399 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002400 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2401 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2402 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2403#endif
2404
2405 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002406 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2407 __FUNCTION__, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002408 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002409 close(fd);
2410 return -1;
2411 }
2412
2413 off_t specific_zone_offset = -1;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002414 ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
2415 char* index = malloc(index_size);
Elliott Hughes329103d2014-04-25 16:55:04 -07002416 if (index == NULL) {
2417 fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n",
2418 __FUNCTION__, index_size, path);
2419 free(path);
2420 close(fd);
2421 return -1;
2422 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002423 if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) {
2424 fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
2425 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002426 free(path);
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002427 free(index);
2428 close(fd);
2429 return -1;
2430 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002431
Elliott Hughes1c295722012-10-19 18:13:15 -07002432 static const size_t NAME_LENGTH = 40;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002433 struct index_entry_t {
2434 char buf[NAME_LENGTH];
2435 int32_t start;
2436 int32_t length;
Elliott Hughes13bab432014-08-06 15:23:11 -07002437 int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L).
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002438 };
Elliott Hughese0175ca2013-03-14 14:38:08 -07002439
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002440 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(struct index_entry_t);
2441 struct index_entry_t* entry = (struct index_entry_t*) index;
Elliott Hughese0175ca2013-03-14 14:38:08 -07002442 for (size_t i = 0; i < id_count; ++i) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002443 char this_id[NAME_LENGTH + 1];
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002444 memcpy(this_id, entry->buf, NAME_LENGTH);
Elliott Hughes1c295722012-10-19 18:13:15 -07002445 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002446
2447 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002448 specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset);
Elliott Hughes81c46fc2016-10-03 12:29:30 -07002449 *entry_length = ntohl(entry->length);
Elliott Hughesd23af232012-10-17 16:30:47 -07002450 break;
2451 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002452
2453 ++entry;
Elliott Hughesd23af232012-10-17 16:30:47 -07002454 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002455 free(index);
Elliott Hughesd23af232012-10-17 16:30:47 -07002456
2457 if (specific_zone_offset == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002458 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002459 close(fd);
2460 return -1;
2461 }
2462
2463 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002464 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2465 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002466 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002467 close(fd);
2468 return -1;
2469 }
2470
Elliott Hughese7aaad82013-04-25 14:02:59 -07002471 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2472
Elliott Hughes329103d2014-04-25 16:55:04 -07002473 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002474 return fd;
2475}
Elliott Hughes1c295722012-10-19 18:13:15 -07002476
Elliott Hughes81c46fc2016-10-03 12:29:30 -07002477static int __bionic_open_tzdata(const char* olson_id, int32_t* entry_length) {
2478 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata",
2479 olson_id, entry_length);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002480 if (fd < 0) {
Elliott Hughes81c46fc2016-10-03 12:29:30 -07002481 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata",
2482 olson_id, entry_length);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002483 if (fd == -2) {
2484 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2485 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2486 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
2487 }
Elliott Hughes1c295722012-10-19 18:13:15 -07002488 }
2489 return fd;
2490}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002491
Elliott Hughesce4783c2013-07-12 17:31:11 -07002492// END android-added