blob: 4ba296897349f9671557e8d9ec57448701390a39 [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
13#include "private.h"
14#include "tzfile.h"
15#include "fcntl.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080017#ifndef TZ_ABBR_MAX_LEN
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070018#define TZ_ABBR_MAX_LEN 16
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080019#endif /* !defined TZ_ABBR_MAX_LEN */
20
21#ifndef TZ_ABBR_CHAR_SET
22#define TZ_ABBR_CHAR_SET \
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070023 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080024#endif /* !defined TZ_ABBR_CHAR_SET */
25
26#ifndef TZ_ABBR_ERR_CHAR
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070027#define TZ_ABBR_ERR_CHAR '_'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080028#endif /* !defined TZ_ABBR_ERR_CHAR */
29
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030/*
31** SunOS 4.1.1 headers lack O_BINARY.
32*/
33
34#ifdef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070035#define OPEN_MODE (O_RDONLY | O_BINARY)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#endif /* defined O_BINARY */
37#ifndef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070038#define OPEN_MODE O_RDONLY
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#endif /* !defined O_BINARY */
40
41#if 0
42# define XLOG(xx) printf xx , fflush(stdout)
43#else
44# define XLOG(x) do{}while (0)
45#endif
46
Elliott Hughesce4783c2013-07-12 17:31:11 -070047/* BEGIN android-added: thread-safety. */
48#include <pthread.h>
49static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER;
50static inline void _tzLock(void) { pthread_mutex_lock(&_tzMutex); }
51static inline void _tzUnlock(void) { pthread_mutex_unlock(&_tzMutex); }
52/* END android-added */
David 'Digit' Turner2093d352009-09-09 17:41:59 -070053
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#ifndef WILDABBR
55/*
56** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070057** 1. They might reference tzname[0] before calling tzset (explicitly
58** or implicitly).
59** 2. They might reference tzname[1] before calling tzset (explicitly
60** or implicitly).
61** 3. They might reference tzname[1] after setting to a time zone
62** in which Daylight Saving Time is never observed.
63** 4. They might reference tzname[0] after setting to a time zone
64** in which Standard Time is never observed.
65** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066** What's best to do in the above cases is open to debate;
67** for now, we just set things up so that in any of the five cases
68** WILDABBR is used. Another possibility: initialize tzname[0] to the
69** string "tzname[0] used before set", and similarly for the other cases.
70** And another: initialize tzname[0] to "ERA", with an explanation in the
71** manual page of what this "time zone abbreviation" means (doing this so
72** that tzname[0] has the "normal" length of three characters).
73*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070074#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075#endif /* !defined WILDABBR */
76
Elliott Hughes906eb992014-06-18 19:46:25 -070077static const char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Calin Juravled8928922014-02-28 12:18:53 +000079static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
81/*
82** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
83** We default to US rules as of 1999-08-17.
84** POSIX 1003.1 section 8.1.1 says that the default DST rules are
85** implementation dependent; for historical reasons, US rules are a
86** common default.
87*/
88#ifndef TZDEFRULESTRING
89#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
90#endif /* !defined TZDEFDST */
91
Calin Juravled8928922014-02-28 12:18:53 +000092struct ttinfo { /* time type information */
Elliott Hughese0d0b152013-09-27 00:04:30 -070093 int_fast32_t tt_gmtoff; /* UT offset in seconds */
Elliott Hughesce4783c2013-07-12 17:31:11 -070094 int tt_isdst; /* used to set tm_isdst */
95 int tt_abbrind; /* abbreviation list index */
96 int tt_ttisstd; /* TRUE if transition is std time */
Elliott Hughese0d0b152013-09-27 00:04:30 -070097 int tt_ttisgmt; /* TRUE if transition is UT */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098};
99
Calin Juravled8928922014-02-28 12:18:53 +0000100struct lsinfo { /* leap second information */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700101 time_t ls_trans; /* transition time */
102 int_fast64_t ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103};
104
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700105#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
107#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700108#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#endif /* defined TZNAME_MAX */
110#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700111#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112#endif /* !defined TZNAME_MAX */
113
114struct state {
Calin Juravled8928922014-02-28 12:18:53 +0000115 int leapcnt;
116 int timecnt;
117 int typecnt;
118 int charcnt;
119 int goback;
120 int goahead;
121 time_t ats[TZ_MAX_TIMES];
122 unsigned char types[TZ_MAX_TIMES];
123 struct ttinfo ttis[TZ_MAX_TYPES];
124 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
125 (2 * (MY_TZNAME_MAX + 1)))];
126 struct lsinfo lsis[TZ_MAX_LEAPS];
127 int defaulttype; /* for early times or if no transitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128};
129
130struct rule {
Elliott Hughes906eb992014-06-18 19:46:25 -0700131 int r_type; /* type of rule; see below */
Calin Juravled8928922014-02-28 12:18:53 +0000132 int r_day; /* day number of rule */
133 int r_week; /* week number of rule */
134 int r_mon; /* month number of rule */
135 int_fast32_t r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136};
137
Elliott Hughes906eb992014-06-18 19:46:25 -0700138#define JULIAN_DAY 0 /* Jn = Julian day */
139#define DAY_OF_YEAR 1 /* n = day of year */
140#define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d = month, week, day of week */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
142/*
143** Prototypes for static functions.
144*/
145
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700146/* NOTE: all internal functions assume that _tzLock() was already called */
147
Elliott Hughesd23af232012-10-17 16:30:47 -0700148static int __bionic_open_tzdata(const char*, int*);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700149static int_fast32_t detzcode(const char * codep);
Calin Juravled8928922014-02-28 12:18:53 +0000150static int_fast64_t detzcode64(const char * codep);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700151static int differ_by_repeat(time_t t1, time_t t0);
152static const char * getzname(const char * strp) ATTRIBUTE_PURE;
153static const char * getqzname(const char * strp, const int delim)
154 ATTRIBUTE_PURE;
155static const char * getnum(const char * strp, int * nump, int min,
156 int max);
157static const char * getsecs(const char * strp, int_fast32_t * secsp);
158static const char * getoffset(const char * strp, int_fast32_t * offsetp);
159static const char * getrule(const char * strp, struct rule * rulep);
160static void gmtload(struct state * sp);
Elliott Hughes906eb992014-06-18 19:46:25 -0700161static struct tm * gmtsub(const time_t * timep, int_fast32_t offset,
Elliott Hughesce4783c2013-07-12 17:31:11 -0700162 struct tm * tmp, const struct state * sp); // android-changed: added sp.
163static struct tm * localsub(const time_t * timep, int_fast32_t offset,
164 struct tm * tmp, const struct state * sp); // android-changed: added sp.
165static int increment_overflow(int * number, int delta);
166static int leaps_thru_end_of(int y) ATTRIBUTE_PURE;
167static int increment_overflow32(int_fast32_t * number, int delta);
Calin Juravled8928922014-02-28 12:18:53 +0000168static int increment_overflow_time(time_t *t, int_fast32_t delta);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700169static int normalize_overflow32(int_fast32_t * tensptr,
170 int * unitsptr, int base);
171static int normalize_overflow(int * tensptr, int * unitsptr,
172 int base);
173static void settzname(void);
174static time_t time1(struct tm * tmp,
175 struct tm * (*funcp)(const time_t *,
176 int_fast32_t, struct tm *, const struct state *), // android-changed: added state*.
Elliott Hughes906eb992014-06-18 19:46:25 -0700177 int_fast32_t, const struct state * sp); // android-changed: added sp.
178static time_t time2(struct tm * tmp,
179 struct tm * (*funcp)(const time_t *,
Elliott Hughesce4783c2013-07-12 17:31:11 -0700180 int_fast32_t, struct tm*, const struct state *), // android-changed: added state*.
181 int_fast32_t offset, int * okayp, const struct state * sp); // android-changed: added sp.
182static time_t time2sub(struct tm *tmp,
183 struct tm * (*funcp) (const time_t *,
184 int_fast32_t, struct tm*, const struct state *), // android-changed: added state*.
185 int_fast32_t offset, int * okayp, int do_norm_secs, const struct state * sp); // android-change: added sp.
186static struct tm * timesub(const time_t * timep, int_fast32_t offset,
187 const struct state * sp, struct tm * tmp);
188static int tmcomp(const struct tm * atmp,
189 const struct tm * btmp);
Calin Juravled8928922014-02-28 12:18:53 +0000190static int_fast32_t transtime(int year, const struct rule * rulep,
191 int_fast32_t offset)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700192 ATTRIBUTE_PURE;
Calin Juravled8928922014-02-28 12:18:53 +0000193static int typesequiv(const struct state * sp, int a, int b);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700194static int tzload(const char * name, struct state * sp,
195 int doextend);
196static int tzparse(const char * name, struct state * sp,
197 int lastditch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
199#ifdef ALL_STATE
Calin Juravled8928922014-02-28 12:18:53 +0000200static struct state * lclptr;
201static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202#endif /* defined ALL_STATE */
203
204#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700205static struct state lclmem;
206static struct state gmtmem;
207#define lclptr (&lclmem)
208#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209#endif /* State Farm */
210
211#ifndef TZ_STRLEN_MAX
212#define TZ_STRLEN_MAX 255
213#endif /* !defined TZ_STRLEN_MAX */
214
Calin Juravled8928922014-02-28 12:18:53 +0000215static char lcl_TZname[TZ_STRLEN_MAX + 1];
216static int lcl_is_set;
217static int gmt_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Calin Juravled8928922014-02-28 12:18:53 +0000219char * tzname[2] = {
Elliott Hughes906eb992014-06-18 19:46:25 -0700220 (char *) wildabbr,
221 (char *) wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222};
223
224/*
225** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700226** Except for the strftime function, these functions [asctime,
227** ctime, gmtime, localtime] return values in one of two static
228** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229** Thanks to Paul Eggert for noting this.
230*/
231
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700232static struct tm tmGlobal;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
234#ifdef USG_COMPAT
Calin Juravled8928922014-02-28 12:18:53 +0000235long timezone = 0;
236int daylight = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237#endif /* defined USG_COMPAT */
238
239#ifdef ALTZONE
Calin Juravled8928922014-02-28 12:18:53 +0000240long altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241#endif /* defined ALTZONE */
242
Elliott Hughesce4783c2013-07-12 17:31:11 -0700243static int_fast32_t
244detzcode(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245{
Calin Juravled8928922014-02-28 12:18:53 +0000246 register int_fast32_t result;
247 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Calin Juravled8928922014-02-28 12:18:53 +0000249 result = (codep[0] & 0x80) ? -1 : 0;
250 for (i = 0; i < 4; ++i)
251 result = (result << 8) | (codep[i] & 0xff);
252 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
Calin Juravle627d37c2014-02-28 11:46:03 +0000255static int_fast64_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700256detzcode64(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257{
Calin Juravled8928922014-02-28 12:18:53 +0000258 register int_fast64_t result;
259 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260
Calin Juravled8928922014-02-28 12:18:53 +0000261 result = (codep[0] & 0x80) ? -1 : 0;
262 for (i = 0; i < 8; ++i)
263 result = (result << 8) | (codep[i] & 0xff);
264 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265}
266
267static void
Elliott Hughesce4783c2013-07-12 17:31:11 -0700268settzname(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269{
Calin Juravled8928922014-02-28 12:18:53 +0000270 register struct state * const sp = lclptr;
271 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272
Elliott Hughes906eb992014-06-18 19:46:25 -0700273 tzname[0] = tzname[1] = (char *) wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700275 daylight = 0;
276 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277#endif /* defined USG_COMPAT */
278#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700279 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280#endif /* defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700281 if (sp == NULL) {
Elliott Hughes5204a9e2014-06-11 17:15:56 -0700282 tzname[0] = tzname[1] = (char *) gmt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700283 return;
284 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700285 /*
286 ** And to get the latest zone names into tzname. . .
287 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700288 for (i = 0; i < sp->typecnt; ++i) {
289 register const struct ttinfo * const ttisp = &sp->ttis[i];
290
291 tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind];
292 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700293 for (i = 0; i < sp->timecnt; ++i) {
294 register const struct ttinfo * const ttisp =
295 &sp->ttis[
296 sp->types[i]];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700298 tzname[ttisp->tt_isdst] =
299 &sp->chars[ttisp->tt_abbrind];
Elliott Hughesce4783c2013-07-12 17:31:11 -0700300#ifdef USG_COMPAT
301 if (ttisp->tt_isdst)
302 daylight = 1;
303 if (!ttisp->tt_isdst)
304 timezone = -(ttisp->tt_gmtoff);
305#endif /* defined USG_COMPAT */
306#ifdef ALTZONE
307 if (ttisp->tt_isdst)
308 altzone = -(ttisp->tt_gmtoff);
309#endif /* defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700310 }
311 /*
312 ** Finally, scrub the abbreviations.
313 ** First, replace bogus characters.
314 */
315 for (i = 0; i < sp->charcnt; ++i)
316 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
317 sp->chars[i] = TZ_ABBR_ERR_CHAR;
318 /*
319 ** Second, truncate long abbreviations.
320 */
321 for (i = 0; i < sp->typecnt; ++i) {
322 register const struct ttinfo * const ttisp = &sp->ttis[i];
323 register char * cp = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700325 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
326 strcmp(cp, GRANDPARENTED) != 0)
327 *(cp + TZ_ABBR_MAX_LEN) = '\0';
328 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329}
330
331static int
Elliott Hughes5204a9e2014-06-11 17:15:56 -0700332differ_by_repeat(const time_t t1 __unused, const time_t t0 __unused)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333{
Calin Juravled8928922014-02-28 12:18:53 +0000334 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
335 return 0;
Elliott Hughes5204a9e2014-06-11 17:15:56 -0700336#if defined(__LP64__) // 32-bit Android only has a signed 32-bit time_t; 64-bit Android is fixed.
Calin Juravled8928922014-02-28 12:18:53 +0000337 return t1 - t0 == SECSPERREPEAT;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700338#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339}
340
341static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700342tzload(register const char* name, register struct state* const sp,
343 register const int doextend)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344{
Calin Juravled8928922014-02-28 12:18:53 +0000345 register const char * p;
346 register int i;
347 register int fid;
348 register int stored;
349 register int nread;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700350 typedef union {
Calin Juravled8928922014-02-28 12:18:53 +0000351 struct tzhead tzhead;
352 char buf[2 * sizeof(struct tzhead) +
353 2 * sizeof *sp +
354 4 * TZ_MAX_TIMES];
Elliott Hughesce4783c2013-07-12 17:31:11 -0700355 } u_t;
Elliott Hughes906eb992014-06-18 19:46:25 -0700356 union local_storage {
357 /*
358 ** Section 4.9.1 of the C standard says that
359 ** "FILENAME_MAX expands to an integral constant expression
360 ** that is the size needed for an array of char large enough
361 ** to hold the longest file name string that the implementation
362 ** guarantees can be opened."
363 */
364 char fullname[FILENAME_MAX + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365
Elliott Hughes906eb992014-06-18 19:46:25 -0700366 /* The main part of the storage for this function. */
367 struct {
368 u_t u;
369 struct state st;
370 } u;
371 };
372 register char *fullname;
373 register u_t *up;
374 register union local_storage *lsp;
375#ifdef ALL_STATE
376 lsp = malloc(sizeof *lsp);
377 if (!lsp)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700378 return -1;
379#else /* !defined ALL_STATE */
Elliott Hughes906eb992014-06-18 19:46:25 -0700380 union local_storage ls;
381 lsp = &ls;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700382#endif /* !defined ALL_STATE */
Elliott Hughes906eb992014-06-18 19:46:25 -0700383 fullname = lsp->fullname;
384 up = &lsp->u.u;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385
Elliott Hughesce4783c2013-07-12 17:31:11 -0700386 sp->goback = sp->goahead = FALSE;
Elliott Hughes906eb992014-06-18 19:46:25 -0700387
388 if (! name) {
389 name = TZDEFAULT;
390 if (! name)
391 goto oops;
392 }
393
Elliott Hughesce4783c2013-07-12 17:31:11 -0700394 int toread;
395 fid = __bionic_open_tzdata(name, &toread);
Elliott Hughes3073f902014-02-27 17:04:38 -0800396 if (fid < 0)
397 goto oops;
Elliott Hughes906eb992014-06-18 19:46:25 -0700398
399 nread = read(fid, up->buf, sizeof up->buf);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700400 if (close(fid) < 0 || nread <= 0)
401 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700402 for (stored = 4; stored <= 8; stored *= 2) {
Calin Juravled8928922014-02-28 12:18:53 +0000403 int ttisstdcnt;
404 int ttisgmtcnt;
405 int timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406
Elliott Hughesce4783c2013-07-12 17:31:11 -0700407 ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt);
408 ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt);
409 sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt);
410 sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt);
411 sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt);
412 sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt);
413 p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700414 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
415 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
416 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
417 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
418 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
419 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700420 goto oops;
421 if (nread - (p - up->buf) <
Calin Juravled8928922014-02-28 12:18:53 +0000422 sp->timecnt * stored + /* ats */
423 sp->timecnt + /* types */
424 sp->typecnt * 6 + /* ttinfos */
425 sp->charcnt + /* chars */
426 sp->leapcnt * (stored + 4) + /* lsinfos */
427 ttisstdcnt + /* ttisstds */
428 ttisgmtcnt) /* ttisgmts */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700429 goto oops;
Calin Juravled8928922014-02-28 12:18:53 +0000430 timecnt = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700431 for (i = 0; i < sp->timecnt; ++i) {
Calin Juravled8928922014-02-28 12:18:53 +0000432 int_fast64_t at
433 = stored == 4 ? detzcode(p) : detzcode64(p);
434 sp->types[i] = ((TYPE_SIGNED(time_t)
435 ? time_t_min <= at
436 : 0 <= at)
437 && at <= time_t_max);
438 if (sp->types[i]) {
439 if (i && !timecnt && at != time_t_min) {
440 /*
441 ** Keep the earlier record, but tweak
442 ** it so that it starts with the
443 ** minimum time_t value.
444 */
445 sp->types[i - 1] = 1;
446 sp->ats[timecnt++] = time_t_min;
447 }
448 sp->ats[timecnt++] = at;
449 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700450 p += stored;
451 }
Calin Juravled8928922014-02-28 12:18:53 +0000452 timecnt = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700453 for (i = 0; i < sp->timecnt; ++i) {
Calin Juravled8928922014-02-28 12:18:53 +0000454 unsigned char typ = *p++;
455 if (sp->typecnt <= typ)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700456 goto oops;
Calin Juravled8928922014-02-28 12:18:53 +0000457 if (sp->types[i])
458 sp->types[timecnt++] = typ;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700459 }
Calin Juravled8928922014-02-28 12:18:53 +0000460 sp->timecnt = timecnt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700461 for (i = 0; i < sp->typecnt; ++i) {
462 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700464 ttisp = &sp->ttis[i];
465 ttisp->tt_gmtoff = detzcode(p);
466 p += 4;
467 ttisp->tt_isdst = (unsigned char) *p++;
468 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700469 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700470 ttisp->tt_abbrind = (unsigned char) *p++;
471 if (ttisp->tt_abbrind < 0 ||
472 ttisp->tt_abbrind > sp->charcnt)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700473 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700474 }
475 for (i = 0; i < sp->charcnt; ++i)
476 sp->chars[i] = *p++;
477 sp->chars[i] = '\0'; /* ensure '\0' at end */
478 for (i = 0; i < sp->leapcnt; ++i) {
479 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800480
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700481 lsisp = &sp->lsis[i];
482 lsisp->ls_trans = (stored == 4) ?
483 detzcode(p) : detzcode64(p);
484 p += stored;
485 lsisp->ls_corr = detzcode(p);
486 p += 4;
487 }
488 for (i = 0; i < sp->typecnt; ++i) {
489 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800490
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700491 ttisp = &sp->ttis[i];
492 if (ttisstdcnt == 0)
493 ttisp->tt_ttisstd = FALSE;
494 else {
495 ttisp->tt_ttisstd = *p++;
496 if (ttisp->tt_ttisstd != TRUE &&
497 ttisp->tt_ttisstd != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700498 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700499 }
500 }
501 for (i = 0; i < sp->typecnt; ++i) {
502 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800503
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700504 ttisp = &sp->ttis[i];
505 if (ttisgmtcnt == 0)
506 ttisp->tt_ttisgmt = FALSE;
507 else {
508 ttisp->tt_ttisgmt = *p++;
509 if (ttisp->tt_ttisgmt != TRUE &&
510 ttisp->tt_ttisgmt != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700511 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700512 }
513 }
514 /*
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700515 ** If this is an old file, we're done.
516 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700517 if (up->tzhead.tzh_version[0] == '\0')
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700518 break;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700519 nread -= p - up->buf;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700520 for (i = 0; i < nread; ++i)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700521 up->buf[i] = p[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700522 /*
Calin Juravled8928922014-02-28 12:18:53 +0000523 ** If this is a signed narrow time_t system, we're done.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700524 */
Calin Juravled8928922014-02-28 12:18:53 +0000525 if (TYPE_SIGNED(time_t) && stored >= (int) sizeof(time_t))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700526 break;
527 }
528 if (doextend && nread > 2 &&
Elliott Hughesce4783c2013-07-12 17:31:11 -0700529 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700530 sp->typecnt + 2 <= TZ_MAX_TYPES) {
Elliott Hughes906eb992014-06-18 19:46:25 -0700531 struct state *ts = &lsp->u.st;
Calin Juravled8928922014-02-28 12:18:53 +0000532 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533
Elliott Hughesce4783c2013-07-12 17:31:11 -0700534 up->buf[nread - 1] = '\0';
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700535 result = tzparse(&up->buf[1], ts, FALSE);
536 if (result == 0 && ts->typecnt == 2 &&
537 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700538 for (i = 0; i < 2; ++i)
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700539 ts->ttis[i].tt_abbrind +=
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700540 sp->charcnt;
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700541 for (i = 0; i < ts->charcnt; ++i)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700542 sp->chars[sp->charcnt++] =
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700543 ts->chars[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700544 i = 0;
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700545 while (i < ts->timecnt &&
546 ts->ats[i] <=
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700547 sp->ats[sp->timecnt - 1])
548 ++i;
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700549 while (i < ts->timecnt &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700550 sp->timecnt < TZ_MAX_TIMES) {
551 sp->ats[sp->timecnt] =
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700552 ts->ats[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700553 sp->types[sp->timecnt] =
554 sp->typecnt +
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700555 ts->types[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700556 ++sp->timecnt;
557 ++i;
558 }
Christopher Ferris8a8b0c92014-05-14 16:06:17 -0700559 sp->ttis[sp->typecnt++] = ts->ttis[0];
560 sp->ttis[sp->typecnt++] = ts->ttis[1];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700561 }
562 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700563 if (sp->timecnt > 1) {
564 for (i = 1; i < sp->timecnt; ++i)
565 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
566 differ_by_repeat(sp->ats[i], sp->ats[0])) {
567 sp->goback = TRUE;
568 break;
569 }
570 for (i = sp->timecnt - 2; i >= 0; --i)
571 if (typesequiv(sp, sp->types[sp->timecnt - 1],
572 sp->types[i]) &&
573 differ_by_repeat(sp->ats[sp->timecnt - 1],
574 sp->ats[i])) {
575 sp->goahead = TRUE;
576 break;
577 }
578 }
579 /*
580 ** If type 0 is is unused in transitions,
581 ** it's the type to use for early times.
582 */
583 for (i = 0; i < sp->typecnt; ++i)
584 if (sp->types[i] == 0)
585 break;
586 i = (i >= sp->typecnt) ? 0 : -1;
587 /*
588 ** Absent the above,
589 ** if there are transition times
590 ** and the first transition is to a daylight time
591 ** find the standard type less than and closest to
592 ** the type of the first transition.
593 */
594 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
595 i = sp->types[0];
596 while (--i >= 0)
597 if (!sp->ttis[i].tt_isdst)
598 break;
599 }
600 /*
601 ** If no result yet, find the first standard type.
602 ** If there is none, punt to type zero.
603 */
604 if (i < 0) {
605 i = 0;
606 while (sp->ttis[i].tt_isdst)
607 if (++i >= sp->typecnt) {
608 i = 0;
609 break;
610 }
611 }
612 sp->defaulttype = i;
613#ifdef ALL_STATE
614 free(up);
615#endif /* defined ALL_STATE */
616 return 0;
617oops:
618#ifdef ALL_STATE
619 free(up);
620#endif /* defined ALL_STATE */
621 return -1;
622}
623
624static int
625typesequiv(const struct state *const sp, const int a, const int b)
626{
Calin Juravled8928922014-02-28 12:18:53 +0000627 register int result;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700628
Calin Juravled8928922014-02-28 12:18:53 +0000629 if (sp == NULL ||
630 a < 0 || a >= sp->typecnt ||
631 b < 0 || b >= sp->typecnt)
632 result = FALSE;
633 else {
634 register const struct ttinfo * ap = &sp->ttis[a];
635 register const struct ttinfo * bp = &sp->ttis[b];
636 result = ap->tt_gmtoff == bp->tt_gmtoff &&
637 ap->tt_isdst == bp->tt_isdst &&
638 ap->tt_ttisstd == bp->tt_ttisstd &&
639 ap->tt_ttisgmt == bp->tt_ttisgmt &&
640 strcmp(&sp->chars[ap->tt_abbrind],
641 &sp->chars[bp->tt_abbrind]) == 0;
642 }
643 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800644}
645
Calin Juravled8928922014-02-28 12:18:53 +0000646static const int mon_lengths[2][MONSPERYEAR] = {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700647 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
648 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649};
650
Calin Juravled8928922014-02-28 12:18:53 +0000651static const int year_lengths[2] = {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700652 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800653};
654
655/*
656** Given a pointer into a time zone string, scan until a character that is not
657** a valid character in a zone name is found. Return a pointer to that
658** character.
659*/
660
661static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700662getzname(register const char * strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700664 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700666 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
667 c != '+')
668 ++strp;
669 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800670}
671
672/*
673** Given a pointer into an extended time zone string, scan until the ending
674** delimiter of the zone name is located. Return a pointer to the delimiter.
675**
676** As with getzname above, the legal character set is actually quite
677** restricted, with other characters producing undefined results.
678** We don't do any checking here; checking is done later in common-case code.
679*/
680
681static const char *
682getqzname(register const char *strp, const int delim)
683{
Calin Juravled8928922014-02-28 12:18:53 +0000684 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700686 while ((c = *strp) != '\0' && c != delim)
687 ++strp;
688 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800689}
690
691/*
692** Given a pointer into a time zone string, extract a number from that string.
693** Check that the number is within a specified range; if it is not, return
694** NULL.
695** Otherwise, return a pointer to the first character not part of the number.
696*/
697
698static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700699getnum(register const char * strp, int * const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700{
Calin Juravled8928922014-02-28 12:18:53 +0000701 register char c;
702 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800703
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700704 if (strp == NULL || !is_digit(c = *strp))
705 return NULL;
706 num = 0;
707 do {
708 num = num * 10 + (c - '0');
709 if (num > max)
710 return NULL; /* illegal value */
711 c = *++strp;
712 } while (is_digit(c));
713 if (num < min)
714 return NULL; /* illegal value */
715 *nump = num;
716 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717}
718
719/*
720** Given a pointer into a time zone string, extract a number of seconds,
721** in hh[:mm[:ss]] form, from the string.
722** If any error occurs, return NULL.
723** Otherwise, return a pointer to the first character not part of the number
724** of seconds.
725*/
726
727static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700728getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800729{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700730 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800731
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700732 /*
Elliott Hughes906eb992014-06-18 19:46:25 -0700733 ** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700734 ** "M10.4.6/26", which does not conform to Posix,
735 ** but which specifies the equivalent of
Elliott Hughes906eb992014-06-18 19:46:25 -0700736 ** "02:00 on the first Sunday on or after 23 Oct".
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700737 */
738 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
739 if (strp == NULL)
740 return NULL;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700741 *secsp = num * (int_fast32_t) SECSPERHOUR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700742 if (*strp == ':') {
743 ++strp;
744 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
745 if (strp == NULL)
746 return NULL;
747 *secsp += num * SECSPERMIN;
748 if (*strp == ':') {
749 ++strp;
Elliott Hughes906eb992014-06-18 19:46:25 -0700750 /* 'SECSPERMIN' allows for leap seconds. */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700751 strp = getnum(strp, &num, 0, SECSPERMIN);
752 if (strp == NULL)
753 return NULL;
754 *secsp += num;
755 }
756 }
757 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800758}
759
760/*
761** Given a pointer into a time zone string, extract an offset, in
762** [+-]hh[:mm[:ss]] form, from the string.
763** If any error occurs, return NULL.
764** Otherwise, return a pointer to the first character not part of the time.
765*/
766
767static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700768getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800769{
Calin Juravled8928922014-02-28 12:18:53 +0000770 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700772 if (*strp == '-') {
773 neg = 1;
774 ++strp;
775 } else if (*strp == '+')
776 ++strp;
777 strp = getsecs(strp, offsetp);
778 if (strp == NULL)
779 return NULL; /* illegal time */
780 if (neg)
781 *offsetp = -*offsetp;
782 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800783}
784
785/*
786** Given a pointer into a time zone string, extract a rule in the form
787** date[/time]. See POSIX section 8 for the format of "date" and "time".
788** If a valid rule is not found, return NULL.
789** Otherwise, return a pointer to the first character not part of the rule.
790*/
791
792static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700793getrule(const char * strp, register struct rule * const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700795 if (*strp == 'J') {
796 /*
797 ** Julian day.
798 */
799 rulep->r_type = JULIAN_DAY;
800 ++strp;
801 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
802 } else if (*strp == 'M') {
803 /*
804 ** Month, week, day.
805 */
806 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
807 ++strp;
808 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
809 if (strp == NULL)
810 return NULL;
811 if (*strp++ != '.')
812 return NULL;
813 strp = getnum(strp, &rulep->r_week, 1, 5);
814 if (strp == NULL)
815 return NULL;
816 if (*strp++ != '.')
817 return NULL;
818 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
819 } else if (is_digit(*strp)) {
820 /*
821 ** Day of year.
822 */
823 rulep->r_type = DAY_OF_YEAR;
824 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
825 } else return NULL; /* invalid format */
826 if (strp == NULL)
827 return NULL;
828 if (*strp == '/') {
829 /*
830 ** Time specified.
831 */
832 ++strp;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700833 strp = getoffset(strp, &rulep->r_time);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700834 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
835 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836}
837
838/*
Calin Juravle627d37c2014-02-28 11:46:03 +0000839** Given a year, a rule, and the offset from UT at the time that rule takes
840** effect, calculate the year-relative time that rule takes effect.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800841*/
842
Calin Juravle627d37c2014-02-28 11:46:03 +0000843static int_fast32_t
844transtime(const int year, register const struct rule *const rulep,
Calin Juravled8928922014-02-28 12:18:53 +0000845 const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846{
Calin Juravled8928922014-02-28 12:18:53 +0000847 register int leapyear;
848 register int_fast32_t value;
849 register int i;
850 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800851
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700852 INITIALIZE(value);
853 leapyear = isleap(year);
854 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800855
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700856 case JULIAN_DAY:
857 /*
858 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
859 ** years.
860 ** In non-leap years, or if the day number is 59 or less, just
861 ** add SECSPERDAY times the day number-1 to the time of
862 ** January 1, midnight, to get the day.
863 */
Calin Juravled8928922014-02-28 12:18:53 +0000864 value = (rulep->r_day - 1) * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700865 if (leapyear && rulep->r_day >= 60)
866 value += SECSPERDAY;
867 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800868
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700869 case DAY_OF_YEAR:
870 /*
871 ** n - day of year.
872 ** Just add SECSPERDAY times the day number to the time of
873 ** January 1, midnight, to get the day.
874 */
Calin Juravled8928922014-02-28 12:18:53 +0000875 value = rulep->r_day * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700876 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800877
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700878 case MONTH_NTH_DAY_OF_WEEK:
879 /*
880 ** Mm.n.d - nth "dth day" of month m.
881 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800882
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700883 /*
884 ** Use Zeller's Congruence to get day-of-week of first day of
885 ** month.
886 */
887 m1 = (rulep->r_mon + 9) % 12 + 1;
888 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
889 yy1 = yy0 / 100;
890 yy2 = yy0 % 100;
891 dow = ((26 * m1 - 2) / 10 +
892 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
893 if (dow < 0)
894 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800895
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700896 /*
897 ** "dow" is the day-of-week of the first day of the month. Get
898 ** the day-of-month (zero-origin) of the first "dow" day of the
899 ** month.
900 */
901 d = rulep->r_day - dow;
902 if (d < 0)
903 d += DAYSPERWEEK;
904 for (i = 1; i < rulep->r_week; ++i) {
905 if (d + DAYSPERWEEK >=
906 mon_lengths[leapyear][rulep->r_mon - 1])
907 break;
908 d += DAYSPERWEEK;
909 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800910
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700911 /*
912 ** "d" is the day-of-month (zero-origin) of the day we want.
913 */
Calin Juravled8928922014-02-28 12:18:53 +0000914 value = d * SECSPERDAY;
915 for (i = 0; i < rulep->r_mon - 1; ++i)
916 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700917 break;
918 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800919
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700920 /*
Calin Juravled8928922014-02-28 12:18:53 +0000921 ** "value" is the year-relative time of 00:00:00 UT on the day in
922 ** question. To get the year-relative time of the specified local
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700923 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -0700924 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700925 */
926 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800927}
928
929/*
930** Given a POSIX section 8-style TZ string, fill in the rule tables as
931** appropriate.
932*/
933
934static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700935tzparse(const char * name, register struct state * const sp,
936 const int lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800937{
Calin Juravled8928922014-02-28 12:18:53 +0000938 const char * stdname;
939 const char * dstname;
940 size_t stdlen;
941 size_t dstlen;
942 int_fast32_t stdoffset;
943 int_fast32_t dstoffset;
944 register char * cp;
945 register int load_result;
946 static struct ttinfo zttinfo;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800947
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700948 INITIALIZE(dstname);
949 stdname = name;
950 if (lastditch) {
951 stdlen = strlen(name); /* length of standard zone name */
952 name += stdlen;
953 if (stdlen >= sizeof sp->chars)
954 stdlen = (sizeof sp->chars) - 1;
955 stdoffset = 0;
956 } else {
957 if (*name == '<') {
958 name++;
959 stdname = name;
960 name = getqzname(name, '>');
961 if (*name != '>')
962 return (-1);
963 stdlen = name - stdname;
964 name++;
965 } else {
966 name = getzname(name);
967 stdlen = name - stdname;
968 }
969 if (*name == '\0')
970 return -1;
971 name = getoffset(name, &stdoffset);
972 if (name == NULL)
973 return -1;
974 }
975 load_result = tzload(TZDEFRULES, sp, FALSE);
976 if (load_result != 0)
977 sp->leapcnt = 0; /* so, we're off a little */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700978 if (*name != '\0') {
979 if (*name == '<') {
980 dstname = ++name;
981 name = getqzname(name, '>');
982 if (*name != '>')
983 return -1;
984 dstlen = name - dstname;
985 name++;
986 } else {
987 dstname = name;
988 name = getzname(name);
989 dstlen = name - dstname; /* length of DST zone name */
990 }
991 if (*name != '\0' && *name != ',' && *name != ';') {
992 name = getoffset(name, &dstoffset);
993 if (name == NULL)
994 return -1;
995 } else dstoffset = stdoffset - SECSPERHOUR;
996 if (*name == '\0' && load_result != 0)
997 name = TZDEFRULESTRING;
998 if (*name == ',' || *name == ';') {
Calin Juravled8928922014-02-28 12:18:53 +0000999 struct rule start;
1000 struct rule end;
1001 register int year;
1002 register int yearlim;
1003 register int timecnt;
1004 time_t janfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001006 ++name;
1007 if ((name = getrule(name, &start)) == NULL)
1008 return -1;
1009 if (*name++ != ',')
1010 return -1;
1011 if ((name = getrule(name, &end)) == NULL)
1012 return -1;
1013 if (*name != '\0')
1014 return -1;
1015 sp->typecnt = 2; /* standard time and DST */
1016 /*
1017 ** Two transitions per year, from EPOCH_YEAR forward.
1018 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001019 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001020 sp->ttis[0].tt_gmtoff = -dstoffset;
1021 sp->ttis[0].tt_isdst = 1;
1022 sp->ttis[0].tt_abbrind = stdlen + 1;
1023 sp->ttis[1].tt_gmtoff = -stdoffset;
1024 sp->ttis[1].tt_isdst = 0;
1025 sp->ttis[1].tt_abbrind = 0;
Elliott Hughes906eb992014-06-18 19:46:25 -07001026 sp->defaulttype = 0;
Calin Juravled8928922014-02-28 12:18:53 +00001027 timecnt = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001028 janfirst = 0;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001029 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1030 for (year = EPOCH_YEAR; year < yearlim; year++) {
Calin Juravled8928922014-02-28 12:18:53 +00001031 int_fast32_t
1032 starttime = transtime(year, &start, stdoffset),
1033 endtime = transtime(year, &end, dstoffset);
1034 int_fast32_t
Elliott Hughese0d0b152013-09-27 00:04:30 -07001035 yearsecs = (year_lengths[isleap(year)]
1036 * SECSPERDAY);
Calin Juravled8928922014-02-28 12:18:53 +00001037 int reversed = endtime < starttime;
1038 if (reversed) {
1039 int_fast32_t swap = starttime;
1040 starttime = endtime;
1041 endtime = swap;
1042 }
1043 if (reversed
Elliott Hughese0d0b152013-09-27 00:04:30 -07001044 || (starttime < endtime
1045 && (endtime - starttime
1046 < (yearsecs
1047 + (stdoffset - dstoffset))))) {
Calin Juravled8928922014-02-28 12:18:53 +00001048 if (TZ_MAX_TIMES - 2 < timecnt)
Elliott Hughese0d0b152013-09-27 00:04:30 -07001049 break;
1050 yearlim = year + YEARSPERREPEAT + 1;
Calin Juravled8928922014-02-28 12:18:53 +00001051 sp->ats[timecnt] = janfirst;
1052 if (increment_overflow_time
1053 (&sp->ats[timecnt], starttime))
1054 break;
1055 sp->types[timecnt++] = reversed;
1056 sp->ats[timecnt] = janfirst;
1057 if (increment_overflow_time
1058 (&sp->ats[timecnt], endtime))
1059 break;
1060 sp->types[timecnt++] = !reversed;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001061 }
Calin Juravled8928922014-02-28 12:18:53 +00001062 if (increment_overflow_time(&janfirst, yearsecs))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001063 break;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001064 }
Calin Juravled8928922014-02-28 12:18:53 +00001065 sp->timecnt = timecnt;
1066 if (!timecnt)
1067 sp->typecnt = 1; /* Perpetual DST. */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001068 } else {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001069 register int_fast32_t theirstdoffset;
1070 register int_fast32_t theirdstoffset;
1071 register int_fast32_t theiroffset;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001072 register int isdst;
1073 register int i;
1074 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001075
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001076 if (*name != '\0')
1077 return -1;
1078 /*
1079 ** Initial values of theirstdoffset and theirdstoffset.
1080 */
1081 theirstdoffset = 0;
1082 for (i = 0; i < sp->timecnt; ++i) {
1083 j = sp->types[i];
1084 if (!sp->ttis[j].tt_isdst) {
1085 theirstdoffset =
1086 -sp->ttis[j].tt_gmtoff;
1087 break;
1088 }
1089 }
1090 theirdstoffset = 0;
1091 for (i = 0; i < sp->timecnt; ++i) {
1092 j = sp->types[i];
1093 if (sp->ttis[j].tt_isdst) {
1094 theirdstoffset =
1095 -sp->ttis[j].tt_gmtoff;
1096 break;
1097 }
1098 }
1099 /*
1100 ** Initially we're assumed to be in standard time.
1101 */
1102 isdst = FALSE;
1103 theiroffset = theirstdoffset;
1104 /*
1105 ** Now juggle transition times and types
1106 ** tracking offsets as you do.
1107 */
1108 for (i = 0; i < sp->timecnt; ++i) {
1109 j = sp->types[i];
1110 sp->types[i] = sp->ttis[j].tt_isdst;
1111 if (sp->ttis[j].tt_ttisgmt) {
1112 /* No adjustment to transition time */
1113 } else {
1114 /*
1115 ** If summer time is in effect, and the
1116 ** transition time was not specified as
1117 ** standard time, add the summer time
1118 ** offset to the transition time;
1119 ** otherwise, add the standard time
1120 ** offset to the transition time.
1121 */
1122 /*
1123 ** Transitions from DST to DDST
1124 ** will effectively disappear since
1125 ** POSIX provides for only one DST
1126 ** offset.
1127 */
1128 if (isdst && !sp->ttis[j].tt_ttisstd) {
1129 sp->ats[i] += dstoffset -
1130 theirdstoffset;
1131 } else {
1132 sp->ats[i] += stdoffset -
1133 theirstdoffset;
1134 }
1135 }
1136 theiroffset = -sp->ttis[j].tt_gmtoff;
1137 if (sp->ttis[j].tt_isdst)
1138 theirdstoffset = theiroffset;
1139 else theirstdoffset = theiroffset;
1140 }
1141 /*
1142 ** Finally, fill in ttis.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001143 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001144 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001145 sp->ttis[0].tt_gmtoff = -stdoffset;
1146 sp->ttis[0].tt_isdst = FALSE;
1147 sp->ttis[0].tt_abbrind = 0;
1148 sp->ttis[1].tt_gmtoff = -dstoffset;
1149 sp->ttis[1].tt_isdst = TRUE;
1150 sp->ttis[1].tt_abbrind = stdlen + 1;
1151 sp->typecnt = 2;
Elliott Hughes906eb992014-06-18 19:46:25 -07001152 sp->defaulttype = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001153 }
1154 } else {
1155 dstlen = 0;
1156 sp->typecnt = 1; /* only standard time */
1157 sp->timecnt = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001158 sp->ttis[0] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001159 sp->ttis[0].tt_gmtoff = -stdoffset;
1160 sp->ttis[0].tt_isdst = 0;
1161 sp->ttis[0].tt_abbrind = 0;
Elliott Hughes906eb992014-06-18 19:46:25 -07001162 sp->defaulttype = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001163 }
1164 sp->charcnt = stdlen + 1;
1165 if (dstlen != 0)
1166 sp->charcnt += dstlen + 1;
1167 if ((size_t) sp->charcnt > sizeof sp->chars)
1168 return -1;
1169 cp = sp->chars;
1170 (void) strncpy(cp, stdname, stdlen);
1171 cp += stdlen;
1172 *cp++ = '\0';
1173 if (dstlen != 0) {
1174 (void) strncpy(cp, dstname, dstlen);
1175 *(cp + dstlen) = '\0';
1176 }
1177 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001178}
1179
1180static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001181gmtload(struct state * const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001182{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001183 if (tzload(gmt, sp, TRUE) != 0)
1184 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001185}
1186
Elliott Hughesce4783c2013-07-12 17:31:11 -07001187#ifndef STD_INSPIRED
1188/*
1189** A non-static declaration of tzsetwall in a system header file
1190** may cause a warning about this upcoming static declaration...
1191*/
1192static
1193#endif /* !defined STD_INSPIRED */
1194void
1195tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001197 if (lcl_is_set < 0)
1198 return;
1199 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200
1201#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001202 if (lclptr == NULL) {
Elliott Hughes906eb992014-06-18 19:46:25 -07001203 lclptr = malloc(sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001204 if (lclptr == NULL) {
1205 settzname(); /* all we can do */
1206 return;
1207 }
1208 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209#endif /* defined ALL_STATE */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001210 if (tzload(NULL, lclptr, TRUE) != 0)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001211 gmtload(lclptr);
1212 settzname();
1213}
1214
Elliott Hughesce4783c2013-07-12 17:31:11 -07001215#include <sys/system_properties.h> // For __system_property_get.
1216
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001217static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001218tzset_locked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001219{
Elliott Hughes906eb992014-06-18 19:46:25 -07001220 register const char * name;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001221
1222 name = getenv("TZ");
1223
1224 // try the "persist.sys.timezone" system property first
Elliott Hughesce4783c2013-07-12 17:31:11 -07001225 static char buf[PROP_VALUE_MAX];
1226 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001227 name = buf;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001228 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001229
1230 if (name == NULL) {
1231 tzsetwall();
1232 return;
1233 }
1234
1235 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1236 return;
1237 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1238 if (lcl_is_set)
1239 (void) strcpy(lcl_TZname, name);
1240
1241#ifdef ALL_STATE
1242 if (lclptr == NULL) {
Elliott Hughes906eb992014-06-18 19:46:25 -07001243 lclptr = malloc(sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001244 if (lclptr == NULL) {
1245 settzname(); /* all we can do */
1246 return;
1247 }
1248 }
1249#endif /* defined ALL_STATE */
1250 if (*name == '\0') {
1251 /*
1252 ** User wants it fast rather than right.
1253 */
1254 lclptr->leapcnt = 0; /* so, we're off a little */
1255 lclptr->timecnt = 0;
1256 lclptr->typecnt = 0;
1257 lclptr->ttis[0].tt_isdst = 0;
1258 lclptr->ttis[0].tt_gmtoff = 0;
1259 lclptr->ttis[0].tt_abbrind = 0;
1260 (void) strcpy(lclptr->chars, gmt);
1261 } else if (tzload(name, lclptr, TRUE) != 0)
1262 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1263 (void) gmtload(lclptr);
1264 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001265}
1266
1267void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001268tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001270 _tzLock();
1271 tzset_locked();
1272 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001273}
1274
1275/*
1276** The easy way to behave "as if no library function calls" localtime
1277** is to not call it--so we drop its guts into "localsub", which can be
1278** freely called. (And no, the PANS doesn't require the above behavior--
1279** but it *is* desirable.)
1280**
1281** The unused offset argument is for the benefit of mktime variants.
1282*/
1283
1284/*ARGSUSED*/
1285static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001286localsub(const time_t * const timep, const int_fast32_t offset,
1287 struct tm * const tmp, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001288{
Calin Juravled8928922014-02-28 12:18:53 +00001289 register const struct ttinfo * ttisp;
1290 register int i;
1291 register struct tm * result;
1292 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001293
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001294 // BEGIN android-changed: support user-supplied sp.
1295 if (sp == NULL) {
1296 sp = lclptr;
1297 }
1298 // END android-changed
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001299 if (sp == NULL)
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001300 return gmtsub(timep, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001301 if ((sp->goback && t < sp->ats[0]) ||
1302 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1303 time_t newt = t;
Calin Juravled8928922014-02-28 12:18:53 +00001304 register time_t seconds;
1305 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001306
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001307 if (t < sp->ats[0])
1308 seconds = sp->ats[0] - t;
1309 else seconds = t - sp->ats[sp->timecnt - 1];
1310 --seconds;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001311 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1312 seconds = years * AVGSECSPERYEAR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001313 if (t < sp->ats[0])
1314 newt += seconds;
1315 else newt -= seconds;
1316 if (newt < sp->ats[0] ||
1317 newt > sp->ats[sp->timecnt - 1])
1318 return NULL; /* "cannot happen" */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001319 result = localsub(&newt, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001320 if (result == tmp) {
1321 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001322
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001323 newy = tmp->tm_year;
1324 if (t < sp->ats[0])
Elliott Hughese0d0b152013-09-27 00:04:30 -07001325 newy -= years;
1326 else newy += years;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001327 tmp->tm_year = newy;
1328 if (tmp->tm_year != newy)
1329 return NULL;
1330 }
1331 return result;
1332 }
1333 if (sp->timecnt == 0 || t < sp->ats[0]) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001334 i = sp->defaulttype;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001335 } else {
Calin Juravled8928922014-02-28 12:18:53 +00001336 register int lo = 1;
1337 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001338
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001339 while (lo < hi) {
1340 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001342 if (t < sp->ats[mid])
1343 hi = mid;
1344 else lo = mid + 1;
1345 }
1346 i = (int) sp->types[lo - 1];
1347 }
1348 ttisp = &sp->ttis[i];
1349 /*
1350 ** To get (wrong) behavior that's compatible with System V Release 2.0
1351 ** you'd replace the statement below with
1352 ** t += ttisp->tt_gmtoff;
1353 ** timesub(&t, 0L, sp, tmp);
1354 */
1355 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1356 tmp->tm_isdst = ttisp->tt_isdst;
1357 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001358#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001359 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001361 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001362}
1363
1364struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001365localtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001367 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001368}
1369
1370/*
1371** Re-entrant version of localtime.
1372*/
1373
1374struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001375localtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001376{
Calin Juravled8928922014-02-28 12:18:53 +00001377 struct tm* result;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001378
1379 _tzLock();
1380 tzset_locked();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001381 result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001382 _tzUnlock();
1383
1384 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001385}
1386
1387/*
1388** gmtsub is to gmtime as localsub is to localtime.
1389*/
1390
1391static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001392gmtsub(const time_t * const timep, const int_fast32_t offset,
Elliott Hughes906eb992014-06-18 19:46:25 -07001393 struct tm *const tmp, const struct state * sp __unused) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001394{
Calin Juravled8928922014-02-28 12:18:53 +00001395 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001397 if (!gmt_is_set) {
1398 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001399#ifdef ALL_STATE
Elliott Hughes906eb992014-06-18 19:46:25 -07001400 gmtptr = malloc(sizeof *gmtptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001401#endif /* defined ALL_STATE */
Elliott Hughes906eb992014-06-18 19:46:25 -07001402 if (gmtptr != NULL)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001403 gmtload(gmtptr);
1404 }
1405 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001407 /*
1408 ** Could get fancy here and deliver something such as
Elliott Hughese0d0b152013-09-27 00:04:30 -07001409 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001410 ** but this is no time for a treasure hunt.
1411 */
Elliott Hughes906eb992014-06-18 19:46:25 -07001412 tmp->TM_ZONE = offset ? wildabbr : gmtptr ? gmtptr->chars : gmt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001414 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001415}
1416
1417struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001418gmtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001420 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421}
1422
1423/*
1424* Re-entrant version of gmtime.
1425*/
1426
1427struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001428gmtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429{
Calin Juravled8928922014-02-28 12:18:53 +00001430 struct tm* result;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001431
1432 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001433 result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001434 _tzUnlock();
1435
1436 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001437}
1438
Elliott Hughes906eb992014-06-18 19:46:25 -07001439#ifdef STD_INSPIRED
1440
1441struct tm *
1442offtime(const time_t *const timep, const long offset)
1443{
1444 return gmtsub(timep, offset, &tmGlobal, NULL); // android-changed: extra parameter.
1445}
1446
1447#endif /* defined STD_INSPIRED */
1448
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001449/*
1450** Return the number of leap years through the end of the given year
1451** where, to make the math easy, the answer for year zero is defined as zero.
1452*/
1453
1454static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001455leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001457 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1458 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459}
1460
1461static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001462timesub(const time_t *const timep, const int_fast32_t offset,
1463 register const struct state *const sp,
1464 register struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001465{
Calin Juravled8928922014-02-28 12:18:53 +00001466 register const struct lsinfo * lp;
1467 register time_t tdays;
1468 register int idays; /* unsigned would be so 2003 */
1469 register int_fast64_t rem;
1470 int y;
1471 register const int * ip;
1472 register int_fast64_t corr;
1473 register int hit;
1474 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001475
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001476 corr = 0;
1477 hit = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001478 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001479 while (--i >= 0) {
1480 lp = &sp->lsis[i];
1481 if (*timep >= lp->ls_trans) {
1482 if (*timep == lp->ls_trans) {
1483 hit = ((i == 0 && lp->ls_corr > 0) ||
1484 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1485 if (hit)
1486 while (i > 0 &&
1487 sp->lsis[i].ls_trans ==
1488 sp->lsis[i - 1].ls_trans + 1 &&
1489 sp->lsis[i].ls_corr ==
1490 sp->lsis[i - 1].ls_corr + 1) {
1491 ++hit;
1492 --i;
1493 }
1494 }
1495 corr = lp->ls_corr;
1496 break;
1497 }
1498 }
1499 y = EPOCH_YEAR;
1500 tdays = *timep / SECSPERDAY;
1501 rem = *timep - tdays * SECSPERDAY;
1502 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1503 int newy;
1504 register time_t tdelta;
1505 register int idelta;
1506 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001507
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001508 tdelta = tdays / DAYSPERLYEAR;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001509 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1510 && tdelta <= INT_MAX))
1511 return NULL;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001512 idelta = tdelta;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001513 if (idelta == 0)
1514 idelta = (tdays < 0) ? -1 : 1;
1515 newy = y;
1516 if (increment_overflow(&newy, idelta))
1517 return NULL;
1518 leapdays = leaps_thru_end_of(newy - 1) -
1519 leaps_thru_end_of(y - 1);
1520 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1521 tdays -= leapdays;
1522 y = newy;
1523 }
1524 {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001525 register int_fast32_t seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001526
Elliott Hughese0d0b152013-09-27 00:04:30 -07001527 seconds = tdays * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001528 tdays = seconds / SECSPERDAY;
1529 rem += seconds - tdays * SECSPERDAY;
1530 }
1531 /*
1532 ** Given the range, we can now fearlessly cast...
1533 */
1534 idays = tdays;
1535 rem += offset - corr;
1536 while (rem < 0) {
1537 rem += SECSPERDAY;
1538 --idays;
1539 }
1540 while (rem >= SECSPERDAY) {
1541 rem -= SECSPERDAY;
1542 ++idays;
1543 }
1544 while (idays < 0) {
1545 if (increment_overflow(&y, -1))
1546 return NULL;
1547 idays += year_lengths[isleap(y)];
1548 }
1549 while (idays >= year_lengths[isleap(y)]) {
1550 idays -= year_lengths[isleap(y)];
1551 if (increment_overflow(&y, 1))
1552 return NULL;
1553 }
1554 tmp->tm_year = y;
1555 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1556 return NULL;
1557 tmp->tm_yday = idays;
1558 /*
1559 ** The "extra" mods below avoid overflow problems.
1560 */
1561 tmp->tm_wday = EPOCH_WDAY +
1562 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1563 (DAYSPERNYEAR % DAYSPERWEEK) +
1564 leaps_thru_end_of(y - 1) -
1565 leaps_thru_end_of(EPOCH_YEAR - 1) +
1566 idays;
1567 tmp->tm_wday %= DAYSPERWEEK;
1568 if (tmp->tm_wday < 0)
1569 tmp->tm_wday += DAYSPERWEEK;
1570 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1571 rem %= SECSPERHOUR;
1572 tmp->tm_min = (int) (rem / SECSPERMIN);
1573 /*
1574 ** A positive leap second requires a special
1575 ** representation. This uses "... ??:59:60" et seq.
1576 */
1577 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1578 ip = mon_lengths[isleap(y)];
1579 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1580 idays -= ip[tmp->tm_mon];
1581 tmp->tm_mday = (int) (idays + 1);
1582 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001583#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001584 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001585#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001586 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587}
1588
1589char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001590ctime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001591{
1592/*
1593** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001594** The ctime function converts the calendar time pointed to by timer
1595** to local time in the form of a string. It is equivalent to
1596** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001597*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001598 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001599}
1600
1601char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001602ctime_r(const time_t * const timep, char * buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001603{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001604 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001606 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001607}
1608
1609/*
1610** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001611** The "best" way to do mktime I think is based on an idea of Bob
1612** Kridle's (so its said...) from a long time ago.
1613** It does a binary search of the time_t space. Since time_t's are
1614** just 32 bits, its a max of 32 iterations (even at 64 bits it
1615** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001616*/
1617
1618#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001619#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620#endif /* !defined WRONG */
1621
1622/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001623** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001624*/
1625
1626static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001627increment_overflow(int *const ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001628{
Calin Juravled8928922014-02-28 12:18:53 +00001629 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001630
Calin Juravled8928922014-02-28 12:18:53 +00001631 /*
1632 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1633 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1634 ** If i < 0 there can only be overflow if i + j < INT_MIN
1635 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1636 */
1637 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1638 return TRUE;
1639 *ip += j;
1640 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001641}
1642
1643static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001644increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001645{
Calin Juravled8928922014-02-28 12:18:53 +00001646 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001647
Calin Juravled8928922014-02-28 12:18:53 +00001648 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1649 return TRUE;
1650 *lp += m;
1651 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652}
1653
1654static int
Calin Juravle627d37c2014-02-28 11:46:03 +00001655increment_overflow_time(time_t *tp, int_fast32_t j)
1656{
Calin Juravled8928922014-02-28 12:18:53 +00001657 /*
1658 ** This is like
1659 ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
1660 ** except that it does the right thing even if *tp + j would overflow.
1661 */
1662 if (! (j < 0
1663 ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
1664 : *tp <= time_t_max - j))
1665 return TRUE;
1666 *tp += j;
1667 return FALSE;
Calin Juravle627d37c2014-02-28 11:46:03 +00001668}
1669
1670static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001671normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001672{
Calin Juravled8928922014-02-28 12:18:53 +00001673 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674
Calin Juravled8928922014-02-28 12:18:53 +00001675 tensdelta = (*unitsptr >= 0) ?
1676 (*unitsptr / base) :
1677 (-1 - (-1 - *unitsptr) / base);
1678 *unitsptr -= tensdelta * base;
1679 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001680}
1681
1682static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001683normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr,
Calin Juravled8928922014-02-28 12:18:53 +00001684 const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001685{
Calin Juravled8928922014-02-28 12:18:53 +00001686 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687
Calin Juravled8928922014-02-28 12:18:53 +00001688 tensdelta = (*unitsptr >= 0) ?
1689 (*unitsptr / base) :
1690 (-1 - (-1 - *unitsptr) / base);
1691 *unitsptr -= tensdelta * base;
1692 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001693}
1694
1695static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001696tmcomp(register const struct tm * const atmp,
1697 register const struct tm * const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001698{
Calin Juravled8928922014-02-28 12:18:53 +00001699 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001700
Elliott Hughese0d0b152013-09-27 00:04:30 -07001701 if (atmp->tm_year != btmp->tm_year)
1702 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1703 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001704 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1705 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1706 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1707 result = atmp->tm_sec - btmp->tm_sec;
1708 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001709}
1710
1711static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001712time2sub(struct tm * const tmp,
1713 struct tm *(*const funcp)(const time_t*, int_fast32_t, struct tm*, const struct state*),
1714 const int_fast32_t offset,
1715 int * const okayp,
1716 const int do_norm_secs, const struct state * sp) // android-changed: added sp
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001717{
Calin Juravled8928922014-02-28 12:18:53 +00001718 register int dir;
1719 register int i, j;
1720 register int saved_seconds;
1721 register int_fast32_t li;
1722 register time_t lo;
1723 register time_t hi;
1724 int_fast32_t y;
1725 time_t newt;
1726 time_t t;
1727 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001728
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001729 *okayp = FALSE;
1730 yourtm = *tmp;
1731 if (do_norm_secs) {
1732 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1733 SECSPERMIN))
1734 return WRONG;
1735 }
1736 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1737 return WRONG;
1738 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1739 return WRONG;
1740 y = yourtm.tm_year;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001741 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001742 return WRONG;
1743 /*
1744 ** Turn y into an actual year number for now.
1745 ** It is converted back to an offset from TM_YEAR_BASE later.
1746 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001747 if (increment_overflow32(&y, TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001748 return WRONG;
1749 while (yourtm.tm_mday <= 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001750 if (increment_overflow32(&y, -1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001751 return WRONG;
1752 li = y + (1 < yourtm.tm_mon);
1753 yourtm.tm_mday += year_lengths[isleap(li)];
1754 }
1755 while (yourtm.tm_mday > DAYSPERLYEAR) {
1756 li = y + (1 < yourtm.tm_mon);
1757 yourtm.tm_mday -= year_lengths[isleap(li)];
Elliott Hughesce4783c2013-07-12 17:31:11 -07001758 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001759 return WRONG;
1760 }
1761 for ( ; ; ) {
1762 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1763 if (yourtm.tm_mday <= i)
1764 break;
1765 yourtm.tm_mday -= i;
1766 if (++yourtm.tm_mon >= MONSPERYEAR) {
1767 yourtm.tm_mon = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001768 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001769 return WRONG;
1770 }
1771 }
Elliott Hughesce4783c2013-07-12 17:31:11 -07001772 if (increment_overflow32(&y, -TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001773 return WRONG;
1774 yourtm.tm_year = y;
1775 if (yourtm.tm_year != y)
1776 return WRONG;
1777 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1778 saved_seconds = 0;
1779 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1780 /*
1781 ** We can't set tm_sec to 0, because that might push the
1782 ** time below the minimum representable time.
1783 ** Set tm_sec to 59 instead.
1784 ** This assumes that the minimum representable time is
1785 ** not in the same minute that a leap second was deleted from,
1786 ** which is a safer assumption than using 58 would be.
1787 */
1788 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1789 return WRONG;
1790 saved_seconds = yourtm.tm_sec;
1791 yourtm.tm_sec = SECSPERMIN - 1;
1792 } else {
1793 saved_seconds = yourtm.tm_sec;
1794 yourtm.tm_sec = 0;
1795 }
1796 /*
1797 ** Do a binary search (this works whatever time_t's type is).
1798 */
1799 if (!TYPE_SIGNED(time_t)) {
1800 lo = 0;
1801 hi = lo - 1;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001802 } else {
1803 lo = 1;
1804 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1805 lo *= 2;
1806 hi = -(lo + 1);
1807 }
1808 for ( ; ; ) {
1809 t = lo / 2 + hi / 2;
1810 if (t < lo)
1811 t = lo;
1812 else if (t > hi)
1813 t = hi;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001814 if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001815 /*
1816 ** Assume that t is too extreme to be represented in
1817 ** a struct tm; arrange things so that it is less
1818 ** extreme on the next pass.
1819 */
1820 dir = (t > 0) ? 1 : -1;
1821 } else dir = tmcomp(&mytm, &yourtm);
1822 if (dir != 0) {
1823 if (t == lo) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001824 if (t == time_t_max)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001825 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001826 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001827 ++lo;
1828 } else if (t == hi) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001829 if (t == time_t_min)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001830 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001831 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001832 --hi;
1833 }
1834 if (lo > hi)
1835 return WRONG;
1836 if (dir > 0)
1837 hi = t;
1838 else lo = t;
1839 continue;
1840 }
1841 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1842 break;
1843 /*
1844 ** Right time, wrong type.
1845 ** Hunt for right time, right type.
1846 ** It's okay to guess wrong since the guess
1847 ** gets checked.
1848 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001849 // BEGIN android-changed: support user-supplied sp
1850 if (sp == NULL) {
1851 sp = (const struct state *)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001852 ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001853 }
1854 // END android-changed
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001855 if (sp == NULL)
1856 return WRONG;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001857 for (i = sp->typecnt - 1; i >= 0; --i) {
1858 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1859 continue;
1860 for (j = sp->typecnt - 1; j >= 0; --j) {
1861 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1862 continue;
1863 newt = t + sp->ttis[j].tt_gmtoff -
1864 sp->ttis[i].tt_gmtoff;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001865 if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001866 continue;
1867 if (tmcomp(&mytm, &yourtm) != 0)
1868 continue;
1869 if (mytm.tm_isdst != yourtm.tm_isdst)
1870 continue;
1871 /*
1872 ** We have a match.
1873 */
1874 t = newt;
1875 goto label;
1876 }
1877 }
1878 return WRONG;
1879 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001880label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001881 newt = t + saved_seconds;
1882 if ((newt < t) != (saved_seconds < 0))
1883 return WRONG;
1884 t = newt;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001885 if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001886 *okayp = TRUE;
1887 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001888}
1889
1890static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001891time2(struct tm * const tmp,
1892 struct tm * (*const funcp)(const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1893 const int_fast32_t offset,
1894 int *const okayp, const struct state* sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001895{
Calin Juravled8928922014-02-28 12:18:53 +00001896 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001897
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001898 /*
1899 ** First try without normalization of seconds
1900 ** (in case tm_sec contains a value associated with a leap second).
1901 ** If that fails, try with normalization of seconds.
1902 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001903 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
1904 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001905}
1906
1907static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001908time1(struct tm * const tmp,
1909 struct tm * (* const funcp) (const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1910 const int_fast32_t offset, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001911{
Calin Juravled8928922014-02-28 12:18:53 +00001912 register time_t t;
1913 register int samei, otheri;
1914 register int sameind, otherind;
1915 register int i;
1916 register int nseen;
Elliott Hughes906eb992014-06-18 19:46:25 -07001917 char seen[TZ_MAX_TYPES];
1918 unsigned char types[TZ_MAX_TYPES];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001919 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001920
Elliott Hughesce4783c2013-07-12 17:31:11 -07001921 if (tmp == NULL) {
1922 errno = EINVAL;
1923 return WRONG;
1924 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001925 if (tmp->tm_isdst > 1)
1926 tmp->tm_isdst = 1;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001927 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001928 if (okay)
1929 return t;
1930 if (tmp->tm_isdst < 0)
Elliott Hughes906eb992014-06-18 19:46:25 -07001931#ifdef PCTS
1932 /*
1933 ** POSIX Conformance Test Suite code courtesy Grant Sullivan.
1934 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001935 tmp->tm_isdst = 0; /* reset to std and try again */
Elliott Hughes906eb992014-06-18 19:46:25 -07001936#else
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001937 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001938#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001939 /*
1940 ** We're supposed to assume that somebody took a time of one type
1941 ** and did some math on it that yielded a "struct tm" that's bad.
1942 ** We try to divine the type they started from and adjust to the
1943 ** type they need.
1944 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001945 // BEGIN android-changed: support user-supplied sp.
1946 if (sp == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001947 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001948 }
1949 // BEGIN android-changed
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001950 if (sp == NULL)
1951 return WRONG;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001952 for (i = 0; i < sp->typecnt; ++i)
1953 seen[i] = FALSE;
1954 nseen = 0;
1955 for (i = sp->timecnt - 1; i >= 0; --i)
1956 if (!seen[sp->types[i]]) {
1957 seen[sp->types[i]] = TRUE;
1958 types[nseen++] = sp->types[i];
1959 }
1960 for (sameind = 0; sameind < nseen; ++sameind) {
1961 samei = types[sameind];
1962 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1963 continue;
1964 for (otherind = 0; otherind < nseen; ++otherind) {
1965 otheri = types[otherind];
1966 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1967 continue;
1968 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1969 sp->ttis[samei].tt_gmtoff;
1970 tmp->tm_isdst = !tmp->tm_isdst;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001971 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001972 if (okay)
1973 return t;
1974 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1975 sp->ttis[samei].tt_gmtoff;
1976 tmp->tm_isdst = !tmp->tm_isdst;
1977 }
1978 }
1979 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001980}
1981
1982time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001983mktime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001985 _tzLock();
1986 tzset_locked();
Elliott Hughesce4783c2013-07-12 17:31:11 -07001987 time_t result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001988 _tzUnlock();
1989 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001990}
1991
1992#ifdef STD_INSPIRED
1993
1994time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001995timelocal(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001996{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001997 if (tmp != NULL)
1998 tmp->tm_isdst = -1; /* in case it wasn't initialized */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001999 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002000}
2001
2002time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002003timegm(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002004{
Calin Juravled8928922014-02-28 12:18:53 +00002005 time_t result;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002006
Elliott Hughesce4783c2013-07-12 17:31:11 -07002007 if (tmp != NULL)
2008 tmp->tm_isdst = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002009 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08002010 result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002011 _tzUnlock();
2012
2013 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002014}
2015
Elliott Hughes906eb992014-06-18 19:46:25 -07002016time_t
2017timeoff(struct tm *const tmp, const long offset)
2018{
2019 if (tmp != NULL)
2020 tmp->tm_isdst = 0;
2021 return time1(tmp, gmtsub, offset, NULL); // android-changed: extra parameter.
2022}
2023
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002024#endif /* defined STD_INSPIRED */
2025
2026#ifdef CMUCS
2027
2028/*
2029** The following is supplied for compatibility with
2030** previous versions of the CMUCS runtime library.
2031*/
2032
Elliott Hughese0d0b152013-09-27 00:04:30 -07002033long
Elliott Hughesce4783c2013-07-12 17:31:11 -07002034gtime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002035{
Calin Juravled8928922014-02-28 12:18:53 +00002036 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002038 if (t == WRONG)
2039 return -1;
2040 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002041}
2042
2043#endif /* defined CMUCS */
2044
2045/*
2046** XXX--is the below the right way to conditionalize??
2047*/
2048
2049#ifdef STD_INSPIRED
2050
2051/*
2052** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2053** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2054** is not the case if we are accounting for leap seconds.
2055** So, we provide the following conversion routines for use
2056** when exchanging timestamps with POSIX conforming systems.
2057*/
2058
Elliott Hughesce4783c2013-07-12 17:31:11 -07002059static int_fast64_t
2060leapcorr(time_t * timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002061{
Calin Juravled8928922014-02-28 12:18:53 +00002062 register struct state * sp;
2063 register struct lsinfo * lp;
2064 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002065
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002066 sp = lclptr;
2067 i = sp->leapcnt;
2068 while (--i >= 0) {
2069 lp = &sp->lsis[i];
2070 if (*timep >= lp->ls_trans)
2071 return lp->ls_corr;
2072 }
2073 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074}
2075
2076time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002077time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002078{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002079 tzset();
2080 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002081}
2082
2083time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002084posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002085{
Calin Juravled8928922014-02-28 12:18:53 +00002086 time_t x;
2087 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002088
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002089 tzset();
2090 /*
2091 ** For a positive leap second hit, the result
2092 ** is not unique. For a negative leap second
2093 ** hit, the corresponding time doesn't exist,
2094 ** so we return an adjacent second.
2095 */
2096 x = t + leapcorr(&t);
2097 y = x - leapcorr(&x);
2098 if (y < t) {
2099 do {
2100 x++;
2101 y = x - leapcorr(&x);
2102 } while (y < t);
2103 if (t != y)
2104 return x - 1;
2105 } else if (y > t) {
2106 do {
2107 --x;
2108 y = x - leapcorr(&x);
2109 } while (y > t);
2110 if (t != y)
2111 return x + 1;
2112 }
2113 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002114}
2115
2116#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002117
Elliott Hughesce4783c2013-07-12 17:31:11 -07002118// BEGIN android-added
2119
Elliott Hughes1c295722012-10-19 18:13:15 -07002120#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002121#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002122#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002123
Elliott Hughescf178bf2013-09-18 19:25:28 -07002124static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
2125 const char* olson_id, int* data_size) {
2126 const char* path_prefix = getenv(path_prefix_variable);
2127 if (path_prefix == NULL) {
2128 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2129 return -1;
2130 }
Elliott Hughes329103d2014-04-25 16:55:04 -07002131 size_t path_length = strlen(path_prefix) + 1 + strlen(path_suffix) + 1;
2132 char* path = malloc(path_length);
2133 if (path == NULL) {
2134 fprintf(stderr, "%s: couldn't allocate %zu-byte path\n", __FUNCTION__, path_length);
2135 return -1;
2136 }
2137 snprintf(path, path_length, "%s/%s", path_prefix, path_suffix);
Elliott Hughes1c295722012-10-19 18:13:15 -07002138 int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
Elliott Hughesd23af232012-10-17 16:30:47 -07002139 if (fd == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002140 XLOG(("%s: could not open \"%s\": %s\n", __FUNCTION__, path, strerror(errno)));
Elliott Hughes329103d2014-04-25 16:55:04 -07002141 free(path);
Elliott Hughes1c295722012-10-19 18:13:15 -07002142 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002143 }
2144
2145 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002146 // int index_offset
2147 // int data_offset
2148 // int zonetab_offset
2149 struct bionic_tzdata_header {
2150 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002151 int32_t index_offset;
2152 int32_t data_offset;
2153 int32_t zonetab_offset;
2154 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002155 memset(&header, 0, sizeof(header));
2156 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2157 if (bytes_read != sizeof(header)) {
2158 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2159 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002160 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002161 close(fd);
2162 return -1;
2163 }
2164
2165 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002166 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2167 __FUNCTION__, path, header.tzdata_version);
Elliott Hughes329103d2014-04-25 16:55:04 -07002168 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002169 close(fd);
2170 return -1;
2171 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002172
2173#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002174 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002175 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2176 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2177 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2178#endif
2179
2180 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002181 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2182 __FUNCTION__, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002183 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002184 close(fd);
2185 return -1;
2186 }
2187
2188 off_t specific_zone_offset = -1;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002189 ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
2190 char* index = malloc(index_size);
Elliott Hughes329103d2014-04-25 16:55:04 -07002191 if (index == NULL) {
2192 fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n",
2193 __FUNCTION__, index_size, path);
2194 free(path);
2195 close(fd);
2196 return -1;
2197 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002198 if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) {
2199 fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
2200 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughes329103d2014-04-25 16:55:04 -07002201 free(path);
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002202 free(index);
2203 close(fd);
2204 return -1;
2205 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002206
Elliott Hughes1c295722012-10-19 18:13:15 -07002207 static const size_t NAME_LENGTH = 40;
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002208 struct index_entry_t {
2209 char buf[NAME_LENGTH];
2210 int32_t start;
2211 int32_t length;
2212 int32_t raw_gmt_offset;
2213 };
Elliott Hughese0175ca2013-03-14 14:38:08 -07002214
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002215 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(struct index_entry_t);
2216 struct index_entry_t* entry = (struct index_entry_t*) index;
Elliott Hughese0175ca2013-03-14 14:38:08 -07002217 for (size_t i = 0; i < id_count; ++i) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002218 char this_id[NAME_LENGTH + 1];
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002219 memcpy(this_id, entry->buf, NAME_LENGTH);
Elliott Hughes1c295722012-10-19 18:13:15 -07002220 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002221
2222 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002223 specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset);
2224 *data_size = ntohl(entry->length);
Elliott Hughesd23af232012-10-17 16:30:47 -07002225 break;
2226 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002227
2228 ++entry;
Elliott Hughesd23af232012-10-17 16:30:47 -07002229 }
Elliott Hughesfd3a9fb2014-02-27 18:18:25 -08002230 free(index);
Elliott Hughesd23af232012-10-17 16:30:47 -07002231
2232 if (specific_zone_offset == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002233 XLOG(("%s: couldn't find zone \"%s\"\n", __FUNCTION__, olson_id));
Elliott Hughes329103d2014-04-25 16:55:04 -07002234 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002235 close(fd);
2236 return -1;
2237 }
2238
2239 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002240 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2241 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughes329103d2014-04-25 16:55:04 -07002242 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002243 close(fd);
2244 return -1;
2245 }
2246
Elliott Hughese7aaad82013-04-25 14:02:59 -07002247 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2248
Elliott Hughes329103d2014-04-25 16:55:04 -07002249 free(path);
Elliott Hughesd23af232012-10-17 16:30:47 -07002250 return fd;
2251}
Elliott Hughes1c295722012-10-19 18:13:15 -07002252
2253static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002254 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002255 if (fd < 0) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002256 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002257 if (fd == -2) {
Elliott Hughes49271d82012-10-25 14:38:51 -07002258 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2259 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2260 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
Elliott Hughes1c295722012-10-19 18:13:15 -07002261 }
2262 }
2263 return fd;
2264}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002265
2266// Caches the most recent timezone (http://b/8270865).
2267static int __bionic_tzload_cached(const char* name, struct state* const sp, const int doextend) {
2268 _tzLock();
2269
2270 // Our single-item cache.
Elliott Hughes1728b232014-05-14 10:02:03 -07002271 static char* g_cached_time_zone_name;
2272 static struct state g_cached_time_zone;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002273
2274 // Do we already have this timezone cached?
Elliott Hughes1728b232014-05-14 10:02:03 -07002275 if (g_cached_time_zone_name != NULL && strcmp(name, g_cached_time_zone_name) == 0) {
2276 *sp = g_cached_time_zone;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002277 _tzUnlock();
2278 return 0;
2279 }
2280
2281 // Can we load it?
2282 int rc = tzload(name, sp, doextend);
2283 if (rc == 0) {
2284 // Update the cache.
Elliott Hughes1728b232014-05-14 10:02:03 -07002285 free(g_cached_time_zone_name);
2286 g_cached_time_zone_name = strdup(name);
2287 g_cached_time_zone = *sp;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002288 }
2289
2290 _tzUnlock();
2291 return rc;
2292}
2293
2294// Non-standard API: mktime(3) but with an explicit timezone parameter.
2295time_t mktime_tz(struct tm* const tmp, const char* tz) {
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002296 struct state* st = malloc(sizeof(*st));
2297 time_t return_value;
2298
2299 if (st == NULL)
2300 return 0;
2301 if (__bionic_tzload_cached(tz, st, TRUE) != 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07002302 // TODO: not sure what's best here, but for now, we fall back to gmt.
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002303 gmtload(st);
Elliott Hughesce4783c2013-07-12 17:31:11 -07002304 }
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002305
2306 return_value = time1(tmp, localsub, 0L, st);
2307 free(st);
2308 return return_value;
Elliott Hughesce4783c2013-07-12 17:31:11 -07002309}
2310
2311// Non-standard API: localtime(3) but with an explicit timezone parameter.
2312void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz) {
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002313 struct state* st = malloc(sizeof(*st));
2314
2315 if (st == NULL)
2316 return;
2317 if (__bionic_tzload_cached(tz, st, TRUE) != 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07002318 // TODO: not sure what's best here, but for now, we fall back to gmt.
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002319 gmtload(st);
Elliott Hughesce4783c2013-07-12 17:31:11 -07002320 }
Christopher Ferris8a8b0c92014-05-14 16:06:17 -07002321 localsub(timep, 0L, tmp, st);
2322 free(st);
Elliott Hughesce4783c2013-07-12 17:31:11 -07002323}
2324
2325// END android-added