blob: a52e334a30005494d6e4a7181a58ed4854837ca0 [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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070077static char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070079static 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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070092struct 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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700100struct 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 {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700115 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];
Elliott Hughesce4783c2013-07-12 17:31:11 -0700127 int defaulttype; /* for early times or if no transitions */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128};
129
130struct rule {
Elliott Hughesce4783c2013-07-12 17:31:11 -0700131 int r_type; /* type of rule--see below */
132 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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -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);
150static time_t detzcode64(const char * codep);
151static 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);
161static struct tm * gmtsub(const time_t * timep, const int_fast32_t offset,
162 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);
168static int normalize_overflow32(int_fast32_t * tensptr,
169 int * unitsptr, int base);
170static int normalize_overflow(int * tensptr, int * unitsptr,
171 int base);
172static void settzname(void);
173static time_t time1(struct tm * tmp,
174 struct tm * (*funcp)(const time_t *,
175 int_fast32_t, struct tm *, const struct state *), // android-changed: added state*.
176 int_fast32_t offset, const struct state * sp); // android-changed: added sp.
177static time_t time2(struct tm * const tmp,
178 struct tm * (*const funcp)(const time_t *,
179 int_fast32_t, struct tm*, const struct state *), // android-changed: added state*.
180 int_fast32_t offset, int * okayp, const struct state * sp); // android-changed: added sp.
181static time_t time2sub(struct tm *tmp,
182 struct tm * (*funcp) (const time_t *,
183 int_fast32_t, struct tm*, const struct state *), // android-changed: added state*.
184 int_fast32_t offset, int * okayp, int do_norm_secs, const struct state * sp); // android-change: added sp.
185static struct tm * timesub(const time_t * timep, int_fast32_t offset,
186 const struct state * sp, struct tm * tmp);
187static int tmcomp(const struct tm * atmp,
188 const struct tm * btmp);
189static time_t transtime(time_t janfirst, int year,
190 const struct rule * rulep, int_fast32_t offset)
191 ATTRIBUTE_PURE;
192static int typesequiv(const struct state * sp, int a, int b);
193static int tzload(const char * name, struct state * sp,
194 int doextend);
195static int tzparse(const char * name, struct state * sp,
196 int lastditch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
198#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700199static struct state * lclptr;
200static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201#endif /* defined ALL_STATE */
202
203#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700204static struct state lclmem;
205static struct state gmtmem;
206#define lclptr (&lclmem)
207#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208#endif /* State Farm */
209
210#ifndef TZ_STRLEN_MAX
211#define TZ_STRLEN_MAX 255
212#endif /* !defined TZ_STRLEN_MAX */
213
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700214static char lcl_TZname[TZ_STRLEN_MAX + 1];
215static int lcl_is_set;
216static int gmt_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700218char * tzname[2] = {
219 wildabbr,
220 wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221};
222
223/*
224** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700225** Except for the strftime function, these functions [asctime,
226** ctime, gmtime, localtime] return values in one of two static
227** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228** Thanks to Paul Eggert for noting this.
229*/
230
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700231static struct tm tmGlobal;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
233#ifdef USG_COMPAT
Elliott Hughese0d0b152013-09-27 00:04:30 -0700234long timezone = 0;
235int daylight = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236#endif /* defined USG_COMPAT */
237
238#ifdef ALTZONE
Elliott Hughese0d0b152013-09-27 00:04:30 -0700239long altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240#endif /* defined ALTZONE */
241
Elliott Hughesce4783c2013-07-12 17:31:11 -0700242static int_fast32_t
243detzcode(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244{
Elliott Hughesce4783c2013-07-12 17:31:11 -0700245 register int_fast32_t result;
246 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247
Elliott Hughesce4783c2013-07-12 17:31:11 -0700248 result = (codep[0] & 0x80) ? -1 : 0;
249 for (i = 0; i < 4; ++i)
250 result = (result << 8) | (codep[i] & 0xff);
251 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252}
253
254static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700255detzcode64(const char *const codep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256{
Elliott Hughesce4783c2013-07-12 17:31:11 -0700257 register time_t result;
258 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259
Elliott Hughesce4783c2013-07-12 17:31:11 -0700260 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
261 for (i = 0; i < 8; ++i)
262 result = result * 256 + (codep[i] & 0xff);
263 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264}
265
266static void
Elliott Hughesce4783c2013-07-12 17:31:11 -0700267settzname(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700269 register struct state * const sp = lclptr;
270 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700272 tzname[0] = wildabbr;
273 tzname[1] = 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 */
281#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700282 if (sp == NULL) {
283 tzname[0] = tzname[1] = gmt;
284 return;
285 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700287 /*
288 ** And to get the latest zone names into tzname. . .
289 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700290 for (i = 0; i < sp->typecnt; ++i) {
291 register const struct ttinfo * const ttisp = &sp->ttis[i];
292
293 tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind];
294 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700295 for (i = 0; i < sp->timecnt; ++i) {
296 register const struct ttinfo * const ttisp =
297 &sp->ttis[
298 sp->types[i]];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700300 tzname[ttisp->tt_isdst] =
301 &sp->chars[ttisp->tt_abbrind];
Elliott Hughesce4783c2013-07-12 17:31:11 -0700302#ifdef USG_COMPAT
303 if (ttisp->tt_isdst)
304 daylight = 1;
305 if (!ttisp->tt_isdst)
306 timezone = -(ttisp->tt_gmtoff);
307#endif /* defined USG_COMPAT */
308#ifdef ALTZONE
309 if (ttisp->tt_isdst)
310 altzone = -(ttisp->tt_gmtoff);
311#endif /* defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700312 }
313 /*
314 ** Finally, scrub the abbreviations.
315 ** First, replace bogus characters.
316 */
317 for (i = 0; i < sp->charcnt; ++i)
318 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
319 sp->chars[i] = TZ_ABBR_ERR_CHAR;
320 /*
321 ** Second, truncate long abbreviations.
322 */
323 for (i = 0; i < sp->typecnt; ++i) {
324 register const struct ttinfo * const ttisp = &sp->ttis[i];
325 register char * cp = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700327 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
328 strcmp(cp, GRANDPARENTED) != 0)
329 *(cp + TZ_ABBR_MAX_LEN) = '\0';
330 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331}
332
333static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700334differ_by_repeat(const time_t t1, const time_t t0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335{
Elliott Hughese0d0b152013-09-27 00:04:30 -0700336 if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700337 return 0;
338 return t1 - t0 == SECSPERREPEAT;
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{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700345 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 {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700351 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;
356#ifdef ALL_STATE
357 register u_t * up;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358
Elliott Hughesce4783c2013-07-12 17:31:11 -0700359 up = (u_t *) calloc(1, sizeof *up);
360 if (up == NULL)
361 return -1;
362#else /* !defined ALL_STATE */
363 u_t u;
364 register u_t * const up = &u;
365#endif /* !defined ALL_STATE */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366
Elliott Hughesce4783c2013-07-12 17:31:11 -0700367 sp->goback = sp->goahead = FALSE;
368 if (name == NULL && (name = TZDEFAULT) == NULL)
369 goto oops;
370 int toread;
371 fid = __bionic_open_tzdata(name, &toread);
372 if (fid < 0) {
373 return -1;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700374 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700375 nread = read(fid, up->buf, toread);
376 if (close(fid) < 0 || nread <= 0)
377 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700378 for (stored = 4; stored <= 8; stored *= 2) {
379 int ttisstdcnt;
380 int ttisgmtcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381
Elliott Hughesce4783c2013-07-12 17:31:11 -0700382 ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt);
383 ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt);
384 sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt);
385 sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt);
386 sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt);
387 sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt);
388 p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700389 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
390 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
391 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
392 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
393 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
394 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700395 goto oops;
396 if (nread - (p - up->buf) <
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700397 sp->timecnt * stored + /* ats */
398 sp->timecnt + /* types */
399 sp->typecnt * 6 + /* ttinfos */
400 sp->charcnt + /* chars */
401 sp->leapcnt * (stored + 4) + /* lsinfos */
402 ttisstdcnt + /* ttisstds */
403 ttisgmtcnt) /* ttisgmts */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700404 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700405 for (i = 0; i < sp->timecnt; ++i) {
406 sp->ats[i] = (stored == 4) ?
407 detzcode(p) : detzcode64(p);
408 p += stored;
409 }
410 for (i = 0; i < sp->timecnt; ++i) {
411 sp->types[i] = (unsigned char) *p++;
412 if (sp->types[i] >= sp->typecnt)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700413 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700414 }
415 for (i = 0; i < sp->typecnt; ++i) {
416 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700418 ttisp = &sp->ttis[i];
419 ttisp->tt_gmtoff = detzcode(p);
420 p += 4;
421 ttisp->tt_isdst = (unsigned char) *p++;
422 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700423 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700424 ttisp->tt_abbrind = (unsigned char) *p++;
425 if (ttisp->tt_abbrind < 0 ||
426 ttisp->tt_abbrind > sp->charcnt)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700427 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700428 }
429 for (i = 0; i < sp->charcnt; ++i)
430 sp->chars[i] = *p++;
431 sp->chars[i] = '\0'; /* ensure '\0' at end */
432 for (i = 0; i < sp->leapcnt; ++i) {
433 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700435 lsisp = &sp->lsis[i];
436 lsisp->ls_trans = (stored == 4) ?
437 detzcode(p) : detzcode64(p);
438 p += stored;
439 lsisp->ls_corr = detzcode(p);
440 p += 4;
441 }
442 for (i = 0; i < sp->typecnt; ++i) {
443 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700445 ttisp = &sp->ttis[i];
446 if (ttisstdcnt == 0)
447 ttisp->tt_ttisstd = FALSE;
448 else {
449 ttisp->tt_ttisstd = *p++;
450 if (ttisp->tt_ttisstd != TRUE &&
451 ttisp->tt_ttisstd != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700452 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700453 }
454 }
455 for (i = 0; i < sp->typecnt; ++i) {
456 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700458 ttisp = &sp->ttis[i];
459 if (ttisgmtcnt == 0)
460 ttisp->tt_ttisgmt = FALSE;
461 else {
462 ttisp->tt_ttisgmt = *p++;
463 if (ttisp->tt_ttisgmt != TRUE &&
464 ttisp->tt_ttisgmt != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700465 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700466 }
467 }
468 /*
469 ** Out-of-sort ats should mean we're running on a
470 ** signed time_t system but using a data file with
471 ** unsigned values (or vice versa).
472 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700473 for (i = 0; i < sp->timecnt; ++i)
474 if ((i < sp->timecnt - 1 &&
475 sp->ats[i] > sp->ats[i + 1]) ||
476 (i == sp->timecnt - 1 && !TYPE_SIGNED(time_t) &&
477 sp->ats[i] >
478 ((stored == 4) ? INT32_MAX : INT64_MAX))) {
479 if (TYPE_SIGNED(time_t)) {
480 /*
481 ** Ignore the end (easy).
482 */
483 sp->timecnt = i + 1;
484 } else {
485 /*
486 ** Ignore the beginning (harder).
487 */
488 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800489
Elliott Hughesce4783c2013-07-12 17:31:11 -0700490 /*
491 ** Keep the record right before the
492 ** epoch boundary,
493 ** but tweak it so that it starts
494 ** right with the epoch
495 ** (thanks to Doug Bailey).
496 */
497 sp->ats[i] = 0;
498 for (j = 0; j + i < sp->timecnt; ++j) {
499 sp->ats[j] = sp->ats[j + i];
500 sp->types[j] = sp->types[j + i];
501 }
502 sp->timecnt = j;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700503 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700504 break;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700505 }
506 /*
507 ** If this is an old file, we're done.
508 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700509 if (up->tzhead.tzh_version[0] == '\0')
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700510 break;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700511 nread -= p - up->buf;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700512 for (i = 0; i < nread; ++i)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700513 up->buf[i] = p[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700514 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700515 ** If this is a narrow time_t system, we're done.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700516 */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700517 if (stored >= (int) sizeof(time_t))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700518 break;
519 }
520 if (doextend && nread > 2 &&
Elliott Hughesce4783c2013-07-12 17:31:11 -0700521 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700522 sp->typecnt + 2 <= TZ_MAX_TYPES) {
523 struct state ts;
524 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800525
Elliott Hughesce4783c2013-07-12 17:31:11 -0700526 up->buf[nread - 1] = '\0';
527 result = tzparse(&up->buf[1], &ts, FALSE);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700528 if (result == 0 && ts.typecnt == 2 &&
529 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
530 for (i = 0; i < 2; ++i)
531 ts.ttis[i].tt_abbrind +=
532 sp->charcnt;
533 for (i = 0; i < ts.charcnt; ++i)
534 sp->chars[sp->charcnt++] =
535 ts.chars[i];
536 i = 0;
537 while (i < ts.timecnt &&
538 ts.ats[i] <=
539 sp->ats[sp->timecnt - 1])
540 ++i;
541 while (i < ts.timecnt &&
542 sp->timecnt < TZ_MAX_TIMES) {
543 sp->ats[sp->timecnt] =
544 ts.ats[i];
545 sp->types[sp->timecnt] =
546 sp->typecnt +
547 ts.types[i];
548 ++sp->timecnt;
549 ++i;
550 }
551 sp->ttis[sp->typecnt++] = ts.ttis[0];
552 sp->ttis[sp->typecnt++] = ts.ttis[1];
553 }
554 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700555 if (sp->timecnt > 1) {
556 for (i = 1; i < sp->timecnt; ++i)
557 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
558 differ_by_repeat(sp->ats[i], sp->ats[0])) {
559 sp->goback = TRUE;
560 break;
561 }
562 for (i = sp->timecnt - 2; i >= 0; --i)
563 if (typesequiv(sp, sp->types[sp->timecnt - 1],
564 sp->types[i]) &&
565 differ_by_repeat(sp->ats[sp->timecnt - 1],
566 sp->ats[i])) {
567 sp->goahead = TRUE;
568 break;
569 }
570 }
571 /*
572 ** If type 0 is is unused in transitions,
573 ** it's the type to use for early times.
574 */
575 for (i = 0; i < sp->typecnt; ++i)
576 if (sp->types[i] == 0)
577 break;
578 i = (i >= sp->typecnt) ? 0 : -1;
579 /*
580 ** Absent the above,
581 ** if there are transition times
582 ** and the first transition is to a daylight time
583 ** find the standard type less than and closest to
584 ** the type of the first transition.
585 */
586 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
587 i = sp->types[0];
588 while (--i >= 0)
589 if (!sp->ttis[i].tt_isdst)
590 break;
591 }
592 /*
593 ** If no result yet, find the first standard type.
594 ** If there is none, punt to type zero.
595 */
596 if (i < 0) {
597 i = 0;
598 while (sp->ttis[i].tt_isdst)
599 if (++i >= sp->typecnt) {
600 i = 0;
601 break;
602 }
603 }
604 sp->defaulttype = i;
605#ifdef ALL_STATE
606 free(up);
607#endif /* defined ALL_STATE */
608 return 0;
609oops:
610#ifdef ALL_STATE
611 free(up);
612#endif /* defined ALL_STATE */
613 return -1;
614}
615
616static int
617typesequiv(const struct state *const sp, const int a, const int b)
618{
619 register int result;
620
621 if (sp == NULL ||
622 a < 0 || a >= sp->typecnt ||
623 b < 0 || b >= sp->typecnt)
624 result = FALSE;
625 else {
626 register const struct ttinfo * ap = &sp->ttis[a];
627 register const struct ttinfo * bp = &sp->ttis[b];
628 result = ap->tt_gmtoff == bp->tt_gmtoff &&
629 ap->tt_isdst == bp->tt_isdst &&
630 ap->tt_ttisstd == bp->tt_ttisstd &&
631 ap->tt_ttisgmt == bp->tt_ttisgmt &&
632 strcmp(&sp->chars[ap->tt_abbrind],
633 &sp->chars[bp->tt_abbrind]) == 0;
634 }
635 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636}
637
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700638static const int mon_lengths[2][MONSPERYEAR] = {
639 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
640 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641};
642
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700643static const int year_lengths[2] = {
644 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800645};
646
647/*
648** Given a pointer into a time zone string, scan until a character that is not
649** a valid character in a zone name is found. Return a pointer to that
650** character.
651*/
652
653static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700654getzname(register const char * strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700656 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700658 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
659 c != '+')
660 ++strp;
661 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662}
663
664/*
665** Given a pointer into an extended time zone string, scan until the ending
666** delimiter of the zone name is located. Return a pointer to the delimiter.
667**
668** As with getzname above, the legal character set is actually quite
669** restricted, with other characters producing undefined results.
670** We don't do any checking here; checking is done later in common-case code.
671*/
672
673static const char *
674getqzname(register const char *strp, const int delim)
675{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700676 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700678 while ((c = *strp) != '\0' && c != delim)
679 ++strp;
680 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681}
682
683/*
684** Given a pointer into a time zone string, extract a number from that string.
685** Check that the number is within a specified range; if it is not, return
686** NULL.
687** Otherwise, return a pointer to the first character not part of the number.
688*/
689
690static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700691getnum(register const char * strp, int * const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700693 register char c;
694 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700696 if (strp == NULL || !is_digit(c = *strp))
697 return NULL;
698 num = 0;
699 do {
700 num = num * 10 + (c - '0');
701 if (num > max)
702 return NULL; /* illegal value */
703 c = *++strp;
704 } while (is_digit(c));
705 if (num < min)
706 return NULL; /* illegal value */
707 *nump = num;
708 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709}
710
711/*
712** Given a pointer into a time zone string, extract a number of seconds,
713** in hh[:mm[:ss]] form, from the string.
714** If any error occurs, return NULL.
715** Otherwise, return a pointer to the first character not part of the number
716** of seconds.
717*/
718
719static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700720getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700722 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700724 /*
725 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
726 ** "M10.4.6/26", which does not conform to Posix,
727 ** but which specifies the equivalent of
728 ** ``02:00 on the first Sunday on or after 23 Oct''.
729 */
730 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
731 if (strp == NULL)
732 return NULL;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700733 *secsp = num * (int_fast32_t) SECSPERHOUR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700734 if (*strp == ':') {
735 ++strp;
736 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
737 if (strp == NULL)
738 return NULL;
739 *secsp += num * SECSPERMIN;
740 if (*strp == ':') {
741 ++strp;
742 /* `SECSPERMIN' allows for leap seconds. */
743 strp = getnum(strp, &num, 0, SECSPERMIN);
744 if (strp == NULL)
745 return NULL;
746 *secsp += num;
747 }
748 }
749 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750}
751
752/*
753** Given a pointer into a time zone string, extract an offset, in
754** [+-]hh[:mm[:ss]] form, from the string.
755** If any error occurs, return NULL.
756** Otherwise, return a pointer to the first character not part of the time.
757*/
758
759static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700760getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800761{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700762 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700764 if (*strp == '-') {
765 neg = 1;
766 ++strp;
767 } else if (*strp == '+')
768 ++strp;
769 strp = getsecs(strp, offsetp);
770 if (strp == NULL)
771 return NULL; /* illegal time */
772 if (neg)
773 *offsetp = -*offsetp;
774 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800775}
776
777/*
778** Given a pointer into a time zone string, extract a rule in the form
779** date[/time]. See POSIX section 8 for the format of "date" and "time".
780** If a valid rule is not found, return NULL.
781** Otherwise, return a pointer to the first character not part of the rule.
782*/
783
784static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700785getrule(const char * strp, register struct rule * const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700787 if (*strp == 'J') {
788 /*
789 ** Julian day.
790 */
791 rulep->r_type = JULIAN_DAY;
792 ++strp;
793 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
794 } else if (*strp == 'M') {
795 /*
796 ** Month, week, day.
797 */
798 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
799 ++strp;
800 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
801 if (strp == NULL)
802 return NULL;
803 if (*strp++ != '.')
804 return NULL;
805 strp = getnum(strp, &rulep->r_week, 1, 5);
806 if (strp == NULL)
807 return NULL;
808 if (*strp++ != '.')
809 return NULL;
810 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
811 } else if (is_digit(*strp)) {
812 /*
813 ** Day of year.
814 */
815 rulep->r_type = DAY_OF_YEAR;
816 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
817 } else return NULL; /* invalid format */
818 if (strp == NULL)
819 return NULL;
820 if (*strp == '/') {
821 /*
822 ** Time specified.
823 */
824 ++strp;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700825 strp = getoffset(strp, &rulep->r_time);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700826 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
827 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800828}
829
830/*
831** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
Elliott Hughese0d0b152013-09-27 00:04:30 -0700832** year, a rule, and the offset from UT at the time that rule takes effect,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800833** calculate the Epoch-relative time that rule takes effect.
834*/
835
836static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700837transtime(const time_t janfirst, const int year,
838 register const struct rule *const rulep, const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800839{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700840 register int leapyear;
841 register time_t value;
842 register int i;
843 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700845 INITIALIZE(value);
846 leapyear = isleap(year);
847 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800848
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700849 case JULIAN_DAY:
850 /*
851 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
852 ** years.
853 ** In non-leap years, or if the day number is 59 or less, just
854 ** add SECSPERDAY times the day number-1 to the time of
855 ** January 1, midnight, to get the day.
856 */
857 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
858 if (leapyear && rulep->r_day >= 60)
859 value += SECSPERDAY;
860 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800861
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700862 case DAY_OF_YEAR:
863 /*
864 ** n - day of year.
865 ** Just add SECSPERDAY times the day number to the time of
866 ** January 1, midnight, to get the day.
867 */
868 value = janfirst + rulep->r_day * SECSPERDAY;
869 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800870
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700871 case MONTH_NTH_DAY_OF_WEEK:
872 /*
873 ** Mm.n.d - nth "dth day" of month m.
874 */
875 value = janfirst;
876 for (i = 0; i < rulep->r_mon - 1; ++i)
877 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700879 /*
880 ** Use Zeller's Congruence to get day-of-week of first day of
881 ** month.
882 */
883 m1 = (rulep->r_mon + 9) % 12 + 1;
884 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
885 yy1 = yy0 / 100;
886 yy2 = yy0 % 100;
887 dow = ((26 * m1 - 2) / 10 +
888 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
889 if (dow < 0)
890 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800891
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700892 /*
893 ** "dow" is the day-of-week of the first day of the month. Get
894 ** the day-of-month (zero-origin) of the first "dow" day of the
895 ** month.
896 */
897 d = rulep->r_day - dow;
898 if (d < 0)
899 d += DAYSPERWEEK;
900 for (i = 1; i < rulep->r_week; ++i) {
901 if (d + DAYSPERWEEK >=
902 mon_lengths[leapyear][rulep->r_mon - 1])
903 break;
904 d += DAYSPERWEEK;
905 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800906
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700907 /*
908 ** "d" is the day-of-month (zero-origin) of the day we want.
909 */
910 value += d * SECSPERDAY;
911 break;
912 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800913
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700914 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700915 ** "value" is the Epoch-relative time of 00:00:00 UT on the day in
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700916 ** question. To get the Epoch-relative time of the specified local
917 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -0700918 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700919 */
920 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800921}
922
923/*
924** Given a POSIX section 8-style TZ string, fill in the rule tables as
925** appropriate.
926*/
927
928static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700929tzparse(const char * name, register struct state * const sp,
930 const int lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700932 const char * stdname;
933 const char * dstname;
934 size_t stdlen;
935 size_t dstlen;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700936 int_fast32_t stdoffset;
937 int_fast32_t dstoffset;
938 register time_t * atp;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700939 register unsigned char * typep;
940 register char * cp;
941 register int load_result;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700942 static struct ttinfo zttinfo;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700944 INITIALIZE(dstname);
945 stdname = name;
946 if (lastditch) {
947 stdlen = strlen(name); /* length of standard zone name */
948 name += stdlen;
949 if (stdlen >= sizeof sp->chars)
950 stdlen = (sizeof sp->chars) - 1;
951 stdoffset = 0;
952 } else {
953 if (*name == '<') {
954 name++;
955 stdname = name;
956 name = getqzname(name, '>');
957 if (*name != '>')
958 return (-1);
959 stdlen = name - stdname;
960 name++;
961 } else {
962 name = getzname(name);
963 stdlen = name - stdname;
964 }
965 if (*name == '\0')
966 return -1;
967 name = getoffset(name, &stdoffset);
968 if (name == NULL)
969 return -1;
970 }
971 load_result = tzload(TZDEFRULES, sp, FALSE);
972 if (load_result != 0)
973 sp->leapcnt = 0; /* so, we're off a little */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700974 if (*name != '\0') {
975 if (*name == '<') {
976 dstname = ++name;
977 name = getqzname(name, '>');
978 if (*name != '>')
979 return -1;
980 dstlen = name - dstname;
981 name++;
982 } else {
983 dstname = name;
984 name = getzname(name);
985 dstlen = name - dstname; /* length of DST zone name */
986 }
987 if (*name != '\0' && *name != ',' && *name != ';') {
988 name = getoffset(name, &dstoffset);
989 if (name == NULL)
990 return -1;
991 } else dstoffset = stdoffset - SECSPERHOUR;
992 if (*name == '\0' && load_result != 0)
993 name = TZDEFRULESTRING;
994 if (*name == ',' || *name == ';') {
995 struct rule start;
996 struct rule end;
997 register int year;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700998 register int yearlim;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700999 register time_t janfirst;
1000 time_t starttime;
1001 time_t endtime;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001002
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001003 ++name;
1004 if ((name = getrule(name, &start)) == NULL)
1005 return -1;
1006 if (*name++ != ',')
1007 return -1;
1008 if ((name = getrule(name, &end)) == NULL)
1009 return -1;
1010 if (*name != '\0')
1011 return -1;
1012 sp->typecnt = 2; /* standard time and DST */
1013 /*
1014 ** Two transitions per year, from EPOCH_YEAR forward.
1015 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001016 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001017 sp->ttis[0].tt_gmtoff = -dstoffset;
1018 sp->ttis[0].tt_isdst = 1;
1019 sp->ttis[0].tt_abbrind = stdlen + 1;
1020 sp->ttis[1].tt_gmtoff = -stdoffset;
1021 sp->ttis[1].tt_isdst = 0;
1022 sp->ttis[1].tt_abbrind = 0;
1023 atp = sp->ats;
1024 typep = sp->types;
1025 janfirst = 0;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001026 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1027 for (year = EPOCH_YEAR; year < yearlim; year++) {
1028 int_fast32_t yearsecs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001029
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001030 starttime = transtime(janfirst, year, &start,
1031 stdoffset);
1032 endtime = transtime(janfirst, year, &end,
1033 dstoffset);
Elliott Hughese0d0b152013-09-27 00:04:30 -07001034 yearsecs = (year_lengths[isleap(year)]
1035 * SECSPERDAY);
1036 if (starttime > endtime
1037 || (starttime < endtime
1038 && (endtime - starttime
1039 < (yearsecs
1040 + (stdoffset - dstoffset))))) {
1041 if (&sp->ats[TZ_MAX_TIMES - 2] < atp)
1042 break;
1043 yearlim = year + YEARSPERREPEAT + 1;
1044 if (starttime > endtime) {
1045 *atp++ = endtime;
1046 *typep++ = 1; /* DST ends */
1047 *atp++ = starttime;
1048 *typep++ = 0; /* DST begins */
1049 } else {
1050 *atp++ = starttime;
1051 *typep++ = 0; /* DST begins */
1052 *atp++ = endtime;
1053 *typep++ = 1; /* DST ends */
1054 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001055 }
Elliott Hughese0d0b152013-09-27 00:04:30 -07001056 if (time_t_max - janfirst < yearsecs)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001057 break;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001058 janfirst += yearsecs;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001059 }
Elliott Hughese0d0b152013-09-27 00:04:30 -07001060 sp->timecnt = atp - sp->ats;
1061 if (!sp->timecnt)
1062 sp->typecnt = 1; /* Perpetual DST. */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001063 } else {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001064 register int_fast32_t theirstdoffset;
1065 register int_fast32_t theirdstoffset;
1066 register int_fast32_t theiroffset;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001067 register int isdst;
1068 register int i;
1069 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001070
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001071 if (*name != '\0')
1072 return -1;
1073 /*
1074 ** Initial values of theirstdoffset and theirdstoffset.
1075 */
1076 theirstdoffset = 0;
1077 for (i = 0; i < sp->timecnt; ++i) {
1078 j = sp->types[i];
1079 if (!sp->ttis[j].tt_isdst) {
1080 theirstdoffset =
1081 -sp->ttis[j].tt_gmtoff;
1082 break;
1083 }
1084 }
1085 theirdstoffset = 0;
1086 for (i = 0; i < sp->timecnt; ++i) {
1087 j = sp->types[i];
1088 if (sp->ttis[j].tt_isdst) {
1089 theirdstoffset =
1090 -sp->ttis[j].tt_gmtoff;
1091 break;
1092 }
1093 }
1094 /*
1095 ** Initially we're assumed to be in standard time.
1096 */
1097 isdst = FALSE;
1098 theiroffset = theirstdoffset;
1099 /*
1100 ** Now juggle transition times and types
1101 ** tracking offsets as you do.
1102 */
1103 for (i = 0; i < sp->timecnt; ++i) {
1104 j = sp->types[i];
1105 sp->types[i] = sp->ttis[j].tt_isdst;
1106 if (sp->ttis[j].tt_ttisgmt) {
1107 /* No adjustment to transition time */
1108 } else {
1109 /*
1110 ** If summer time is in effect, and the
1111 ** transition time was not specified as
1112 ** standard time, add the summer time
1113 ** offset to the transition time;
1114 ** otherwise, add the standard time
1115 ** offset to the transition time.
1116 */
1117 /*
1118 ** Transitions from DST to DDST
1119 ** will effectively disappear since
1120 ** POSIX provides for only one DST
1121 ** offset.
1122 */
1123 if (isdst && !sp->ttis[j].tt_ttisstd) {
1124 sp->ats[i] += dstoffset -
1125 theirdstoffset;
1126 } else {
1127 sp->ats[i] += stdoffset -
1128 theirstdoffset;
1129 }
1130 }
1131 theiroffset = -sp->ttis[j].tt_gmtoff;
1132 if (sp->ttis[j].tt_isdst)
1133 theirdstoffset = theiroffset;
1134 else theirstdoffset = theiroffset;
1135 }
1136 /*
1137 ** Finally, fill in ttis.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001138 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001139 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001140 sp->ttis[0].tt_gmtoff = -stdoffset;
1141 sp->ttis[0].tt_isdst = FALSE;
1142 sp->ttis[0].tt_abbrind = 0;
1143 sp->ttis[1].tt_gmtoff = -dstoffset;
1144 sp->ttis[1].tt_isdst = TRUE;
1145 sp->ttis[1].tt_abbrind = stdlen + 1;
1146 sp->typecnt = 2;
1147 }
1148 } else {
1149 dstlen = 0;
1150 sp->typecnt = 1; /* only standard time */
1151 sp->timecnt = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001152 sp->ttis[0] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001153 sp->ttis[0].tt_gmtoff = -stdoffset;
1154 sp->ttis[0].tt_isdst = 0;
1155 sp->ttis[0].tt_abbrind = 0;
1156 }
1157 sp->charcnt = stdlen + 1;
1158 if (dstlen != 0)
1159 sp->charcnt += dstlen + 1;
1160 if ((size_t) sp->charcnt > sizeof sp->chars)
1161 return -1;
1162 cp = sp->chars;
1163 (void) strncpy(cp, stdname, stdlen);
1164 cp += stdlen;
1165 *cp++ = '\0';
1166 if (dstlen != 0) {
1167 (void) strncpy(cp, dstname, dstlen);
1168 *(cp + dstlen) = '\0';
1169 }
1170 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001171}
1172
1173static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001174gmtload(struct state * const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001175{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001176 if (tzload(gmt, sp, TRUE) != 0)
1177 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001178}
1179
Elliott Hughesce4783c2013-07-12 17:31:11 -07001180#ifndef STD_INSPIRED
1181/*
1182** A non-static declaration of tzsetwall in a system header file
1183** may cause a warning about this upcoming static declaration...
1184*/
1185static
1186#endif /* !defined STD_INSPIRED */
1187void
1188tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001189{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001190 if (lcl_is_set < 0)
1191 return;
1192 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193
1194#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001195 if (lclptr == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001196 lclptr = calloc(1, sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001197 if (lclptr == NULL) {
1198 settzname(); /* all we can do */
1199 return;
1200 }
1201 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202#endif /* defined ALL_STATE */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001203 if (tzload(NULL, lclptr, TRUE) != 0)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001204 gmtload(lclptr);
1205 settzname();
1206}
1207
Elliott Hughesce4783c2013-07-12 17:31:11 -07001208#include <sys/system_properties.h> // For __system_property_get.
1209
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001210static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001211tzset_locked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001212{
1213 register const char * name = NULL;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001214
1215 name = getenv("TZ");
1216
1217 // try the "persist.sys.timezone" system property first
Elliott Hughesce4783c2013-07-12 17:31:11 -07001218 static char buf[PROP_VALUE_MAX];
1219 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001220 name = buf;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001221 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001222
1223 if (name == NULL) {
1224 tzsetwall();
1225 return;
1226 }
1227
1228 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1229 return;
1230 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1231 if (lcl_is_set)
1232 (void) strcpy(lcl_TZname, name);
1233
1234#ifdef ALL_STATE
1235 if (lclptr == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001236 lclptr = calloc(1, sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001237 if (lclptr == NULL) {
1238 settzname(); /* all we can do */
1239 return;
1240 }
1241 }
1242#endif /* defined ALL_STATE */
1243 if (*name == '\0') {
1244 /*
1245 ** User wants it fast rather than right.
1246 */
1247 lclptr->leapcnt = 0; /* so, we're off a little */
1248 lclptr->timecnt = 0;
1249 lclptr->typecnt = 0;
1250 lclptr->ttis[0].tt_isdst = 0;
1251 lclptr->ttis[0].tt_gmtoff = 0;
1252 lclptr->ttis[0].tt_abbrind = 0;
1253 (void) strcpy(lclptr->chars, gmt);
1254 } else if (tzload(name, lclptr, TRUE) != 0)
1255 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1256 (void) gmtload(lclptr);
1257 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001258}
1259
1260void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001261tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001262{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001263 _tzLock();
1264 tzset_locked();
1265 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001266}
1267
1268/*
1269** The easy way to behave "as if no library function calls" localtime
1270** is to not call it--so we drop its guts into "localsub", which can be
1271** freely called. (And no, the PANS doesn't require the above behavior--
1272** but it *is* desirable.)
1273**
1274** The unused offset argument is for the benefit of mktime variants.
1275*/
1276
1277/*ARGSUSED*/
1278static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001279localsub(const time_t * const timep, const int_fast32_t offset,
1280 struct tm * const tmp, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001281{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001282 register const struct ttinfo * ttisp;
1283 register int i;
1284 register struct tm * result;
1285 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001286
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001287 // BEGIN android-changed: support user-supplied sp.
1288 if (sp == NULL) {
1289 sp = lclptr;
1290 }
1291 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001292#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001293 if (sp == NULL)
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001294 return gmtsub(timep, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001295#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001296 if ((sp->goback && t < sp->ats[0]) ||
1297 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1298 time_t newt = t;
1299 register time_t seconds;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001300 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001302 if (t < sp->ats[0])
1303 seconds = sp->ats[0] - t;
1304 else seconds = t - sp->ats[sp->timecnt - 1];
1305 --seconds;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001306 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1307 seconds = years * AVGSECSPERYEAR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001308 if (t < sp->ats[0])
1309 newt += seconds;
1310 else newt -= seconds;
1311 if (newt < sp->ats[0] ||
1312 newt > sp->ats[sp->timecnt - 1])
1313 return NULL; /* "cannot happen" */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001314 result = localsub(&newt, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001315 if (result == tmp) {
1316 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001317
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001318 newy = tmp->tm_year;
1319 if (t < sp->ats[0])
Elliott Hughese0d0b152013-09-27 00:04:30 -07001320 newy -= years;
1321 else newy += years;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001322 tmp->tm_year = newy;
1323 if (tmp->tm_year != newy)
1324 return NULL;
1325 }
1326 return result;
1327 }
1328 if (sp->timecnt == 0 || t < sp->ats[0]) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001329 i = sp->defaulttype;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001330 } else {
1331 register int lo = 1;
1332 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001333
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001334 while (lo < hi) {
1335 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001336
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001337 if (t < sp->ats[mid])
1338 hi = mid;
1339 else lo = mid + 1;
1340 }
1341 i = (int) sp->types[lo - 1];
1342 }
1343 ttisp = &sp->ttis[i];
1344 /*
1345 ** To get (wrong) behavior that's compatible with System V Release 2.0
1346 ** you'd replace the statement below with
1347 ** t += ttisp->tt_gmtoff;
1348 ** timesub(&t, 0L, sp, tmp);
1349 */
1350 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1351 tmp->tm_isdst = ttisp->tt_isdst;
1352 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001353#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001354 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001355#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001356 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001357}
1358
1359struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001360localtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001361{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001362 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001363}
1364
1365/*
1366** Re-entrant version of localtime.
1367*/
1368
1369struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001370localtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001371{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001372 struct tm* result;
1373
1374 _tzLock();
1375 tzset_locked();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001376 result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001377 _tzUnlock();
1378
1379 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001380}
1381
1382/*
1383** gmtsub is to gmtime as localsub is to localtime.
1384*/
1385
1386static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001387gmtsub(const time_t * const timep, const int_fast32_t offset,
1388 struct tm *const tmp, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001390 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001392 (void) sp; // android-added: unused.
1393
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001394 if (!gmt_is_set) {
1395 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396#ifdef ALL_STATE
Elliott Hughesce4783c2013-07-12 17:31:11 -07001397 gmtptr = calloc(1, sizeof *gmtptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001398 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001399#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001400 gmtload(gmtptr);
1401 }
1402 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001404 /*
1405 ** Could get fancy here and deliver something such as
Elliott Hughese0d0b152013-09-27 00:04:30 -07001406 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001407 ** but this is no time for a treasure hunt.
1408 */
1409 if (offset != 0)
1410 tmp->TM_ZONE = wildabbr;
1411 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001413 if (gmtptr == NULL)
1414 tmp->TM_ZONE = gmt;
1415 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416#endif /* defined ALL_STATE */
1417#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001418 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001420 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001422 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423}
1424
1425struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001426gmtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001427{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001428 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429}
1430
1431/*
1432* Re-entrant version of gmtime.
1433*/
1434
1435struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001436gmtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001437{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001438 struct tm* result;
1439
1440 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001441 result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001442 _tzUnlock();
1443
1444 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001445}
1446
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001447/*
1448** Return the number of leap years through the end of the given year
1449** where, to make the math easy, the answer for year zero is defined as zero.
1450*/
1451
1452static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001453leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001454{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001455 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1456 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001457}
1458
1459static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001460timesub(const time_t *const timep, const int_fast32_t offset,
1461 register const struct state *const sp,
1462 register struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001464 register const struct lsinfo * lp;
1465 register time_t tdays;
1466 register int idays; /* unsigned would be so 2003 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001467 register int_fast64_t rem;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001468 int y;
1469 register const int * ip;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001470 register int_fast64_t corr;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001471 register int hit;
1472 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001473
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001474 corr = 0;
1475 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001476#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001477 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001478#endif /* defined ALL_STATE */
1479#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001480 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001481#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001482 while (--i >= 0) {
1483 lp = &sp->lsis[i];
1484 if (*timep >= lp->ls_trans) {
1485 if (*timep == lp->ls_trans) {
1486 hit = ((i == 0 && lp->ls_corr > 0) ||
1487 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1488 if (hit)
1489 while (i > 0 &&
1490 sp->lsis[i].ls_trans ==
1491 sp->lsis[i - 1].ls_trans + 1 &&
1492 sp->lsis[i].ls_corr ==
1493 sp->lsis[i - 1].ls_corr + 1) {
1494 ++hit;
1495 --i;
1496 }
1497 }
1498 corr = lp->ls_corr;
1499 break;
1500 }
1501 }
1502 y = EPOCH_YEAR;
1503 tdays = *timep / SECSPERDAY;
1504 rem = *timep - tdays * SECSPERDAY;
1505 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1506 int newy;
1507 register time_t tdelta;
1508 register int idelta;
1509 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001511 tdelta = tdays / DAYSPERLYEAR;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001512 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1513 && tdelta <= INT_MAX))
1514 return NULL;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001515 idelta = tdelta;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001516 if (idelta == 0)
1517 idelta = (tdays < 0) ? -1 : 1;
1518 newy = y;
1519 if (increment_overflow(&newy, idelta))
1520 return NULL;
1521 leapdays = leaps_thru_end_of(newy - 1) -
1522 leaps_thru_end_of(y - 1);
1523 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1524 tdays -= leapdays;
1525 y = newy;
1526 }
1527 {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001528 register int_fast32_t seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529
Elliott Hughese0d0b152013-09-27 00:04:30 -07001530 seconds = tdays * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001531 tdays = seconds / SECSPERDAY;
1532 rem += seconds - tdays * SECSPERDAY;
1533 }
1534 /*
1535 ** Given the range, we can now fearlessly cast...
1536 */
1537 idays = tdays;
1538 rem += offset - corr;
1539 while (rem < 0) {
1540 rem += SECSPERDAY;
1541 --idays;
1542 }
1543 while (rem >= SECSPERDAY) {
1544 rem -= SECSPERDAY;
1545 ++idays;
1546 }
1547 while (idays < 0) {
1548 if (increment_overflow(&y, -1))
1549 return NULL;
1550 idays += year_lengths[isleap(y)];
1551 }
1552 while (idays >= year_lengths[isleap(y)]) {
1553 idays -= year_lengths[isleap(y)];
1554 if (increment_overflow(&y, 1))
1555 return NULL;
1556 }
1557 tmp->tm_year = y;
1558 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1559 return NULL;
1560 tmp->tm_yday = idays;
1561 /*
1562 ** The "extra" mods below avoid overflow problems.
1563 */
1564 tmp->tm_wday = EPOCH_WDAY +
1565 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1566 (DAYSPERNYEAR % DAYSPERWEEK) +
1567 leaps_thru_end_of(y - 1) -
1568 leaps_thru_end_of(EPOCH_YEAR - 1) +
1569 idays;
1570 tmp->tm_wday %= DAYSPERWEEK;
1571 if (tmp->tm_wday < 0)
1572 tmp->tm_wday += DAYSPERWEEK;
1573 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1574 rem %= SECSPERHOUR;
1575 tmp->tm_min = (int) (rem / SECSPERMIN);
1576 /*
1577 ** A positive leap second requires a special
1578 ** representation. This uses "... ??:59:60" et seq.
1579 */
1580 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1581 ip = mon_lengths[isleap(y)];
1582 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1583 idays -= ip[tmp->tm_mon];
1584 tmp->tm_mday = (int) (idays + 1);
1585 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001587 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001588#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001589 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001590}
1591
1592char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001593ctime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001594{
1595/*
1596** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001597** The ctime function converts the calendar time pointed to by timer
1598** to local time in the form of a string. It is equivalent to
1599** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001600*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001601 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001602}
1603
1604char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001605ctime_r(const time_t * const timep, char * buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001606{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001607 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001608
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001609 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001610}
1611
1612/*
1613** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001614** The "best" way to do mktime I think is based on an idea of Bob
1615** Kridle's (so its said...) from a long time ago.
1616** It does a binary search of the time_t space. Since time_t's are
1617** just 32 bits, its a max of 32 iterations (even at 64 bits it
1618** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001619*/
1620
1621#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001622#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001623#endif /* !defined WRONG */
1624
1625/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001626** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001627*/
1628
1629static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001630increment_overflow(int *const ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001631{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001632 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001633
Elliott Hughesce4783c2013-07-12 17:31:11 -07001634 /*
1635 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1636 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1637 ** If i < 0 there can only be overflow if i + j < INT_MIN
1638 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1639 */
1640 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1641 return TRUE;
1642 *ip += j;
1643 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001644}
1645
1646static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001647increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001648{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001649 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650
Elliott Hughesce4783c2013-07-12 17:31:11 -07001651 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1652 return TRUE;
1653 *lp += m;
1654 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655}
1656
1657static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001658normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001659{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001660 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001661
Elliott Hughesce4783c2013-07-12 17:31:11 -07001662 tensdelta = (*unitsptr >= 0) ?
1663 (*unitsptr / base) :
1664 (-1 - (-1 - *unitsptr) / base);
1665 *unitsptr -= tensdelta * base;
1666 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001667}
1668
1669static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001670normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr,
1671 const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001672{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001673 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674
Elliott Hughesce4783c2013-07-12 17:31:11 -07001675 tensdelta = (*unitsptr >= 0) ?
1676 (*unitsptr / base) :
1677 (-1 - (-1 - *unitsptr) / base);
1678 *unitsptr -= tensdelta * base;
1679 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001680}
1681
1682static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001683tmcomp(register const struct tm * const atmp,
1684 register const struct tm * const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001685{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001686 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687
Elliott Hughese0d0b152013-09-27 00:04:30 -07001688 if (atmp->tm_year != btmp->tm_year)
1689 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1690 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001691 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1692 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1693 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1694 result = atmp->tm_sec - btmp->tm_sec;
1695 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696}
1697
1698static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001699time2sub(struct tm * const tmp,
1700 struct tm *(*const funcp)(const time_t*, int_fast32_t, struct tm*, const struct state*),
1701 const int_fast32_t offset,
1702 int * const okayp,
1703 const int do_norm_secs, const struct state * sp) // android-changed: added sp
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001705 register int dir;
1706 register int i, j;
1707 register int saved_seconds;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001708 register int_fast32_t li;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001709 register time_t lo;
1710 register time_t hi;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001711 int_fast32_t y;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001712 time_t newt;
1713 time_t t;
1714 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001715
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001716 *okayp = FALSE;
1717 yourtm = *tmp;
1718 if (do_norm_secs) {
1719 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1720 SECSPERMIN))
1721 return WRONG;
1722 }
1723 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1724 return WRONG;
1725 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1726 return WRONG;
1727 y = yourtm.tm_year;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001728 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001729 return WRONG;
1730 /*
1731 ** Turn y into an actual year number for now.
1732 ** It is converted back to an offset from TM_YEAR_BASE later.
1733 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001734 if (increment_overflow32(&y, TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001735 return WRONG;
1736 while (yourtm.tm_mday <= 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001737 if (increment_overflow32(&y, -1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001738 return WRONG;
1739 li = y + (1 < yourtm.tm_mon);
1740 yourtm.tm_mday += year_lengths[isleap(li)];
1741 }
1742 while (yourtm.tm_mday > DAYSPERLYEAR) {
1743 li = y + (1 < yourtm.tm_mon);
1744 yourtm.tm_mday -= year_lengths[isleap(li)];
Elliott Hughesce4783c2013-07-12 17:31:11 -07001745 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001746 return WRONG;
1747 }
1748 for ( ; ; ) {
1749 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1750 if (yourtm.tm_mday <= i)
1751 break;
1752 yourtm.tm_mday -= i;
1753 if (++yourtm.tm_mon >= MONSPERYEAR) {
1754 yourtm.tm_mon = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001755 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001756 return WRONG;
1757 }
1758 }
Elliott Hughesce4783c2013-07-12 17:31:11 -07001759 if (increment_overflow32(&y, -TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001760 return WRONG;
1761 yourtm.tm_year = y;
1762 if (yourtm.tm_year != y)
1763 return WRONG;
1764 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1765 saved_seconds = 0;
1766 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1767 /*
1768 ** We can't set tm_sec to 0, because that might push the
1769 ** time below the minimum representable time.
1770 ** Set tm_sec to 59 instead.
1771 ** This assumes that the minimum representable time is
1772 ** not in the same minute that a leap second was deleted from,
1773 ** which is a safer assumption than using 58 would be.
1774 */
1775 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1776 return WRONG;
1777 saved_seconds = yourtm.tm_sec;
1778 yourtm.tm_sec = SECSPERMIN - 1;
1779 } else {
1780 saved_seconds = yourtm.tm_sec;
1781 yourtm.tm_sec = 0;
1782 }
1783 /*
1784 ** Do a binary search (this works whatever time_t's type is).
1785 */
1786 if (!TYPE_SIGNED(time_t)) {
1787 lo = 0;
1788 hi = lo - 1;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001789 } else {
1790 lo = 1;
1791 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1792 lo *= 2;
1793 hi = -(lo + 1);
1794 }
1795 for ( ; ; ) {
1796 t = lo / 2 + hi / 2;
1797 if (t < lo)
1798 t = lo;
1799 else if (t > hi)
1800 t = hi;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001801 if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001802 /*
1803 ** Assume that t is too extreme to be represented in
1804 ** a struct tm; arrange things so that it is less
1805 ** extreme on the next pass.
1806 */
1807 dir = (t > 0) ? 1 : -1;
1808 } else dir = tmcomp(&mytm, &yourtm);
1809 if (dir != 0) {
1810 if (t == lo) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001811 if (t == time_t_max)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001812 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001813 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001814 ++lo;
1815 } else if (t == hi) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001816 if (t == time_t_min)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001817 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001818 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001819 --hi;
1820 }
1821 if (lo > hi)
1822 return WRONG;
1823 if (dir > 0)
1824 hi = t;
1825 else lo = t;
1826 continue;
1827 }
1828 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1829 break;
1830 /*
1831 ** Right time, wrong type.
1832 ** Hunt for right time, right type.
1833 ** It's okay to guess wrong since the guess
1834 ** gets checked.
1835 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001836 // BEGIN android-changed: support user-supplied sp
1837 if (sp == NULL) {
1838 sp = (const struct state *)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001839 ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001840 }
1841 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001842#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001843 if (sp == NULL)
1844 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001845#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001846 for (i = sp->typecnt - 1; i >= 0; --i) {
1847 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1848 continue;
1849 for (j = sp->typecnt - 1; j >= 0; --j) {
1850 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1851 continue;
1852 newt = t + sp->ttis[j].tt_gmtoff -
1853 sp->ttis[i].tt_gmtoff;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001854 if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001855 continue;
1856 if (tmcomp(&mytm, &yourtm) != 0)
1857 continue;
1858 if (mytm.tm_isdst != yourtm.tm_isdst)
1859 continue;
1860 /*
1861 ** We have a match.
1862 */
1863 t = newt;
1864 goto label;
1865 }
1866 }
1867 return WRONG;
1868 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001869label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001870 newt = t + saved_seconds;
1871 if ((newt < t) != (saved_seconds < 0))
1872 return WRONG;
1873 t = newt;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001874 if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001875 *okayp = TRUE;
1876 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001877}
1878
1879static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001880time2(struct tm * const tmp,
1881 struct tm * (*const funcp)(const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1882 const int_fast32_t offset,
1883 int *const okayp, const struct state* sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001884{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001885 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001886
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001887 /*
1888 ** First try without normalization of seconds
1889 ** (in case tm_sec contains a value associated with a leap second).
1890 ** If that fails, try with normalization of seconds.
1891 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001892 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
1893 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001894}
1895
1896static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001897time1(struct tm * const tmp,
1898 struct tm * (* const funcp) (const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1899 const int_fast32_t offset, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001900{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001901 register time_t t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001902 register int samei, otheri;
1903 register int sameind, otherind;
1904 register int i;
1905 register int nseen;
1906 int seen[TZ_MAX_TYPES];
1907 int types[TZ_MAX_TYPES];
1908 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001909
Elliott Hughesce4783c2013-07-12 17:31:11 -07001910 if (tmp == NULL) {
1911 errno = EINVAL;
1912 return WRONG;
1913 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001914 if (tmp->tm_isdst > 1)
1915 tmp->tm_isdst = 1;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001916 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001917#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001918 /*
1919 ** PCTS code courtesy Grant Sullivan.
1920 */
1921 if (okay)
1922 return t;
1923 if (tmp->tm_isdst < 0)
1924 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001925#endif /* defined PCTS */
1926#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001927 if (okay || tmp->tm_isdst < 0)
1928 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001930 /*
1931 ** We're supposed to assume that somebody took a time of one type
1932 ** and did some math on it that yielded a "struct tm" that's bad.
1933 ** We try to divine the type they started from and adjust to the
1934 ** type they need.
1935 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001936 // BEGIN android-changed: support user-supplied sp.
1937 if (sp == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001938 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001939 }
1940 // BEGIN android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001941#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001942 if (sp == NULL)
1943 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001944#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001945 for (i = 0; i < sp->typecnt; ++i)
1946 seen[i] = FALSE;
1947 nseen = 0;
1948 for (i = sp->timecnt - 1; i >= 0; --i)
1949 if (!seen[sp->types[i]]) {
1950 seen[sp->types[i]] = TRUE;
1951 types[nseen++] = sp->types[i];
1952 }
1953 for (sameind = 0; sameind < nseen; ++sameind) {
1954 samei = types[sameind];
1955 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1956 continue;
1957 for (otherind = 0; otherind < nseen; ++otherind) {
1958 otheri = types[otherind];
1959 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1960 continue;
1961 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1962 sp->ttis[samei].tt_gmtoff;
1963 tmp->tm_isdst = !tmp->tm_isdst;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001964 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001965 if (okay)
1966 return t;
1967 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1968 sp->ttis[samei].tt_gmtoff;
1969 tmp->tm_isdst = !tmp->tm_isdst;
1970 }
1971 }
1972 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001973}
1974
1975time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001976mktime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001977{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001978 _tzLock();
1979 tzset_locked();
Elliott Hughesce4783c2013-07-12 17:31:11 -07001980 time_t result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001981 _tzUnlock();
1982 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001983}
1984
1985#ifdef STD_INSPIRED
1986
1987time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001988timelocal(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001989{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001990 if (tmp != NULL)
1991 tmp->tm_isdst = -1; /* in case it wasn't initialized */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001992 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001993}
1994
1995time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001996timegm(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001997{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001998 time_t result;
1999
Elliott Hughesce4783c2013-07-12 17:31:11 -07002000 if (tmp != NULL)
2001 tmp->tm_isdst = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002002 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08002003 result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002004 _tzUnlock();
2005
2006 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002007}
2008
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002009#endif /* defined STD_INSPIRED */
2010
2011#ifdef CMUCS
2012
2013/*
2014** The following is supplied for compatibility with
2015** previous versions of the CMUCS runtime library.
2016*/
2017
Elliott Hughese0d0b152013-09-27 00:04:30 -07002018long
Elliott Hughesce4783c2013-07-12 17:31:11 -07002019gtime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002020{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002021 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002022
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002023 if (t == WRONG)
2024 return -1;
2025 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002026}
2027
2028#endif /* defined CMUCS */
2029
2030/*
2031** XXX--is the below the right way to conditionalize??
2032*/
2033
2034#ifdef STD_INSPIRED
2035
2036/*
2037** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2038** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2039** is not the case if we are accounting for leap seconds.
2040** So, we provide the following conversion routines for use
2041** when exchanging timestamps with POSIX conforming systems.
2042*/
2043
Elliott Hughesce4783c2013-07-12 17:31:11 -07002044static int_fast64_t
2045leapcorr(time_t * timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002046{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002047 register struct state * sp;
2048 register struct lsinfo * lp;
2049 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002050
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002051 sp = lclptr;
2052 i = sp->leapcnt;
2053 while (--i >= 0) {
2054 lp = &sp->lsis[i];
2055 if (*timep >= lp->ls_trans)
2056 return lp->ls_corr;
2057 }
2058 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002059}
2060
2061time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002062time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002063{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002064 tzset();
2065 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002066}
2067
2068time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002069posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002070{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002071 time_t x;
2072 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002073
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002074 tzset();
2075 /*
2076 ** For a positive leap second hit, the result
2077 ** is not unique. For a negative leap second
2078 ** hit, the corresponding time doesn't exist,
2079 ** so we return an adjacent second.
2080 */
2081 x = t + leapcorr(&t);
2082 y = x - leapcorr(&x);
2083 if (y < t) {
2084 do {
2085 x++;
2086 y = x - leapcorr(&x);
2087 } while (y < t);
2088 if (t != y)
2089 return x - 1;
2090 } else if (y > t) {
2091 do {
2092 --x;
2093 y = x - leapcorr(&x);
2094 } while (y > t);
2095 if (t != y)
2096 return x + 1;
2097 }
2098 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002099}
2100
2101#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002102
Elliott Hughesce4783c2013-07-12 17:31:11 -07002103// BEGIN android-added
2104
Elliott Hughes1c295722012-10-19 18:13:15 -07002105#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002106#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002107#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002108
Elliott Hughesce4783c2013-07-12 17:31:11 -07002109static int to_int(unsigned char* s) {
2110 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
2111}
2112
Elliott Hughescf178bf2013-09-18 19:25:28 -07002113static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
2114 const char* olson_id, int* data_size) {
2115 const char* path_prefix = getenv(path_prefix_variable);
2116 if (path_prefix == NULL) {
2117 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2118 return -1;
2119 }
2120 char path[PATH_MAX];
2121 snprintf(path, sizeof(path), "%s/%s", path_prefix, path_suffix);
Elliott Hughes1c295722012-10-19 18:13:15 -07002122 int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
Elliott Hughesd23af232012-10-17 16:30:47 -07002123 if (fd == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002124 XLOG(("%s: could not open \"%s\": %s\n", __FUNCTION__, path, strerror(errno)));
2125 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002126 }
2127
2128 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002129 // int index_offset
2130 // int data_offset
2131 // int zonetab_offset
2132 struct bionic_tzdata_header {
2133 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002134 int32_t index_offset;
2135 int32_t data_offset;
2136 int32_t zonetab_offset;
2137 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002138 memset(&header, 0, sizeof(header));
2139 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2140 if (bytes_read != sizeof(header)) {
2141 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2142 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughesd23af232012-10-17 16:30:47 -07002143 close(fd);
2144 return -1;
2145 }
2146
2147 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002148 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2149 __FUNCTION__, path, header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002150 close(fd);
2151 return -1;
2152 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002153
2154#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002155 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002156 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2157 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2158 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2159#endif
2160
2161 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002162 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2163 __FUNCTION__, path, strerror(errno));
Elliott Hughesd23af232012-10-17 16:30:47 -07002164 close(fd);
2165 return -1;
2166 }
2167
2168 off_t specific_zone_offset = -1;
2169
Elliott Hughes1c295722012-10-19 18:13:15 -07002170 static const size_t NAME_LENGTH = 40;
2171 unsigned char buf[NAME_LENGTH + 3 * sizeof(int32_t)];
Elliott Hughese0175ca2013-03-14 14:38:08 -07002172
2173 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(buf);
2174 for (size_t i = 0; i < id_count; ++i) {
2175 if (TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf))) != (ssize_t) sizeof(buf)) {
2176 break;
2177 }
2178
Elliott Hughes1c295722012-10-19 18:13:15 -07002179 char this_id[NAME_LENGTH + 1];
2180 memcpy(this_id, buf, NAME_LENGTH);
2181 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002182
2183 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07002184 specific_zone_offset = to_int(buf + NAME_LENGTH) + ntohl(header.data_offset);
2185 *data_size = to_int(buf + NAME_LENGTH + sizeof(int32_t));
Elliott Hughesd23af232012-10-17 16:30:47 -07002186 break;
2187 }
2188 }
2189
2190 if (specific_zone_offset == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002191 XLOG(("%s: couldn't find zone \"%s\"\n", __FUNCTION__, olson_id));
Elliott Hughesd23af232012-10-17 16:30:47 -07002192 close(fd);
2193 return -1;
2194 }
2195
2196 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002197 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2198 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughesd23af232012-10-17 16:30:47 -07002199 close(fd);
2200 return -1;
2201 }
2202
Elliott Hughese7aaad82013-04-25 14:02:59 -07002203 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2204
Elliott Hughesd23af232012-10-17 16:30:47 -07002205 return fd;
2206}
Elliott Hughes1c295722012-10-19 18:13:15 -07002207
2208static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002209 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002210 if (fd < 0) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002211 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002212 if (fd == -2) {
Elliott Hughes49271d82012-10-25 14:38:51 -07002213 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2214 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2215 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
Elliott Hughes1c295722012-10-19 18:13:15 -07002216 }
2217 }
2218 return fd;
2219}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002220
2221// Caches the most recent timezone (http://b/8270865).
2222static int __bionic_tzload_cached(const char* name, struct state* const sp, const int doextend) {
2223 _tzLock();
2224
2225 // Our single-item cache.
2226 static char* gCachedTimeZoneName;
2227 static struct state gCachedTimeZone;
2228
2229 // Do we already have this timezone cached?
2230 if (gCachedTimeZoneName != NULL && strcmp(name, gCachedTimeZoneName) == 0) {
2231 *sp = gCachedTimeZone;
2232 _tzUnlock();
2233 return 0;
2234 }
2235
2236 // Can we load it?
2237 int rc = tzload(name, sp, doextend);
2238 if (rc == 0) {
2239 // Update the cache.
2240 free(gCachedTimeZoneName);
2241 gCachedTimeZoneName = strdup(name);
2242 gCachedTimeZone = *sp;
2243 }
2244
2245 _tzUnlock();
2246 return rc;
2247}
2248
2249// Non-standard API: mktime(3) but with an explicit timezone parameter.
2250time_t mktime_tz(struct tm* const tmp, const char* tz) {
2251 struct state st;
2252 if (__bionic_tzload_cached(tz, &st, TRUE) != 0) {
2253 // TODO: not sure what's best here, but for now, we fall back to gmt.
2254 gmtload(&st);
2255 }
2256 return time1(tmp, localsub, 0L, &st);
2257}
2258
2259// Non-standard API: localtime(3) but with an explicit timezone parameter.
2260void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz) {
2261 struct state st;
2262 if (__bionic_tzload_cached(tz, &st, TRUE) != 0) {
2263 // TODO: not sure what's best here, but for now, we fall back to gmt.
2264 gmtload(&st);
2265 }
2266 localsub(timep, 0L, tmp, &st);
2267}
2268
2269// END android-added