blob: 165dd2d1240a0359fbea7d89e3c5d4784424d80b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2** This file is in the public domain, so clarified as of
3** 1996-06-05 by Arthur David Olson.
4*/
5
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08006/*
7** Leap second handling from Bradley White.
8** POSIX-style TZ environment variable handling from Guy Harris.
9*/
10
11/*LINTLIBRARY*/
12
Elliott Hughes9fb22a32015-10-07 17:13:40 -070013#define LOCALTIME_IMPLEMENTATION
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080014#include "private.h"
Elliott Hughes9fb22a32015-10-07 17:13:40 -070015
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016#include "tzfile.h"
17#include "fcntl.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080018
Elliott Hughes9fb22a32015-10-07 17:13:40 -070019#if THREAD_SAFE
20# include <pthread.h>
21static pthread_mutex_t locallock = PTHREAD_MUTEX_INITIALIZER;
22static int lock(void) { return pthread_mutex_lock(&locallock); }
23static void unlock(void) { pthread_mutex_unlock(&locallock); }
24#else
25static int lock(void) { return 0; }
26static void unlock(void) { }
27#endif
28
29/* NETBSD_INSPIRED_EXTERN functions are exported to callers if
30 NETBSD_INSPIRED is defined, and are private otherwise. */
31#if NETBSD_INSPIRED
32# define NETBSD_INSPIRED_EXTERN
33#else
34# define NETBSD_INSPIRED_EXTERN static
35#endif
36
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#ifndef TZ_ABBR_MAX_LEN
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070038#define TZ_ABBR_MAX_LEN 16
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#endif /* !defined TZ_ABBR_MAX_LEN */
40
41#ifndef TZ_ABBR_CHAR_SET
42#define TZ_ABBR_CHAR_SET \
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070043 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#endif /* !defined TZ_ABBR_CHAR_SET */
45
46#ifndef TZ_ABBR_ERR_CHAR
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070047#define TZ_ABBR_ERR_CHAR '_'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048#endif /* !defined TZ_ABBR_ERR_CHAR */
49
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050/*
51** SunOS 4.1.1 headers lack O_BINARY.
52*/
53
54#ifdef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070055#define OPEN_MODE (O_RDONLY | O_BINARY)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#endif /* defined O_BINARY */
57#ifndef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070058#define OPEN_MODE O_RDONLY
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#endif /* !defined O_BINARY */
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061#ifndef WILDABBR
62/*
63** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070064** 1. They might reference tzname[0] before calling tzset (explicitly
65** or implicitly).
66** 2. They might reference tzname[1] before calling tzset (explicitly
67** or implicitly).
68** 3. They might reference tzname[1] after setting to a time zone
69** in which Daylight Saving Time is never observed.
70** 4. They might reference tzname[0] after setting to a time zone
71** in which Standard Time is never observed.
72** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073** What's best to do in the above cases is open to debate;
74** for now, we just set things up so that in any of the five cases
75** WILDABBR is used. Another possibility: initialize tzname[0] to the
76** string "tzname[0] used before set", and similarly for the other cases.
77** And another: initialize tzname[0] to "ERA", with an explanation in the
78** manual page of what this "time zone abbreviation" means (doing this so
79** that tzname[0] has the "normal" length of three characters).
80*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070081#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082#endif /* !defined WILDABBR */
83
Elliott Hughes906eb992014-06-18 19:46:25 -070084static const char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Calin Juravled8928922014-02-28 12:18:53 +000086static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88/*
89** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
90** We default to US rules as of 1999-08-17.
91** POSIX 1003.1 section 8.1.1 says that the default DST rules are
92** implementation dependent; for historical reasons, US rules are a
93** common default.
94*/
95#ifndef TZDEFRULESTRING
96#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
97#endif /* !defined TZDEFDST */
98
Calin Juravled8928922014-02-28 12:18:53 +000099struct ttinfo { /* time type information */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700100 int_fast32_t tt_gmtoff; /* UT offset in seconds */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700101 bool tt_isdst; /* used to set tm_isdst */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700102 int tt_abbrind; /* abbreviation list index */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700103 bool tt_ttisstd; /* transition is std time */
104 bool tt_ttisgmt; /* transition is UT */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105};
106
Calin Juravled8928922014-02-28 12:18:53 +0000107struct lsinfo { /* leap second information */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700108 time_t ls_trans; /* transition time */
109 int_fast64_t ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110};
111
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700112#define SMALLEST(a, b) (((a) < (b)) ? (a) : (b))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700113#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
115#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700116#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#endif /* defined TZNAME_MAX */
118#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700119#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120#endif /* !defined TZNAME_MAX */
121
122struct state {
Calin Juravled8928922014-02-28 12:18:53 +0000123 int leapcnt;
124 int timecnt;
125 int typecnt;
126 int charcnt;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700127 bool goback;
128 bool goahead;
Calin Juravled8928922014-02-28 12:18:53 +0000129 time_t ats[TZ_MAX_TIMES];
130 unsigned char types[TZ_MAX_TIMES];
131 struct ttinfo ttis[TZ_MAX_TYPES];
132 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
133 (2 * (MY_TZNAME_MAX + 1)))];
134 struct lsinfo lsis[TZ_MAX_LEAPS];
135 int defaulttype; /* for early times or if no transitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136};
137
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700138enum r_type {
139 JULIAN_DAY, /* Jn = Julian day */
140 DAY_OF_YEAR, /* n = day of year */
141 MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
142};
143
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144struct rule {
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700145 enum r_type r_type; /* type of rule */
Calin Juravled8928922014-02-28 12:18:53 +0000146 int r_day; /* day number of rule */
147 int r_week; /* week number of rule */
148 int r_mon; /* month number of rule */
149 int_fast32_t r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150};
151
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700152static struct tm *gmtsub(struct state const *, time_t const *, int_fast32_t,
153 struct tm *);
154static bool increment_overflow(int *, int);
155static bool increment_overflow_time(time_t *, int_fast32_t);
156static bool normalize_overflow32(int_fast32_t *, int *, int);
157static struct tm *timesub(time_t const *, int_fast32_t, struct state const *,
158 struct tm *);
159static bool typesequiv(struct state const *, int, int);
160static bool tzparse(char const *, struct state *, bool);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
162#ifdef ALL_STATE
Calin Juravled8928922014-02-28 12:18:53 +0000163static struct state * lclptr;
164static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165#endif /* defined ALL_STATE */
166
167#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700168static struct state lclmem;
169static struct state gmtmem;
170#define lclptr (&lclmem)
171#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172#endif /* State Farm */
173
174#ifndef TZ_STRLEN_MAX
175#define TZ_STRLEN_MAX 255
176#endif /* !defined TZ_STRLEN_MAX */
177
Calin Juravled8928922014-02-28 12:18:53 +0000178static char lcl_TZname[TZ_STRLEN_MAX + 1];
179static int lcl_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Calin Juravled8928922014-02-28 12:18:53 +0000181char * tzname[2] = {
Elliott Hughes906eb992014-06-18 19:46:25 -0700182 (char *) wildabbr,
183 (char *) wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184};
185
186/*
187** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700188** Except for the strftime function, these functions [asctime,
189** ctime, gmtime, localtime] return values in one of two static
190** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191** Thanks to Paul Eggert for noting this.
192*/
193
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700194static struct tm tm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
196#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700197long timezone;
198int daylight;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199#endif /* defined USG_COMPAT */
200
201#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700202long altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203#endif /* defined ALTZONE */
204
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700205/* Initialize *S to a value based on GMTOFF, ISDST, and ABBRIND. */
206static void
207init_ttinfo(struct ttinfo *s, int_fast32_t gmtoff, bool isdst, int abbrind)
208{
209 s->tt_gmtoff = gmtoff;
210 s->tt_isdst = isdst;
211 s->tt_abbrind = abbrind;
212 s->tt_ttisstd = false;
213 s->tt_ttisgmt = false;
214}
215
Elliott Hughesce4783c2013-07-12 17:31:11 -0700216static int_fast32_t
217detzcode(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700219 register int_fast32_t result;
220 register int i;
221 int_fast32_t one = 1;
222 int_fast32_t halfmaxval = one << (32 - 2);
223 int_fast32_t maxval = halfmaxval - 1 + halfmaxval;
224 int_fast32_t minval = -1 - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700226 result = codep[0] & 0x7f;
227 for (i = 1; i < 4; ++i)
228 result = (result << 8) | (codep[i] & 0xff);
229
230 if (codep[0] & 0x80) {
231 /* Do two's-complement negation even on non-two's-complement machines.
232 If the result would be minval - 1, return minval. */
233 result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0;
234 result += minval;
235 }
236 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237}
238
Calin Juravle627d37c2014-02-28 11:46:03 +0000239static int_fast64_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700240detzcode64(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700242 register uint_fast64_t result;
243 register int i;
244 int_fast64_t one = 1;
245 int_fast64_t halfmaxval = one << (64 - 2);
246 int_fast64_t maxval = halfmaxval - 1 + halfmaxval;
247 int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700249 result = codep[0] & 0x7f;
250 for (i = 1; i < 8; ++i)
251 result = (result << 8) | (codep[i] & 0xff);
252
253 if (codep[0] & 0x80) {
254 /* Do two's-complement negation even on non-two's-complement machines.
255 If the result would be minval - 1, return minval. */
256 result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0;
257 result += minval;
258 }
259 return result;
260}
261
262static void
263update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
264{
265 tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
266#ifdef USG_COMPAT
267 if (!ttisp->tt_isdst)
268 timezone = - ttisp->tt_gmtoff;
269#endif
270#ifdef ALTZONE
271 if (ttisp->tt_isdst)
272 altzone = - ttisp->tt_gmtoff;
273#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274}
275
276static void
Elliott Hughesce4783c2013-07-12 17:31:11 -0700277settzname(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700279 register struct state * const sp = lclptr;
280 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700282 tzname[0] = tzname[1] = (char *) wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700284 daylight = 0;
285 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286#endif /* defined USG_COMPAT */
287#ifdef ALTZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700288 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289#endif /* defined ALTZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700290 if (sp == NULL) {
291 tzname[0] = tzname[1] = (char *) gmt;
292 return;
293 }
294 /*
295 ** And to get the latest zone names into tzname. . .
296 */
297 for (i = 0; i < sp->typecnt; ++i) {
298 register const struct ttinfo * const ttisp = &sp->ttis[i];
299 update_tzname_etc(sp, ttisp);
300 }
301 for (i = 0; i < sp->timecnt; ++i) {
302 register const struct ttinfo * const ttisp =
303 &sp->ttis[
304 sp->types[i]];
305 update_tzname_etc(sp, ttisp);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700306#ifdef USG_COMPAT
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700307 if (ttisp->tt_isdst)
308 daylight = 1;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700309#endif /* defined USG_COMPAT */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700310 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311}
312
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700313static void
314scrub_abbrs(struct state *sp)
315{
316 int i;
317 /*
318 ** First, replace bogus characters.
319 */
320 for (i = 0; i < sp->charcnt; ++i)
321 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
322 sp->chars[i] = TZ_ABBR_ERR_CHAR;
323 /*
324 ** Second, truncate long abbreviations.
325 */
326 for (i = 0; i < sp->typecnt; ++i) {
327 register const struct ttinfo * const ttisp = &sp->ttis[i];
328 register char * cp = &sp->chars[ttisp->tt_abbrind];
329
330 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
331 strcmp(cp, GRANDPARENTED) != 0)
332 *(cp + TZ_ABBR_MAX_LEN) = '\0';
333 }
334}
335
336static bool
337differ_by_repeat(const time_t t1, const time_t t0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338{
Calin Juravled8928922014-02-28 12:18:53 +0000339 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
340 return 0;
Christopher Ferris384ffe32015-11-02 12:56:37 -0800341#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 +0000342 return t1 - t0 == SECSPERREPEAT;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700343#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344}
345
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700346/* Input buffer for data read from a compiled tz file. */
347union input_buffer {
348 /* The first part of the buffer, interpreted as a header. */
349 struct tzhead tzhead;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700351 /* The entire buffer. */
352 char buf[2 * sizeof(struct tzhead) + 2 * sizeof (struct state)
353 + 4 * TZ_MAX_TIMES];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354};
355
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700356/* Local storage needed for 'tzloadbody'. */
357union local_storage {
358 /* The file name to be opened. */
359 char fullname[FILENAME_MAX + 1];
360
361 /* The results of analyzing the file's contents after it is opened. */
362 struct {
363 /* The input buffer. */
364 union input_buffer u;
365
366 /* A temporary state used for parsing a TZ string in the file. */
367 struct state st;
368 } u;
369};
370
371static int __bionic_open_tzdata(const char*);
372
373/* Load tz data from the file named NAME into *SP. Read extended
374 format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
375 success, an errno value on failure. */
376static int
377tzloadbody(char const *name, struct state *sp, bool doextend,
378 union local_storage *lsp)
379{
380 register int i;
381 register int fid;
382 register int stored;
383 register ssize_t nread;
384#if !defined(__ANDROID__)
385 register bool doaccess;
386 register char *fullname = lsp->fullname;
387#endif
388 register union input_buffer *up = &lsp->u.u;
389 register int tzheadsize = sizeof (struct tzhead);
390
391 sp->goback = sp->goahead = false;
392
393 if (! name) {
394 name = TZDEFAULT;
395 if (! name)
396 return EINVAL;
397 }
398
399#if defined(__ANDROID__)
400 fid = __bionic_open_tzdata(name);
401#else
402 if (name[0] == ':')
403 ++name;
404 doaccess = name[0] == '/';
405 if (!doaccess) {
406 char const *p = TZDIR;
407 if (! p)
408 return EINVAL;
409 if (sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
410 return ENAMETOOLONG;
411 strcpy(fullname, p);
412 strcat(fullname, "/");
413 strcat(fullname, name);
414 /* Set doaccess if '.' (as in "../") shows up in name. */
415 if (strchr(name, '.'))
416 doaccess = true;
417 name = fullname;
418 }
419 if (doaccess && access(name, R_OK) != 0)
420 return errno;
421 fid = open(name, OPEN_MODE);
422#endif
423 if (fid < 0)
424 return errno;
425
426 nread = read(fid, up->buf, sizeof up->buf);
427 if (nread < tzheadsize) {
428 int err = nread < 0 ? errno : EINVAL;
429 close(fid);
430 return err;
431 }
432 if (close(fid) < 0)
433 return errno;
434 for (stored = 4; stored <= 8; stored *= 2) {
435 int_fast32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
436 int_fast32_t ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
437 int_fast32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
438 int_fast32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
439 int_fast32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
440 int_fast32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
441 char const *p = up->buf + tzheadsize;
442 if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
443 && 0 < typecnt && typecnt < TZ_MAX_TYPES
444 && 0 <= timecnt && timecnt < TZ_MAX_TIMES
445 && 0 <= charcnt && charcnt < TZ_MAX_CHARS
446 && (ttisstdcnt == typecnt || ttisstdcnt == 0)
447 && (ttisgmtcnt == typecnt || ttisgmtcnt == 0)))
448 return EINVAL;
449 if (nread
450 < (tzheadsize /* struct tzhead */
451 + timecnt * stored /* ats */
452 + timecnt /* types */
453 + typecnt * 6 /* ttinfos */
454 + charcnt /* chars */
455 + leapcnt * (stored + 4) /* lsinfos */
456 + ttisstdcnt /* ttisstds */
457 + ttisgmtcnt)) /* ttisgmts */
458 return EINVAL;
459 sp->leapcnt = leapcnt;
460 sp->timecnt = timecnt;
461 sp->typecnt = typecnt;
462 sp->charcnt = charcnt;
463
464 /* Read transitions, discarding those out of time_t range.
465 But pretend the last transition before time_t_min
466 occurred at time_t_min. */
467 timecnt = 0;
468 for (i = 0; i < sp->timecnt; ++i) {
469 int_fast64_t at
470 = stored == 4 ? detzcode(p) : detzcode64(p);
471 sp->types[i] = at <= time_t_max;
472 if (sp->types[i]) {
473 time_t attime
474 = ((TYPE_SIGNED(time_t) ? at < time_t_min : at < 0)
475 ? time_t_min : at);
476 if (timecnt && attime <= sp->ats[timecnt - 1]) {
477 if (attime < sp->ats[timecnt - 1])
478 return EINVAL;
479 sp->types[i - 1] = 0;
480 timecnt--;
481 }
482 sp->ats[timecnt++] = attime;
483 }
484 p += stored;
485 }
486
487 timecnt = 0;
488 for (i = 0; i < sp->timecnt; ++i) {
489 unsigned char typ = *p++;
490 if (sp->typecnt <= typ)
491 return EINVAL;
492 if (sp->types[i])
493 sp->types[timecnt++] = typ;
494 }
495 sp->timecnt = timecnt;
496 for (i = 0; i < sp->typecnt; ++i) {
497 register struct ttinfo * ttisp;
498 unsigned char isdst, abbrind;
499
500 ttisp = &sp->ttis[i];
501 ttisp->tt_gmtoff = detzcode(p);
502 p += 4;
503 isdst = *p++;
504 if (! (isdst < 2))
505 return EINVAL;
506 ttisp->tt_isdst = isdst;
507 abbrind = *p++;
508 if (! (abbrind < sp->charcnt))
509 return EINVAL;
510 ttisp->tt_abbrind = abbrind;
511 }
512 for (i = 0; i < sp->charcnt; ++i)
513 sp->chars[i] = *p++;
514 sp->chars[i] = '\0'; /* ensure '\0' at end */
515
516 /* Read leap seconds, discarding those out of time_t range. */
517 leapcnt = 0;
518 for (i = 0; i < sp->leapcnt; ++i) {
519 int_fast64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
520 int_fast32_t corr = detzcode(p + stored);
521 p += stored + 4;
522 if (tr <= time_t_max) {
523 time_t trans
524 = ((TYPE_SIGNED(time_t) ? tr < time_t_min : tr < 0)
525 ? time_t_min : tr);
526 if (leapcnt && trans <= sp->lsis[leapcnt - 1].ls_trans) {
527 if (trans < sp->lsis[leapcnt - 1].ls_trans)
528 return EINVAL;
529 leapcnt--;
530 }
531 sp->lsis[leapcnt].ls_trans = trans;
532 sp->lsis[leapcnt].ls_corr = corr;
533 leapcnt++;
534 }
535 }
536 sp->leapcnt = leapcnt;
537
538 for (i = 0; i < sp->typecnt; ++i) {
539 register struct ttinfo * ttisp;
540
541 ttisp = &sp->ttis[i];
542 if (ttisstdcnt == 0)
543 ttisp->tt_ttisstd = false;
544 else {
545 if (*p != true && *p != false)
546 return EINVAL;
547 ttisp->tt_ttisstd = *p++;
548 }
549 }
550 for (i = 0; i < sp->typecnt; ++i) {
551 register struct ttinfo * ttisp;
552
553 ttisp = &sp->ttis[i];
554 if (ttisgmtcnt == 0)
555 ttisp->tt_ttisgmt = false;
556 else {
557 if (*p != true && *p != false)
558 return EINVAL;
559 ttisp->tt_ttisgmt = *p++;
560 }
561 }
562 /*
563 ** If this is an old file, we're done.
564 */
565 if (up->tzhead.tzh_version[0] == '\0')
566 break;
567 nread -= p - up->buf;
568 memmove(up->buf, p, nread);
569 }
570 if (doextend && nread > 2 &&
571 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
572 sp->typecnt + 2 <= TZ_MAX_TYPES) {
573 struct state *ts = &lsp->u.st;
574
575 up->buf[nread - 1] = '\0';
576 if (tzparse(&up->buf[1], ts, false)
577 && ts->typecnt == 2) {
578
579 /* Attempt to reuse existing abbreviations.
580 Without this, America/Anchorage would stop
581 working after 2037 when TZ_MAX_CHARS is 50, as
582 sp->charcnt equals 42 (for LMT CAT CAWT CAPT AHST
583 AHDT YST AKDT AKST) and ts->charcnt equals 10
584 (for AKST AKDT). Reusing means sp->charcnt can
585 stay 42 in this example. */
586 int gotabbr = 0;
587 int charcnt = sp->charcnt;
588 for (i = 0; i < 2; i++) {
589 char *tsabbr = ts->chars + ts->ttis[i].tt_abbrind;
590 int j;
591 for (j = 0; j < charcnt; j++)
592 if (strcmp(sp->chars + j, tsabbr) == 0) {
593 ts->ttis[i].tt_abbrind = j;
594 gotabbr++;
595 break;
596 }
597 if (! (j < charcnt)) {
598 int tsabbrlen = strlen(tsabbr);
599 if (j + tsabbrlen < TZ_MAX_CHARS) {
600 strcpy(sp->chars + j, tsabbr);
601 charcnt = j + tsabbrlen + 1;
602 ts->ttis[i].tt_abbrind = j;
603 gotabbr++;
604 }
605 }
606 }
607 if (gotabbr == 2) {
608 sp->charcnt = charcnt;
609 for (i = 0; i < ts->timecnt; i++)
610 if (sp->ats[sp->timecnt - 1] < ts->ats[i])
611 break;
612 while (i < ts->timecnt
613 && sp->timecnt < TZ_MAX_TIMES) {
614 sp->ats[sp->timecnt] = ts->ats[i];
615 sp->types[sp->timecnt] = (sp->typecnt
616 + ts->types[i]);
617 sp->timecnt++;
618 i++;
619 }
620 sp->ttis[sp->typecnt++] = ts->ttis[0];
621 sp->ttis[sp->typecnt++] = ts->ttis[1];
622 }
623 }
624 }
625 if (sp->timecnt > 1) {
626 for (i = 1; i < sp->timecnt; ++i)
627 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
628 differ_by_repeat(sp->ats[i], sp->ats[0])) {
629 sp->goback = true;
630 break;
631 }
632 for (i = sp->timecnt - 2; i >= 0; --i)
633 if (typesequiv(sp, sp->types[sp->timecnt - 1],
634 sp->types[i]) &&
635 differ_by_repeat(sp->ats[sp->timecnt - 1],
636 sp->ats[i])) {
637 sp->goahead = true;
638 break;
639 }
640 }
641 /*
642 ** If type 0 is is unused in transitions,
643 ** it's the type to use for early times.
644 */
645 for (i = 0; i < sp->timecnt; ++i)
646 if (sp->types[i] == 0)
647 break;
648 i = i < sp->timecnt ? -1 : 0;
649 /*
650 ** Absent the above,
651 ** if there are transition times
652 ** and the first transition is to a daylight time
653 ** find the standard type less than and closest to
654 ** the type of the first transition.
655 */
656 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
657 i = sp->types[0];
658 while (--i >= 0)
659 if (!sp->ttis[i].tt_isdst)
660 break;
661 }
662 /*
663 ** If no result yet, find the first standard type.
664 ** If there is none, punt to type zero.
665 */
666 if (i < 0) {
667 i = 0;
668 while (sp->ttis[i].tt_isdst)
669 if (++i >= sp->typecnt) {
670 i = 0;
671 break;
672 }
673 }
674 sp->defaulttype = i;
675 return 0;
676}
677
678/* Load tz data from the file named NAME into *SP. Read extended
679 format if DOEXTEND. Return 0 on success, an errno value on failure. */
680static int
681tzload(char const *name, struct state *sp, bool doextend)
682{
683#ifdef ALL_STATE
684 union local_storage *lsp = malloc(sizeof *lsp);
685 if (!lsp)
686 return errno;
687 else {
688 int err = tzloadbody(name, sp, doextend, lsp);
689 free(lsp);
690 return err;
691 }
692#else
693 union local_storage ls;
694 return tzloadbody(name, sp, doextend, &ls);
695#endif
696}
697
698static bool
699typesequiv(const struct state *sp, int a, int b)
700{
701 register bool result;
702
703 if (sp == NULL ||
704 a < 0 || a >= sp->typecnt ||
705 b < 0 || b >= sp->typecnt)
706 result = false;
707 else {
708 register const struct ttinfo * ap = &sp->ttis[a];
709 register const struct ttinfo * bp = &sp->ttis[b];
710 result = ap->tt_gmtoff == bp->tt_gmtoff &&
711 ap->tt_isdst == bp->tt_isdst &&
712 ap->tt_ttisstd == bp->tt_ttisstd &&
713 ap->tt_ttisgmt == bp->tt_ttisgmt &&
714 strcmp(&sp->chars[ap->tt_abbrind],
715 &sp->chars[bp->tt_abbrind]) == 0;
716 }
717 return result;
718}
719
720static const int mon_lengths[2][MONSPERYEAR] = {
721 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
722 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
723};
724
725static const int year_lengths[2] = {
726 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727};
728
729/*
730** Given a pointer into a time zone string, scan until a character that is not
731** a valid character in a zone name is found. Return a pointer to that
732** character.
733*/
734
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700735static const char * ATTRIBUTE_PURE
736getzname(register const char *strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700738 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700740 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
741 c != '+')
742 ++strp;
743 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744}
745
746/*
747** Given a pointer into an extended time zone string, scan until the ending
748** delimiter of the zone name is located. Return a pointer to the delimiter.
749**
750** As with getzname above, the legal character set is actually quite
751** restricted, with other characters producing undefined results.
752** We don't do any checking here; checking is done later in common-case code.
753*/
754
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700755static const char * ATTRIBUTE_PURE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800756getqzname(register const char *strp, const int delim)
757{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700758 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700760 while ((c = *strp) != '\0' && c != delim)
761 ++strp;
762 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763}
764
765/*
766** Given a pointer into a time zone string, extract a number from that string.
767** Check that the number is within a specified range; if it is not, return
768** NULL.
769** Otherwise, return a pointer to the first character not part of the number.
770*/
771
772static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700773getnum(register const char *strp, int *const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800774{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700775 register char c;
776 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700778 if (strp == NULL || !is_digit(c = *strp))
779 return NULL;
780 num = 0;
781 do {
782 num = num * 10 + (c - '0');
783 if (num > max)
784 return NULL; /* illegal value */
785 c = *++strp;
786 } while (is_digit(c));
787 if (num < min)
788 return NULL; /* illegal value */
789 *nump = num;
790 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791}
792
793/*
794** Given a pointer into a time zone string, extract a number of seconds,
795** in hh[:mm[:ss]] form, from the string.
796** If any error occurs, return NULL.
797** Otherwise, return a pointer to the first character not part of the number
798** of seconds.
799*/
800
801static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700802getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800803{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700804 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800805
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700806 /*
807 ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
808 ** "M10.4.6/26", which does not conform to Posix,
809 ** but which specifies the equivalent of
810 ** "02:00 on the first Sunday on or after 23 Oct".
811 */
812 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
813 if (strp == NULL)
814 return NULL;
815 *secsp = num * (int_fast32_t) SECSPERHOUR;
816 if (*strp == ':') {
817 ++strp;
818 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
819 if (strp == NULL)
820 return NULL;
821 *secsp += num * SECSPERMIN;
822 if (*strp == ':') {
823 ++strp;
824 /* 'SECSPERMIN' allows for leap seconds. */
825 strp = getnum(strp, &num, 0, SECSPERMIN);
826 if (strp == NULL)
827 return NULL;
828 *secsp += num;
829 }
830 }
831 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832}
833
834/*
835** Given a pointer into a time zone string, extract an offset, in
836** [+-]hh[:mm[:ss]] form, from the string.
837** If any error occurs, return NULL.
838** Otherwise, return a pointer to the first character not part of the time.
839*/
840
841static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700842getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800843{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700844 register bool neg = false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700846 if (*strp == '-') {
847 neg = true;
848 ++strp;
849 } else if (*strp == '+')
850 ++strp;
851 strp = getsecs(strp, offsetp);
852 if (strp == NULL)
853 return NULL; /* illegal time */
854 if (neg)
855 *offsetp = -*offsetp;
856 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800857}
858
859/*
860** Given a pointer into a time zone string, extract a rule in the form
861** date[/time]. See POSIX section 8 for the format of "date" and "time".
862** If a valid rule is not found, return NULL.
863** Otherwise, return a pointer to the first character not part of the rule.
864*/
865
866static const char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700867getrule(const char *strp, register struct rule *const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700869 if (*strp == 'J') {
870 /*
871 ** Julian day.
872 */
873 rulep->r_type = JULIAN_DAY;
874 ++strp;
875 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
876 } else if (*strp == 'M') {
877 /*
878 ** Month, week, day.
879 */
880 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
881 ++strp;
882 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
883 if (strp == NULL)
884 return NULL;
885 if (*strp++ != '.')
886 return NULL;
887 strp = getnum(strp, &rulep->r_week, 1, 5);
888 if (strp == NULL)
889 return NULL;
890 if (*strp++ != '.')
891 return NULL;
892 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
893 } else if (is_digit(*strp)) {
894 /*
895 ** Day of year.
896 */
897 rulep->r_type = DAY_OF_YEAR;
898 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
899 } else return NULL; /* invalid format */
900 if (strp == NULL)
901 return NULL;
902 if (*strp == '/') {
903 /*
904 ** Time specified.
905 */
906 ++strp;
907 strp = getoffset(strp, &rulep->r_time);
908 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
909 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910}
911
912/*
Calin Juravle627d37c2014-02-28 11:46:03 +0000913** Given a year, a rule, and the offset from UT at the time that rule takes
914** effect, calculate the year-relative time that rule takes effect.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915*/
916
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700917static int_fast32_t ATTRIBUTE_PURE
Calin Juravle627d37c2014-02-28 11:46:03 +0000918transtime(const int year, register const struct rule *const rulep,
Calin Juravled8928922014-02-28 12:18:53 +0000919 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800920{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700921 register bool leapyear;
Calin Juravled8928922014-02-28 12:18:53 +0000922 register int_fast32_t value;
923 register int i;
924 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800925
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700926 INITIALIZE(value);
927 leapyear = isleap(year);
928 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700930 case JULIAN_DAY:
931 /*
932 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
933 ** years.
934 ** In non-leap years, or if the day number is 59 or less, just
935 ** add SECSPERDAY times the day number-1 to the time of
936 ** January 1, midnight, to get the day.
937 */
Calin Juravled8928922014-02-28 12:18:53 +0000938 value = (rulep->r_day - 1) * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700939 if (leapyear && rulep->r_day >= 60)
940 value += SECSPERDAY;
941 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800942
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700943 case DAY_OF_YEAR:
944 /*
945 ** n - day of year.
946 ** Just add SECSPERDAY times the day number to the time of
947 ** January 1, midnight, to get the day.
948 */
Calin Juravled8928922014-02-28 12:18:53 +0000949 value = rulep->r_day * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700950 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800951
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700952 case MONTH_NTH_DAY_OF_WEEK:
953 /*
954 ** Mm.n.d - nth "dth day" of month m.
955 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800956
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700957 /*
958 ** Use Zeller's Congruence to get day-of-week of first day of
959 ** month.
960 */
961 m1 = (rulep->r_mon + 9) % 12 + 1;
962 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
963 yy1 = yy0 / 100;
964 yy2 = yy0 % 100;
965 dow = ((26 * m1 - 2) / 10 +
966 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
967 if (dow < 0)
968 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800969
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700970 /*
971 ** "dow" is the day-of-week of the first day of the month. Get
972 ** the day-of-month (zero-origin) of the first "dow" day of the
973 ** month.
974 */
975 d = rulep->r_day - dow;
976 if (d < 0)
977 d += DAYSPERWEEK;
978 for (i = 1; i < rulep->r_week; ++i) {
979 if (d + DAYSPERWEEK >=
980 mon_lengths[leapyear][rulep->r_mon - 1])
981 break;
982 d += DAYSPERWEEK;
983 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700985 /*
986 ** "d" is the day-of-month (zero-origin) of the day we want.
987 */
Calin Juravled8928922014-02-28 12:18:53 +0000988 value = d * SECSPERDAY;
989 for (i = 0; i < rulep->r_mon - 1; ++i)
990 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700991 break;
992 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800993
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700994 /*
Calin Juravled8928922014-02-28 12:18:53 +0000995 ** "value" is the year-relative time of 00:00:00 UT on the day in
996 ** question. To get the year-relative time of the specified local
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700997 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -0700998 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700999 */
1000 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001001}
1002
1003/*
1004** Given a POSIX section 8-style TZ string, fill in the rule tables as
1005** appropriate.
1006*/
1007
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001008static bool
1009tzparse(const char *name, struct state *sp, bool lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001010{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001011 const char * stdname;
1012 const char * dstname;
1013 size_t stdlen;
1014 size_t dstlen;
1015 size_t charcnt;
1016 int_fast32_t stdoffset;
1017 int_fast32_t dstoffset;
1018 register char * cp;
1019 register bool load_ok;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001020
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001021 stdname = name;
1022 if (lastditch) {
1023 stdlen = sizeof gmt - 1;
1024 name += stdlen;
1025 stdoffset = 0;
1026 } else {
1027 if (*name == '<') {
1028 name++;
1029 stdname = name;
1030 name = getqzname(name, '>');
1031 if (*name != '>')
1032 return false;
1033 stdlen = name - stdname;
1034 name++;
1035 } else {
1036 name = getzname(name);
1037 stdlen = name - stdname;
1038 }
1039 if (!stdlen)
1040 return false;
1041 name = getoffset(name, &stdoffset);
1042 if (name == NULL)
1043 return false;
1044 }
1045 charcnt = stdlen + 1;
1046 if (sizeof sp->chars < charcnt)
1047 return false;
1048 load_ok = tzload(TZDEFRULES, sp, false) == 0;
1049 if (!load_ok)
1050 sp->leapcnt = 0; /* so, we're off a little */
1051 if (*name != '\0') {
1052 if (*name == '<') {
1053 dstname = ++name;
1054 name = getqzname(name, '>');
1055 if (*name != '>')
1056 return false;
1057 dstlen = name - dstname;
1058 name++;
1059 } else {
1060 dstname = name;
1061 name = getzname(name);
1062 dstlen = name - dstname; /* length of DST zone name */
1063 }
1064 if (!dstlen)
1065 return false;
1066 charcnt += dstlen + 1;
1067 if (sizeof sp->chars < charcnt)
1068 return false;
1069 if (*name != '\0' && *name != ',' && *name != ';') {
1070 name = getoffset(name, &dstoffset);
1071 if (name == NULL)
1072 return false;
1073 } else dstoffset = stdoffset - SECSPERHOUR;
1074 if (*name == '\0' && !load_ok)
1075 name = TZDEFRULESTRING;
1076 if (*name == ',' || *name == ';') {
1077 struct rule start;
1078 struct rule end;
1079 register int year;
1080 register int yearlim;
1081 register int timecnt;
1082 time_t janfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001084 ++name;
1085 if ((name = getrule(name, &start)) == NULL)
1086 return false;
1087 if (*name++ != ',')
1088 return false;
1089 if ((name = getrule(name, &end)) == NULL)
1090 return false;
1091 if (*name != '\0')
1092 return false;
1093 sp->typecnt = 2; /* standard time and DST */
1094 /*
1095 ** Two transitions per year, from EPOCH_YEAR forward.
1096 */
1097 init_ttinfo(&sp->ttis[0], -dstoffset, true, stdlen + 1);
1098 init_ttinfo(&sp->ttis[1], -stdoffset, false, 0);
1099 sp->defaulttype = 0;
1100 timecnt = 0;
1101 janfirst = 0;
1102 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1103 for (year = EPOCH_YEAR; year < yearlim; year++) {
1104 int_fast32_t
1105 starttime = transtime(year, &start, stdoffset),
1106 endtime = transtime(year, &end, dstoffset);
1107 int_fast32_t
1108 yearsecs = (year_lengths[isleap(year)]
1109 * SECSPERDAY);
1110 bool reversed = endtime < starttime;
1111 if (reversed) {
1112 int_fast32_t swap = starttime;
1113 starttime = endtime;
1114 endtime = swap;
1115 }
1116 if (reversed
1117 || (starttime < endtime
1118 && (endtime - starttime
1119 < (yearsecs
1120 + (stdoffset - dstoffset))))) {
1121 if (TZ_MAX_TIMES - 2 < timecnt)
1122 break;
1123 yearlim = year + YEARSPERREPEAT + 1;
1124 sp->ats[timecnt] = janfirst;
1125 if (increment_overflow_time
1126 (&sp->ats[timecnt], starttime))
1127 break;
1128 sp->types[timecnt++] = reversed;
1129 sp->ats[timecnt] = janfirst;
1130 if (increment_overflow_time
1131 (&sp->ats[timecnt], endtime))
1132 break;
1133 sp->types[timecnt++] = !reversed;
1134 }
1135 if (increment_overflow_time(&janfirst, yearsecs))
1136 break;
1137 }
1138 sp->timecnt = timecnt;
1139 if (!timecnt)
1140 sp->typecnt = 1; /* Perpetual DST. */
1141 } else {
1142 register int_fast32_t theirstdoffset;
1143 register int_fast32_t theirdstoffset;
1144 register int_fast32_t theiroffset;
1145 register bool isdst;
1146 register int i;
1147 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001148
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001149 if (*name != '\0')
1150 return false;
1151 /*
1152 ** Initial values of theirstdoffset and theirdstoffset.
1153 */
1154 theirstdoffset = 0;
1155 for (i = 0; i < sp->timecnt; ++i) {
1156 j = sp->types[i];
1157 if (!sp->ttis[j].tt_isdst) {
1158 theirstdoffset =
1159 -sp->ttis[j].tt_gmtoff;
1160 break;
1161 }
1162 }
1163 theirdstoffset = 0;
1164 for (i = 0; i < sp->timecnt; ++i) {
1165 j = sp->types[i];
1166 if (sp->ttis[j].tt_isdst) {
1167 theirdstoffset =
1168 -sp->ttis[j].tt_gmtoff;
1169 break;
1170 }
1171 }
1172 /*
1173 ** Initially we're assumed to be in standard time.
1174 */
1175 isdst = false;
1176 theiroffset = theirstdoffset;
1177 /*
1178 ** Now juggle transition times and types
1179 ** tracking offsets as you do.
1180 */
1181 for (i = 0; i < sp->timecnt; ++i) {
1182 j = sp->types[i];
1183 sp->types[i] = sp->ttis[j].tt_isdst;
1184 if (sp->ttis[j].tt_ttisgmt) {
1185 /* No adjustment to transition time */
1186 } else {
1187 /*
1188 ** If summer time is in effect, and the
1189 ** transition time was not specified as
1190 ** standard time, add the summer time
1191 ** offset to the transition time;
1192 ** otherwise, add the standard time
1193 ** offset to the transition time.
1194 */
1195 /*
1196 ** Transitions from DST to DDST
1197 ** will effectively disappear since
1198 ** POSIX provides for only one DST
1199 ** offset.
1200 */
1201 if (isdst && !sp->ttis[j].tt_ttisstd) {
1202 sp->ats[i] += dstoffset -
1203 theirdstoffset;
1204 } else {
1205 sp->ats[i] += stdoffset -
1206 theirstdoffset;
1207 }
1208 }
1209 theiroffset = -sp->ttis[j].tt_gmtoff;
1210 if (sp->ttis[j].tt_isdst)
1211 theirdstoffset = theiroffset;
1212 else theirstdoffset = theiroffset;
1213 }
1214 /*
1215 ** Finally, fill in ttis.
1216 */
1217 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1218 init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
1219 sp->typecnt = 2;
1220 sp->defaulttype = 0;
1221 }
1222 } else {
1223 dstlen = 0;
1224 sp->typecnt = 1; /* only standard time */
1225 sp->timecnt = 0;
1226 init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
1227 sp->defaulttype = 0;
1228 }
1229 sp->charcnt = charcnt;
1230 cp = sp->chars;
1231 memcpy(cp, stdname, stdlen);
1232 cp += stdlen;
1233 *cp++ = '\0';
1234 if (dstlen != 0) {
1235 memcpy(cp, dstname, dstlen);
1236 *(cp + dstlen) = '\0';
1237 }
1238 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001239}
1240
1241static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001242gmtload(struct state *const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001244 if (tzload(gmt, sp, true) != 0)
1245 tzparse(gmt, sp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001246}
1247
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001248/* Initialize *SP to a value appropriate for the TZ setting NAME.
1249 Return 0 on success, an errno value on failure. */
1250static int
1251zoneinit(struct state *sp, char const *name)
1252{
1253 if (name && ! name[0]) {
1254 /*
1255 ** User wants it fast rather than right.
1256 */
1257 sp->leapcnt = 0; /* so, we're off a little */
1258 sp->timecnt = 0;
1259 sp->typecnt = 0;
1260 sp->charcnt = 0;
1261 sp->goback = sp->goahead = false;
1262 init_ttinfo(&sp->ttis[0], 0, false, 0);
1263 strcpy(sp->chars, gmt);
1264 sp->defaulttype = 0;
1265 return 0;
1266 } else {
1267 int err = tzload(name, sp, true);
1268 if (err != 0 && name && name[0] != ':' && tzparse(name, sp, false))
1269 err = 0;
1270 if (err == 0)
1271 scrub_abbrs(sp);
1272 return err;
1273 }
1274}
1275
1276static void
1277tzsetlcl(char const *name)
1278{
1279 struct state *sp = lclptr;
1280 int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
1281 if (lcl < 0
1282 ? lcl_is_set < 0
1283 : 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
1284 return;
1285#ifdef ALL_STATE
1286 if (! sp)
1287 lclptr = sp = malloc(sizeof *lclptr);
1288#endif /* defined ALL_STATE */
1289 if (sp) {
1290 if (zoneinit(sp, name) != 0)
1291 zoneinit(sp, "");
1292 if (0 < lcl)
1293 strcpy(lcl_TZname, name);
1294 }
1295 settzname();
1296 lcl_is_set = lcl;
1297}
1298
1299#ifdef STD_INSPIRED
Elliott Hughesce4783c2013-07-12 17:31:11 -07001300void
1301tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001303 if (lock() != 0)
1304 return;
1305 tzsetlcl(NULL);
1306 unlock();
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001307}
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001308#endif
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001309
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001310#if defined(__ANDROID__)
Mark Salyzynd0578942015-10-02 13:15:07 -07001311#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
1312#include <sys/_system_properties.h> // For __system_property_serial.
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001313#endif
Elliott Hughesce4783c2013-07-12 17:31:11 -07001314
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001315static void
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001316tzset_unlocked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001317{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001318#if defined(__ANDROID__)
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001319 // The TZ environment variable is meant to override the system-wide setting.
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001320 const char* name = getenv("TZ");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001321
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001322 // If that's not set, look at the "persist.sys.timezone" system property.
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001323 if (name == NULL) {
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001324 static const prop_info* pi;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001325
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001326 if (!pi) {
1327 pi = __system_property_find("persist.sys.timezone");
Elliott Hughesce4783c2013-07-12 17:31:11 -07001328 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001329 if (pi) {
1330 static char buf[PROP_VALUE_MAX];
1331 static uint32_t s = -1;
1332 static bool ok = false;
1333 uint32_t serial = __system_property_serial(pi);
1334 if (serial != s) {
1335 ok = __system_property_read(pi, 0, buf) > 0;
1336 s = serial;
1337 }
1338 if (ok) {
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001339 // POSIX and Java disagree about the sign in a timezone string. For POSIX, "GMT+3" means
1340 // "3 hours west/behind", but for Java it means "3 hours east/ahead". Since (a) Java is
1341 // the one that matches human expectations and (b) this system property is used directly
1342 // by Java, we flip the sign here to translate from Java to POSIX. http://b/25463955.
1343 if (buf[3] == '-') {
1344 buf[3] = '+';
1345 } else if (buf[3] == '+') {
1346 buf[3] = '-';
1347 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001348 name = buf;
1349 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001350 }
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001351 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001352
Elliott Hughes3e3f4a52016-07-20 17:23:54 -07001353 // If the system property is also not available (because you're running AOSP on a WiFi-only
Elliott Hughesd1c28a32015-11-13 08:38:48 -08001354 // device, say), fall back to GMT.
1355 if (name == NULL) name = gmt;
1356
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001357 tzsetlcl(name);
1358#else
1359 tzsetlcl(getenv("TZ"));
1360#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361}
1362
1363void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001364tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001366 if (lock() != 0)
1367 return;
1368 tzset_unlocked();
1369 unlock();
1370}
1371
1372static void
1373gmtcheck(void)
1374{
1375 static bool gmt_is_set;
1376 if (lock() != 0)
1377 return;
1378 if (! gmt_is_set) {
1379#ifdef ALL_STATE
1380 gmtptr = malloc(sizeof *gmtptr);
1381#endif
1382 if (gmtptr)
1383 gmtload(gmtptr);
1384 gmt_is_set = true;
1385 }
1386 unlock();
1387}
1388
1389#if NETBSD_INSPIRED
1390
1391timezone_t
1392tzalloc(char const *name)
1393{
1394 timezone_t sp = malloc(sizeof *sp);
1395 if (sp) {
1396 int err = zoneinit(sp, name);
1397 if (err != 0) {
1398 free(sp);
1399 errno = err;
1400 return NULL;
1401 }
1402 }
1403 return sp;
1404}
1405
1406void
1407tzfree(timezone_t sp)
1408{
1409 free(sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410}
1411
1412/*
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001413** NetBSD 6.1.4 has ctime_rz, but omit it because POSIX says ctime and
1414** ctime_r are obsolescent and have potential security problems that
1415** ctime_rz would share. Callers can instead use localtime_rz + strftime.
1416**
1417** NetBSD 6.1.4 has tzgetname, but omit it because it doesn't work
1418** in zones with three or more time zone abbreviations.
1419** Callers can instead use localtime_rz + strftime.
1420*/
1421
1422#endif
1423
1424/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425** The easy way to behave "as if no library function calls" localtime
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001426** is to not call it, so we drop its guts into "localsub", which can be
1427** freely called. (And no, the PANS doesn't require the above behavior,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428** but it *is* desirable.)
1429**
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001430** If successful and SETNAME is nonzero,
1431** set the applicable parts of tzname, timezone and altzone;
1432** however, it's OK to omit this step if the time zone is POSIX-compatible,
1433** since in that case tzset should have already done this step correctly.
1434** SETNAME's type is intfast32_t for compatibility with gmtsub,
1435** but it is actually a boolean and its value should be 0 or 1.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436*/
1437
1438/*ARGSUSED*/
1439static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001440localsub(struct state const *sp, time_t const *timep, int_fast32_t setname,
1441 struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001442{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001443 register const struct ttinfo * ttisp;
1444 register int i;
1445 register struct tm * result;
1446 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001447
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001448 if (sp == NULL) {
1449 /* Don't bother to set tzname etc.; tzset has already done it. */
1450 return gmtsub(gmtptr, timep, 0, tmp);
1451 }
1452 if ((sp->goback && t < sp->ats[0]) ||
1453 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1454 time_t newt = t;
1455 register time_t seconds;
1456 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001457
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001458 if (t < sp->ats[0])
1459 seconds = sp->ats[0] - t;
1460 else seconds = t - sp->ats[sp->timecnt - 1];
1461 --seconds;
1462 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1463 seconds = years * AVGSECSPERYEAR;
1464 if (t < sp->ats[0])
1465 newt += seconds;
1466 else newt -= seconds;
1467 if (newt < sp->ats[0] ||
1468 newt > sp->ats[sp->timecnt - 1])
1469 return NULL; /* "cannot happen" */
1470 result = localsub(sp, &newt, setname, tmp);
1471 if (result) {
1472 register int_fast64_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001474 newy = result->tm_year;
1475 if (t < sp->ats[0])
1476 newy -= years;
1477 else newy += years;
1478 if (! (INT_MIN <= newy && newy <= INT_MAX))
1479 return NULL;
1480 result->tm_year = newy;
1481 }
1482 return result;
1483 }
1484 if (sp->timecnt == 0 || t < sp->ats[0]) {
1485 i = sp->defaulttype;
1486 } else {
1487 register int lo = 1;
1488 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001489
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001490 while (lo < hi) {
1491 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001493 if (t < sp->ats[mid])
1494 hi = mid;
1495 else lo = mid + 1;
1496 }
1497 i = (int) sp->types[lo - 1];
1498 }
1499 ttisp = &sp->ttis[i];
1500 /*
1501 ** To get (wrong) behavior that's compatible with System V Release 2.0
1502 ** you'd replace the statement below with
1503 ** t += ttisp->tt_gmtoff;
1504 ** timesub(&t, 0L, sp, tmp);
1505 */
1506 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1507 if (result) {
1508 result->tm_isdst = ttisp->tt_isdst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001509#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001510 result->TM_ZONE = (char *) &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001512 if (setname)
1513 update_tzname_etc(sp, ttisp);
1514 }
1515 return result;
1516}
1517
1518#if NETBSD_INSPIRED
1519
1520struct tm *
1521localtime_rz(struct state *sp, time_t const *timep, struct tm *tmp)
1522{
1523 return localsub(sp, timep, 0, tmp);
1524}
1525
1526#endif
1527
1528static struct tm *
1529localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
1530{
1531 int err = lock();
1532 if (err) {
1533 errno = err;
1534 return NULL;
1535 }
1536 if (setname || !lcl_is_set)
1537 tzset_unlocked();
1538 tmp = localsub(lclptr, timep, setname, tmp);
1539 unlock();
1540 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001541}
1542
1543struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001544localtime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001545{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001546 return localtime_tzset(timep, &tm, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001547}
1548
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001550localtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001551{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001552 return localtime_tzset(timep, tmp, false);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553}
1554
1555/*
1556** gmtsub is to gmtime as localsub is to localtime.
1557*/
1558
1559static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001560gmtsub(struct state const *sp, time_t const *timep, int_fast32_t offset,
1561 struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001562{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001563 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001564
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001565 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001566#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001567 /*
1568 ** Could get fancy here and deliver something such as
1569 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
1570 ** but this is no time for a treasure hunt.
1571 */
1572 tmp->TM_ZONE = ((char *)
1573 (offset ? wildabbr : gmtptr ? gmtptr->chars : gmt));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001574#endif /* defined TM_ZONE */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001575 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001576}
1577
1578/*
1579* Re-entrant version of gmtime.
1580*/
1581
1582struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001583gmtime_r(const time_t *timep, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001584{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001585 gmtcheck();
1586 return gmtsub(gmtptr, timep, 0, tmp);
1587}
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001588
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001589struct tm *
1590gmtime(const time_t *timep)
1591{
1592 return gmtime_r(timep, &tm);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001593}
1594
Elliott Hughes906eb992014-06-18 19:46:25 -07001595#ifdef STD_INSPIRED
1596
1597struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001598offtime(const time_t *timep, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07001599{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001600 gmtcheck();
1601 return gmtsub(gmtptr, timep, offset, &tm);
Elliott Hughes906eb992014-06-18 19:46:25 -07001602}
1603
1604#endif /* defined STD_INSPIRED */
1605
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606/*
1607** Return the number of leap years through the end of the given year
1608** where, to make the math easy, the answer for year zero is defined as zero.
1609*/
1610
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001611static int ATTRIBUTE_PURE
Elliott Hughesce4783c2013-07-12 17:31:11 -07001612leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001613{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001614 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1615 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001616}
1617
1618static struct tm *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001619timesub(const time_t *timep, int_fast32_t offset,
1620 const struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001621{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001622 register const struct lsinfo * lp;
1623 register time_t tdays;
1624 register int idays; /* unsigned would be so 2003 */
1625 register int_fast64_t rem;
1626 int y;
1627 register const int * ip;
1628 register int_fast64_t corr;
1629 register bool hit;
1630 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001632 corr = 0;
1633 hit = false;
1634 i = (sp == NULL) ? 0 : sp->leapcnt;
1635 while (--i >= 0) {
1636 lp = &sp->lsis[i];
1637 if (*timep >= lp->ls_trans) {
1638 if (*timep == lp->ls_trans) {
1639 hit = ((i == 0 && lp->ls_corr > 0) ||
1640 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1641 if (hit)
1642 while (i > 0 &&
1643 sp->lsis[i].ls_trans ==
1644 sp->lsis[i - 1].ls_trans + 1 &&
1645 sp->lsis[i].ls_corr ==
1646 sp->lsis[i - 1].ls_corr + 1) {
1647 ++hit;
1648 --i;
1649 }
1650 }
1651 corr = lp->ls_corr;
1652 break;
1653 }
1654 }
1655 y = EPOCH_YEAR;
1656 tdays = *timep / SECSPERDAY;
1657 rem = *timep % SECSPERDAY;
1658 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1659 int newy;
1660 register time_t tdelta;
1661 register int idelta;
1662 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001663
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001664 tdelta = tdays / DAYSPERLYEAR;
1665 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1666 && tdelta <= INT_MAX))
1667 goto out_of_range;
1668 idelta = tdelta;
1669 if (idelta == 0)
1670 idelta = (tdays < 0) ? -1 : 1;
1671 newy = y;
1672 if (increment_overflow(&newy, idelta))
1673 goto out_of_range;
1674 leapdays = leaps_thru_end_of(newy - 1) -
1675 leaps_thru_end_of(y - 1);
1676 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1677 tdays -= leapdays;
1678 y = newy;
1679 }
1680 /*
1681 ** Given the range, we can now fearlessly cast...
1682 */
1683 idays = tdays;
1684 rem += offset - corr;
1685 while (rem < 0) {
1686 rem += SECSPERDAY;
1687 --idays;
1688 }
1689 while (rem >= SECSPERDAY) {
1690 rem -= SECSPERDAY;
1691 ++idays;
1692 }
1693 while (idays < 0) {
1694 if (increment_overflow(&y, -1))
1695 goto out_of_range;
1696 idays += year_lengths[isleap(y)];
1697 }
1698 while (idays >= year_lengths[isleap(y)]) {
1699 idays -= year_lengths[isleap(y)];
1700 if (increment_overflow(&y, 1))
1701 goto out_of_range;
1702 }
1703 tmp->tm_year = y;
1704 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1705 goto out_of_range;
1706 tmp->tm_yday = idays;
1707 /*
1708 ** The "extra" mods below avoid overflow problems.
1709 */
1710 tmp->tm_wday = EPOCH_WDAY +
1711 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1712 (DAYSPERNYEAR % DAYSPERWEEK) +
1713 leaps_thru_end_of(y - 1) -
1714 leaps_thru_end_of(EPOCH_YEAR - 1) +
1715 idays;
1716 tmp->tm_wday %= DAYSPERWEEK;
1717 if (tmp->tm_wday < 0)
1718 tmp->tm_wday += DAYSPERWEEK;
1719 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1720 rem %= SECSPERHOUR;
1721 tmp->tm_min = (int) (rem / SECSPERMIN);
1722 /*
1723 ** A positive leap second requires a special
1724 ** representation. This uses "... ??:59:60" et seq.
1725 */
1726 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1727 ip = mon_lengths[isleap(y)];
1728 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1729 idays -= ip[tmp->tm_mon];
1730 tmp->tm_mday = (int) (idays + 1);
1731 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732#ifdef TM_GMTOFF
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001733 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001734#endif /* defined TM_GMTOFF */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001735 return tmp;
1736
1737 out_of_range:
1738 errno = EOVERFLOW;
1739 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740}
1741
1742char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001743ctime(const time_t *timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001744{
1745/*
1746** Section 4.12.3.2 of X3.159-1989 requires that
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001747** The ctime function converts the calendar time pointed to by timer
1748** to local time in the form of a string. It is equivalent to
1749** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001750*/
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001751 struct tm *tmp = localtime(timep);
1752 return tmp ? asctime(tmp) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001753}
1754
1755char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001756ctime_r(const time_t *timep, char *buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001757{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001758 struct tm mytm;
1759 struct tm *tmp = localtime_r(timep, &mytm);
1760 return tmp ? asctime_r(tmp, buf) : NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001761}
1762
1763/*
1764** Adapted from code provided by Robert Elz, who writes:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001765** The "best" way to do mktime I think is based on an idea of Bob
1766** Kridle's (so its said...) from a long time ago.
1767** It does a binary search of the time_t space. Since time_t's are
1768** just 32 bits, its a max of 32 iterations (even at 64 bits it
1769** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001770*/
1771
1772#ifndef WRONG
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001773#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001774#endif /* !defined WRONG */
1775
1776/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001777** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001778*/
1779
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001780static bool
1781increment_overflow(int *ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001782{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001783 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001784
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001785 /*
1786 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1787 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1788 ** If i < 0 there can only be overflow if i + j < INT_MIN
1789 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1790 */
1791 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1792 return true;
1793 *ip += j;
1794 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001795}
1796
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001797static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001798increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001799{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001800 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001801
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001802 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1803 return true;
1804 *lp += m;
1805 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001806}
1807
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001808static bool
Calin Juravle627d37c2014-02-28 11:46:03 +00001809increment_overflow_time(time_t *tp, int_fast32_t j)
1810{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001811 /*
1812 ** This is like
1813 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1814 ** except that it does the right thing even if *tp + j would overflow.
1815 */
1816 if (! (j < 0
1817 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1818 : *tp <= time_t_max - j))
1819 return true;
1820 *tp += j;
1821 return false;
Calin Juravle627d37c2014-02-28 11:46:03 +00001822}
1823
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001824static bool
Elliott Hughesce4783c2013-07-12 17:31:11 -07001825normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001826{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001827 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001828
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001829 tensdelta = (*unitsptr >= 0) ?
1830 (*unitsptr / base) :
1831 (-1 - (-1 - *unitsptr) / base);
1832 *unitsptr -= tensdelta * base;
1833 return increment_overflow(tensptr, tensdelta);
1834}
1835
1836static bool
1837normalize_overflow32(int_fast32_t *tensptr, int *unitsptr, int base)
1838{
1839 register int tensdelta;
1840
1841 tensdelta = (*unitsptr >= 0) ?
1842 (*unitsptr / base) :
1843 (-1 - (-1 - *unitsptr) / base);
1844 *unitsptr -= tensdelta * base;
1845 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001846}
1847
1848static int
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001849tmcomp(register const struct tm *const atmp,
1850 register const struct tm *const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001851{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001852 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001853
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001854 if (atmp->tm_year != btmp->tm_year)
1855 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1856 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1857 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1858 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1859 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1860 result = atmp->tm_sec - btmp->tm_sec;
1861 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001862}
1863
1864static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001865time2sub(struct tm *const tmp,
1866 struct tm *(*funcp)(struct state const *, time_t const *,
1867 int_fast32_t, struct tm *),
1868 struct state const *sp,
1869 const int_fast32_t offset,
1870 bool *okayp,
1871 bool do_norm_secs)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001872{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001873 register int dir;
1874 register int i, j;
1875 register int saved_seconds;
1876 register int_fast32_t li;
1877 register time_t lo;
1878 register time_t hi;
1879 int_fast32_t y;
1880 time_t newt;
1881 time_t t;
1882 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001883
Elliott Hughes9fb22a32015-10-07 17:13:40 -07001884 *okayp = false;
1885 yourtm = *tmp;
1886 if (do_norm_secs) {
1887 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1888 SECSPERMIN))
1889 return WRONG;
1890 }
1891 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1892 return WRONG;
1893 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1894 return WRONG;
1895 y = yourtm.tm_year;
1896 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
1897 return WRONG;
1898 /*
1899 ** Turn y into an actual year number for now.
1900 ** It is converted back to an offset from TM_YEAR_BASE later.
1901 */
1902 if (increment_overflow32(&y, TM_YEAR_BASE))
1903 return WRONG;
1904 while (yourtm.tm_mday <= 0) {
1905 if (increment_overflow32(&y, -1))
1906 return WRONG;
1907 li = y + (1 < yourtm.tm_mon);
1908 yourtm.tm_mday += year_lengths[isleap(li)];
1909 }
1910 while (yourtm.tm_mday > DAYSPERLYEAR) {
1911 li = y + (1 < yourtm.tm_mon);
1912 yourtm.tm_mday -= year_lengths[isleap(li)];
1913 if (increment_overflow32(&y, 1))
1914 return WRONG;
1915 }
1916 for ( ; ; ) {
1917 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1918 if (yourtm.tm_mday <= i)
1919 break;
1920 yourtm.tm_mday -= i;
1921 if (++yourtm.tm_mon >= MONSPERYEAR) {
1922 yourtm.tm_mon = 0;
1923 if (increment_overflow32(&y, 1))
1924 return WRONG;
1925 }
1926 }
1927 if (increment_overflow32(&y, -TM_YEAR_BASE))
1928 return WRONG;
1929 if (! (INT_MIN <= y && y <= INT_MAX))
1930 return WRONG;
1931 yourtm.tm_year = y;
1932 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1933 saved_seconds = 0;
1934 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1935 /*
1936 ** We can't set tm_sec to 0, because that might push the
1937 ** time below the minimum representable time.
1938 ** Set tm_sec to 59 instead.
1939 ** This assumes that the minimum representable time is
1940 ** not in the same minute that a leap second was deleted from,
1941 ** which is a safer assumption than using 58 would be.
1942 */
1943 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1944 return WRONG;
1945 saved_seconds = yourtm.tm_sec;
1946 yourtm.tm_sec = SECSPERMIN - 1;
1947 } else {
1948 saved_seconds = yourtm.tm_sec;
1949 yourtm.tm_sec = 0;
1950 }
1951 /*
1952 ** Do a binary search (this works whatever time_t's type is).
1953 */
1954 lo = time_t_min;
1955 hi = time_t_max;
1956 for ( ; ; ) {
1957 t = lo / 2 + hi / 2;
1958 if (t < lo)
1959 t = lo;
1960 else if (t > hi)
1961 t = hi;
1962 if (! funcp(sp, &t, offset, &mytm)) {
1963 /*
1964 ** Assume that t is too extreme to be represented in
1965 ** a struct tm; arrange things so that it is less
1966 ** extreme on the next pass.
1967 */
1968 dir = (t > 0) ? 1 : -1;
1969 } else dir = tmcomp(&mytm, &yourtm);
1970 if (dir != 0) {
1971 if (t == lo) {
1972 if (t == time_t_max)
1973 return WRONG;
1974 ++t;
1975 ++lo;
1976 } else if (t == hi) {
1977 if (t == time_t_min)
1978 return WRONG;
1979 --t;
1980 --hi;
1981 }
1982 if (lo > hi)
1983 return WRONG;
1984 if (dir > 0)
1985 hi = t;
1986 else lo = t;
1987 continue;
1988 }
1989#if defined TM_GMTOFF && ! UNINIT_TRAP
1990 if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
1991 && (yourtm.TM_GMTOFF < 0
1992 ? (-SECSPERDAY <= yourtm.TM_GMTOFF
1993 && (mytm.TM_GMTOFF <=
1994 (SMALLEST (INT_FAST32_MAX, LONG_MAX)
1995 + yourtm.TM_GMTOFF)))
1996 : (yourtm.TM_GMTOFF <= SECSPERDAY
1997 && ((BIGGEST (INT_FAST32_MIN, LONG_MIN)
1998 + yourtm.TM_GMTOFF)
1999 <= mytm.TM_GMTOFF)))) {
2000 /* MYTM matches YOURTM except with the wrong UTC offset.
2001 YOURTM.TM_GMTOFF is plausible, so try it instead.
2002 It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
2003 since the guess gets checked. */
2004 time_t altt = t;
2005 int_fast32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
2006 if (!increment_overflow_time(&altt, diff)) {
2007 struct tm alttm;
2008 if (funcp(sp, &altt, offset, &alttm)
2009 && alttm.tm_isdst == mytm.tm_isdst
2010 && alttm.TM_GMTOFF == yourtm.TM_GMTOFF
2011 && tmcomp(&alttm, &yourtm) == 0) {
2012 t = altt;
2013 mytm = alttm;
2014 }
2015 }
2016 }
2017#endif
2018 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
2019 break;
2020 /*
2021 ** Right time, wrong type.
2022 ** Hunt for right time, right type.
2023 ** It's okay to guess wrong since the guess
2024 ** gets checked.
2025 */
2026 if (sp == NULL)
2027 return WRONG;
2028 for (i = sp->typecnt - 1; i >= 0; --i) {
2029 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2030 continue;
2031 for (j = sp->typecnt - 1; j >= 0; --j) {
2032 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2033 continue;
2034 newt = t + sp->ttis[j].tt_gmtoff -
2035 sp->ttis[i].tt_gmtoff;
2036 if (! funcp(sp, &newt, offset, &mytm))
2037 continue;
2038 if (tmcomp(&mytm, &yourtm) != 0)
2039 continue;
2040 if (mytm.tm_isdst != yourtm.tm_isdst)
2041 continue;
2042 /*
2043 ** We have a match.
2044 */
2045 t = newt;
2046 goto label;
2047 }
2048 }
2049 return WRONG;
2050 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002051label:
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002052 newt = t + saved_seconds;
2053 if ((newt < t) != (saved_seconds < 0))
2054 return WRONG;
2055 t = newt;
2056 if (funcp(sp, &t, offset, tmp))
2057 *okayp = true;
2058 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002059}
2060
2061static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002062time2(struct tm * const tmp,
2063 struct tm *(*funcp)(struct state const *, time_t const *,
2064 int_fast32_t, struct tm *),
2065 struct state const *sp,
Elliott Hughesce4783c2013-07-12 17:31:11 -07002066 const int_fast32_t offset,
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002067 bool *okayp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002068{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002069 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002070
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002071 /*
2072 ** First try without normalization of seconds
2073 ** (in case tm_sec contains a value associated with a leap second).
2074 ** If that fails, try with normalization of seconds.
2075 */
2076 t = time2sub(tmp, funcp, sp, offset, okayp, false);
2077 return *okayp ? t : time2sub(tmp, funcp, sp, offset, okayp, true);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002078}
2079
2080static time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002081time1(struct tm *const tmp,
2082 struct tm *(*funcp) (struct state const *, time_t const *,
2083 int_fast32_t, struct tm *),
2084 struct state const *sp,
2085 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002086{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002087 register time_t t;
2088 register int samei, otheri;
2089 register int sameind, otherind;
2090 register int i;
2091 register int nseen;
2092 char seen[TZ_MAX_TYPES];
2093 unsigned char types[TZ_MAX_TYPES];
2094 bool okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002095
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002096 if (tmp == NULL) {
2097 errno = EINVAL;
2098 return WRONG;
2099 }
2100 if (tmp->tm_isdst > 1)
2101 tmp->tm_isdst = 1;
2102 t = time2(tmp, funcp, sp, offset, &okay);
2103 if (okay)
2104 return t;
2105 if (tmp->tm_isdst < 0)
Elliott Hughes906eb992014-06-18 19:46:25 -07002106#ifdef PCTS
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002107 /*
2108 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
2109 */
2110 tmp->tm_isdst = 0; /* reset to std and try again */
Elliott Hughes906eb992014-06-18 19:46:25 -07002111#else
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002112 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002113#endif /* !defined PCTS */
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002114 /*
2115 ** We're supposed to assume that somebody took a time of one type
2116 ** and did some math on it that yielded a "struct tm" that's bad.
2117 ** We try to divine the type they started from and adjust to the
2118 ** type they need.
2119 */
2120 if (sp == NULL)
2121 return WRONG;
2122 for (i = 0; i < sp->typecnt; ++i)
2123 seen[i] = false;
2124 nseen = 0;
2125 for (i = sp->timecnt - 1; i >= 0; --i)
2126 if (!seen[sp->types[i]]) {
2127 seen[sp->types[i]] = true;
2128 types[nseen++] = sp->types[i];
2129 }
2130 for (sameind = 0; sameind < nseen; ++sameind) {
2131 samei = types[sameind];
2132 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2133 continue;
2134 for (otherind = 0; otherind < nseen; ++otherind) {
2135 otheri = types[otherind];
2136 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2137 continue;
2138 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2139 sp->ttis[samei].tt_gmtoff;
2140 tmp->tm_isdst = !tmp->tm_isdst;
2141 t = time2(tmp, funcp, sp, offset, &okay);
2142 if (okay)
2143 return t;
2144 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2145 sp->ttis[samei].tt_gmtoff;
2146 tmp->tm_isdst = !tmp->tm_isdst;
2147 }
2148 }
2149 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002150}
2151
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002152static time_t
2153mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
2154{
2155 if (sp)
2156 return time1(tmp, localsub, sp, setname);
2157 else {
2158 gmtcheck();
2159 return time1(tmp, gmtsub, gmtptr, 0);
2160 }
2161}
2162
2163#if NETBSD_INSPIRED
2164
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002165time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002166mktime_z(struct state *sp, struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002167{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002168 return mktime_tzname(sp, tmp, false);
2169}
2170
2171#endif
2172
2173time_t
2174mktime(struct tm *tmp)
2175{
2176 time_t t;
2177 int err = lock();
2178 if (err) {
2179 errno = err;
2180 return -1;
2181 }
2182 tzset_unlocked();
2183 t = mktime_tzname(lclptr, tmp, true);
2184 unlock();
2185 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002186}
2187
2188#ifdef STD_INSPIRED
2189
2190time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002191timelocal(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002192{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002193 if (tmp != NULL)
2194 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2195 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002196}
2197
2198time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002199timegm(struct tm *tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002200{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002201 return timeoff(tmp, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002202}
2203
Elliott Hughes906eb992014-06-18 19:46:25 -07002204time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002205timeoff(struct tm *tmp, long offset)
Elliott Hughes906eb992014-06-18 19:46:25 -07002206{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002207 if (tmp)
2208 tmp->tm_isdst = 0;
2209 gmtcheck();
2210 return time1(tmp, gmtsub, gmtptr, offset);
Elliott Hughes906eb992014-06-18 19:46:25 -07002211}
2212
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002213#endif /* defined STD_INSPIRED */
2214
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002215/*
2216** XXX--is the below the right way to conditionalize??
2217*/
2218
2219#ifdef STD_INSPIRED
2220
2221/*
2222** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2223** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2224** is not the case if we are accounting for leap seconds.
2225** So, we provide the following conversion routines for use
2226** when exchanging timestamps with POSIX conforming systems.
2227*/
2228
Elliott Hughesce4783c2013-07-12 17:31:11 -07002229static int_fast64_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002230leapcorr(struct state const *sp, time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002231{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002232 register struct lsinfo const * lp;
2233 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002234
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002235 i = sp->leapcnt;
2236 while (--i >= 0) {
2237 lp = &sp->lsis[i];
2238 if (t >= lp->ls_trans)
2239 return lp->ls_corr;
2240 }
2241 return 0;
2242}
2243
2244NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2245time2posix_z(struct state *sp, time_t t)
2246{
2247 return t - leapcorr(sp, t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002248}
2249
2250time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002251time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002252{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002253 int err = lock();
2254 if (err) {
2255 errno = err;
2256 return -1;
2257 }
2258 if (!lcl_is_set)
2259 tzset_unlocked();
2260 if (lclptr)
2261 t = time2posix_z(lclptr, t);
2262 unlock();
2263 return t;
2264}
2265
2266NETBSD_INSPIRED_EXTERN time_t ATTRIBUTE_PURE
2267posix2time_z(struct state *sp, time_t t)
2268{
2269 time_t x;
2270 time_t y;
2271 /*
2272 ** For a positive leap second hit, the result
2273 ** is not unique. For a negative leap second
2274 ** hit, the corresponding time doesn't exist,
2275 ** so we return an adjacent second.
2276 */
2277 x = t + leapcorr(sp, t);
2278 y = x - leapcorr(sp, x);
2279 if (y < t) {
2280 do {
2281 x++;
2282 y = x - leapcorr(sp, x);
2283 } while (y < t);
2284 x -= y != t;
2285 } else if (y > t) {
2286 do {
2287 --x;
2288 y = x - leapcorr(sp, x);
2289 } while (y > t);
2290 x += y != t;
2291 }
2292 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002293}
2294
2295time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002296posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002297{
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002298 int err = lock();
2299 if (err) {
2300 errno = err;
2301 return -1;
2302 }
2303 if (!lcl_is_set)
2304 tzset_unlocked();
2305 if (lclptr)
2306 t = posix2time_z(lclptr, t);
2307 unlock();
2308 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002309}
2310
2311#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002312
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002313#ifdef time_tz
2314
2315/* Convert from the underlying system's time_t to the ersatz time_tz,
2316 which is called 'time_t' in this file. */
2317
2318time_t
2319time(time_t *p)
2320{
2321 time_t r = sys_time(0);
2322 if (p)
2323 *p = r;
2324 return r;
2325}
2326
2327#endif
2328
Elliott Hughesce4783c2013-07-12 17:31:11 -07002329// BEGIN android-added
2330
Elliott Hughes1c295722012-10-19 18:13:15 -07002331#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002332#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002333#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002334
Elliott Hughescf178bf2013-09-18 19:25:28 -07002335static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002336 const char* olson_id) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002337 const char* path_prefix = getenv(path_prefix_variable);
2338 if (path_prefix == NULL) {
2339 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2340 return -1;
2341 }
Elliott Hughes329103d2014-04-25 16:55:04 -07002342 size_t path_length = strlen(path_prefix) + 1 + strlen(path_suffix) + 1;
2343 char* path = malloc(path_length);
2344 if (path == NULL) {
2345 fprintf(stderr, "%s: couldn't allocate %zu-byte path\n", __FUNCTION__, path_length);
2346 return -1;
2347 }
2348 snprintf(path, path_length, "%s/%s", path_prefix, path_suffix);
Elliott Hughes1c295722012-10-19 18:13:15 -07002349 int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
Elliott Hughesd23af232012-10-17 16:30:47 -07002350 if (fd == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002351 free(path);
Elliott Hughes1c295722012-10-19 18:13:15 -07002352 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002353 }
2354
2355 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002356 // int index_offset
2357 // int data_offset
2358 // int zonetab_offset
2359 struct bionic_tzdata_header {
2360 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002361 int32_t index_offset;
2362 int32_t data_offset;
2363 int32_t zonetab_offset;
2364 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002365 memset(&header, 0, sizeof(header));
2366 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2367 if (bytes_read != sizeof(header)) {
2368 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2369 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002370 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002371 close(fd);
2372 return -1;
2373 }
2374
2375 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002376 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2377 __FUNCTION__, path, header.tzdata_version);
Elliott Hughes329103d2014-04-25 16:55:04 -07002378 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002379 close(fd);
2380 return -1;
2381 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002382
2383#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002384 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002385 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2386 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2387 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2388#endif
2389
2390 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002391 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2392 __FUNCTION__, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002393 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002394 close(fd);
2395 return -1;
2396 }
2397
2398 off_t specific_zone_offset = -1;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002399 ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
2400 char* index = malloc(index_size);
Elliott Hughes329103d2014-04-25 16:55:04 -07002401 if (index == NULL) {
2402 fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n",
2403 __FUNCTION__, index_size, path);
2404 free(path);
2405 close(fd);
2406 return -1;
2407 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002408 if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) {
2409 fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
2410 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002411 free(path);
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002412 free(index);
2413 close(fd);
2414 return -1;
2415 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002416
Elliott Hughes1c295722012-10-19 18:13:15 -07002417 static const size_t NAME_LENGTH = 40;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002418 struct index_entry_t {
2419 char buf[NAME_LENGTH];
2420 int32_t start;
2421 int32_t length;
Elliott Hughes13bab432014-08-06 15:23:11 -07002422 int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L).
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002423 };
Elliott Hughese0175ca2013-03-14 14:38:08 -07002424
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002425 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(struct index_entry_t);
2426 struct index_entry_t* entry = (struct index_entry_t*) index;
Elliott Hughese0175ca2013-03-14 14:38:08 -07002427 for (size_t i = 0; i < id_count; ++i) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002428 char this_id[NAME_LENGTH + 1];
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002429 memcpy(this_id, entry->buf, NAME_LENGTH);
Elliott Hughes1c295722012-10-19 18:13:15 -07002430 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002431
2432 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002433 specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset);
Elliott Hughesd23af232012-10-17 16:30:47 -07002434 break;
2435 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002436
2437 ++entry;
Elliott Hughesd23af232012-10-17 16:30:47 -07002438 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002439 free(index);
Elliott Hughesd23af232012-10-17 16:30:47 -07002440
2441 if (specific_zone_offset == -1) {
Elliott Hughes329103d2014-04-25 16:55:04 -07002442 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002443 close(fd);
2444 return -1;
2445 }
2446
2447 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002448 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2449 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002450 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002451 close(fd);
2452 return -1;
2453 }
2454
Elliott Hughese7aaad82013-04-25 14:02:59 -07002455 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2456
Elliott Hughes329103d2014-04-25 16:55:04 -07002457 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002458 return fd;
2459}
Elliott Hughes1c295722012-10-19 18:13:15 -07002460
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002461static int __bionic_open_tzdata(const char* olson_id) {
2462 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata", olson_id);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002463 if (fd < 0) {
Elliott Hughes9fb22a32015-10-07 17:13:40 -07002464 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id);
Neil Fuller1f95ffe2015-03-02 17:43:42 +00002465 if (fd == -2) {
2466 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2467 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2468 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
2469 }
Elliott Hughes1c295722012-10-19 18:13:15 -07002470 }
2471 return fd;
2472}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002473
Elliott Hughesce4783c2013-07-12 17:31:11 -07002474// END android-added