blob: 94dc5b935f78cf597aec1024aced4eaa6e1f8899 [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
6#ifndef lint
7#ifndef NOID
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07008static char elsieid[] = "@(#)localtime.c 8.3";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08009#endif /* !defined NOID */
10#endif /* !defined lint */
11
12/*
13** Leap second handling from Bradley White.
14** POSIX-style TZ environment variable handling from Guy Harris.
15*/
16
17/*LINTLIBRARY*/
18
19#include "private.h"
20#include "tzfile.h"
21#include "fcntl.h"
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070022#include "float.h" /* for FLT_MAX and DBL_MAX */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070024#include "thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025#include <sys/system_properties.h>
26
27#ifndef TZ_ABBR_MAX_LEN
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070028#define TZ_ABBR_MAX_LEN 16
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#endif /* !defined TZ_ABBR_MAX_LEN */
30
31#ifndef TZ_ABBR_CHAR_SET
32#define TZ_ABBR_CHAR_SET \
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070033 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#endif /* !defined TZ_ABBR_CHAR_SET */
35
36#ifndef TZ_ABBR_ERR_CHAR
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070037#define TZ_ABBR_ERR_CHAR '_'
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#endif /* !defined TZ_ABBR_ERR_CHAR */
39
Elliott Hughesd23af232012-10-17 16:30:47 -070040#define TZDATA_PATH "/system/usr/share/zoneinfo/tzdata"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#define NAMELEN 40
42#define INTLEN 4
43#define READLEN (NAMELEN + 3 * INTLEN)
44
45/*
46** SunOS 4.1.1 headers lack O_BINARY.
47*/
48
49#ifdef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070050#define OPEN_MODE (O_RDONLY | O_BINARY)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#endif /* defined O_BINARY */
52#ifndef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070053#define OPEN_MODE O_RDONLY
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#endif /* !defined O_BINARY */
55
56#if 0
57# define XLOG(xx) printf xx , fflush(stdout)
58#else
59# define XLOG(x) do{}while (0)
60#endif
61
David 'Digit' Turner6481b912010-12-06 12:23:16 +010062/* Add the following function implementations:
63 * timelocal()
64 * timegm()
65 * time2posix()
66 * posix2time()
67 */
68#define STD_INSPIRED 1
69
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070070/* THREAD-SAFETY SUPPORT GOES HERE */
71static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER;
72
73static __inline__ void _tzLock(void)
74{
75 if (__isthreaded)
76 pthread_mutex_lock(&_tzMutex);
77}
78
79static __inline__ void _tzUnlock(void)
80{
81 if (__isthreaded)
82 pthread_mutex_unlock(&_tzMutex);
83}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
David 'Digit' Turner2093d352009-09-09 17:41:59 -070085/* Complex computations to determine the min/max of time_t depending
86 * on TYPE_BIT / TYPE_SIGNED / TYPE_INTEGRAL.
87 * These macros cannot be used in pre-processor directives, so we
88 * let the C compiler do the work, which makes things a bit funky.
89 */
90static const time_t TIME_T_MAX =
91 TYPE_INTEGRAL(time_t) ?
92 ( TYPE_SIGNED(time_t) ?
93 ~((time_t)1 << (TYPE_BIT(time_t)-1))
94 :
95 ~(time_t)0
96 )
97 : /* if time_t is a floating point number */
98 ( sizeof(time_t) > sizeof(float) ? (time_t)DBL_MAX : (time_t)FLT_MAX );
99
100static const time_t TIME_T_MIN =
101 TYPE_INTEGRAL(time_t) ?
102 ( TYPE_SIGNED(time_t) ?
103 ((time_t)1 << (TYPE_BIT(time_t)-1))
104 :
105 0
106 )
107 :
108 ( sizeof(time_t) > sizeof(float) ? (time_t)DBL_MIN : (time_t)FLT_MIN );
109
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110#ifndef WILDABBR
111/*
112** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700113** 1. They might reference tzname[0] before calling tzset (explicitly
114** or implicitly).
115** 2. They might reference tzname[1] before calling tzset (explicitly
116** or implicitly).
117** 3. They might reference tzname[1] after setting to a time zone
118** in which Daylight Saving Time is never observed.
119** 4. They might reference tzname[0] after setting to a time zone
120** in which Standard Time is never observed.
121** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122** What's best to do in the above cases is open to debate;
123** for now, we just set things up so that in any of the five cases
124** WILDABBR is used. Another possibility: initialize tzname[0] to the
125** string "tzname[0] used before set", and similarly for the other cases.
126** And another: initialize tzname[0] to "ERA", with an explanation in the
127** manual page of what this "time zone abbreviation" means (doing this so
128** that tzname[0] has the "normal" length of three characters).
129*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700130#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131#endif /* !defined WILDABBR */
132
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700133static char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700135static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136
137/*
138** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
139** We default to US rules as of 1999-08-17.
140** POSIX 1003.1 section 8.1.1 says that the default DST rules are
141** implementation dependent; for historical reasons, US rules are a
142** common default.
143*/
144#ifndef TZDEFRULESTRING
145#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
146#endif /* !defined TZDEFDST */
147
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700148struct ttinfo { /* time type information */
149 long tt_gmtoff; /* UTC offset in seconds */
150 int tt_isdst; /* used to set tm_isdst */
151 int tt_abbrind; /* abbreviation list index */
152 int tt_ttisstd; /* TRUE if transition is std time */
153 int tt_ttisgmt; /* TRUE if transition is UTC */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154};
155
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700156struct lsinfo { /* leap second information */
157 time_t ls_trans; /* transition time */
158 long ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159};
160
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700161#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
163#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700164#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165#endif /* defined TZNAME_MAX */
166#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700167#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168#endif /* !defined TZNAME_MAX */
169
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700170/* XXX: This code should really use time64_t instead of time_t
171 * but we can't change it without re-generating the index
172 * file first with the correct data.
173 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174struct state {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700175 int leapcnt;
176 int timecnt;
177 int typecnt;
178 int charcnt;
179 int goback;
180 int goahead;
181 time_t ats[TZ_MAX_TIMES];
182 unsigned char types[TZ_MAX_TIMES];
183 struct ttinfo ttis[TZ_MAX_TYPES];
184 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
185 (2 * (MY_TZNAME_MAX + 1)))];
186 struct lsinfo lsis[TZ_MAX_LEAPS];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187};
188
189struct rule {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700190 int r_type; /* type of rule--see below */
191 int r_day; /* day number of rule */
192 int r_week; /* week number of rule */
193 int r_mon; /* month number of rule */
194 long r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195};
196
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700197#define JULIAN_DAY 0 /* Jn - Julian day */
198#define DAY_OF_YEAR 1 /* n - day of year */
199#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 -0800200
201/*
202** Prototypes for static functions.
203*/
204
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700205/* NOTE: all internal functions assume that _tzLock() was already called */
206
Elliott Hughesd23af232012-10-17 16:30:47 -0700207static int __bionic_open_tzdata(const char*, int*);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700208static long detzcode P((const char * codep));
209static time_t detzcode64 P((const char * codep));
210static int differ_by_repeat P((time_t t1, time_t t0));
211static const char * getzname P((const char * strp));
212static const char * getqzname P((const char * strp, const int delim));
213static const char * getnum P((const char * strp, int * nump, int min,
214 int max));
215static const char * getsecs P((const char * strp, long * secsp));
216static const char * getoffset P((const char * strp, long * offsetp));
217static const char * getrule P((const char * strp, struct rule * rulep));
218static void gmtload P((struct state * sp));
219static struct tm * gmtsub P((const time_t * timep, long offset,
Elliott Hughes3a936a42012-09-11 11:15:53 -0700220 struct tm * tmp, const struct state * sp)); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700221static struct tm * localsub P((const time_t * timep, long offset,
Elliott Hughes3a936a42012-09-11 11:15:53 -0700222 struct tm * tmp, const struct state * sp)); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700223static int increment_overflow P((int * number, int delta));
224static int leaps_thru_end_of P((int y));
225static int long_increment_overflow P((long * number, int delta));
226static int long_normalize_overflow P((long * tensptr,
227 int * unitsptr, int base));
228static int normalize_overflow P((int * tensptr, int * unitsptr,
229 int base));
230static void settzname P((void));
231static time_t time1 P((struct tm * tmp,
232 struct tm * (*funcp) P((const time_t *,
Elliott Hughes3a936a42012-09-11 11:15:53 -0700233 long, struct tm *, const struct state *)), // android-changed: added state*.
234 long offset, const struct state * sp)); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700235static time_t time2 P((struct tm *tmp,
236 struct tm * (*funcp) P((const time_t *,
Elliott Hughes3a936a42012-09-11 11:15:53 -0700237 long, struct tm*, const struct state *)), // android-changed: added state*.
238 long offset, int * okayp, const struct state * sp)); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700239static time_t time2sub P((struct tm *tmp,
240 struct tm * (*funcp) P((const time_t *,
Elliott Hughes3a936a42012-09-11 11:15:53 -0700241 long, struct tm*, const struct state *)), // android-changed: added state*.
242 long offset, int * okayp, int do_norm_secs, const struct state * sp)); // android-change: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700243static struct tm * timesub P((const time_t * timep, long offset,
244 const struct state * sp, struct tm * tmp));
245static int tmcomp P((const struct tm * atmp,
246 const struct tm * btmp));
247static time_t transtime P((time_t janfirst, int year,
248 const struct rule * rulep, long offset));
249static int tzload P((const char * name, struct state * sp,
250 int doextend));
251static int tzparse P((const char * name, struct state * sp,
252 int lastditch));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253
254#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700255static struct state * lclptr;
256static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257#endif /* defined ALL_STATE */
258
259#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700260static struct state lclmem;
261static struct state gmtmem;
262#define lclptr (&lclmem)
263#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264#endif /* State Farm */
265
266#ifndef TZ_STRLEN_MAX
267#define TZ_STRLEN_MAX 255
268#endif /* !defined TZ_STRLEN_MAX */
269
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700270static char lcl_TZname[TZ_STRLEN_MAX + 1];
271static int lcl_is_set;
272static int gmt_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700274char * tzname[2] = {
275 wildabbr,
276 wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277};
278
279/*
280** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700281** Except for the strftime function, these functions [asctime,
282** ctime, gmtime, localtime] return values in one of two static
283** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284** Thanks to Paul Eggert for noting this.
285*/
286
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700287static struct tm tmGlobal;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
289#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700290time_t timezone = 0;
291int daylight = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292#endif /* defined USG_COMPAT */
293
294#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700295time_t altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296#endif /* defined ALTZONE */
297
298static long
299detzcode(codep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700300const char * const codep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700302 register long result;
303 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700305 result = (codep[0] & 0x80) ? ~0L : 0;
306 for (i = 0; i < 4; ++i)
307 result = (result << 8) | (codep[i] & 0xff);
308 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309}
310
311static time_t
312detzcode64(codep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700313const char * const codep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700315 register time_t result;
316 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700318 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
319 for (i = 0; i < 8; ++i)
320 result = result * 256 + (codep[i] & 0xff);
321 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322}
323
324static void
325settzname P((void))
326{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700327 register struct state * const sp = lclptr;
328 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700330 tzname[0] = wildabbr;
331 tzname[1] = wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800332#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700333 daylight = 0;
334 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335#endif /* defined USG_COMPAT */
336#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700337 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338#endif /* defined ALTZONE */
339#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700340 if (sp == NULL) {
341 tzname[0] = tzname[1] = gmt;
342 return;
343 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800344#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700345 for (i = 0; i < sp->typecnt; ++i) {
346 register const struct ttinfo * const ttisp = &sp->ttis[i];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700348 tzname[ttisp->tt_isdst] =
349 &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700351 if (ttisp->tt_isdst)
352 daylight = 1;
353 if (i == 0 || !ttisp->tt_isdst)
354 timezone = -(ttisp->tt_gmtoff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355#endif /* defined USG_COMPAT */
356#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700357 if (i == 0 || ttisp->tt_isdst)
358 altzone = -(ttisp->tt_gmtoff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359#endif /* defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700360 }
361 /*
362 ** And to get the latest zone names into tzname. . .
363 */
364 for (i = 0; i < sp->timecnt; ++i) {
365 register const struct ttinfo * const ttisp =
366 &sp->ttis[
367 sp->types[i]];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700369 tzname[ttisp->tt_isdst] =
370 &sp->chars[ttisp->tt_abbrind];
371 }
372 /*
373 ** Finally, scrub the abbreviations.
374 ** First, replace bogus characters.
375 */
376 for (i = 0; i < sp->charcnt; ++i)
377 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
378 sp->chars[i] = TZ_ABBR_ERR_CHAR;
379 /*
380 ** Second, truncate long abbreviations.
381 */
382 for (i = 0; i < sp->typecnt; ++i) {
383 register const struct ttinfo * const ttisp = &sp->ttis[i];
384 register char * cp = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700386 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
387 strcmp(cp, GRANDPARENTED) != 0)
388 *(cp + TZ_ABBR_MAX_LEN) = '\0';
389 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390}
391
392static int
393differ_by_repeat(t1, t0)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700394const time_t t1;
395const time_t t0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700397 if (TYPE_INTEGRAL(time_t) &&
398 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
399 return 0;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700400#if SECSPERREPEAT_BITS <= 32 /* to avoid compiler warning (condition is always false) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 return (t1 - t0) == SECSPERREPEAT;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700402#else
403 return 0;
404#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405}
406
407static int toint(unsigned char *s) {
408 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
409}
410
411static int
412tzload(name, sp, doextend)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700413register const char * name;
414register struct state * const sp;
415register const int doextend;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700417 register const char * p;
418 register int i;
419 register int fid;
420 register int stored;
421 register int nread;
422 union {
423 struct tzhead tzhead;
424 char buf[2 * sizeof(struct tzhead) +
425 2 * sizeof *sp +
426 4 * TZ_MAX_TIMES];
427 } u;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 int toread = sizeof u.buf;
429
430 if (name == NULL && (name = TZDEFAULT) == NULL) {
431 XLOG(("tzload: null 'name' parameter\n" ));
432 return -1;
433 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700434 {
435 register int doaccess;
436 /*
437 ** Section 4.9.1 of the C standard says that
438 ** "FILENAME_MAX expands to an integral constant expression
439 ** that is the size needed for an array of char large enough
440 ** to hold the longest file name string that the implementation
441 ** guarantees can be opened."
442 */
443 char fullname[FILENAME_MAX + 1];
444 char *origname = (char*) name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700446 if (name[0] == ':')
447 ++name;
448 doaccess = name[0] == '/';
449 if (!doaccess) {
450 if ((p = TZDIR) == NULL) {
451 XLOG(("tzload: null TZDIR macro ?\n" ));
452 return -1;
453 }
454 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) {
455 XLOG(( "tzload: path too long: %s/%s\n", p, name ));
456 return -1;
457 }
458 (void) strcpy(fullname, p);
459 (void) strcat(fullname, "/");
460 (void) strcat(fullname, name);
461 /*
462 ** Set doaccess if '.' (as in "../") shows up in name.
463 */
464 if (strchr(name, '.') != NULL)
465 doaccess = TRUE;
466 name = fullname;
467 }
468 if (doaccess && access(name, R_OK) != 0) {
469 XLOG(( "tzload: could not find '%s'\n", name ));
470 return -1;
471 }
472 if ((fid = open(name, OPEN_MODE)) == -1) {
Elliott Hughesd23af232012-10-17 16:30:47 -0700473 fid = __bionic_open_tzdata(origname, &toread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800474 if (fid < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800475 return -1;
476 }
477 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700478 }
479 nread = read(fid, u.buf, toread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800480 if (close(fid) < 0 || nread <= 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700481 XLOG(( "tzload: could not read content of '%s'\n", DATAFILE ));
482 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800483 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700484 for (stored = 4; stored <= 8; stored *= 2) {
485 int ttisstdcnt;
486 int ttisgmtcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800487
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700488 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
489 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
490 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
491 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
492 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
493 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
494 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
495 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
496 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
497 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
498 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
499 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
500 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
501 return -1;
502 if (nread - (p - u.buf) <
503 sp->timecnt * stored + /* ats */
504 sp->timecnt + /* types */
505 sp->typecnt * 6 + /* ttinfos */
506 sp->charcnt + /* chars */
507 sp->leapcnt * (stored + 4) + /* lsinfos */
508 ttisstdcnt + /* ttisstds */
509 ttisgmtcnt) /* ttisgmts */
510 return -1;
511 for (i = 0; i < sp->timecnt; ++i) {
512 sp->ats[i] = (stored == 4) ?
513 detzcode(p) : detzcode64(p);
514 p += stored;
515 }
516 for (i = 0; i < sp->timecnt; ++i) {
517 sp->types[i] = (unsigned char) *p++;
518 if (sp->types[i] >= sp->typecnt)
519 return -1;
520 }
521 for (i = 0; i < sp->typecnt; ++i) {
522 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700524 ttisp = &sp->ttis[i];
525 ttisp->tt_gmtoff = detzcode(p);
526 p += 4;
527 ttisp->tt_isdst = (unsigned char) *p++;
528 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
529 return -1;
530 ttisp->tt_abbrind = (unsigned char) *p++;
531 if (ttisp->tt_abbrind < 0 ||
532 ttisp->tt_abbrind > sp->charcnt)
533 return -1;
534 }
535 for (i = 0; i < sp->charcnt; ++i)
536 sp->chars[i] = *p++;
537 sp->chars[i] = '\0'; /* ensure '\0' at end */
538 for (i = 0; i < sp->leapcnt; ++i) {
539 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700541 lsisp = &sp->lsis[i];
542 lsisp->ls_trans = (stored == 4) ?
543 detzcode(p) : detzcode64(p);
544 p += stored;
545 lsisp->ls_corr = detzcode(p);
546 p += 4;
547 }
548 for (i = 0; i < sp->typecnt; ++i) {
549 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700551 ttisp = &sp->ttis[i];
552 if (ttisstdcnt == 0)
553 ttisp->tt_ttisstd = FALSE;
554 else {
555 ttisp->tt_ttisstd = *p++;
556 if (ttisp->tt_ttisstd != TRUE &&
557 ttisp->tt_ttisstd != FALSE)
558 return -1;
559 }
560 }
561 for (i = 0; i < sp->typecnt; ++i) {
562 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800563
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700564 ttisp = &sp->ttis[i];
565 if (ttisgmtcnt == 0)
566 ttisp->tt_ttisgmt = FALSE;
567 else {
568 ttisp->tt_ttisgmt = *p++;
569 if (ttisp->tt_ttisgmt != TRUE &&
570 ttisp->tt_ttisgmt != FALSE)
571 return -1;
572 }
573 }
574 /*
575 ** Out-of-sort ats should mean we're running on a
576 ** signed time_t system but using a data file with
577 ** unsigned values (or vice versa).
578 */
579 for (i = 0; i < sp->timecnt - 2; ++i)
580 if (sp->ats[i] > sp->ats[i + 1]) {
581 ++i;
582 if (TYPE_SIGNED(time_t)) {
583 /*
584 ** Ignore the end (easy).
585 */
586 sp->timecnt = i;
587 } else {
588 /*
589 ** Ignore the beginning (harder).
590 */
591 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800592
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700593 for (j = 0; j + i < sp->timecnt; ++j) {
594 sp->ats[j] = sp->ats[j + i];
595 sp->types[j] = sp->types[j + i];
596 }
597 sp->timecnt = j;
598 }
599 break;
600 }
601 /*
602 ** If this is an old file, we're done.
603 */
604 if (u.tzhead.tzh_version[0] == '\0')
605 break;
606 nread -= p - u.buf;
607 for (i = 0; i < nread; ++i)
608 u.buf[i] = p[i];
609 /*
610 ** If this is a narrow integer time_t system, we're done.
611 */
612 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
613 break;
614 }
615 if (doextend && nread > 2 &&
616 u.buf[0] == '\n' && u.buf[nread - 1] == '\n' &&
617 sp->typecnt + 2 <= TZ_MAX_TYPES) {
618 struct state ts;
619 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800620
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700621 u.buf[nread - 1] = '\0';
622 result = tzparse(&u.buf[1], &ts, FALSE);
623 if (result == 0 && ts.typecnt == 2 &&
624 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
625 for (i = 0; i < 2; ++i)
626 ts.ttis[i].tt_abbrind +=
627 sp->charcnt;
628 for (i = 0; i < ts.charcnt; ++i)
629 sp->chars[sp->charcnt++] =
630 ts.chars[i];
631 i = 0;
632 while (i < ts.timecnt &&
633 ts.ats[i] <=
634 sp->ats[sp->timecnt - 1])
635 ++i;
636 while (i < ts.timecnt &&
637 sp->timecnt < TZ_MAX_TIMES) {
638 sp->ats[sp->timecnt] =
639 ts.ats[i];
640 sp->types[sp->timecnt] =
641 sp->typecnt +
642 ts.types[i];
643 ++sp->timecnt;
644 ++i;
645 }
646 sp->ttis[sp->typecnt++] = ts.ttis[0];
647 sp->ttis[sp->typecnt++] = ts.ttis[1];
648 }
649 }
650 i = 2 * YEARSPERREPEAT;
651 sp->goback = sp->goahead = sp->timecnt > i;
652 sp->goback &= sp->types[i] == sp->types[0] &&
653 differ_by_repeat(sp->ats[i], sp->ats[0]);
654 sp->goahead &=
655 sp->types[sp->timecnt - 1] == sp->types[sp->timecnt - 1 - i] &&
656 differ_by_repeat(sp->ats[sp->timecnt - 1],
657 sp->ats[sp->timecnt - 1 - i]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800658 XLOG(( "tzload: load ok !!\n" ));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700659 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660}
661
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700662static const int mon_lengths[2][MONSPERYEAR] = {
663 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
664 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665};
666
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700667static const int year_lengths[2] = {
668 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669};
670
671/*
672** Given a pointer into a time zone string, scan until a character that is not
673** a valid character in a zone name is found. Return a pointer to that
674** character.
675*/
676
677static const char *
678getzname(strp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700679register const char * strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800680{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700681 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700683 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
684 c != '+')
685 ++strp;
686 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800687}
688
689/*
690** Given a pointer into an extended time zone string, scan until the ending
691** delimiter of the zone name is located. Return a pointer to the delimiter.
692**
693** As with getzname above, the legal character set is actually quite
694** restricted, with other characters producing undefined results.
695** We don't do any checking here; checking is done later in common-case code.
696*/
697
698static const char *
699getqzname(register const char *strp, const int delim)
700{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700701 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700703 while ((c = *strp) != '\0' && c != delim)
704 ++strp;
705 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706}
707
708/*
709** Given a pointer into a time zone string, extract a number from that string.
710** Check that the number is within a specified range; if it is not, return
711** NULL.
712** Otherwise, return a pointer to the first character not part of the number.
713*/
714
715static const char *
716getnum(strp, nump, min, max)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700717register const char * strp;
718int * const nump;
719const int min;
720const int max;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800721{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700722 register char c;
723 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700725 if (strp == NULL || !is_digit(c = *strp))
726 return NULL;
727 num = 0;
728 do {
729 num = num * 10 + (c - '0');
730 if (num > max)
731 return NULL; /* illegal value */
732 c = *++strp;
733 } while (is_digit(c));
734 if (num < min)
735 return NULL; /* illegal value */
736 *nump = num;
737 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738}
739
740/*
741** Given a pointer into a time zone string, extract a number of seconds,
742** in hh[:mm[:ss]] form, from the string.
743** If any error occurs, return NULL.
744** Otherwise, return a pointer to the first character not part of the number
745** of seconds.
746*/
747
748static const char *
749getsecs(strp, secsp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700750register const char * strp;
751long * const secsp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800752{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700753 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800754
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700755 /*
756 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
757 ** "M10.4.6/26", which does not conform to Posix,
758 ** but which specifies the equivalent of
759 ** ``02:00 on the first Sunday on or after 23 Oct''.
760 */
761 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
762 if (strp == NULL)
763 return NULL;
764 *secsp = num * (long) SECSPERHOUR;
765 if (*strp == ':') {
766 ++strp;
767 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
768 if (strp == NULL)
769 return NULL;
770 *secsp += num * SECSPERMIN;
771 if (*strp == ':') {
772 ++strp;
773 /* `SECSPERMIN' allows for leap seconds. */
774 strp = getnum(strp, &num, 0, SECSPERMIN);
775 if (strp == NULL)
776 return NULL;
777 *secsp += num;
778 }
779 }
780 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800781}
782
783/*
784** Given a pointer into a time zone string, extract an offset, in
785** [+-]hh[:mm[:ss]] form, from the string.
786** If any error occurs, return NULL.
787** Otherwise, return a pointer to the first character not part of the time.
788*/
789
790static const char *
791getoffset(strp, offsetp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700792register const char * strp;
793long * const offsetp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700795 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800796
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700797 if (*strp == '-') {
798 neg = 1;
799 ++strp;
800 } else if (*strp == '+')
801 ++strp;
802 strp = getsecs(strp, offsetp);
803 if (strp == NULL)
804 return NULL; /* illegal time */
805 if (neg)
806 *offsetp = -*offsetp;
807 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800808}
809
810/*
811** Given a pointer into a time zone string, extract a rule in the form
812** date[/time]. See POSIX section 8 for the format of "date" and "time".
813** If a valid rule is not found, return NULL.
814** Otherwise, return a pointer to the first character not part of the rule.
815*/
816
817static const char *
818getrule(strp, rulep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700819const char * strp;
820register struct rule * const rulep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800821{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700822 if (*strp == 'J') {
823 /*
824 ** Julian day.
825 */
826 rulep->r_type = JULIAN_DAY;
827 ++strp;
828 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
829 } else if (*strp == 'M') {
830 /*
831 ** Month, week, day.
832 */
833 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
834 ++strp;
835 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
836 if (strp == NULL)
837 return NULL;
838 if (*strp++ != '.')
839 return NULL;
840 strp = getnum(strp, &rulep->r_week, 1, 5);
841 if (strp == NULL)
842 return NULL;
843 if (*strp++ != '.')
844 return NULL;
845 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
846 } else if (is_digit(*strp)) {
847 /*
848 ** Day of year.
849 */
850 rulep->r_type = DAY_OF_YEAR;
851 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
852 } else return NULL; /* invalid format */
853 if (strp == NULL)
854 return NULL;
855 if (*strp == '/') {
856 /*
857 ** Time specified.
858 */
859 ++strp;
860 strp = getsecs(strp, &rulep->r_time);
861 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
862 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800863}
864
865/*
866** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
867** year, a rule, and the offset from UTC at the time that rule takes effect,
868** calculate the Epoch-relative time that rule takes effect.
869*/
870
871static time_t
872transtime(janfirst, year, rulep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700873const time_t janfirst;
874const int year;
875register const struct rule * const rulep;
876const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800877{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700878 register int leapyear;
879 register time_t value;
880 register int i;
881 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800882
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700883 INITIALIZE(value);
884 leapyear = isleap(year);
885 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800886
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700887 case JULIAN_DAY:
888 /*
889 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
890 ** years.
891 ** In non-leap years, or if the day number is 59 or less, just
892 ** add SECSPERDAY times the day number-1 to the time of
893 ** January 1, midnight, to get the day.
894 */
895 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
896 if (leapyear && rulep->r_day >= 60)
897 value += SECSPERDAY;
898 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800899
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700900 case DAY_OF_YEAR:
901 /*
902 ** n - day of year.
903 ** Just add SECSPERDAY times the day number to the time of
904 ** January 1, midnight, to get the day.
905 */
906 value = janfirst + rulep->r_day * SECSPERDAY;
907 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800908
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700909 case MONTH_NTH_DAY_OF_WEEK:
910 /*
911 ** Mm.n.d - nth "dth day" of month m.
912 */
913 value = janfirst;
914 for (i = 0; i < rulep->r_mon - 1; ++i)
915 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800916
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700917 /*
918 ** Use Zeller's Congruence to get day-of-week of first day of
919 ** month.
920 */
921 m1 = (rulep->r_mon + 9) % 12 + 1;
922 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
923 yy1 = yy0 / 100;
924 yy2 = yy0 % 100;
925 dow = ((26 * m1 - 2) / 10 +
926 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
927 if (dow < 0)
928 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800929
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700930 /*
931 ** "dow" is the day-of-week of the first day of the month. Get
932 ** the day-of-month (zero-origin) of the first "dow" day of the
933 ** month.
934 */
935 d = rulep->r_day - dow;
936 if (d < 0)
937 d += DAYSPERWEEK;
938 for (i = 1; i < rulep->r_week; ++i) {
939 if (d + DAYSPERWEEK >=
940 mon_lengths[leapyear][rulep->r_mon - 1])
941 break;
942 d += DAYSPERWEEK;
943 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700945 /*
946 ** "d" is the day-of-month (zero-origin) of the day we want.
947 */
948 value += d * SECSPERDAY;
949 break;
950 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800951
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700952 /*
953 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
954 ** question. To get the Epoch-relative time of the specified local
955 ** time on that day, add the transition time and the current offset
956 ** from UTC.
957 */
958 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800959}
960
961/*
962** Given a POSIX section 8-style TZ string, fill in the rule tables as
963** appropriate.
964*/
965
966static int
967tzparse(name, sp, lastditch)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700968const char * name;
969register struct state * const sp;
970const int lastditch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800971{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700972 const char * stdname;
973 const char * dstname;
974 size_t stdlen;
975 size_t dstlen;
976 long stdoffset;
977 long dstoffset;
978 register time_t * atp;
979 register unsigned char * typep;
980 register char * cp;
981 register int load_result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700983 INITIALIZE(dstname);
984 stdname = name;
985 if (lastditch) {
986 stdlen = strlen(name); /* length of standard zone name */
987 name += stdlen;
988 if (stdlen >= sizeof sp->chars)
989 stdlen = (sizeof sp->chars) - 1;
990 stdoffset = 0;
991 } else {
992 if (*name == '<') {
993 name++;
994 stdname = name;
995 name = getqzname(name, '>');
996 if (*name != '>')
997 return (-1);
998 stdlen = name - stdname;
999 name++;
1000 } else {
1001 name = getzname(name);
1002 stdlen = name - stdname;
1003 }
1004 if (*name == '\0')
1005 return -1;
1006 name = getoffset(name, &stdoffset);
1007 if (name == NULL)
1008 return -1;
1009 }
1010 load_result = tzload(TZDEFRULES, sp, FALSE);
1011 if (load_result != 0)
1012 sp->leapcnt = 0; /* so, we're off a little */
1013 sp->timecnt = 0;
1014 if (*name != '\0') {
1015 if (*name == '<') {
1016 dstname = ++name;
1017 name = getqzname(name, '>');
1018 if (*name != '>')
1019 return -1;
1020 dstlen = name - dstname;
1021 name++;
1022 } else {
1023 dstname = name;
1024 name = getzname(name);
1025 dstlen = name - dstname; /* length of DST zone name */
1026 }
1027 if (*name != '\0' && *name != ',' && *name != ';') {
1028 name = getoffset(name, &dstoffset);
1029 if (name == NULL)
1030 return -1;
1031 } else dstoffset = stdoffset - SECSPERHOUR;
1032 if (*name == '\0' && load_result != 0)
1033 name = TZDEFRULESTRING;
1034 if (*name == ',' || *name == ';') {
1035 struct rule start;
1036 struct rule end;
1037 register int year;
1038 register time_t janfirst;
1039 time_t starttime;
1040 time_t endtime;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001041
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001042 ++name;
1043 if ((name = getrule(name, &start)) == NULL)
1044 return -1;
1045 if (*name++ != ',')
1046 return -1;
1047 if ((name = getrule(name, &end)) == NULL)
1048 return -1;
1049 if (*name != '\0')
1050 return -1;
1051 sp->typecnt = 2; /* standard time and DST */
1052 /*
1053 ** Two transitions per year, from EPOCH_YEAR forward.
1054 */
1055 sp->ttis[0].tt_gmtoff = -dstoffset;
1056 sp->ttis[0].tt_isdst = 1;
1057 sp->ttis[0].tt_abbrind = stdlen + 1;
1058 sp->ttis[1].tt_gmtoff = -stdoffset;
1059 sp->ttis[1].tt_isdst = 0;
1060 sp->ttis[1].tt_abbrind = 0;
1061 atp = sp->ats;
1062 typep = sp->types;
1063 janfirst = 0;
1064 for (year = EPOCH_YEAR;
1065 sp->timecnt + 2 <= TZ_MAX_TIMES;
1066 ++year) {
1067 time_t newfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001068
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001069 starttime = transtime(janfirst, year, &start,
1070 stdoffset);
1071 endtime = transtime(janfirst, year, &end,
1072 dstoffset);
1073 if (starttime > endtime) {
1074 *atp++ = endtime;
1075 *typep++ = 1; /* DST ends */
1076 *atp++ = starttime;
1077 *typep++ = 0; /* DST begins */
1078 } else {
1079 *atp++ = starttime;
1080 *typep++ = 0; /* DST begins */
1081 *atp++ = endtime;
1082 *typep++ = 1; /* DST ends */
1083 }
1084 sp->timecnt += 2;
1085 newfirst = janfirst;
1086 newfirst += year_lengths[isleap(year)] *
1087 SECSPERDAY;
1088 if (newfirst <= janfirst)
1089 break;
1090 janfirst = newfirst;
1091 }
1092 } else {
1093 register long theirstdoffset;
1094 register long theirdstoffset;
1095 register long theiroffset;
1096 register int isdst;
1097 register int i;
1098 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001099
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001100 if (*name != '\0')
1101 return -1;
1102 /*
1103 ** Initial values of theirstdoffset and theirdstoffset.
1104 */
1105 theirstdoffset = 0;
1106 for (i = 0; i < sp->timecnt; ++i) {
1107 j = sp->types[i];
1108 if (!sp->ttis[j].tt_isdst) {
1109 theirstdoffset =
1110 -sp->ttis[j].tt_gmtoff;
1111 break;
1112 }
1113 }
1114 theirdstoffset = 0;
1115 for (i = 0; i < sp->timecnt; ++i) {
1116 j = sp->types[i];
1117 if (sp->ttis[j].tt_isdst) {
1118 theirdstoffset =
1119 -sp->ttis[j].tt_gmtoff;
1120 break;
1121 }
1122 }
1123 /*
1124 ** Initially we're assumed to be in standard time.
1125 */
1126 isdst = FALSE;
1127 theiroffset = theirstdoffset;
1128 /*
1129 ** Now juggle transition times and types
1130 ** tracking offsets as you do.
1131 */
1132 for (i = 0; i < sp->timecnt; ++i) {
1133 j = sp->types[i];
1134 sp->types[i] = sp->ttis[j].tt_isdst;
1135 if (sp->ttis[j].tt_ttisgmt) {
1136 /* No adjustment to transition time */
1137 } else {
1138 /*
1139 ** If summer time is in effect, and the
1140 ** transition time was not specified as
1141 ** standard time, add the summer time
1142 ** offset to the transition time;
1143 ** otherwise, add the standard time
1144 ** offset to the transition time.
1145 */
1146 /*
1147 ** Transitions from DST to DDST
1148 ** will effectively disappear since
1149 ** POSIX provides for only one DST
1150 ** offset.
1151 */
1152 if (isdst && !sp->ttis[j].tt_ttisstd) {
1153 sp->ats[i] += dstoffset -
1154 theirdstoffset;
1155 } else {
1156 sp->ats[i] += stdoffset -
1157 theirstdoffset;
1158 }
1159 }
1160 theiroffset = -sp->ttis[j].tt_gmtoff;
1161 if (sp->ttis[j].tt_isdst)
1162 theirdstoffset = theiroffset;
1163 else theirstdoffset = theiroffset;
1164 }
1165 /*
1166 ** Finally, fill in ttis.
1167 ** ttisstd and ttisgmt need not be handled.
1168 */
1169 sp->ttis[0].tt_gmtoff = -stdoffset;
1170 sp->ttis[0].tt_isdst = FALSE;
1171 sp->ttis[0].tt_abbrind = 0;
1172 sp->ttis[1].tt_gmtoff = -dstoffset;
1173 sp->ttis[1].tt_isdst = TRUE;
1174 sp->ttis[1].tt_abbrind = stdlen + 1;
1175 sp->typecnt = 2;
1176 }
1177 } else {
1178 dstlen = 0;
1179 sp->typecnt = 1; /* only standard time */
1180 sp->timecnt = 0;
1181 sp->ttis[0].tt_gmtoff = -stdoffset;
1182 sp->ttis[0].tt_isdst = 0;
1183 sp->ttis[0].tt_abbrind = 0;
1184 }
1185 sp->charcnt = stdlen + 1;
1186 if (dstlen != 0)
1187 sp->charcnt += dstlen + 1;
1188 if ((size_t) sp->charcnt > sizeof sp->chars)
1189 return -1;
1190 cp = sp->chars;
1191 (void) strncpy(cp, stdname, stdlen);
1192 cp += stdlen;
1193 *cp++ = '\0';
1194 if (dstlen != 0) {
1195 (void) strncpy(cp, dstname, dstlen);
1196 *(cp + dstlen) = '\0';
1197 }
1198 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199}
1200
1201static void
1202gmtload(sp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001203struct state * const sp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001204{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001205 if (tzload(gmt, sp, TRUE) != 0)
1206 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001207}
1208
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001209static void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001210tzsetwall P((void))
1211{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001212 if (lcl_is_set < 0)
1213 return;
1214 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001215
1216#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001217 if (lclptr == NULL) {
1218 lclptr = (struct state *) malloc(sizeof *lclptr);
1219 if (lclptr == NULL) {
1220 settzname(); /* all we can do */
1221 return;
1222 }
1223 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001224#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001225 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1226 gmtload(lclptr);
1227 settzname();
1228}
1229
1230static void
1231tzset_locked P((void))
1232{
1233 register const char * name = NULL;
1234 static char buf[PROP_VALUE_MAX];
1235
1236 name = getenv("TZ");
1237
1238 // try the "persist.sys.timezone" system property first
1239 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0)
1240 name = buf;
1241
1242 if (name == NULL) {
1243 tzsetwall();
1244 return;
1245 }
1246
1247 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1248 return;
1249 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1250 if (lcl_is_set)
1251 (void) strcpy(lcl_TZname, name);
1252
1253#ifdef ALL_STATE
1254 if (lclptr == NULL) {
1255 lclptr = (struct state *) malloc(sizeof *lclptr);
1256 if (lclptr == NULL) {
1257 settzname(); /* all we can do */
1258 return;
1259 }
1260 }
1261#endif /* defined ALL_STATE */
1262 if (*name == '\0') {
1263 /*
1264 ** User wants it fast rather than right.
1265 */
1266 lclptr->leapcnt = 0; /* so, we're off a little */
1267 lclptr->timecnt = 0;
1268 lclptr->typecnt = 0;
1269 lclptr->ttis[0].tt_isdst = 0;
1270 lclptr->ttis[0].tt_gmtoff = 0;
1271 lclptr->ttis[0].tt_abbrind = 0;
1272 (void) strcpy(lclptr->chars, gmt);
1273 } else if (tzload(name, lclptr, TRUE) != 0)
1274 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1275 (void) gmtload(lclptr);
1276 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001277}
1278
1279void
1280tzset P((void))
1281{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001282 _tzLock();
1283 tzset_locked();
1284 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001285}
1286
1287/*
1288** The easy way to behave "as if no library function calls" localtime
1289** is to not call it--so we drop its guts into "localsub", which can be
1290** freely called. (And no, the PANS doesn't require the above behavior--
1291** but it *is* desirable.)
1292**
1293** The unused offset argument is for the benefit of mktime variants.
1294*/
1295
1296/*ARGSUSED*/
1297static struct tm *
Elliott Hughes3a936a42012-09-11 11:15:53 -07001298localsub(timep, offset, tmp, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001299const time_t * const timep;
1300const long offset;
1301struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001302const struct state * sp; // android-added: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001303{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001304 register const struct ttinfo * ttisp;
1305 register int i;
1306 register struct tm * result;
1307 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001308
Elliott Hughes3a936a42012-09-11 11:15:53 -07001309 // BEGIN android-changed: support user-supplied sp.
1310 if (sp == NULL) {
1311 sp = lclptr;
1312 }
1313 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001314#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001315 if (sp == NULL)
Elliott Hughes3a936a42012-09-11 11:15:53 -07001316 return gmtsub(timep, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001317#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001318 if ((sp->goback && t < sp->ats[0]) ||
1319 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1320 time_t newt = t;
1321 register time_t seconds;
1322 register time_t tcycles;
1323 register int_fast64_t icycles;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001324
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001325 if (t < sp->ats[0])
1326 seconds = sp->ats[0] - t;
1327 else seconds = t - sp->ats[sp->timecnt - 1];
1328 --seconds;
1329 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1330 ++tcycles;
1331 icycles = tcycles;
1332 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1333 return NULL;
1334 seconds = icycles;
1335 seconds *= YEARSPERREPEAT;
1336 seconds *= AVGSECSPERYEAR;
1337 if (t < sp->ats[0])
1338 newt += seconds;
1339 else newt -= seconds;
1340 if (newt < sp->ats[0] ||
1341 newt > sp->ats[sp->timecnt - 1])
1342 return NULL; /* "cannot happen" */
Elliott Hughes3a936a42012-09-11 11:15:53 -07001343 result = localsub(&newt, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001344 if (result == tmp) {
1345 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001347 newy = tmp->tm_year;
1348 if (t < sp->ats[0])
1349 newy -= icycles * YEARSPERREPEAT;
1350 else newy += icycles * YEARSPERREPEAT;
1351 tmp->tm_year = newy;
1352 if (tmp->tm_year != newy)
1353 return NULL;
1354 }
1355 return result;
1356 }
1357 if (sp->timecnt == 0 || t < sp->ats[0]) {
1358 i = 0;
1359 while (sp->ttis[i].tt_isdst)
1360 if (++i >= sp->typecnt) {
1361 i = 0;
1362 break;
1363 }
1364 } else {
1365 register int lo = 1;
1366 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001367
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001368 while (lo < hi) {
1369 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001370
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001371 if (t < sp->ats[mid])
1372 hi = mid;
1373 else lo = mid + 1;
1374 }
1375 i = (int) sp->types[lo - 1];
1376 }
1377 ttisp = &sp->ttis[i];
1378 /*
1379 ** To get (wrong) behavior that's compatible with System V Release 2.0
1380 ** you'd replace the statement below with
1381 ** t += ttisp->tt_gmtoff;
1382 ** timesub(&t, 0L, sp, tmp);
1383 */
1384 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1385 tmp->tm_isdst = ttisp->tt_isdst;
1386 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001387#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001388 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001390 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391}
1392
1393struct tm *
1394localtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001395const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001397 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001398}
1399
1400/*
1401** Re-entrant version of localtime.
1402*/
1403
1404struct tm *
1405localtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001406const time_t * const timep;
1407struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001408{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001409 struct tm* result;
1410
1411 _tzLock();
1412 tzset_locked();
Elliott Hughes3a936a42012-09-11 11:15:53 -07001413 result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001414 _tzUnlock();
1415
1416 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001417}
1418
1419/*
1420** gmtsub is to gmtime as localsub is to localtime.
1421*/
1422
1423static struct tm *
Elliott Hughes3a936a42012-09-11 11:15:53 -07001424gmtsub(timep, offset, tmp, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001425const time_t * const timep;
1426const long offset;
1427struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001428const struct state * sp; // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001429{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001430 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431
Elliott Hughes3a936a42012-09-11 11:15:53 -07001432 (void) sp; // android-added: unused.
1433
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001434 if (!gmt_is_set) {
1435 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001436#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001437 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1438 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001439#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001440 gmtload(gmtptr);
1441 }
1442 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001443#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001444 /*
1445 ** Could get fancy here and deliver something such as
1446 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1447 ** but this is no time for a treasure hunt.
1448 */
1449 if (offset != 0)
1450 tmp->TM_ZONE = wildabbr;
1451 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001452#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001453 if (gmtptr == NULL)
1454 tmp->TM_ZONE = gmt;
1455 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001456#endif /* defined ALL_STATE */
1457#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001458 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001459#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001460 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001461#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001462 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001463}
1464
1465struct tm *
1466gmtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001467const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001468{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001469 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001470}
1471
1472/*
1473* Re-entrant version of gmtime.
1474*/
1475
1476struct tm *
1477gmtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001478const time_t * const timep;
1479struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001480{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001481 struct tm* result;
1482
1483 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07001484 result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001485 _tzUnlock();
1486
1487 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488}
1489
1490#ifdef STD_INSPIRED
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001491#if 0 /* disabled because there is no good documentation for this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492struct tm *
1493offtime(timep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001494const time_t * const timep;
1495const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496{
Elliott Hughes3a936a42012-09-11 11:15:53 -07001497 return gmtsub(timep, offset, &tmGlobal, NULL); // android-changed: extra parameter.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001498}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001499#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001500#endif /* defined STD_INSPIRED */
1501
1502/*
1503** Return the number of leap years through the end of the given year
1504** where, to make the math easy, the answer for year zero is defined as zero.
1505*/
1506
1507static int
1508leaps_thru_end_of(y)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001509register const int y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001510{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001511 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1512 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001513}
1514
1515static struct tm *
1516timesub(timep, offset, sp, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001517const time_t * const timep;
1518const long offset;
1519register const struct state * const sp;
1520register struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001521{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001522 register const struct lsinfo * lp;
1523 register time_t tdays;
1524 register int idays; /* unsigned would be so 2003 */
1525 register long rem;
1526 int y;
1527 register const int * ip;
1528 register long corr;
1529 register int hit;
1530 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001532 corr = 0;
1533 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001535 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536#endif /* defined ALL_STATE */
1537#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001538 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001540 while (--i >= 0) {
1541 lp = &sp->lsis[i];
1542 if (*timep >= lp->ls_trans) {
1543 if (*timep == lp->ls_trans) {
1544 hit = ((i == 0 && lp->ls_corr > 0) ||
1545 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1546 if (hit)
1547 while (i > 0 &&
1548 sp->lsis[i].ls_trans ==
1549 sp->lsis[i - 1].ls_trans + 1 &&
1550 sp->lsis[i].ls_corr ==
1551 sp->lsis[i - 1].ls_corr + 1) {
1552 ++hit;
1553 --i;
1554 }
1555 }
1556 corr = lp->ls_corr;
1557 break;
1558 }
1559 }
1560 y = EPOCH_YEAR;
1561 tdays = *timep / SECSPERDAY;
1562 rem = *timep - tdays * SECSPERDAY;
1563 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1564 int newy;
1565 register time_t tdelta;
1566 register int idelta;
1567 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001569 tdelta = tdays / DAYSPERLYEAR;
1570 idelta = tdelta;
1571 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1572 return NULL;
1573 if (idelta == 0)
1574 idelta = (tdays < 0) ? -1 : 1;
1575 newy = y;
1576 if (increment_overflow(&newy, idelta))
1577 return NULL;
1578 leapdays = leaps_thru_end_of(newy - 1) -
1579 leaps_thru_end_of(y - 1);
1580 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1581 tdays -= leapdays;
1582 y = newy;
1583 }
1584 {
1585 register long seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001586
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001587 seconds = tdays * SECSPERDAY + 0.5;
1588 tdays = seconds / SECSPERDAY;
1589 rem += seconds - tdays * SECSPERDAY;
1590 }
1591 /*
1592 ** Given the range, we can now fearlessly cast...
1593 */
1594 idays = tdays;
1595 rem += offset - corr;
1596 while (rem < 0) {
1597 rem += SECSPERDAY;
1598 --idays;
1599 }
1600 while (rem >= SECSPERDAY) {
1601 rem -= SECSPERDAY;
1602 ++idays;
1603 }
1604 while (idays < 0) {
1605 if (increment_overflow(&y, -1))
1606 return NULL;
1607 idays += year_lengths[isleap(y)];
1608 }
1609 while (idays >= year_lengths[isleap(y)]) {
1610 idays -= year_lengths[isleap(y)];
1611 if (increment_overflow(&y, 1))
1612 return NULL;
1613 }
1614 tmp->tm_year = y;
1615 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1616 return NULL;
1617 tmp->tm_yday = idays;
1618 /*
1619 ** The "extra" mods below avoid overflow problems.
1620 */
1621 tmp->tm_wday = EPOCH_WDAY +
1622 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1623 (DAYSPERNYEAR % DAYSPERWEEK) +
1624 leaps_thru_end_of(y - 1) -
1625 leaps_thru_end_of(EPOCH_YEAR - 1) +
1626 idays;
1627 tmp->tm_wday %= DAYSPERWEEK;
1628 if (tmp->tm_wday < 0)
1629 tmp->tm_wday += DAYSPERWEEK;
1630 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1631 rem %= SECSPERHOUR;
1632 tmp->tm_min = (int) (rem / SECSPERMIN);
1633 /*
1634 ** A positive leap second requires a special
1635 ** representation. This uses "... ??:59:60" et seq.
1636 */
1637 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1638 ip = mon_lengths[isleap(y)];
1639 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1640 idays -= ip[tmp->tm_mon];
1641 tmp->tm_mday = (int) (idays + 1);
1642 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001643#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001644 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001645#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001646 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001647}
1648
1649char *
1650ctime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001651const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652{
1653/*
1654** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001655** The ctime function converts the calendar time pointed to by timer
1656** to local time in the form of a string. It is equivalent to
1657** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001658*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001659 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660}
1661
1662char *
1663ctime_r(timep, buf)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001664const time_t * const timep;
1665char * buf;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001666{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001667 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001668
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001669 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001670}
1671
1672/*
1673** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001674** The "best" way to do mktime I think is based on an idea of Bob
1675** Kridle's (so its said...) from a long time ago.
1676** It does a binary search of the time_t space. Since time_t's are
1677** just 32 bits, its a max of 32 iterations (even at 64 bits it
1678** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679*/
1680
1681#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001682#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683#endif /* !defined WRONG */
1684
1685/*
1686** Simplified normalize logic courtesy Paul Eggert.
1687*/
1688
1689static int
1690increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001691int * number;
1692int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001693{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001694 unsigned number0 = (unsigned)*number;
1695 unsigned number1 = (unsigned)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001697 *number = (int)number1;
1698
1699 if (delta >= 0) {
1700 return ((int)number1 < (int)number0);
1701 } else {
1702 return ((int)number1 > (int)number0);
1703 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704}
1705
1706static int
1707long_increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001708long * number;
1709int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001710{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001711 unsigned long number0 = (unsigned long)*number;
1712 unsigned long number1 = (unsigned long)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001714 *number = (long)number1;
1715
1716 if (delta >= 0) {
1717 return ((long)number1 < (long)number0);
1718 } else {
1719 return ((long)number1 > (long)number0);
1720 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001721}
1722
1723static int
1724normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001725int * const tensptr;
1726int * const unitsptr;
1727const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001728{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001729 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001730
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001731 tensdelta = (*unitsptr >= 0) ?
1732 (*unitsptr / base) :
1733 (-1 - (-1 - *unitsptr) / base);
1734 *unitsptr -= tensdelta * base;
1735 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001736}
1737
1738static int
1739long_normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001740long * const tensptr;
1741int * const unitsptr;
1742const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001743{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001744 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001745
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001746 tensdelta = (*unitsptr >= 0) ?
1747 (*unitsptr / base) :
1748 (-1 - (-1 - *unitsptr) / base);
1749 *unitsptr -= tensdelta * base;
1750 return long_increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001751}
1752
1753static int
1754tmcomp(atmp, btmp)
1755register const struct tm * const atmp;
1756register const struct tm * const btmp;
1757{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001758 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001759
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001760 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1761 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1762 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1763 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1764 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1765 result = atmp->tm_sec - btmp->tm_sec;
1766 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001767}
1768
1769static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07001770time2sub(tmp, funcp, offset, okayp, do_norm_secs, sp) // android-changed: added sp
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001771struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001772struct tm * (* const funcp) P((const time_t*, long, struct tm*, const struct state*)); // android-changed: added state*
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001773const long offset;
1774int * const okayp;
1775const int do_norm_secs;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001776const struct state * sp; // android-changed: added sp
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001777{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001778 register int dir;
1779 register int i, j;
1780 register int saved_seconds;
1781 register long li;
1782 register time_t lo;
1783 register time_t hi;
1784 long y;
1785 time_t newt;
1786 time_t t;
1787 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001788
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001789 *okayp = FALSE;
1790 yourtm = *tmp;
1791 if (do_norm_secs) {
1792 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1793 SECSPERMIN))
1794 return WRONG;
1795 }
1796 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1797 return WRONG;
1798 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1799 return WRONG;
1800 y = yourtm.tm_year;
1801 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1802 return WRONG;
1803 /*
1804 ** Turn y into an actual year number for now.
1805 ** It is converted back to an offset from TM_YEAR_BASE later.
1806 */
1807 if (long_increment_overflow(&y, TM_YEAR_BASE))
1808 return WRONG;
1809 while (yourtm.tm_mday <= 0) {
1810 if (long_increment_overflow(&y, -1))
1811 return WRONG;
1812 li = y + (1 < yourtm.tm_mon);
1813 yourtm.tm_mday += year_lengths[isleap(li)];
1814 }
1815 while (yourtm.tm_mday > DAYSPERLYEAR) {
1816 li = y + (1 < yourtm.tm_mon);
1817 yourtm.tm_mday -= year_lengths[isleap(li)];
1818 if (long_increment_overflow(&y, 1))
1819 return WRONG;
1820 }
1821 for ( ; ; ) {
1822 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1823 if (yourtm.tm_mday <= i)
1824 break;
1825 yourtm.tm_mday -= i;
1826 if (++yourtm.tm_mon >= MONSPERYEAR) {
1827 yourtm.tm_mon = 0;
1828 if (long_increment_overflow(&y, 1))
1829 return WRONG;
1830 }
1831 }
1832 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1833 return WRONG;
1834 yourtm.tm_year = y;
1835 if (yourtm.tm_year != y)
1836 return WRONG;
1837 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1838 saved_seconds = 0;
1839 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1840 /*
1841 ** We can't set tm_sec to 0, because that might push the
1842 ** time below the minimum representable time.
1843 ** Set tm_sec to 59 instead.
1844 ** This assumes that the minimum representable time is
1845 ** not in the same minute that a leap second was deleted from,
1846 ** which is a safer assumption than using 58 would be.
1847 */
1848 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1849 return WRONG;
1850 saved_seconds = yourtm.tm_sec;
1851 yourtm.tm_sec = SECSPERMIN - 1;
1852 } else {
1853 saved_seconds = yourtm.tm_sec;
1854 yourtm.tm_sec = 0;
1855 }
1856 /*
1857 ** Do a binary search (this works whatever time_t's type is).
1858 */
1859 if (!TYPE_SIGNED(time_t)) {
1860 lo = 0;
1861 hi = lo - 1;
1862 } else if (!TYPE_INTEGRAL(time_t)) {
1863 if (sizeof(time_t) > sizeof(float))
1864 hi = (time_t) DBL_MAX;
1865 else hi = (time_t) FLT_MAX;
1866 lo = -hi;
1867 } else {
1868 lo = 1;
1869 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1870 lo *= 2;
1871 hi = -(lo + 1);
1872 }
1873 for ( ; ; ) {
1874 t = lo / 2 + hi / 2;
1875 if (t < lo)
1876 t = lo;
1877 else if (t > hi)
1878 t = hi;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001879 if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001880 /*
1881 ** Assume that t is too extreme to be represented in
1882 ** a struct tm; arrange things so that it is less
1883 ** extreme on the next pass.
1884 */
1885 dir = (t > 0) ? 1 : -1;
1886 } else dir = tmcomp(&mytm, &yourtm);
1887 if (dir != 0) {
1888 if (t == lo) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001889 if (t == TIME_T_MAX)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001890 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001891 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001892 ++lo;
1893 } else if (t == hi) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001894 if (t == TIME_T_MIN)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001895 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001896 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001897 --hi;
1898 }
1899 if (lo > hi)
1900 return WRONG;
1901 if (dir > 0)
1902 hi = t;
1903 else lo = t;
1904 continue;
1905 }
1906 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1907 break;
1908 /*
1909 ** Right time, wrong type.
1910 ** Hunt for right time, right type.
1911 ** It's okay to guess wrong since the guess
1912 ** gets checked.
1913 */
1914 /*
1915 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1916 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07001917 // BEGIN android-changed: support user-supplied sp
1918 if (sp == NULL) {
1919 sp = (const struct state *)
1920 (((void *) funcp == (void *) localsub) ?
1921 lclptr : gmtptr);
1922 }
1923 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001924#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001925 if (sp == NULL)
1926 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001927#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001928 for (i = sp->typecnt - 1; i >= 0; --i) {
1929 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1930 continue;
1931 for (j = sp->typecnt - 1; j >= 0; --j) {
1932 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1933 continue;
1934 newt = t + sp->ttis[j].tt_gmtoff -
1935 sp->ttis[i].tt_gmtoff;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001936 if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001937 continue;
1938 if (tmcomp(&mytm, &yourtm) != 0)
1939 continue;
1940 if (mytm.tm_isdst != yourtm.tm_isdst)
1941 continue;
1942 /*
1943 ** We have a match.
1944 */
1945 t = newt;
1946 goto label;
1947 }
1948 }
1949 return WRONG;
1950 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001951label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001952 newt = t + saved_seconds;
1953 if ((newt < t) != (saved_seconds < 0))
1954 return WRONG;
1955 t = newt;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001956 if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001957 *okayp = TRUE;
1958 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001959}
1960
Elliott Hughes3a936a42012-09-11 11:15:53 -07001961// BEGIN android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001962static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07001963time2(tmp, funcp, offset, okayp, sp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001964struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001965struct tm * (* const funcp) P((const time_t*, long, struct tm*, const struct state*));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001966const long offset;
1967int * const okayp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001968const struct state * sp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001969{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001970 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001971
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001972 /*
1973 ** First try without normalization of seconds
1974 ** (in case tm_sec contains a value associated with a leap second).
1975 ** If that fails, try with normalization of seconds.
1976 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07001977 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
1978 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001979}
Elliott Hughes3a936a42012-09-11 11:15:53 -07001980// END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001981
1982static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07001983time1(tmp, funcp, offset, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001984struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001985struct tm * (* const funcp) P((const time_t *, long, struct tm *, const struct state *));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001986const long offset;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001987const struct state * sp; // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001988{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001989 register time_t t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001990 register int samei, otheri;
1991 register int sameind, otherind;
1992 register int i;
1993 register int nseen;
1994 int seen[TZ_MAX_TYPES];
1995 int types[TZ_MAX_TYPES];
1996 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001997
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001998 if (tmp->tm_isdst > 1)
1999 tmp->tm_isdst = 1;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002000 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002001#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002002 /*
2003 ** PCTS code courtesy Grant Sullivan.
2004 */
2005 if (okay)
2006 return t;
2007 if (tmp->tm_isdst < 0)
2008 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002009#endif /* defined PCTS */
2010#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002011 if (okay || tmp->tm_isdst < 0)
2012 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002013#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002014 /*
2015 ** We're supposed to assume that somebody took a time of one type
2016 ** and did some math on it that yielded a "struct tm" that's bad.
2017 ** We try to divine the type they started from and adjust to the
2018 ** type they need.
2019 */
2020 /*
2021 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
2022 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07002023 // BEGIN android-changed: support user-supplied sp.
2024 if (sp == NULL) {
2025 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
2026 lclptr : gmtptr);
2027 }
2028 // BEGIN android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002029#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002030 if (sp == NULL)
2031 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002032#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002033 for (i = 0; i < sp->typecnt; ++i)
2034 seen[i] = FALSE;
2035 nseen = 0;
2036 for (i = sp->timecnt - 1; i >= 0; --i)
2037 if (!seen[sp->types[i]]) {
2038 seen[sp->types[i]] = TRUE;
2039 types[nseen++] = sp->types[i];
2040 }
2041 for (sameind = 0; sameind < nseen; ++sameind) {
2042 samei = types[sameind];
2043 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2044 continue;
2045 for (otherind = 0; otherind < nseen; ++otherind) {
2046 otheri = types[otherind];
2047 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2048 continue;
2049 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2050 sp->ttis[samei].tt_gmtoff;
2051 tmp->tm_isdst = !tmp->tm_isdst;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002052 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002053 if (okay)
2054 return t;
2055 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2056 sp->ttis[samei].tt_gmtoff;
2057 tmp->tm_isdst = !tmp->tm_isdst;
2058 }
2059 }
2060 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002061}
2062
2063time_t
2064mktime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002065struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002066{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002067 time_t result;
2068 _tzLock();
2069 tzset_locked();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002070 result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002071 _tzUnlock();
2072 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002073}
2074
Elliott Hughes3a936a42012-09-11 11:15:53 -07002075// BEGIN android-added
2076time_t
2077mktime_tz(tmp, tz)
2078struct tm * const tmp;
2079char const * tz;
2080{
2081 struct state st;
2082 if (tzload(tz, &st, TRUE) != 0) {
2083 // TODO: not sure what's best here, but for now, we fall back to gmt.
2084 gmtload(&st);
2085 }
2086 return time1(tmp, localsub, 0L, &st);
2087}
2088
2089void
2090localtime_tz(timep, tmp, tz)
2091const time_t * const timep;
2092struct tm * tmp;
2093const char* tz;
2094{
2095 struct state st;
2096 if (tzload(tz, &st, TRUE) != 0) {
2097 // TODO: not sure what's best here, but for now, we fall back to gmt.
2098 gmtload(&st);
2099 }
2100 localsub(timep, 0L, tmp, &st);
2101}
2102// END android-added
2103
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002104#ifdef STD_INSPIRED
2105
2106time_t
2107timelocal(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002108struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002109{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002110 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2111 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002112}
2113
2114time_t
2115timegm(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002116struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002117{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002118 time_t result;
2119
2120 tmp->tm_isdst = 0;
2121 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002122 result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002123 _tzUnlock();
2124
2125 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002126}
2127
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002128#if 0 /* disable due to lack of clear documentation on this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002129time_t
2130timeoff(tmp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002131struct tm * const tmp;
2132const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002133{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002134 time_t result;
2135
2136 tmp->tm_isdst = 0;
2137 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002138 result = time1(tmp, gmtsub, offset, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002139 _tzUnlock();
2140
2141 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002142}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002143#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002144
2145#endif /* defined STD_INSPIRED */
2146
2147#ifdef CMUCS
2148
2149/*
2150** The following is supplied for compatibility with
2151** previous versions of the CMUCS runtime library.
2152*/
2153
2154long
2155gtime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002156struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002157{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002158 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002159
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002160 if (t == WRONG)
2161 return -1;
2162 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002163}
2164
2165#endif /* defined CMUCS */
2166
2167/*
2168** XXX--is the below the right way to conditionalize??
2169*/
2170
2171#ifdef STD_INSPIRED
2172
2173/*
2174** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2175** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2176** is not the case if we are accounting for leap seconds.
2177** So, we provide the following conversion routines for use
2178** when exchanging timestamps with POSIX conforming systems.
2179*/
2180
2181static long
2182leapcorr(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002183time_t * timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002184{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002185 register struct state * sp;
2186 register struct lsinfo * lp;
2187 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002188
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002189 sp = lclptr;
2190 i = sp->leapcnt;
2191 while (--i >= 0) {
2192 lp = &sp->lsis[i];
2193 if (*timep >= lp->ls_trans)
2194 return lp->ls_corr;
2195 }
2196 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002197}
2198
2199time_t
2200time2posix(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002201time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002202{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002203 tzset();
2204 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002205}
2206
2207time_t
2208posix2time(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002209time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002210{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002211 time_t x;
2212 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002213
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002214 tzset();
2215 /*
2216 ** For a positive leap second hit, the result
2217 ** is not unique. For a negative leap second
2218 ** hit, the corresponding time doesn't exist,
2219 ** so we return an adjacent second.
2220 */
2221 x = t + leapcorr(&t);
2222 y = x - leapcorr(&x);
2223 if (y < t) {
2224 do {
2225 x++;
2226 y = x - leapcorr(&x);
2227 } while (y < t);
2228 if (t != y)
2229 return x - 1;
2230 } else if (y > t) {
2231 do {
2232 --x;
2233 y = x - leapcorr(&x);
2234 } while (y > t);
2235 if (t != y)
2236 return x + 1;
2237 }
2238 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002239}
2240
2241#endif /* defined STD_INSPIRED */
Elliott Hughesd23af232012-10-17 16:30:47 -07002242
2243#include <stdint.h>
2244#include <sys/endian.h>
2245
2246static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
2247 int fd = TEMP_FAILURE_RETRY(open(TZDATA_PATH, OPEN_MODE));
2248 if (fd == -1) {
2249 fprintf(stderr, "__bionic_open_tzdata: could not open \"%s\": %s\n", TZDATA_PATH, strerror(errno));
2250 return -1;
2251 }
2252
2253 // byte[12] tzdata_version -- "tzdata2012f\0"
2254 // int file_format_version -- 1
2255 // int index_offset
2256 // int data_offset
2257 // int zonetab_offset
2258 struct bionic_tzdata_header {
2259 char tzdata_version[12];
2260 int32_t file_format_version;
2261 int32_t index_offset;
2262 int32_t data_offset;
2263 int32_t zonetab_offset;
2264 } header;
2265 if (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) != sizeof(header)) {
2266 fprintf(stderr, "__bionic_open_tzdata: could not read header: %s\n", strerror(errno));
2267 close(fd);
2268 return -1;
2269 }
2270
2271 if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
2272 fprintf(stderr, "__bionic_open_tzdata: bad magic: %s\n", header.tzdata_version);
2273 close(fd);
2274 return -1;
2275 }
2276 if (ntohl(header.file_format_version) != 1) {
2277 fprintf(stderr, "__bionic_open_tzdata: bad file format version: %d\n", header.file_format_version);
2278 close(fd);
2279 return -1;
2280 }
2281
2282#if 0
2283 fprintf(stderr, "version: %s (%d)\n", header.tzdata_version, ntohl(header.file_format_version));
2284 fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset));
2285 fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset));
2286 fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset));
2287#endif
2288
2289 if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
2290 fprintf(stderr, "__bionic_open_tzdata: couldn't seek to index: %s\n", strerror(errno));
2291 close(fd);
2292 return -1;
2293 }
2294
2295 off_t specific_zone_offset = -1;
2296
2297 unsigned char buf[READLEN];
2298 while (read(fd, buf, sizeof(buf)) == sizeof(buf)) {
2299 char this_id[NAMELEN + 1];
2300 memcpy(this_id, buf, NAMELEN);
2301 this_id[NAMELEN] = '\0';
2302
2303 if (strcmp(this_id, olson_id) == 0) {
2304 specific_zone_offset = toint(buf + NAMELEN) + ntohl(header.data_offset);
2305 *data_size = toint(buf + NAMELEN + INTLEN);
2306 break;
2307 }
2308 }
2309
2310 if (specific_zone_offset == -1) {
2311 XLOG(("__bionic_open_tzdata: couldn't find zone \"%s\"\n", olson_id));
2312 close(fd);
2313 return -1;
2314 }
2315
2316 if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
2317 fprintf(stderr, "__bionic_open_tzdata: could not seek to %ld: %s\n", specific_zone_offset, strerror(errno));
2318 close(fd);
2319 return -1;
2320 }
2321
2322 return fd;
2323}