blob: 202f031afcb79cd29d38dc9f24d391b622c5b315 [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;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700338#if __LP64__ // 32-bit Android only has a signed 32-bit time_t; 64-bit Android is fixed.
Elliott Hughesce4783c2013-07-12 17:31:11 -0700339 return t1 - t0 == SECSPERREPEAT;
Elliott Hughes51aeff72013-10-08 18:30:44 -0700340#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341}
342
343static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700344tzload(register const char* name, register struct state* const sp,
345 register const int doextend)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800346{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700347 register const char * p;
348 register int i;
349 register int fid;
350 register int stored;
351 register int nread;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700352 typedef union {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700353 struct tzhead tzhead;
354 char buf[2 * sizeof(struct tzhead) +
355 2 * sizeof *sp +
356 4 * TZ_MAX_TIMES];
Elliott Hughesce4783c2013-07-12 17:31:11 -0700357 } u_t;
358#ifdef ALL_STATE
359 register u_t * up;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360
Elliott Hughesce4783c2013-07-12 17:31:11 -0700361 up = (u_t *) calloc(1, sizeof *up);
362 if (up == NULL)
363 return -1;
364#else /* !defined ALL_STATE */
365 u_t u;
366 register u_t * const up = &u;
367#endif /* !defined ALL_STATE */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368
Elliott Hughesce4783c2013-07-12 17:31:11 -0700369 sp->goback = sp->goahead = FALSE;
370 if (name == NULL && (name = TZDEFAULT) == NULL)
371 goto oops;
372 int toread;
373 fid = __bionic_open_tzdata(name, &toread);
Elliott Hughes3073f902014-02-27 17:04:38 -0800374 if (fid < 0)
375 goto oops;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700376 nread = read(fid, up->buf, toread);
377 if (close(fid) < 0 || nread <= 0)
378 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700379 for (stored = 4; stored <= 8; stored *= 2) {
380 int ttisstdcnt;
381 int ttisgmtcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382
Elliott Hughesce4783c2013-07-12 17:31:11 -0700383 ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt);
384 ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt);
385 sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt);
386 sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt);
387 sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt);
388 sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt);
389 p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700390 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
391 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
392 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
393 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
394 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
395 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700396 goto oops;
397 if (nread - (p - up->buf) <
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700398 sp->timecnt * stored + /* ats */
399 sp->timecnt + /* types */
400 sp->typecnt * 6 + /* ttinfos */
401 sp->charcnt + /* chars */
402 sp->leapcnt * (stored + 4) + /* lsinfos */
403 ttisstdcnt + /* ttisstds */
404 ttisgmtcnt) /* ttisgmts */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700405 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700406 for (i = 0; i < sp->timecnt; ++i) {
407 sp->ats[i] = (stored == 4) ?
408 detzcode(p) : detzcode64(p);
409 p += stored;
410 }
411 for (i = 0; i < sp->timecnt; ++i) {
412 sp->types[i] = (unsigned char) *p++;
413 if (sp->types[i] >= sp->typecnt)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700414 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700415 }
416 for (i = 0; i < sp->typecnt; ++i) {
417 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700419 ttisp = &sp->ttis[i];
420 ttisp->tt_gmtoff = detzcode(p);
421 p += 4;
422 ttisp->tt_isdst = (unsigned char) *p++;
423 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700424 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700425 ttisp->tt_abbrind = (unsigned char) *p++;
426 if (ttisp->tt_abbrind < 0 ||
427 ttisp->tt_abbrind > sp->charcnt)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700428 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700429 }
430 for (i = 0; i < sp->charcnt; ++i)
431 sp->chars[i] = *p++;
432 sp->chars[i] = '\0'; /* ensure '\0' at end */
433 for (i = 0; i < sp->leapcnt; ++i) {
434 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700436 lsisp = &sp->lsis[i];
437 lsisp->ls_trans = (stored == 4) ?
438 detzcode(p) : detzcode64(p);
439 p += stored;
440 lsisp->ls_corr = detzcode(p);
441 p += 4;
442 }
443 for (i = 0; i < sp->typecnt; ++i) {
444 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700446 ttisp = &sp->ttis[i];
447 if (ttisstdcnt == 0)
448 ttisp->tt_ttisstd = FALSE;
449 else {
450 ttisp->tt_ttisstd = *p++;
451 if (ttisp->tt_ttisstd != TRUE &&
452 ttisp->tt_ttisstd != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700453 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700454 }
455 }
456 for (i = 0; i < sp->typecnt; ++i) {
457 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700459 ttisp = &sp->ttis[i];
460 if (ttisgmtcnt == 0)
461 ttisp->tt_ttisgmt = FALSE;
462 else {
463 ttisp->tt_ttisgmt = *p++;
464 if (ttisp->tt_ttisgmt != TRUE &&
465 ttisp->tt_ttisgmt != FALSE)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700466 goto oops;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700467 }
468 }
469 /*
470 ** Out-of-sort ats should mean we're running on a
471 ** signed time_t system but using a data file with
472 ** unsigned values (or vice versa).
473 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700474 for (i = 0; i < sp->timecnt; ++i)
475 if ((i < sp->timecnt - 1 &&
476 sp->ats[i] > sp->ats[i + 1]) ||
477 (i == sp->timecnt - 1 && !TYPE_SIGNED(time_t) &&
478 sp->ats[i] >
479 ((stored == 4) ? INT32_MAX : INT64_MAX))) {
480 if (TYPE_SIGNED(time_t)) {
481 /*
482 ** Ignore the end (easy).
483 */
484 sp->timecnt = i + 1;
485 } else {
486 /*
487 ** Ignore the beginning (harder).
488 */
489 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800490
Elliott Hughesce4783c2013-07-12 17:31:11 -0700491 /*
492 ** Keep the record right before the
493 ** epoch boundary,
494 ** but tweak it so that it starts
495 ** right with the epoch
496 ** (thanks to Doug Bailey).
497 */
498 sp->ats[i] = 0;
499 for (j = 0; j + i < sp->timecnt; ++j) {
500 sp->ats[j] = sp->ats[j + i];
501 sp->types[j] = sp->types[j + i];
502 }
503 sp->timecnt = j;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700504 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700505 break;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700506 }
507 /*
508 ** If this is an old file, we're done.
509 */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700510 if (up->tzhead.tzh_version[0] == '\0')
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700511 break;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700512 nread -= p - up->buf;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700513 for (i = 0; i < nread; ++i)
Elliott Hughesce4783c2013-07-12 17:31:11 -0700514 up->buf[i] = p[i];
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700515 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700516 ** If this is a narrow time_t system, we're done.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700517 */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700518 if (stored >= (int) sizeof(time_t))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700519 break;
520 }
521 if (doextend && nread > 2 &&
Elliott Hughesce4783c2013-07-12 17:31:11 -0700522 up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700523 sp->typecnt + 2 <= TZ_MAX_TYPES) {
524 struct state ts;
525 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526
Elliott Hughesce4783c2013-07-12 17:31:11 -0700527 up->buf[nread - 1] = '\0';
528 result = tzparse(&up->buf[1], &ts, FALSE);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700529 if (result == 0 && ts.typecnt == 2 &&
530 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
531 for (i = 0; i < 2; ++i)
532 ts.ttis[i].tt_abbrind +=
533 sp->charcnt;
534 for (i = 0; i < ts.charcnt; ++i)
535 sp->chars[sp->charcnt++] =
536 ts.chars[i];
537 i = 0;
538 while (i < ts.timecnt &&
539 ts.ats[i] <=
540 sp->ats[sp->timecnt - 1])
541 ++i;
542 while (i < ts.timecnt &&
543 sp->timecnt < TZ_MAX_TIMES) {
544 sp->ats[sp->timecnt] =
545 ts.ats[i];
546 sp->types[sp->timecnt] =
547 sp->typecnt +
548 ts.types[i];
549 ++sp->timecnt;
550 ++i;
551 }
552 sp->ttis[sp->typecnt++] = ts.ttis[0];
553 sp->ttis[sp->typecnt++] = ts.ttis[1];
554 }
555 }
Elliott Hughesce4783c2013-07-12 17:31:11 -0700556 if (sp->timecnt > 1) {
557 for (i = 1; i < sp->timecnt; ++i)
558 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
559 differ_by_repeat(sp->ats[i], sp->ats[0])) {
560 sp->goback = TRUE;
561 break;
562 }
563 for (i = sp->timecnt - 2; i >= 0; --i)
564 if (typesequiv(sp, sp->types[sp->timecnt - 1],
565 sp->types[i]) &&
566 differ_by_repeat(sp->ats[sp->timecnt - 1],
567 sp->ats[i])) {
568 sp->goahead = TRUE;
569 break;
570 }
571 }
572 /*
573 ** If type 0 is is unused in transitions,
574 ** it's the type to use for early times.
575 */
576 for (i = 0; i < sp->typecnt; ++i)
577 if (sp->types[i] == 0)
578 break;
579 i = (i >= sp->typecnt) ? 0 : -1;
580 /*
581 ** Absent the above,
582 ** if there are transition times
583 ** and the first transition is to a daylight time
584 ** find the standard type less than and closest to
585 ** the type of the first transition.
586 */
587 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
588 i = sp->types[0];
589 while (--i >= 0)
590 if (!sp->ttis[i].tt_isdst)
591 break;
592 }
593 /*
594 ** If no result yet, find the first standard type.
595 ** If there is none, punt to type zero.
596 */
597 if (i < 0) {
598 i = 0;
599 while (sp->ttis[i].tt_isdst)
600 if (++i >= sp->typecnt) {
601 i = 0;
602 break;
603 }
604 }
605 sp->defaulttype = i;
606#ifdef ALL_STATE
607 free(up);
608#endif /* defined ALL_STATE */
609 return 0;
610oops:
611#ifdef ALL_STATE
612 free(up);
613#endif /* defined ALL_STATE */
614 return -1;
615}
616
617static int
618typesequiv(const struct state *const sp, const int a, const int b)
619{
620 register int result;
621
622 if (sp == NULL ||
623 a < 0 || a >= sp->typecnt ||
624 b < 0 || b >= sp->typecnt)
625 result = FALSE;
626 else {
627 register const struct ttinfo * ap = &sp->ttis[a];
628 register const struct ttinfo * bp = &sp->ttis[b];
629 result = ap->tt_gmtoff == bp->tt_gmtoff &&
630 ap->tt_isdst == bp->tt_isdst &&
631 ap->tt_ttisstd == bp->tt_ttisstd &&
632 ap->tt_ttisgmt == bp->tt_ttisgmt &&
633 strcmp(&sp->chars[ap->tt_abbrind],
634 &sp->chars[bp->tt_abbrind]) == 0;
635 }
636 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800637}
638
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700639static const int mon_lengths[2][MONSPERYEAR] = {
640 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
641 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800642};
643
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700644static const int year_lengths[2] = {
645 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646};
647
648/*
649** Given a pointer into a time zone string, scan until a character that is not
650** a valid character in a zone name is found. Return a pointer to that
651** character.
652*/
653
654static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700655getzname(register const char * strp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700657 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800658
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700659 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
660 c != '+')
661 ++strp;
662 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663}
664
665/*
666** Given a pointer into an extended time zone string, scan until the ending
667** delimiter of the zone name is located. Return a pointer to the delimiter.
668**
669** As with getzname above, the legal character set is actually quite
670** restricted, with other characters producing undefined results.
671** We don't do any checking here; checking is done later in common-case code.
672*/
673
674static const char *
675getqzname(register const char *strp, const int delim)
676{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700677 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800678
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700679 while ((c = *strp) != '\0' && c != delim)
680 ++strp;
681 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682}
683
684/*
685** Given a pointer into a time zone string, extract a number from that string.
686** Check that the number is within a specified range; if it is not, return
687** NULL.
688** Otherwise, return a pointer to the first character not part of the number.
689*/
690
691static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700692getnum(register const char * strp, int * const nump, const int min, const int max)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700694 register char c;
695 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700697 if (strp == NULL || !is_digit(c = *strp))
698 return NULL;
699 num = 0;
700 do {
701 num = num * 10 + (c - '0');
702 if (num > max)
703 return NULL; /* illegal value */
704 c = *++strp;
705 } while (is_digit(c));
706 if (num < min)
707 return NULL; /* illegal value */
708 *nump = num;
709 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710}
711
712/*
713** Given a pointer into a time zone string, extract a number of seconds,
714** in hh[:mm[:ss]] form, from the string.
715** If any error occurs, return NULL.
716** Otherwise, return a pointer to the first character not part of the number
717** of seconds.
718*/
719
720static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700721getsecs(register const char *strp, int_fast32_t *const secsp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700723 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700725 /*
726 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
727 ** "M10.4.6/26", which does not conform to Posix,
728 ** but which specifies the equivalent of
729 ** ``02:00 on the first Sunday on or after 23 Oct''.
730 */
731 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
732 if (strp == NULL)
733 return NULL;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700734 *secsp = num * (int_fast32_t) SECSPERHOUR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700735 if (*strp == ':') {
736 ++strp;
737 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
738 if (strp == NULL)
739 return NULL;
740 *secsp += num * SECSPERMIN;
741 if (*strp == ':') {
742 ++strp;
743 /* `SECSPERMIN' allows for leap seconds. */
744 strp = getnum(strp, &num, 0, SECSPERMIN);
745 if (strp == NULL)
746 return NULL;
747 *secsp += num;
748 }
749 }
750 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751}
752
753/*
754** Given a pointer into a time zone string, extract an offset, in
755** [+-]hh[:mm[:ss]] form, from the string.
756** If any error occurs, return NULL.
757** Otherwise, return a pointer to the first character not part of the time.
758*/
759
760static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700761getoffset(register const char *strp, int_fast32_t *const offsetp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700763 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800764
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700765 if (*strp == '-') {
766 neg = 1;
767 ++strp;
768 } else if (*strp == '+')
769 ++strp;
770 strp = getsecs(strp, offsetp);
771 if (strp == NULL)
772 return NULL; /* illegal time */
773 if (neg)
774 *offsetp = -*offsetp;
775 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776}
777
778/*
779** Given a pointer into a time zone string, extract a rule in the form
780** date[/time]. See POSIX section 8 for the format of "date" and "time".
781** If a valid rule is not found, return NULL.
782** Otherwise, return a pointer to the first character not part of the rule.
783*/
784
785static const char *
Elliott Hughesce4783c2013-07-12 17:31:11 -0700786getrule(const char * strp, register struct rule * const rulep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800787{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700788 if (*strp == 'J') {
789 /*
790 ** Julian day.
791 */
792 rulep->r_type = JULIAN_DAY;
793 ++strp;
794 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
795 } else if (*strp == 'M') {
796 /*
797 ** Month, week, day.
798 */
799 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
800 ++strp;
801 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
802 if (strp == NULL)
803 return NULL;
804 if (*strp++ != '.')
805 return NULL;
806 strp = getnum(strp, &rulep->r_week, 1, 5);
807 if (strp == NULL)
808 return NULL;
809 if (*strp++ != '.')
810 return NULL;
811 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
812 } else if (is_digit(*strp)) {
813 /*
814 ** Day of year.
815 */
816 rulep->r_type = DAY_OF_YEAR;
817 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
818 } else return NULL; /* invalid format */
819 if (strp == NULL)
820 return NULL;
821 if (*strp == '/') {
822 /*
823 ** Time specified.
824 */
825 ++strp;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700826 strp = getoffset(strp, &rulep->r_time);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700827 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
828 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800829}
830
831/*
832** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
Elliott Hughese0d0b152013-09-27 00:04:30 -0700833** 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 -0800834** calculate the Epoch-relative time that rule takes effect.
835*/
836
837static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -0700838transtime(const time_t janfirst, const int year,
839 register const struct rule *const rulep, const int_fast32_t offset)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800840{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700841 register int leapyear;
842 register time_t value;
843 register int i;
844 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800845
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700846 INITIALIZE(value);
847 leapyear = isleap(year);
848 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800849
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700850 case JULIAN_DAY:
851 /*
852 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
853 ** years.
854 ** In non-leap years, or if the day number is 59 or less, just
855 ** add SECSPERDAY times the day number-1 to the time of
856 ** January 1, midnight, to get the day.
857 */
858 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
859 if (leapyear && rulep->r_day >= 60)
860 value += SECSPERDAY;
861 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800862
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700863 case DAY_OF_YEAR:
864 /*
865 ** n - day of year.
866 ** Just add SECSPERDAY times the day number to the time of
867 ** January 1, midnight, to get the day.
868 */
869 value = janfirst + rulep->r_day * SECSPERDAY;
870 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800871
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700872 case MONTH_NTH_DAY_OF_WEEK:
873 /*
874 ** Mm.n.d - nth "dth day" of month m.
875 */
876 value = janfirst;
877 for (i = 0; i < rulep->r_mon - 1; ++i)
878 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800879
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700880 /*
881 ** Use Zeller's Congruence to get day-of-week of first day of
882 ** month.
883 */
884 m1 = (rulep->r_mon + 9) % 12 + 1;
885 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
886 yy1 = yy0 / 100;
887 yy2 = yy0 % 100;
888 dow = ((26 * m1 - 2) / 10 +
889 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
890 if (dow < 0)
891 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800892
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700893 /*
894 ** "dow" is the day-of-week of the first day of the month. Get
895 ** the day-of-month (zero-origin) of the first "dow" day of the
896 ** month.
897 */
898 d = rulep->r_day - dow;
899 if (d < 0)
900 d += DAYSPERWEEK;
901 for (i = 1; i < rulep->r_week; ++i) {
902 if (d + DAYSPERWEEK >=
903 mon_lengths[leapyear][rulep->r_mon - 1])
904 break;
905 d += DAYSPERWEEK;
906 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800907
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700908 /*
909 ** "d" is the day-of-month (zero-origin) of the day we want.
910 */
911 value += d * SECSPERDAY;
912 break;
913 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800914
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700915 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700916 ** "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 -0700917 ** question. To get the Epoch-relative time of the specified local
918 ** time on that day, add the transition time and the current offset
Elliott Hughese0d0b152013-09-27 00:04:30 -0700919 ** from UT.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700920 */
921 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800922}
923
924/*
925** Given a POSIX section 8-style TZ string, fill in the rule tables as
926** appropriate.
927*/
928
929static int
Elliott Hughesce4783c2013-07-12 17:31:11 -0700930tzparse(const char * name, register struct state * const sp,
931 const int lastditch)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800932{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700933 const char * stdname;
934 const char * dstname;
935 size_t stdlen;
936 size_t dstlen;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700937 int_fast32_t stdoffset;
938 int_fast32_t dstoffset;
939 register time_t * atp;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700940 register unsigned char * typep;
941 register char * cp;
942 register int load_result;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700943 static struct ttinfo zttinfo;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700945 INITIALIZE(dstname);
946 stdname = name;
947 if (lastditch) {
948 stdlen = strlen(name); /* length of standard zone name */
949 name += stdlen;
950 if (stdlen >= sizeof sp->chars)
951 stdlen = (sizeof sp->chars) - 1;
952 stdoffset = 0;
953 } else {
954 if (*name == '<') {
955 name++;
956 stdname = name;
957 name = getqzname(name, '>');
958 if (*name != '>')
959 return (-1);
960 stdlen = name - stdname;
961 name++;
962 } else {
963 name = getzname(name);
964 stdlen = name - stdname;
965 }
966 if (*name == '\0')
967 return -1;
968 name = getoffset(name, &stdoffset);
969 if (name == NULL)
970 return -1;
971 }
972 load_result = tzload(TZDEFRULES, sp, FALSE);
973 if (load_result != 0)
974 sp->leapcnt = 0; /* so, we're off a little */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700975 if (*name != '\0') {
976 if (*name == '<') {
977 dstname = ++name;
978 name = getqzname(name, '>');
979 if (*name != '>')
980 return -1;
981 dstlen = name - dstname;
982 name++;
983 } else {
984 dstname = name;
985 name = getzname(name);
986 dstlen = name - dstname; /* length of DST zone name */
987 }
988 if (*name != '\0' && *name != ',' && *name != ';') {
989 name = getoffset(name, &dstoffset);
990 if (name == NULL)
991 return -1;
992 } else dstoffset = stdoffset - SECSPERHOUR;
993 if (*name == '\0' && load_result != 0)
994 name = TZDEFRULESTRING;
995 if (*name == ',' || *name == ';') {
996 struct rule start;
997 struct rule end;
998 register int year;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700999 register int yearlim;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001000 register time_t janfirst;
1001 time_t starttime;
1002 time_t endtime;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001003
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001004 ++name;
1005 if ((name = getrule(name, &start)) == NULL)
1006 return -1;
1007 if (*name++ != ',')
1008 return -1;
1009 if ((name = getrule(name, &end)) == NULL)
1010 return -1;
1011 if (*name != '\0')
1012 return -1;
1013 sp->typecnt = 2; /* standard time and DST */
1014 /*
1015 ** Two transitions per year, from EPOCH_YEAR forward.
1016 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001017 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001018 sp->ttis[0].tt_gmtoff = -dstoffset;
1019 sp->ttis[0].tt_isdst = 1;
1020 sp->ttis[0].tt_abbrind = stdlen + 1;
1021 sp->ttis[1].tt_gmtoff = -stdoffset;
1022 sp->ttis[1].tt_isdst = 0;
1023 sp->ttis[1].tt_abbrind = 0;
1024 atp = sp->ats;
1025 typep = sp->types;
1026 janfirst = 0;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001027 yearlim = EPOCH_YEAR + YEARSPERREPEAT;
1028 for (year = EPOCH_YEAR; year < yearlim; year++) {
1029 int_fast32_t yearsecs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001030
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001031 starttime = transtime(janfirst, year, &start,
1032 stdoffset);
1033 endtime = transtime(janfirst, year, &end,
1034 dstoffset);
Elliott Hughese0d0b152013-09-27 00:04:30 -07001035 yearsecs = (year_lengths[isleap(year)]
1036 * SECSPERDAY);
1037 if (starttime > endtime
1038 || (starttime < endtime
1039 && (endtime - starttime
1040 < (yearsecs
1041 + (stdoffset - dstoffset))))) {
1042 if (&sp->ats[TZ_MAX_TIMES - 2] < atp)
1043 break;
1044 yearlim = year + YEARSPERREPEAT + 1;
1045 if (starttime > endtime) {
1046 *atp++ = endtime;
1047 *typep++ = 1; /* DST ends */
1048 *atp++ = starttime;
1049 *typep++ = 0; /* DST begins */
1050 } else {
1051 *atp++ = starttime;
1052 *typep++ = 0; /* DST begins */
1053 *atp++ = endtime;
1054 *typep++ = 1; /* DST ends */
1055 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001056 }
Elliott Hughese0d0b152013-09-27 00:04:30 -07001057 if (time_t_max - janfirst < yearsecs)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001058 break;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001059 janfirst += yearsecs;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001060 }
Elliott Hughese0d0b152013-09-27 00:04:30 -07001061 sp->timecnt = atp - sp->ats;
1062 if (!sp->timecnt)
1063 sp->typecnt = 1; /* Perpetual DST. */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001064 } else {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001065 register int_fast32_t theirstdoffset;
1066 register int_fast32_t theirdstoffset;
1067 register int_fast32_t theiroffset;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001068 register int isdst;
1069 register int i;
1070 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001071
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001072 if (*name != '\0')
1073 return -1;
1074 /*
1075 ** Initial values of theirstdoffset and theirdstoffset.
1076 */
1077 theirstdoffset = 0;
1078 for (i = 0; i < sp->timecnt; ++i) {
1079 j = sp->types[i];
1080 if (!sp->ttis[j].tt_isdst) {
1081 theirstdoffset =
1082 -sp->ttis[j].tt_gmtoff;
1083 break;
1084 }
1085 }
1086 theirdstoffset = 0;
1087 for (i = 0; i < sp->timecnt; ++i) {
1088 j = sp->types[i];
1089 if (sp->ttis[j].tt_isdst) {
1090 theirdstoffset =
1091 -sp->ttis[j].tt_gmtoff;
1092 break;
1093 }
1094 }
1095 /*
1096 ** Initially we're assumed to be in standard time.
1097 */
1098 isdst = FALSE;
1099 theiroffset = theirstdoffset;
1100 /*
1101 ** Now juggle transition times and types
1102 ** tracking offsets as you do.
1103 */
1104 for (i = 0; i < sp->timecnt; ++i) {
1105 j = sp->types[i];
1106 sp->types[i] = sp->ttis[j].tt_isdst;
1107 if (sp->ttis[j].tt_ttisgmt) {
1108 /* No adjustment to transition time */
1109 } else {
1110 /*
1111 ** If summer time is in effect, and the
1112 ** transition time was not specified as
1113 ** standard time, add the summer time
1114 ** offset to the transition time;
1115 ** otherwise, add the standard time
1116 ** offset to the transition time.
1117 */
1118 /*
1119 ** Transitions from DST to DDST
1120 ** will effectively disappear since
1121 ** POSIX provides for only one DST
1122 ** offset.
1123 */
1124 if (isdst && !sp->ttis[j].tt_ttisstd) {
1125 sp->ats[i] += dstoffset -
1126 theirdstoffset;
1127 } else {
1128 sp->ats[i] += stdoffset -
1129 theirstdoffset;
1130 }
1131 }
1132 theiroffset = -sp->ttis[j].tt_gmtoff;
1133 if (sp->ttis[j].tt_isdst)
1134 theirdstoffset = theiroffset;
1135 else theirstdoffset = theiroffset;
1136 }
1137 /*
1138 ** Finally, fill in ttis.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001139 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001140 sp->ttis[0] = sp->ttis[1] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001141 sp->ttis[0].tt_gmtoff = -stdoffset;
1142 sp->ttis[0].tt_isdst = FALSE;
1143 sp->ttis[0].tt_abbrind = 0;
1144 sp->ttis[1].tt_gmtoff = -dstoffset;
1145 sp->ttis[1].tt_isdst = TRUE;
1146 sp->ttis[1].tt_abbrind = stdlen + 1;
1147 sp->typecnt = 2;
1148 }
1149 } else {
1150 dstlen = 0;
1151 sp->typecnt = 1; /* only standard time */
1152 sp->timecnt = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001153 sp->ttis[0] = zttinfo;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001154 sp->ttis[0].tt_gmtoff = -stdoffset;
1155 sp->ttis[0].tt_isdst = 0;
1156 sp->ttis[0].tt_abbrind = 0;
1157 }
1158 sp->charcnt = stdlen + 1;
1159 if (dstlen != 0)
1160 sp->charcnt += dstlen + 1;
1161 if ((size_t) sp->charcnt > sizeof sp->chars)
1162 return -1;
1163 cp = sp->chars;
1164 (void) strncpy(cp, stdname, stdlen);
1165 cp += stdlen;
1166 *cp++ = '\0';
1167 if (dstlen != 0) {
1168 (void) strncpy(cp, dstname, dstlen);
1169 *(cp + dstlen) = '\0';
1170 }
1171 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001172}
1173
1174static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001175gmtload(struct state * const sp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001176{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001177 if (tzload(gmt, sp, TRUE) != 0)
1178 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001179}
1180
Elliott Hughesce4783c2013-07-12 17:31:11 -07001181#ifndef STD_INSPIRED
1182/*
1183** A non-static declaration of tzsetwall in a system header file
1184** may cause a warning about this upcoming static declaration...
1185*/
1186static
1187#endif /* !defined STD_INSPIRED */
1188void
1189tzsetwall(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001190{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001191 if (lcl_is_set < 0)
1192 return;
1193 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194
1195#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001196 if (lclptr == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001197 lclptr = calloc(1, sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001198 if (lclptr == NULL) {
1199 settzname(); /* all we can do */
1200 return;
1201 }
1202 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203#endif /* defined ALL_STATE */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001204 if (tzload(NULL, lclptr, TRUE) != 0)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001205 gmtload(lclptr);
1206 settzname();
1207}
1208
Elliott Hughesce4783c2013-07-12 17:31:11 -07001209#include <sys/system_properties.h> // For __system_property_get.
1210
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001211static void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001212tzset_locked(void)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001213{
1214 register const char * name = NULL;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001215
1216 name = getenv("TZ");
1217
1218 // try the "persist.sys.timezone" system property first
Elliott Hughesce4783c2013-07-12 17:31:11 -07001219 static char buf[PROP_VALUE_MAX];
1220 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001221 name = buf;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001222 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001223
1224 if (name == NULL) {
1225 tzsetwall();
1226 return;
1227 }
1228
1229 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1230 return;
1231 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1232 if (lcl_is_set)
1233 (void) strcpy(lcl_TZname, name);
1234
1235#ifdef ALL_STATE
1236 if (lclptr == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001237 lclptr = calloc(1, sizeof *lclptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001238 if (lclptr == NULL) {
1239 settzname(); /* all we can do */
1240 return;
1241 }
1242 }
1243#endif /* defined ALL_STATE */
1244 if (*name == '\0') {
1245 /*
1246 ** User wants it fast rather than right.
1247 */
1248 lclptr->leapcnt = 0; /* so, we're off a little */
1249 lclptr->timecnt = 0;
1250 lclptr->typecnt = 0;
1251 lclptr->ttis[0].tt_isdst = 0;
1252 lclptr->ttis[0].tt_gmtoff = 0;
1253 lclptr->ttis[0].tt_abbrind = 0;
1254 (void) strcpy(lclptr->chars, gmt);
1255 } else if (tzload(name, lclptr, TRUE) != 0)
1256 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1257 (void) gmtload(lclptr);
1258 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001259}
1260
1261void
Elliott Hughesce4783c2013-07-12 17:31:11 -07001262tzset(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001263{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001264 _tzLock();
1265 tzset_locked();
1266 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001267}
1268
1269/*
1270** The easy way to behave "as if no library function calls" localtime
1271** is to not call it--so we drop its guts into "localsub", which can be
1272** freely called. (And no, the PANS doesn't require the above behavior--
1273** but it *is* desirable.)
1274**
1275** The unused offset argument is for the benefit of mktime variants.
1276*/
1277
1278/*ARGSUSED*/
1279static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001280localsub(const time_t * const timep, const int_fast32_t offset,
1281 struct tm * const tmp, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001282{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001283 register const struct ttinfo * ttisp;
1284 register int i;
1285 register struct tm * result;
1286 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001287
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001288 // BEGIN android-changed: support user-supplied sp.
1289 if (sp == NULL) {
1290 sp = lclptr;
1291 }
1292 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001293#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001294 if (sp == NULL)
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001295 return gmtsub(timep, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001297 if ((sp->goback && t < sp->ats[0]) ||
1298 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1299 time_t newt = t;
1300 register time_t seconds;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001301 register time_t years;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001303 if (t < sp->ats[0])
1304 seconds = sp->ats[0] - t;
1305 else seconds = t - sp->ats[sp->timecnt - 1];
1306 --seconds;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001307 years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT;
1308 seconds = years * AVGSECSPERYEAR;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001309 if (t < sp->ats[0])
1310 newt += seconds;
1311 else newt -= seconds;
1312 if (newt < sp->ats[0] ||
1313 newt > sp->ats[sp->timecnt - 1])
1314 return NULL; /* "cannot happen" */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001315 result = localsub(&newt, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001316 if (result == tmp) {
1317 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001318
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001319 newy = tmp->tm_year;
1320 if (t < sp->ats[0])
Elliott Hughese0d0b152013-09-27 00:04:30 -07001321 newy -= years;
1322 else newy += years;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001323 tmp->tm_year = newy;
1324 if (tmp->tm_year != newy)
1325 return NULL;
1326 }
1327 return result;
1328 }
1329 if (sp->timecnt == 0 || t < sp->ats[0]) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001330 i = sp->defaulttype;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001331 } else {
1332 register int lo = 1;
1333 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001335 while (lo < hi) {
1336 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001337
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001338 if (t < sp->ats[mid])
1339 hi = mid;
1340 else lo = mid + 1;
1341 }
1342 i = (int) sp->types[lo - 1];
1343 }
1344 ttisp = &sp->ttis[i];
1345 /*
1346 ** To get (wrong) behavior that's compatible with System V Release 2.0
1347 ** you'd replace the statement below with
1348 ** t += ttisp->tt_gmtoff;
1349 ** timesub(&t, 0L, sp, tmp);
1350 */
1351 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1352 tmp->tm_isdst = ttisp->tt_isdst;
1353 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001354#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001355 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001356#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001357 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001358}
1359
1360struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001361localtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001362{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001363 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001364}
1365
1366/*
1367** Re-entrant version of localtime.
1368*/
1369
1370struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001371localtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001372{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001373 struct tm* result;
1374
1375 _tzLock();
1376 tzset_locked();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001377 result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001378 _tzUnlock();
1379
1380 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001381}
1382
1383/*
1384** gmtsub is to gmtime as localsub is to localtime.
1385*/
1386
1387static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001388gmtsub(const time_t * const timep, const int_fast32_t offset,
1389 struct tm *const tmp, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001390{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001391 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001393 (void) sp; // android-added: unused.
1394
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001395 if (!gmt_is_set) {
1396 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001397#ifdef ALL_STATE
Elliott Hughesce4783c2013-07-12 17:31:11 -07001398 gmtptr = calloc(1, sizeof *gmtptr);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001399 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001400#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001401 gmtload(gmtptr);
1402 }
1403 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001405 /*
1406 ** Could get fancy here and deliver something such as
Elliott Hughese0d0b152013-09-27 00:04:30 -07001407 ** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001408 ** but this is no time for a treasure hunt.
1409 */
1410 if (offset != 0)
1411 tmp->TM_ZONE = wildabbr;
1412 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001414 if (gmtptr == NULL)
1415 tmp->TM_ZONE = gmt;
1416 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001417#endif /* defined ALL_STATE */
1418#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001419 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001420#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001422#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001423 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001424}
1425
1426struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001427gmtime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001429 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430}
1431
1432/*
1433* Re-entrant version of gmtime.
1434*/
1435
1436struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001437gmtime_r(const time_t * const timep, struct tm * tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001438{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001439 struct tm* result;
1440
1441 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001442 result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001443 _tzUnlock();
1444
1445 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001446}
1447
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448/*
1449** Return the number of leap years through the end of the given year
1450** where, to make the math easy, the answer for year zero is defined as zero.
1451*/
1452
1453static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001454leaps_thru_end_of(register const int y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001456 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1457 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001458}
1459
1460static struct tm *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001461timesub(const time_t *const timep, const int_fast32_t offset,
1462 register const struct state *const sp,
1463 register struct tm *const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001464{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001465 register const struct lsinfo * lp;
1466 register time_t tdays;
1467 register int idays; /* unsigned would be so 2003 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001468 register int_fast64_t rem;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001469 int y;
1470 register const int * ip;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001471 register int_fast64_t corr;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001472 register int hit;
1473 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001474
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001475 corr = 0;
1476 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001477#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001478 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479#endif /* defined ALL_STATE */
1480#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001481 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001482#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001483 while (--i >= 0) {
1484 lp = &sp->lsis[i];
1485 if (*timep >= lp->ls_trans) {
1486 if (*timep == lp->ls_trans) {
1487 hit = ((i == 0 && lp->ls_corr > 0) ||
1488 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1489 if (hit)
1490 while (i > 0 &&
1491 sp->lsis[i].ls_trans ==
1492 sp->lsis[i - 1].ls_trans + 1 &&
1493 sp->lsis[i].ls_corr ==
1494 sp->lsis[i - 1].ls_corr + 1) {
1495 ++hit;
1496 --i;
1497 }
1498 }
1499 corr = lp->ls_corr;
1500 break;
1501 }
1502 }
1503 y = EPOCH_YEAR;
1504 tdays = *timep / SECSPERDAY;
1505 rem = *timep - tdays * SECSPERDAY;
1506 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1507 int newy;
1508 register time_t tdelta;
1509 register int idelta;
1510 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001511
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001512 tdelta = tdays / DAYSPERLYEAR;
Elliott Hughese0d0b152013-09-27 00:04:30 -07001513 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
1514 && tdelta <= INT_MAX))
1515 return NULL;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001516 idelta = tdelta;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001517 if (idelta == 0)
1518 idelta = (tdays < 0) ? -1 : 1;
1519 newy = y;
1520 if (increment_overflow(&newy, idelta))
1521 return NULL;
1522 leapdays = leaps_thru_end_of(newy - 1) -
1523 leaps_thru_end_of(y - 1);
1524 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1525 tdays -= leapdays;
1526 y = newy;
1527 }
1528 {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001529 register int_fast32_t seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001530
Elliott Hughese0d0b152013-09-27 00:04:30 -07001531 seconds = tdays * SECSPERDAY;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001532 tdays = seconds / SECSPERDAY;
1533 rem += seconds - tdays * SECSPERDAY;
1534 }
1535 /*
1536 ** Given the range, we can now fearlessly cast...
1537 */
1538 idays = tdays;
1539 rem += offset - corr;
1540 while (rem < 0) {
1541 rem += SECSPERDAY;
1542 --idays;
1543 }
1544 while (rem >= SECSPERDAY) {
1545 rem -= SECSPERDAY;
1546 ++idays;
1547 }
1548 while (idays < 0) {
1549 if (increment_overflow(&y, -1))
1550 return NULL;
1551 idays += year_lengths[isleap(y)];
1552 }
1553 while (idays >= year_lengths[isleap(y)]) {
1554 idays -= year_lengths[isleap(y)];
1555 if (increment_overflow(&y, 1))
1556 return NULL;
1557 }
1558 tmp->tm_year = y;
1559 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1560 return NULL;
1561 tmp->tm_yday = idays;
1562 /*
1563 ** The "extra" mods below avoid overflow problems.
1564 */
1565 tmp->tm_wday = EPOCH_WDAY +
1566 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1567 (DAYSPERNYEAR % DAYSPERWEEK) +
1568 leaps_thru_end_of(y - 1) -
1569 leaps_thru_end_of(EPOCH_YEAR - 1) +
1570 idays;
1571 tmp->tm_wday %= DAYSPERWEEK;
1572 if (tmp->tm_wday < 0)
1573 tmp->tm_wday += DAYSPERWEEK;
1574 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1575 rem %= SECSPERHOUR;
1576 tmp->tm_min = (int) (rem / SECSPERMIN);
1577 /*
1578 ** A positive leap second requires a special
1579 ** representation. This uses "... ??:59:60" et seq.
1580 */
1581 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1582 ip = mon_lengths[isleap(y)];
1583 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1584 idays -= ip[tmp->tm_mon];
1585 tmp->tm_mday = (int) (idays + 1);
1586 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001587#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001588 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001589#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001590 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001591}
1592
1593char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001594ctime(const time_t * const timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001595{
1596/*
1597** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001598** The ctime function converts the calendar time pointed to by timer
1599** to local time in the form of a string. It is equivalent to
1600** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001601*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001602 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001603}
1604
1605char *
Elliott Hughesce4783c2013-07-12 17:31:11 -07001606ctime_r(const time_t * const timep, char * buf)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001607{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001608 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001610 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001611}
1612
1613/*
1614** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001615** The "best" way to do mktime I think is based on an idea of Bob
1616** Kridle's (so its said...) from a long time ago.
1617** It does a binary search of the time_t space. Since time_t's are
1618** just 32 bits, its a max of 32 iterations (even at 64 bits it
1619** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001620*/
1621
1622#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001623#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001624#endif /* !defined WRONG */
1625
1626/*
Elliott Hughesce4783c2013-07-12 17:31:11 -07001627** Normalize logic courtesy Paul Eggert.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001628*/
1629
1630static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001631increment_overflow(int *const ip, int j)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001633 register int const i = *ip;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001634
Elliott Hughesce4783c2013-07-12 17:31:11 -07001635 /*
1636 ** If i >= 0 there can only be overflow if i + j > INT_MAX
1637 ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
1638 ** If i < 0 there can only be overflow if i + j < INT_MIN
1639 ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
1640 */
1641 if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
1642 return TRUE;
1643 *ip += j;
1644 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001645}
1646
1647static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001648increment_overflow32(int_fast32_t *const lp, int const m)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001649{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001650 register int_fast32_t const l = *lp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001651
Elliott Hughesce4783c2013-07-12 17:31:11 -07001652 if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
1653 return TRUE;
1654 *lp += m;
1655 return FALSE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001656}
1657
1658static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001659normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001661 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001662
Elliott Hughesce4783c2013-07-12 17:31:11 -07001663 tensdelta = (*unitsptr >= 0) ?
1664 (*unitsptr / base) :
1665 (-1 - (-1 - *unitsptr) / base);
1666 *unitsptr -= tensdelta * base;
1667 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001668}
1669
1670static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001671normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr,
1672 const int base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001673{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001674 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001675
Elliott Hughesce4783c2013-07-12 17:31:11 -07001676 tensdelta = (*unitsptr >= 0) ?
1677 (*unitsptr / base) :
1678 (-1 - (-1 - *unitsptr) / base);
1679 *unitsptr -= tensdelta * base;
1680 return increment_overflow32(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681}
1682
1683static int
Elliott Hughesce4783c2013-07-12 17:31:11 -07001684tmcomp(register const struct tm * const atmp,
1685 register const struct tm * const btmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001686{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001687 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001688
Elliott Hughese0d0b152013-09-27 00:04:30 -07001689 if (atmp->tm_year != btmp->tm_year)
1690 return atmp->tm_year < btmp->tm_year ? -1 : 1;
1691 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001692 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1693 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1694 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1695 result = atmp->tm_sec - btmp->tm_sec;
1696 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697}
1698
1699static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001700time2sub(struct tm * const tmp,
1701 struct tm *(*const funcp)(const time_t*, int_fast32_t, struct tm*, const struct state*),
1702 const int_fast32_t offset,
1703 int * const okayp,
1704 const int do_norm_secs, const struct state * sp) // android-changed: added sp
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001705{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001706 register int dir;
1707 register int i, j;
1708 register int saved_seconds;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001709 register int_fast32_t li;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001710 register time_t lo;
1711 register time_t hi;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001712 int_fast32_t y;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001713 time_t newt;
1714 time_t t;
1715 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001716
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001717 *okayp = FALSE;
1718 yourtm = *tmp;
1719 if (do_norm_secs) {
1720 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1721 SECSPERMIN))
1722 return WRONG;
1723 }
1724 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1725 return WRONG;
1726 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1727 return WRONG;
1728 y = yourtm.tm_year;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001729 if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001730 return WRONG;
1731 /*
1732 ** Turn y into an actual year number for now.
1733 ** It is converted back to an offset from TM_YEAR_BASE later.
1734 */
Elliott Hughesce4783c2013-07-12 17:31:11 -07001735 if (increment_overflow32(&y, TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001736 return WRONG;
1737 while (yourtm.tm_mday <= 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001738 if (increment_overflow32(&y, -1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001739 return WRONG;
1740 li = y + (1 < yourtm.tm_mon);
1741 yourtm.tm_mday += year_lengths[isleap(li)];
1742 }
1743 while (yourtm.tm_mday > DAYSPERLYEAR) {
1744 li = y + (1 < yourtm.tm_mon);
1745 yourtm.tm_mday -= year_lengths[isleap(li)];
Elliott Hughesce4783c2013-07-12 17:31:11 -07001746 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001747 return WRONG;
1748 }
1749 for ( ; ; ) {
1750 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1751 if (yourtm.tm_mday <= i)
1752 break;
1753 yourtm.tm_mday -= i;
1754 if (++yourtm.tm_mon >= MONSPERYEAR) {
1755 yourtm.tm_mon = 0;
Elliott Hughesce4783c2013-07-12 17:31:11 -07001756 if (increment_overflow32(&y, 1))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001757 return WRONG;
1758 }
1759 }
Elliott Hughesce4783c2013-07-12 17:31:11 -07001760 if (increment_overflow32(&y, -TM_YEAR_BASE))
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001761 return WRONG;
1762 yourtm.tm_year = y;
1763 if (yourtm.tm_year != y)
1764 return WRONG;
1765 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1766 saved_seconds = 0;
1767 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1768 /*
1769 ** We can't set tm_sec to 0, because that might push the
1770 ** time below the minimum representable time.
1771 ** Set tm_sec to 59 instead.
1772 ** This assumes that the minimum representable time is
1773 ** not in the same minute that a leap second was deleted from,
1774 ** which is a safer assumption than using 58 would be.
1775 */
1776 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1777 return WRONG;
1778 saved_seconds = yourtm.tm_sec;
1779 yourtm.tm_sec = SECSPERMIN - 1;
1780 } else {
1781 saved_seconds = yourtm.tm_sec;
1782 yourtm.tm_sec = 0;
1783 }
1784 /*
1785 ** Do a binary search (this works whatever time_t's type is).
1786 */
1787 if (!TYPE_SIGNED(time_t)) {
1788 lo = 0;
1789 hi = lo - 1;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001790 } else {
1791 lo = 1;
1792 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1793 lo *= 2;
1794 hi = -(lo + 1);
1795 }
1796 for ( ; ; ) {
1797 t = lo / 2 + hi / 2;
1798 if (t < lo)
1799 t = lo;
1800 else if (t > hi)
1801 t = hi;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001802 if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001803 /*
1804 ** Assume that t is too extreme to be represented in
1805 ** a struct tm; arrange things so that it is less
1806 ** extreme on the next pass.
1807 */
1808 dir = (t > 0) ? 1 : -1;
1809 } else dir = tmcomp(&mytm, &yourtm);
1810 if (dir != 0) {
1811 if (t == lo) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001812 if (t == time_t_max)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001813 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001814 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001815 ++lo;
1816 } else if (t == hi) {
Elliott Hughes713fe642013-08-22 14:13:50 -07001817 if (t == time_t_min)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001818 return WRONG;
Elliott Hughes713fe642013-08-22 14:13:50 -07001819 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001820 --hi;
1821 }
1822 if (lo > hi)
1823 return WRONG;
1824 if (dir > 0)
1825 hi = t;
1826 else lo = t;
1827 continue;
1828 }
1829 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1830 break;
1831 /*
1832 ** Right time, wrong type.
1833 ** Hunt for right time, right type.
1834 ** It's okay to guess wrong since the guess
1835 ** gets checked.
1836 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001837 // BEGIN android-changed: support user-supplied sp
1838 if (sp == NULL) {
1839 sp = (const struct state *)
Elliott Hughesce4783c2013-07-12 17:31:11 -07001840 ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001841 }
1842 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001843#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001844 if (sp == NULL)
1845 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001846#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001847 for (i = sp->typecnt - 1; i >= 0; --i) {
1848 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1849 continue;
1850 for (j = sp->typecnt - 1; j >= 0; --j) {
1851 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1852 continue;
1853 newt = t + sp->ttis[j].tt_gmtoff -
1854 sp->ttis[i].tt_gmtoff;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001855 if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001856 continue;
1857 if (tmcomp(&mytm, &yourtm) != 0)
1858 continue;
1859 if (mytm.tm_isdst != yourtm.tm_isdst)
1860 continue;
1861 /*
1862 ** We have a match.
1863 */
1864 t = newt;
1865 goto label;
1866 }
1867 }
1868 return WRONG;
1869 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001870label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001871 newt = t + saved_seconds;
1872 if ((newt < t) != (saved_seconds < 0))
1873 return WRONG;
1874 t = newt;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001875 if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001876 *okayp = TRUE;
1877 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001878}
1879
1880static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001881time2(struct tm * const tmp,
1882 struct tm * (*const funcp)(const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1883 const int_fast32_t offset,
1884 int *const okayp, const struct state* sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001885{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001886 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001887
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001888 /*
1889 ** First try without normalization of seconds
1890 ** (in case tm_sec contains a value associated with a leap second).
1891 ** If that fails, try with normalization of seconds.
1892 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001893 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
1894 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001895}
1896
1897static time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001898time1(struct tm * const tmp,
1899 struct tm * (* const funcp) (const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp.
1900 const int_fast32_t offset, const struct state * sp) // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001901{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001902 register time_t t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001903 register int samei, otheri;
1904 register int sameind, otherind;
1905 register int i;
1906 register int nseen;
1907 int seen[TZ_MAX_TYPES];
1908 int types[TZ_MAX_TYPES];
1909 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001910
Elliott Hughesce4783c2013-07-12 17:31:11 -07001911 if (tmp == NULL) {
1912 errno = EINVAL;
1913 return WRONG;
1914 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001915 if (tmp->tm_isdst > 1)
1916 tmp->tm_isdst = 1;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001917 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001918#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001919 /*
1920 ** PCTS code courtesy Grant Sullivan.
1921 */
1922 if (okay)
1923 return t;
1924 if (tmp->tm_isdst < 0)
1925 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001926#endif /* defined PCTS */
1927#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001928 if (okay || tmp->tm_isdst < 0)
1929 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001930#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001931 /*
1932 ** We're supposed to assume that somebody took a time of one type
1933 ** and did some math on it that yielded a "struct tm" that's bad.
1934 ** We try to divine the type they started from and adjust to the
1935 ** type they need.
1936 */
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001937 // BEGIN android-changed: support user-supplied sp.
1938 if (sp == NULL) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07001939 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001940 }
1941 // BEGIN android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001942#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001943 if (sp == NULL)
1944 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001945#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001946 for (i = 0; i < sp->typecnt; ++i)
1947 seen[i] = FALSE;
1948 nseen = 0;
1949 for (i = sp->timecnt - 1; i >= 0; --i)
1950 if (!seen[sp->types[i]]) {
1951 seen[sp->types[i]] = TRUE;
1952 types[nseen++] = sp->types[i];
1953 }
1954 for (sameind = 0; sameind < nseen; ++sameind) {
1955 samei = types[sameind];
1956 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1957 continue;
1958 for (otherind = 0; otherind < nseen; ++otherind) {
1959 otheri = types[otherind];
1960 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1961 continue;
1962 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1963 sp->ttis[samei].tt_gmtoff;
1964 tmp->tm_isdst = !tmp->tm_isdst;
Elliott Hughesb989c9c2013-01-16 10:34:33 -08001965 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001966 if (okay)
1967 return t;
1968 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1969 sp->ttis[samei].tt_gmtoff;
1970 tmp->tm_isdst = !tmp->tm_isdst;
1971 }
1972 }
1973 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001974}
1975
1976time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001977mktime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001978{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001979 _tzLock();
1980 tzset_locked();
Elliott Hughesce4783c2013-07-12 17:31:11 -07001981 time_t result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001982 _tzUnlock();
1983 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984}
1985
1986#ifdef STD_INSPIRED
1987
1988time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001989timelocal(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001990{
Elliott Hughesce4783c2013-07-12 17:31:11 -07001991 if (tmp != NULL)
1992 tmp->tm_isdst = -1; /* in case it wasn't initialized */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001993 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001994}
1995
1996time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07001997timegm(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001998{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001999 time_t result;
2000
Elliott Hughesce4783c2013-07-12 17:31:11 -07002001 if (tmp != NULL)
2002 tmp->tm_isdst = 0;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002003 _tzLock();
Elliott Hughesb989c9c2013-01-16 10:34:33 -08002004 result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002005 _tzUnlock();
2006
2007 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002008}
2009
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002010#endif /* defined STD_INSPIRED */
2011
2012#ifdef CMUCS
2013
2014/*
2015** The following is supplied for compatibility with
2016** previous versions of the CMUCS runtime library.
2017*/
2018
Elliott Hughese0d0b152013-09-27 00:04:30 -07002019long
Elliott Hughesce4783c2013-07-12 17:31:11 -07002020gtime(struct tm * const tmp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002021{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002022 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002023
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002024 if (t == WRONG)
2025 return -1;
2026 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002027}
2028
2029#endif /* defined CMUCS */
2030
2031/*
2032** XXX--is the below the right way to conditionalize??
2033*/
2034
2035#ifdef STD_INSPIRED
2036
2037/*
2038** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2039** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2040** is not the case if we are accounting for leap seconds.
2041** So, we provide the following conversion routines for use
2042** when exchanging timestamps with POSIX conforming systems.
2043*/
2044
Elliott Hughesce4783c2013-07-12 17:31:11 -07002045static int_fast64_t
2046leapcorr(time_t * timep)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002047{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002048 register struct state * sp;
2049 register struct lsinfo * lp;
2050 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002051
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002052 sp = lclptr;
2053 i = sp->leapcnt;
2054 while (--i >= 0) {
2055 lp = &sp->lsis[i];
2056 if (*timep >= lp->ls_trans)
2057 return lp->ls_corr;
2058 }
2059 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002060}
2061
2062time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002063time2posix(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002064{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002065 tzset();
2066 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002067}
2068
2069time_t
Elliott Hughesce4783c2013-07-12 17:31:11 -07002070posix2time(time_t t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002071{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002072 time_t x;
2073 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002075 tzset();
2076 /*
2077 ** For a positive leap second hit, the result
2078 ** is not unique. For a negative leap second
2079 ** hit, the corresponding time doesn't exist,
2080 ** so we return an adjacent second.
2081 */
2082 x = t + leapcorr(&t);
2083 y = x - leapcorr(&x);
2084 if (y < t) {
2085 do {
2086 x++;
2087 y = x - leapcorr(&x);
2088 } while (y < t);
2089 if (t != y)
2090 return x - 1;
2091 } else if (y > t) {
2092 do {
2093 --x;
2094 y = x - leapcorr(&x);
2095 } while (y > t);
2096 if (t != y)
2097 return x + 1;
2098 }
2099 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002100}
2101
2102#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002103
Elliott Hughesce4783c2013-07-12 17:31:11 -07002104// BEGIN android-added
2105
Elliott Hughes1c295722012-10-19 18:13:15 -07002106#include <assert.h>
Elliott Hughesd23af232012-10-17 16:30:47 -07002107#include <stdint.h>
Elliott Hughes8b954042012-10-18 13:42:59 -07002108#include <arpa/inet.h> // For ntohl(3).
Elliott Hughesd23af232012-10-17 16:30:47 -07002109
Elliott Hughesce4783c2013-07-12 17:31:11 -07002110static int to_int(unsigned char* s) {
2111 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
2112}
2113
Elliott Hughescf178bf2013-09-18 19:25:28 -07002114static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix,
2115 const char* olson_id, int* data_size) {
2116 const char* path_prefix = getenv(path_prefix_variable);
2117 if (path_prefix == NULL) {
2118 fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
2119 return -1;
2120 }
2121 char path[PATH_MAX];
2122 snprintf(path, sizeof(path), "%s/%s", path_prefix, path_suffix);
Elliott Hughes1c295722012-10-19 18:13:15 -07002123 int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
Elliott Hughesd23af232012-10-17 16:30:47 -07002124 if (fd == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002125 XLOG(("%s: could not open \"%s\": %s\n", __FUNCTION__, path, strerror(errno)));
2126 return -2; // Distinguish failure to find any data from failure to find a specific id.
Elliott Hughesd23af232012-10-17 16:30:47 -07002127 }
2128
2129 // byte[12] tzdata_version -- "tzdata2012f\0"
Elliott Hughesd23af232012-10-17 16:30:47 -07002130 // int index_offset
2131 // int data_offset
2132 // int zonetab_offset
2133 struct bionic_tzdata_header {
2134 char tzdata_version[12];
Elliott Hughesd23af232012-10-17 16:30:47 -07002135 int32_t index_offset;
2136 int32_t data_offset;
2137 int32_t zonetab_offset;
2138 } header;
Elliott Hughese7aaad82013-04-25 14:02:59 -07002139 memset(&header, 0, sizeof(header));
2140 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
2141 if (bytes_read != sizeof(header)) {
2142 fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
2143 __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
Elliott Hughesd23af232012-10-17 16:30:47 -07002144 close(fd);
2145 return -1;
2146 }
2147
2148 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002149 fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
2150 __FUNCTION__, path, header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002151 close(fd);
2152 return -1;
2153 }
Elliott Hughesd23af232012-10-17 16:30:47 -07002154
2155#if 0
Elliott Hughes23935352012-10-22 14:47:58 -07002156 fprintf(stderr, "version: %s\n", header.tzdata_version);
Elliott Hughesd23af232012-10-17 16:30:47 -07002157 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2158 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2159 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2160#endif
2161
2162 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002163 fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
2164 __FUNCTION__, path, strerror(errno));
Elliott Hughesd23af232012-10-17 16:30:47 -07002165 close(fd);
2166 return -1;
2167 }
2168
2169 off_t specific_zone_offset = -1;
2170
Elliott Hughes1c295722012-10-19 18:13:15 -07002171 static const size_t NAME_LENGTH = 40;
2172 unsigned char buf[NAME_LENGTH + 3 * sizeof(int32_t)];
Elliott Hughese0175ca2013-03-14 14:38:08 -07002173
2174 size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(buf);
2175 for (size_t i = 0; i < id_count; ++i) {
2176 if (TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf))) != (ssize_t) sizeof(buf)) {
2177 break;
2178 }
2179
Elliott Hughes1c295722012-10-19 18:13:15 -07002180 char this_id[NAME_LENGTH + 1];
2181 memcpy(this_id, buf, NAME_LENGTH);
2182 this_id[NAME_LENGTH] = '\0';
Elliott Hughesd23af232012-10-17 16:30:47 -07002183
2184 if (strcmp(this_id, olson_id) == 0) {
Elliott Hughesce4783c2013-07-12 17:31:11 -07002185 specific_zone_offset = to_int(buf + NAME_LENGTH) + ntohl(header.data_offset);
2186 *data_size = to_int(buf + NAME_LENGTH + sizeof(int32_t));
Elliott Hughesd23af232012-10-17 16:30:47 -07002187 break;
2188 }
2189 }
2190
2191 if (specific_zone_offset == -1) {
Elliott Hughes1c295722012-10-19 18:13:15 -07002192 XLOG(("%s: couldn't find zone \"%s\"\n", __FUNCTION__, olson_id));
Elliott Hughesd23af232012-10-17 16:30:47 -07002193 close(fd);
2194 return -1;
2195 }
2196
2197 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
Elliott Hughese7aaad82013-04-25 14:02:59 -07002198 fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
2199 __FUNCTION__, specific_zone_offset, path, strerror(errno));
Elliott Hughesd23af232012-10-17 16:30:47 -07002200 close(fd);
2201 return -1;
2202 }
2203
Elliott Hughese7aaad82013-04-25 14:02:59 -07002204 // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
2205
Elliott Hughesd23af232012-10-17 16:30:47 -07002206 return fd;
2207}
Elliott Hughes1c295722012-10-19 18:13:15 -07002208
2209static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002210 int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002211 if (fd < 0) {
Elliott Hughescf178bf2013-09-18 19:25:28 -07002212 fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
Elliott Hughes1c295722012-10-19 18:13:15 -07002213 if (fd == -2) {
Elliott Hughes49271d82012-10-25 14:38:51 -07002214 // The first thing that 'recovery' does is try to format the current time. It doesn't have
2215 // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
2216 fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
Elliott Hughes1c295722012-10-19 18:13:15 -07002217 }
2218 }
2219 return fd;
2220}
Elliott Hughesce4783c2013-07-12 17:31:11 -07002221
2222// Caches the most recent timezone (http://b/8270865).
2223static int __bionic_tzload_cached(const char* name, struct state* const sp, const int doextend) {
2224 _tzLock();
2225
2226 // Our single-item cache.
2227 static char* gCachedTimeZoneName;
2228 static struct state gCachedTimeZone;
2229
2230 // Do we already have this timezone cached?
2231 if (gCachedTimeZoneName != NULL && strcmp(name, gCachedTimeZoneName) == 0) {
2232 *sp = gCachedTimeZone;
2233 _tzUnlock();
2234 return 0;
2235 }
2236
2237 // Can we load it?
2238 int rc = tzload(name, sp, doextend);
2239 if (rc == 0) {
2240 // Update the cache.
2241 free(gCachedTimeZoneName);
2242 gCachedTimeZoneName = strdup(name);
2243 gCachedTimeZone = *sp;
2244 }
2245
2246 _tzUnlock();
2247 return rc;
2248}
2249
2250// Non-standard API: mktime(3) but with an explicit timezone parameter.
2251time_t mktime_tz(struct tm* const tmp, const char* tz) {
2252 struct state st;
2253 if (__bionic_tzload_cached(tz, &st, TRUE) != 0) {
2254 // TODO: not sure what's best here, but for now, we fall back to gmt.
2255 gmtload(&st);
2256 }
2257 return time1(tmp, localsub, 0L, &st);
2258}
2259
2260// Non-standard API: localtime(3) but with an explicit timezone parameter.
2261void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz) {
2262 struct state st;
2263 if (__bionic_tzload_cached(tz, &st, TRUE) != 0) {
2264 // TODO: not sure what's best here, but for now, we fall back to gmt.
2265 gmtload(&st);
2266 }
2267 localsub(timep, 0L, tmp, &st);
2268}
2269
2270// END android-added