blob: 68381809d99ae563e7282fe34de9d922845d54c7 [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
40#define INDEXFILE "/system/usr/share/zoneinfo/zoneinfo.idx"
41#define DATAFILE "/system/usr/share/zoneinfo/zoneinfo.dat"
42#define NAMELEN 40
43#define INTLEN 4
44#define READLEN (NAMELEN + 3 * INTLEN)
45
46/*
47** SunOS 4.1.1 headers lack O_BINARY.
48*/
49
50#ifdef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070051#define OPEN_MODE (O_RDONLY | O_BINARY)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052#endif /* defined O_BINARY */
53#ifndef O_BINARY
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070054#define OPEN_MODE O_RDONLY
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#endif /* !defined O_BINARY */
56
57#if 0
58# define XLOG(xx) printf xx , fflush(stdout)
59#else
60# define XLOG(x) do{}while (0)
61#endif
62
David 'Digit' Turner6481b912010-12-06 12:23:16 +010063/* Add the following function implementations:
64 * timelocal()
65 * timegm()
66 * time2posix()
67 * posix2time()
68 */
69#define STD_INSPIRED 1
70
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070071/* THREAD-SAFETY SUPPORT GOES HERE */
72static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER;
73
74static __inline__ void _tzLock(void)
75{
76 if (__isthreaded)
77 pthread_mutex_lock(&_tzMutex);
78}
79
80static __inline__ void _tzUnlock(void)
81{
82 if (__isthreaded)
83 pthread_mutex_unlock(&_tzMutex);
84}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
David 'Digit' Turner2093d352009-09-09 17:41:59 -070086/* Complex computations to determine the min/max of time_t depending
87 * on TYPE_BIT / TYPE_SIGNED / TYPE_INTEGRAL.
88 * These macros cannot be used in pre-processor directives, so we
89 * let the C compiler do the work, which makes things a bit funky.
90 */
91static const time_t TIME_T_MAX =
92 TYPE_INTEGRAL(time_t) ?
93 ( TYPE_SIGNED(time_t) ?
94 ~((time_t)1 << (TYPE_BIT(time_t)-1))
95 :
96 ~(time_t)0
97 )
98 : /* if time_t is a floating point number */
99 ( sizeof(time_t) > sizeof(float) ? (time_t)DBL_MAX : (time_t)FLT_MAX );
100
101static const time_t TIME_T_MIN =
102 TYPE_INTEGRAL(time_t) ?
103 ( TYPE_SIGNED(time_t) ?
104 ((time_t)1 << (TYPE_BIT(time_t)-1))
105 :
106 0
107 )
108 :
109 ( sizeof(time_t) > sizeof(float) ? (time_t)DBL_MIN : (time_t)FLT_MIN );
110
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111#ifndef WILDABBR
112/*
113** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700114** 1. They might reference tzname[0] before calling tzset (explicitly
115** or implicitly).
116** 2. They might reference tzname[1] before calling tzset (explicitly
117** or implicitly).
118** 3. They might reference tzname[1] after setting to a time zone
119** in which Daylight Saving Time is never observed.
120** 4. They might reference tzname[0] after setting to a time zone
121** in which Standard Time is never observed.
122** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123** What's best to do in the above cases is open to debate;
124** for now, we just set things up so that in any of the five cases
125** WILDABBR is used. Another possibility: initialize tzname[0] to the
126** string "tzname[0] used before set", and similarly for the other cases.
127** And another: initialize tzname[0] to "ERA", with an explanation in the
128** manual page of what this "time zone abbreviation" means (doing this so
129** that tzname[0] has the "normal" length of three characters).
130*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700131#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132#endif /* !defined WILDABBR */
133
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700134static char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700136static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137
138/*
139** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
140** We default to US rules as of 1999-08-17.
141** POSIX 1003.1 section 8.1.1 says that the default DST rules are
142** implementation dependent; for historical reasons, US rules are a
143** common default.
144*/
145#ifndef TZDEFRULESTRING
146#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
147#endif /* !defined TZDEFDST */
148
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700149struct ttinfo { /* time type information */
150 long tt_gmtoff; /* UTC offset in seconds */
151 int tt_isdst; /* used to set tm_isdst */
152 int tt_abbrind; /* abbreviation list index */
153 int tt_ttisstd; /* TRUE if transition is std time */
154 int tt_ttisgmt; /* TRUE if transition is UTC */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155};
156
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700157struct lsinfo { /* leap second information */
158 time_t ls_trans; /* transition time */
159 long ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160};
161
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700162#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
164#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700165#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166#endif /* defined TZNAME_MAX */
167#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700168#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169#endif /* !defined TZNAME_MAX */
170
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700171/* XXX: This code should really use time64_t instead of time_t
172 * but we can't change it without re-generating the index
173 * file first with the correct data.
174 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175struct state {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700176 int leapcnt;
177 int timecnt;
178 int typecnt;
179 int charcnt;
180 int goback;
181 int goahead;
182 time_t ats[TZ_MAX_TIMES];
183 unsigned char types[TZ_MAX_TIMES];
184 struct ttinfo ttis[TZ_MAX_TYPES];
185 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
186 (2 * (MY_TZNAME_MAX + 1)))];
187 struct lsinfo lsis[TZ_MAX_LEAPS];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188};
189
190struct rule {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700191 int r_type; /* type of rule--see below */
192 int r_day; /* day number of rule */
193 int r_week; /* week number of rule */
194 int r_mon; /* month number of rule */
195 long r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196};
197
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700198#define JULIAN_DAY 0 /* Jn - Julian day */
199#define DAY_OF_YEAR 1 /* n - day of year */
200#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 -0800201
202/*
203** Prototypes for static functions.
204*/
205
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700206/* NOTE: all internal functions assume that _tzLock() was already called */
207
208static 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) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473 char buf[READLEN];
474 char name[NAMELEN + 1];
475 int fidix = open(INDEXFILE, OPEN_MODE);
476 int off = -1;
477
478 XLOG(( "tzload: could not open '%s', trying '%s'\n", fullname, INDEXFILE ));
479 if (fidix < 0) {
480 XLOG(( "tzload: could not find '%s'\n", INDEXFILE ));
481 return -1;
482 }
483
484 while (read(fidix, buf, sizeof(buf)) == sizeof(buf)) {
485 memcpy(name, buf, NAMELEN);
486 name[NAMELEN] = '\0';
487
488 if (strcmp(name, origname) == 0) {
489 off = toint((unsigned char *) buf + NAMELEN);
490 toread = toint((unsigned char *) buf + NAMELEN + INTLEN);
491 break;
492 }
493 }
494
495 close(fidix);
496
497 if (off < 0) {
498 XLOG(( "tzload: invalid offset (%d)\n", off ));
499 return -1;
500 }
501
502 fid = open(DATAFILE, OPEN_MODE);
503
504 if (fid < 0) {
505 XLOG(( "tzload: could not open '%s'\n", DATAFILE ));
506 return -1;
507 }
508
509 if (lseek(fid, off, SEEK_SET) < 0) {
510 XLOG(( "tzload: could not seek to %d in '%s'\n", off, DATAFILE ));
511 return -1;
512 }
513 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700514 }
515 nread = read(fid, u.buf, toread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800516 if (close(fid) < 0 || nread <= 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700517 XLOG(( "tzload: could not read content of '%s'\n", DATAFILE ));
518 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800519 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700520 for (stored = 4; stored <= 8; stored *= 2) {
521 int ttisstdcnt;
522 int ttisgmtcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700524 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
525 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
526 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
527 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
528 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
529 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
530 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
531 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
532 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
533 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
534 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
535 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
536 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
537 return -1;
538 if (nread - (p - u.buf) <
539 sp->timecnt * stored + /* ats */
540 sp->timecnt + /* types */
541 sp->typecnt * 6 + /* ttinfos */
542 sp->charcnt + /* chars */
543 sp->leapcnt * (stored + 4) + /* lsinfos */
544 ttisstdcnt + /* ttisstds */
545 ttisgmtcnt) /* ttisgmts */
546 return -1;
547 for (i = 0; i < sp->timecnt; ++i) {
548 sp->ats[i] = (stored == 4) ?
549 detzcode(p) : detzcode64(p);
550 p += stored;
551 }
552 for (i = 0; i < sp->timecnt; ++i) {
553 sp->types[i] = (unsigned char) *p++;
554 if (sp->types[i] >= sp->typecnt)
555 return -1;
556 }
557 for (i = 0; i < sp->typecnt; ++i) {
558 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800559
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700560 ttisp = &sp->ttis[i];
561 ttisp->tt_gmtoff = detzcode(p);
562 p += 4;
563 ttisp->tt_isdst = (unsigned char) *p++;
564 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
565 return -1;
566 ttisp->tt_abbrind = (unsigned char) *p++;
567 if (ttisp->tt_abbrind < 0 ||
568 ttisp->tt_abbrind > sp->charcnt)
569 return -1;
570 }
571 for (i = 0; i < sp->charcnt; ++i)
572 sp->chars[i] = *p++;
573 sp->chars[i] = '\0'; /* ensure '\0' at end */
574 for (i = 0; i < sp->leapcnt; ++i) {
575 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800576
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700577 lsisp = &sp->lsis[i];
578 lsisp->ls_trans = (stored == 4) ?
579 detzcode(p) : detzcode64(p);
580 p += stored;
581 lsisp->ls_corr = detzcode(p);
582 p += 4;
583 }
584 for (i = 0; i < sp->typecnt; ++i) {
585 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800586
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700587 ttisp = &sp->ttis[i];
588 if (ttisstdcnt == 0)
589 ttisp->tt_ttisstd = FALSE;
590 else {
591 ttisp->tt_ttisstd = *p++;
592 if (ttisp->tt_ttisstd != TRUE &&
593 ttisp->tt_ttisstd != FALSE)
594 return -1;
595 }
596 }
597 for (i = 0; i < sp->typecnt; ++i) {
598 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800599
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700600 ttisp = &sp->ttis[i];
601 if (ttisgmtcnt == 0)
602 ttisp->tt_ttisgmt = FALSE;
603 else {
604 ttisp->tt_ttisgmt = *p++;
605 if (ttisp->tt_ttisgmt != TRUE &&
606 ttisp->tt_ttisgmt != FALSE)
607 return -1;
608 }
609 }
610 /*
611 ** Out-of-sort ats should mean we're running on a
612 ** signed time_t system but using a data file with
613 ** unsigned values (or vice versa).
614 */
615 for (i = 0; i < sp->timecnt - 2; ++i)
616 if (sp->ats[i] > sp->ats[i + 1]) {
617 ++i;
618 if (TYPE_SIGNED(time_t)) {
619 /*
620 ** Ignore the end (easy).
621 */
622 sp->timecnt = i;
623 } else {
624 /*
625 ** Ignore the beginning (harder).
626 */
627 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700629 for (j = 0; j + i < sp->timecnt; ++j) {
630 sp->ats[j] = sp->ats[j + i];
631 sp->types[j] = sp->types[j + i];
632 }
633 sp->timecnt = j;
634 }
635 break;
636 }
637 /*
638 ** If this is an old file, we're done.
639 */
640 if (u.tzhead.tzh_version[0] == '\0')
641 break;
642 nread -= p - u.buf;
643 for (i = 0; i < nread; ++i)
644 u.buf[i] = p[i];
645 /*
646 ** If this is a narrow integer time_t system, we're done.
647 */
648 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
649 break;
650 }
651 if (doextend && nread > 2 &&
652 u.buf[0] == '\n' && u.buf[nread - 1] == '\n' &&
653 sp->typecnt + 2 <= TZ_MAX_TYPES) {
654 struct state ts;
655 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700657 u.buf[nread - 1] = '\0';
658 result = tzparse(&u.buf[1], &ts, FALSE);
659 if (result == 0 && ts.typecnt == 2 &&
660 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
661 for (i = 0; i < 2; ++i)
662 ts.ttis[i].tt_abbrind +=
663 sp->charcnt;
664 for (i = 0; i < ts.charcnt; ++i)
665 sp->chars[sp->charcnt++] =
666 ts.chars[i];
667 i = 0;
668 while (i < ts.timecnt &&
669 ts.ats[i] <=
670 sp->ats[sp->timecnt - 1])
671 ++i;
672 while (i < ts.timecnt &&
673 sp->timecnt < TZ_MAX_TIMES) {
674 sp->ats[sp->timecnt] =
675 ts.ats[i];
676 sp->types[sp->timecnt] =
677 sp->typecnt +
678 ts.types[i];
679 ++sp->timecnt;
680 ++i;
681 }
682 sp->ttis[sp->typecnt++] = ts.ttis[0];
683 sp->ttis[sp->typecnt++] = ts.ttis[1];
684 }
685 }
686 i = 2 * YEARSPERREPEAT;
687 sp->goback = sp->goahead = sp->timecnt > i;
688 sp->goback &= sp->types[i] == sp->types[0] &&
689 differ_by_repeat(sp->ats[i], sp->ats[0]);
690 sp->goahead &=
691 sp->types[sp->timecnt - 1] == sp->types[sp->timecnt - 1 - i] &&
692 differ_by_repeat(sp->ats[sp->timecnt - 1],
693 sp->ats[sp->timecnt - 1 - i]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800694 XLOG(( "tzload: load ok !!\n" ));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700695 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800696}
697
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700698static const int mon_lengths[2][MONSPERYEAR] = {
699 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
700 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701};
702
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700703static const int year_lengths[2] = {
704 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705};
706
707/*
708** Given a pointer into a time zone string, scan until a character that is not
709** a valid character in a zone name is found. Return a pointer to that
710** character.
711*/
712
713static const char *
714getzname(strp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700715register const char * strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700717 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800718
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700719 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
720 c != '+')
721 ++strp;
722 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723}
724
725/*
726** Given a pointer into an extended time zone string, scan until the ending
727** delimiter of the zone name is located. Return a pointer to the delimiter.
728**
729** As with getzname above, the legal character set is actually quite
730** restricted, with other characters producing undefined results.
731** We don't do any checking here; checking is done later in common-case code.
732*/
733
734static const char *
735getqzname(register const char *strp, const int delim)
736{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700737 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700739 while ((c = *strp) != '\0' && c != delim)
740 ++strp;
741 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742}
743
744/*
745** Given a pointer into a time zone string, extract a number from that string.
746** Check that the number is within a specified range; if it is not, return
747** NULL.
748** Otherwise, return a pointer to the first character not part of the number.
749*/
750
751static const char *
752getnum(strp, nump, min, max)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700753register const char * strp;
754int * const nump;
755const int min;
756const int max;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800757{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700758 register char c;
759 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700761 if (strp == NULL || !is_digit(c = *strp))
762 return NULL;
763 num = 0;
764 do {
765 num = num * 10 + (c - '0');
766 if (num > max)
767 return NULL; /* illegal value */
768 c = *++strp;
769 } while (is_digit(c));
770 if (num < min)
771 return NULL; /* illegal value */
772 *nump = num;
773 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800774}
775
776/*
777** Given a pointer into a time zone string, extract a number of seconds,
778** in hh[:mm[:ss]] form, from the string.
779** If any error occurs, return NULL.
780** Otherwise, return a pointer to the first character not part of the number
781** of seconds.
782*/
783
784static const char *
785getsecs(strp, secsp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700786register const char * strp;
787long * const secsp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800788{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700789 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800790
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700791 /*
792 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
793 ** "M10.4.6/26", which does not conform to Posix,
794 ** but which specifies the equivalent of
795 ** ``02:00 on the first Sunday on or after 23 Oct''.
796 */
797 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
798 if (strp == NULL)
799 return NULL;
800 *secsp = num * (long) SECSPERHOUR;
801 if (*strp == ':') {
802 ++strp;
803 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
804 if (strp == NULL)
805 return NULL;
806 *secsp += num * SECSPERMIN;
807 if (*strp == ':') {
808 ++strp;
809 /* `SECSPERMIN' allows for leap seconds. */
810 strp = getnum(strp, &num, 0, SECSPERMIN);
811 if (strp == NULL)
812 return NULL;
813 *secsp += num;
814 }
815 }
816 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800817}
818
819/*
820** Given a pointer into a time zone string, extract an offset, in
821** [+-]hh[:mm[:ss]] form, from the string.
822** If any error occurs, return NULL.
823** Otherwise, return a pointer to the first character not part of the time.
824*/
825
826static const char *
827getoffset(strp, offsetp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700828register const char * strp;
829long * const offsetp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800830{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700831 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700833 if (*strp == '-') {
834 neg = 1;
835 ++strp;
836 } else if (*strp == '+')
837 ++strp;
838 strp = getsecs(strp, offsetp);
839 if (strp == NULL)
840 return NULL; /* illegal time */
841 if (neg)
842 *offsetp = -*offsetp;
843 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800844}
845
846/*
847** Given a pointer into a time zone string, extract a rule in the form
848** date[/time]. See POSIX section 8 for the format of "date" and "time".
849** If a valid rule is not found, return NULL.
850** Otherwise, return a pointer to the first character not part of the rule.
851*/
852
853static const char *
854getrule(strp, rulep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700855const char * strp;
856register struct rule * const rulep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800857{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700858 if (*strp == 'J') {
859 /*
860 ** Julian day.
861 */
862 rulep->r_type = JULIAN_DAY;
863 ++strp;
864 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
865 } else if (*strp == 'M') {
866 /*
867 ** Month, week, day.
868 */
869 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
870 ++strp;
871 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
872 if (strp == NULL)
873 return NULL;
874 if (*strp++ != '.')
875 return NULL;
876 strp = getnum(strp, &rulep->r_week, 1, 5);
877 if (strp == NULL)
878 return NULL;
879 if (*strp++ != '.')
880 return NULL;
881 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
882 } else if (is_digit(*strp)) {
883 /*
884 ** Day of year.
885 */
886 rulep->r_type = DAY_OF_YEAR;
887 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
888 } else return NULL; /* invalid format */
889 if (strp == NULL)
890 return NULL;
891 if (*strp == '/') {
892 /*
893 ** Time specified.
894 */
895 ++strp;
896 strp = getsecs(strp, &rulep->r_time);
897 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
898 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800899}
900
901/*
902** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
903** year, a rule, and the offset from UTC at the time that rule takes effect,
904** calculate the Epoch-relative time that rule takes effect.
905*/
906
907static time_t
908transtime(janfirst, year, rulep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700909const time_t janfirst;
910const int year;
911register const struct rule * const rulep;
912const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800913{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700914 register int leapyear;
915 register time_t value;
916 register int i;
917 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800918
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700919 INITIALIZE(value);
920 leapyear = isleap(year);
921 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800922
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700923 case JULIAN_DAY:
924 /*
925 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
926 ** years.
927 ** In non-leap years, or if the day number is 59 or less, just
928 ** add SECSPERDAY times the day number-1 to the time of
929 ** January 1, midnight, to get the day.
930 */
931 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
932 if (leapyear && rulep->r_day >= 60)
933 value += SECSPERDAY;
934 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800935
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700936 case DAY_OF_YEAR:
937 /*
938 ** n - day of year.
939 ** Just add SECSPERDAY times the day number to the time of
940 ** January 1, midnight, to get the day.
941 */
942 value = janfirst + rulep->r_day * SECSPERDAY;
943 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800944
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700945 case MONTH_NTH_DAY_OF_WEEK:
946 /*
947 ** Mm.n.d - nth "dth day" of month m.
948 */
949 value = janfirst;
950 for (i = 0; i < rulep->r_mon - 1; ++i)
951 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800952
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700953 /*
954 ** Use Zeller's Congruence to get day-of-week of first day of
955 ** month.
956 */
957 m1 = (rulep->r_mon + 9) % 12 + 1;
958 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
959 yy1 = yy0 / 100;
960 yy2 = yy0 % 100;
961 dow = ((26 * m1 - 2) / 10 +
962 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
963 if (dow < 0)
964 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800965
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700966 /*
967 ** "dow" is the day-of-week of the first day of the month. Get
968 ** the day-of-month (zero-origin) of the first "dow" day of the
969 ** month.
970 */
971 d = rulep->r_day - dow;
972 if (d < 0)
973 d += DAYSPERWEEK;
974 for (i = 1; i < rulep->r_week; ++i) {
975 if (d + DAYSPERWEEK >=
976 mon_lengths[leapyear][rulep->r_mon - 1])
977 break;
978 d += DAYSPERWEEK;
979 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800980
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700981 /*
982 ** "d" is the day-of-month (zero-origin) of the day we want.
983 */
984 value += d * SECSPERDAY;
985 break;
986 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800987
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700988 /*
989 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
990 ** question. To get the Epoch-relative time of the specified local
991 ** time on that day, add the transition time and the current offset
992 ** from UTC.
993 */
994 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800995}
996
997/*
998** Given a POSIX section 8-style TZ string, fill in the rule tables as
999** appropriate.
1000*/
1001
1002static int
1003tzparse(name, sp, lastditch)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001004const char * name;
1005register struct state * const sp;
1006const int lastditch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001007{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001008 const char * stdname;
1009 const char * dstname;
1010 size_t stdlen;
1011 size_t dstlen;
1012 long stdoffset;
1013 long dstoffset;
1014 register time_t * atp;
1015 register unsigned char * typep;
1016 register char * cp;
1017 register int load_result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001019 INITIALIZE(dstname);
1020 stdname = name;
1021 if (lastditch) {
1022 stdlen = strlen(name); /* length of standard zone name */
1023 name += stdlen;
1024 if (stdlen >= sizeof sp->chars)
1025 stdlen = (sizeof sp->chars) - 1;
1026 stdoffset = 0;
1027 } else {
1028 if (*name == '<') {
1029 name++;
1030 stdname = name;
1031 name = getqzname(name, '>');
1032 if (*name != '>')
1033 return (-1);
1034 stdlen = name - stdname;
1035 name++;
1036 } else {
1037 name = getzname(name);
1038 stdlen = name - stdname;
1039 }
1040 if (*name == '\0')
1041 return -1;
1042 name = getoffset(name, &stdoffset);
1043 if (name == NULL)
1044 return -1;
1045 }
1046 load_result = tzload(TZDEFRULES, sp, FALSE);
1047 if (load_result != 0)
1048 sp->leapcnt = 0; /* so, we're off a little */
1049 sp->timecnt = 0;
1050 if (*name != '\0') {
1051 if (*name == '<') {
1052 dstname = ++name;
1053 name = getqzname(name, '>');
1054 if (*name != '>')
1055 return -1;
1056 dstlen = name - dstname;
1057 name++;
1058 } else {
1059 dstname = name;
1060 name = getzname(name);
1061 dstlen = name - dstname; /* length of DST zone name */
1062 }
1063 if (*name != '\0' && *name != ',' && *name != ';') {
1064 name = getoffset(name, &dstoffset);
1065 if (name == NULL)
1066 return -1;
1067 } else dstoffset = stdoffset - SECSPERHOUR;
1068 if (*name == '\0' && load_result != 0)
1069 name = TZDEFRULESTRING;
1070 if (*name == ',' || *name == ';') {
1071 struct rule start;
1072 struct rule end;
1073 register int year;
1074 register time_t janfirst;
1075 time_t starttime;
1076 time_t endtime;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001077
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001078 ++name;
1079 if ((name = getrule(name, &start)) == NULL)
1080 return -1;
1081 if (*name++ != ',')
1082 return -1;
1083 if ((name = getrule(name, &end)) == NULL)
1084 return -1;
1085 if (*name != '\0')
1086 return -1;
1087 sp->typecnt = 2; /* standard time and DST */
1088 /*
1089 ** Two transitions per year, from EPOCH_YEAR forward.
1090 */
1091 sp->ttis[0].tt_gmtoff = -dstoffset;
1092 sp->ttis[0].tt_isdst = 1;
1093 sp->ttis[0].tt_abbrind = stdlen + 1;
1094 sp->ttis[1].tt_gmtoff = -stdoffset;
1095 sp->ttis[1].tt_isdst = 0;
1096 sp->ttis[1].tt_abbrind = 0;
1097 atp = sp->ats;
1098 typep = sp->types;
1099 janfirst = 0;
1100 for (year = EPOCH_YEAR;
1101 sp->timecnt + 2 <= TZ_MAX_TIMES;
1102 ++year) {
1103 time_t newfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001105 starttime = transtime(janfirst, year, &start,
1106 stdoffset);
1107 endtime = transtime(janfirst, year, &end,
1108 dstoffset);
1109 if (starttime > endtime) {
1110 *atp++ = endtime;
1111 *typep++ = 1; /* DST ends */
1112 *atp++ = starttime;
1113 *typep++ = 0; /* DST begins */
1114 } else {
1115 *atp++ = starttime;
1116 *typep++ = 0; /* DST begins */
1117 *atp++ = endtime;
1118 *typep++ = 1; /* DST ends */
1119 }
1120 sp->timecnt += 2;
1121 newfirst = janfirst;
1122 newfirst += year_lengths[isleap(year)] *
1123 SECSPERDAY;
1124 if (newfirst <= janfirst)
1125 break;
1126 janfirst = newfirst;
1127 }
1128 } else {
1129 register long theirstdoffset;
1130 register long theirdstoffset;
1131 register long theiroffset;
1132 register int isdst;
1133 register int i;
1134 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001135
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001136 if (*name != '\0')
1137 return -1;
1138 /*
1139 ** Initial values of theirstdoffset and theirdstoffset.
1140 */
1141 theirstdoffset = 0;
1142 for (i = 0; i < sp->timecnt; ++i) {
1143 j = sp->types[i];
1144 if (!sp->ttis[j].tt_isdst) {
1145 theirstdoffset =
1146 -sp->ttis[j].tt_gmtoff;
1147 break;
1148 }
1149 }
1150 theirdstoffset = 0;
1151 for (i = 0; i < sp->timecnt; ++i) {
1152 j = sp->types[i];
1153 if (sp->ttis[j].tt_isdst) {
1154 theirdstoffset =
1155 -sp->ttis[j].tt_gmtoff;
1156 break;
1157 }
1158 }
1159 /*
1160 ** Initially we're assumed to be in standard time.
1161 */
1162 isdst = FALSE;
1163 theiroffset = theirstdoffset;
1164 /*
1165 ** Now juggle transition times and types
1166 ** tracking offsets as you do.
1167 */
1168 for (i = 0; i < sp->timecnt; ++i) {
1169 j = sp->types[i];
1170 sp->types[i] = sp->ttis[j].tt_isdst;
1171 if (sp->ttis[j].tt_ttisgmt) {
1172 /* No adjustment to transition time */
1173 } else {
1174 /*
1175 ** If summer time is in effect, and the
1176 ** transition time was not specified as
1177 ** standard time, add the summer time
1178 ** offset to the transition time;
1179 ** otherwise, add the standard time
1180 ** offset to the transition time.
1181 */
1182 /*
1183 ** Transitions from DST to DDST
1184 ** will effectively disappear since
1185 ** POSIX provides for only one DST
1186 ** offset.
1187 */
1188 if (isdst && !sp->ttis[j].tt_ttisstd) {
1189 sp->ats[i] += dstoffset -
1190 theirdstoffset;
1191 } else {
1192 sp->ats[i] += stdoffset -
1193 theirstdoffset;
1194 }
1195 }
1196 theiroffset = -sp->ttis[j].tt_gmtoff;
1197 if (sp->ttis[j].tt_isdst)
1198 theirdstoffset = theiroffset;
1199 else theirstdoffset = theiroffset;
1200 }
1201 /*
1202 ** Finally, fill in ttis.
1203 ** ttisstd and ttisgmt need not be handled.
1204 */
1205 sp->ttis[0].tt_gmtoff = -stdoffset;
1206 sp->ttis[0].tt_isdst = FALSE;
1207 sp->ttis[0].tt_abbrind = 0;
1208 sp->ttis[1].tt_gmtoff = -dstoffset;
1209 sp->ttis[1].tt_isdst = TRUE;
1210 sp->ttis[1].tt_abbrind = stdlen + 1;
1211 sp->typecnt = 2;
1212 }
1213 } else {
1214 dstlen = 0;
1215 sp->typecnt = 1; /* only standard time */
1216 sp->timecnt = 0;
1217 sp->ttis[0].tt_gmtoff = -stdoffset;
1218 sp->ttis[0].tt_isdst = 0;
1219 sp->ttis[0].tt_abbrind = 0;
1220 }
1221 sp->charcnt = stdlen + 1;
1222 if (dstlen != 0)
1223 sp->charcnt += dstlen + 1;
1224 if ((size_t) sp->charcnt > sizeof sp->chars)
1225 return -1;
1226 cp = sp->chars;
1227 (void) strncpy(cp, stdname, stdlen);
1228 cp += stdlen;
1229 *cp++ = '\0';
1230 if (dstlen != 0) {
1231 (void) strncpy(cp, dstname, dstlen);
1232 *(cp + dstlen) = '\0';
1233 }
1234 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001235}
1236
1237static void
1238gmtload(sp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001239struct state * const sp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001240{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001241 if (tzload(gmt, sp, TRUE) != 0)
1242 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001243}
1244
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001245static void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001246tzsetwall P((void))
1247{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001248 if (lcl_is_set < 0)
1249 return;
1250 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001251
1252#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001253 if (lclptr == NULL) {
1254 lclptr = (struct state *) malloc(sizeof *lclptr);
1255 if (lclptr == NULL) {
1256 settzname(); /* all we can do */
1257 return;
1258 }
1259 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001260#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001261 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1262 gmtload(lclptr);
1263 settzname();
1264}
1265
1266static void
1267tzset_locked P((void))
1268{
1269 register const char * name = NULL;
1270 static char buf[PROP_VALUE_MAX];
1271
1272 name = getenv("TZ");
1273
1274 // try the "persist.sys.timezone" system property first
1275 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0)
1276 name = buf;
1277
1278 if (name == NULL) {
1279 tzsetwall();
1280 return;
1281 }
1282
1283 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1284 return;
1285 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1286 if (lcl_is_set)
1287 (void) strcpy(lcl_TZname, name);
1288
1289#ifdef ALL_STATE
1290 if (lclptr == NULL) {
1291 lclptr = (struct state *) malloc(sizeof *lclptr);
1292 if (lclptr == NULL) {
1293 settzname(); /* all we can do */
1294 return;
1295 }
1296 }
1297#endif /* defined ALL_STATE */
1298 if (*name == '\0') {
1299 /*
1300 ** User wants it fast rather than right.
1301 */
1302 lclptr->leapcnt = 0; /* so, we're off a little */
1303 lclptr->timecnt = 0;
1304 lclptr->typecnt = 0;
1305 lclptr->ttis[0].tt_isdst = 0;
1306 lclptr->ttis[0].tt_gmtoff = 0;
1307 lclptr->ttis[0].tt_abbrind = 0;
1308 (void) strcpy(lclptr->chars, gmt);
1309 } else if (tzload(name, lclptr, TRUE) != 0)
1310 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1311 (void) gmtload(lclptr);
1312 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001313}
1314
1315void
1316tzset P((void))
1317{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001318 _tzLock();
1319 tzset_locked();
1320 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001321}
1322
1323/*
1324** The easy way to behave "as if no library function calls" localtime
1325** is to not call it--so we drop its guts into "localsub", which can be
1326** freely called. (And no, the PANS doesn't require the above behavior--
1327** but it *is* desirable.)
1328**
1329** The unused offset argument is for the benefit of mktime variants.
1330*/
1331
1332/*ARGSUSED*/
1333static struct tm *
Elliott Hughes3a936a42012-09-11 11:15:53 -07001334localsub(timep, offset, tmp, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001335const time_t * const timep;
1336const long offset;
1337struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001338const struct state * sp; // android-added: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001339{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001340 register const struct ttinfo * ttisp;
1341 register int i;
1342 register struct tm * result;
1343 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344
Elliott Hughes3a936a42012-09-11 11:15:53 -07001345 // BEGIN android-changed: support user-supplied sp.
1346 if (sp == NULL) {
1347 sp = lclptr;
1348 }
1349 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001350#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001351 if (sp == NULL)
Elliott Hughes3a936a42012-09-11 11:15:53 -07001352 return gmtsub(timep, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001353#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001354 if ((sp->goback && t < sp->ats[0]) ||
1355 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1356 time_t newt = t;
1357 register time_t seconds;
1358 register time_t tcycles;
1359 register int_fast64_t icycles;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001360
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001361 if (t < sp->ats[0])
1362 seconds = sp->ats[0] - t;
1363 else seconds = t - sp->ats[sp->timecnt - 1];
1364 --seconds;
1365 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1366 ++tcycles;
1367 icycles = tcycles;
1368 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1369 return NULL;
1370 seconds = icycles;
1371 seconds *= YEARSPERREPEAT;
1372 seconds *= AVGSECSPERYEAR;
1373 if (t < sp->ats[0])
1374 newt += seconds;
1375 else newt -= seconds;
1376 if (newt < sp->ats[0] ||
1377 newt > sp->ats[sp->timecnt - 1])
1378 return NULL; /* "cannot happen" */
Elliott Hughes3a936a42012-09-11 11:15:53 -07001379 result = localsub(&newt, offset, tmp, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001380 if (result == tmp) {
1381 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001382
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001383 newy = tmp->tm_year;
1384 if (t < sp->ats[0])
1385 newy -= icycles * YEARSPERREPEAT;
1386 else newy += icycles * YEARSPERREPEAT;
1387 tmp->tm_year = newy;
1388 if (tmp->tm_year != newy)
1389 return NULL;
1390 }
1391 return result;
1392 }
1393 if (sp->timecnt == 0 || t < sp->ats[0]) {
1394 i = 0;
1395 while (sp->ttis[i].tt_isdst)
1396 if (++i >= sp->typecnt) {
1397 i = 0;
1398 break;
1399 }
1400 } else {
1401 register int lo = 1;
1402 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001404 while (lo < hi) {
1405 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001406
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001407 if (t < sp->ats[mid])
1408 hi = mid;
1409 else lo = mid + 1;
1410 }
1411 i = (int) sp->types[lo - 1];
1412 }
1413 ttisp = &sp->ttis[i];
1414 /*
1415 ** To get (wrong) behavior that's compatible with System V Release 2.0
1416 ** you'd replace the statement below with
1417 ** t += ttisp->tt_gmtoff;
1418 ** timesub(&t, 0L, sp, tmp);
1419 */
1420 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1421 tmp->tm_isdst = ttisp->tt_isdst;
1422 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001424 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001426 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001427}
1428
1429struct tm *
1430localtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001431const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001432{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001433 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001434}
1435
1436/*
1437** Re-entrant version of localtime.
1438*/
1439
1440struct tm *
1441localtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001442const time_t * const timep;
1443struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001445 struct tm* result;
1446
1447 _tzLock();
1448 tzset_locked();
Elliott Hughes3a936a42012-09-11 11:15:53 -07001449 result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001450 _tzUnlock();
1451
1452 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453}
1454
1455/*
1456** gmtsub is to gmtime as localsub is to localtime.
1457*/
1458
1459static struct tm *
Elliott Hughes3a936a42012-09-11 11:15:53 -07001460gmtsub(timep, offset, tmp, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001461const time_t * const timep;
1462const long offset;
1463struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001464const struct state * sp; // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001465{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001466 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001467
Elliott Hughes3a936a42012-09-11 11:15:53 -07001468 (void) sp; // android-added: unused.
1469
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001470 if (!gmt_is_set) {
1471 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001473 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1474 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001475#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001476 gmtload(gmtptr);
1477 }
1478 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001480 /*
1481 ** Could get fancy here and deliver something such as
1482 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1483 ** but this is no time for a treasure hunt.
1484 */
1485 if (offset != 0)
1486 tmp->TM_ZONE = wildabbr;
1487 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001489 if (gmtptr == NULL)
1490 tmp->TM_ZONE = gmt;
1491 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492#endif /* defined ALL_STATE */
1493#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001494 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001495#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001496 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001498 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499}
1500
1501struct tm *
1502gmtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001503const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001504{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001505 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001506}
1507
1508/*
1509* Re-entrant version of gmtime.
1510*/
1511
1512struct tm *
1513gmtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001514const time_t * const timep;
1515struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001516{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001517 struct tm* result;
1518
1519 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07001520 result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001521 _tzUnlock();
1522
1523 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001524}
1525
1526#ifdef STD_INSPIRED
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001527#if 0 /* disabled because there is no good documentation for this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528struct tm *
1529offtime(timep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001530const time_t * const timep;
1531const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001532{
Elliott Hughes3a936a42012-09-11 11:15:53 -07001533 return gmtsub(timep, offset, &tmGlobal, NULL); // android-changed: extra parameter.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001534}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001535#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001536#endif /* defined STD_INSPIRED */
1537
1538/*
1539** Return the number of leap years through the end of the given year
1540** where, to make the math easy, the answer for year zero is defined as zero.
1541*/
1542
1543static int
1544leaps_thru_end_of(y)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001545register const int y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001546{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001547 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1548 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001549}
1550
1551static struct tm *
1552timesub(timep, offset, sp, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001553const time_t * const timep;
1554const long offset;
1555register const struct state * const sp;
1556register struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001557{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001558 register const struct lsinfo * lp;
1559 register time_t tdays;
1560 register int idays; /* unsigned would be so 2003 */
1561 register long rem;
1562 int y;
1563 register const int * ip;
1564 register long corr;
1565 register int hit;
1566 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001567
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001568 corr = 0;
1569 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001570#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001571 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001572#endif /* defined ALL_STATE */
1573#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001574 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001575#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001576 while (--i >= 0) {
1577 lp = &sp->lsis[i];
1578 if (*timep >= lp->ls_trans) {
1579 if (*timep == lp->ls_trans) {
1580 hit = ((i == 0 && lp->ls_corr > 0) ||
1581 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1582 if (hit)
1583 while (i > 0 &&
1584 sp->lsis[i].ls_trans ==
1585 sp->lsis[i - 1].ls_trans + 1 &&
1586 sp->lsis[i].ls_corr ==
1587 sp->lsis[i - 1].ls_corr + 1) {
1588 ++hit;
1589 --i;
1590 }
1591 }
1592 corr = lp->ls_corr;
1593 break;
1594 }
1595 }
1596 y = EPOCH_YEAR;
1597 tdays = *timep / SECSPERDAY;
1598 rem = *timep - tdays * SECSPERDAY;
1599 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1600 int newy;
1601 register time_t tdelta;
1602 register int idelta;
1603 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001604
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001605 tdelta = tdays / DAYSPERLYEAR;
1606 idelta = tdelta;
1607 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1608 return NULL;
1609 if (idelta == 0)
1610 idelta = (tdays < 0) ? -1 : 1;
1611 newy = y;
1612 if (increment_overflow(&newy, idelta))
1613 return NULL;
1614 leapdays = leaps_thru_end_of(newy - 1) -
1615 leaps_thru_end_of(y - 1);
1616 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1617 tdays -= leapdays;
1618 y = newy;
1619 }
1620 {
1621 register long seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001622
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001623 seconds = tdays * SECSPERDAY + 0.5;
1624 tdays = seconds / SECSPERDAY;
1625 rem += seconds - tdays * SECSPERDAY;
1626 }
1627 /*
1628 ** Given the range, we can now fearlessly cast...
1629 */
1630 idays = tdays;
1631 rem += offset - corr;
1632 while (rem < 0) {
1633 rem += SECSPERDAY;
1634 --idays;
1635 }
1636 while (rem >= SECSPERDAY) {
1637 rem -= SECSPERDAY;
1638 ++idays;
1639 }
1640 while (idays < 0) {
1641 if (increment_overflow(&y, -1))
1642 return NULL;
1643 idays += year_lengths[isleap(y)];
1644 }
1645 while (idays >= year_lengths[isleap(y)]) {
1646 idays -= year_lengths[isleap(y)];
1647 if (increment_overflow(&y, 1))
1648 return NULL;
1649 }
1650 tmp->tm_year = y;
1651 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1652 return NULL;
1653 tmp->tm_yday = idays;
1654 /*
1655 ** The "extra" mods below avoid overflow problems.
1656 */
1657 tmp->tm_wday = EPOCH_WDAY +
1658 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1659 (DAYSPERNYEAR % DAYSPERWEEK) +
1660 leaps_thru_end_of(y - 1) -
1661 leaps_thru_end_of(EPOCH_YEAR - 1) +
1662 idays;
1663 tmp->tm_wday %= DAYSPERWEEK;
1664 if (tmp->tm_wday < 0)
1665 tmp->tm_wday += DAYSPERWEEK;
1666 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1667 rem %= SECSPERHOUR;
1668 tmp->tm_min = (int) (rem / SECSPERMIN);
1669 /*
1670 ** A positive leap second requires a special
1671 ** representation. This uses "... ??:59:60" et seq.
1672 */
1673 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1674 ip = mon_lengths[isleap(y)];
1675 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1676 idays -= ip[tmp->tm_mon];
1677 tmp->tm_mday = (int) (idays + 1);
1678 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001679#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001680 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001682 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683}
1684
1685char *
1686ctime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001687const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001688{
1689/*
1690** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001691** The ctime function converts the calendar time pointed to by timer
1692** to local time in the form of a string. It is equivalent to
1693** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001694*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001695 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001696}
1697
1698char *
1699ctime_r(timep, buf)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001700const time_t * const timep;
1701char * buf;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001702{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001703 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001704
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001705 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001706}
1707
1708/*
1709** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001710** The "best" way to do mktime I think is based on an idea of Bob
1711** Kridle's (so its said...) from a long time ago.
1712** It does a binary search of the time_t space. Since time_t's are
1713** just 32 bits, its a max of 32 iterations (even at 64 bits it
1714** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001715*/
1716
1717#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001718#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001719#endif /* !defined WRONG */
1720
1721/*
1722** Simplified normalize logic courtesy Paul Eggert.
1723*/
1724
1725static int
1726increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001727int * number;
1728int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001729{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001730 unsigned number0 = (unsigned)*number;
1731 unsigned number1 = (unsigned)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001732
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001733 *number = (int)number1;
1734
1735 if (delta >= 0) {
1736 return ((int)number1 < (int)number0);
1737 } else {
1738 return ((int)number1 > (int)number0);
1739 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001740}
1741
1742static int
1743long_increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001744long * number;
1745int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001746{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001747 unsigned long number0 = (unsigned long)*number;
1748 unsigned long number1 = (unsigned long)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001749
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001750 *number = (long)number1;
1751
1752 if (delta >= 0) {
1753 return ((long)number1 < (long)number0);
1754 } else {
1755 return ((long)number1 > (long)number0);
1756 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001757}
1758
1759static int
1760normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001761int * const tensptr;
1762int * const unitsptr;
1763const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001764{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001765 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001766
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001767 tensdelta = (*unitsptr >= 0) ?
1768 (*unitsptr / base) :
1769 (-1 - (-1 - *unitsptr) / base);
1770 *unitsptr -= tensdelta * base;
1771 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001772}
1773
1774static int
1775long_normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001776long * const tensptr;
1777int * const unitsptr;
1778const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001779{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001780 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001781
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001782 tensdelta = (*unitsptr >= 0) ?
1783 (*unitsptr / base) :
1784 (-1 - (-1 - *unitsptr) / base);
1785 *unitsptr -= tensdelta * base;
1786 return long_increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001787}
1788
1789static int
1790tmcomp(atmp, btmp)
1791register const struct tm * const atmp;
1792register const struct tm * const btmp;
1793{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001794 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001795
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001796 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1797 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1798 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1799 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1800 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1801 result = atmp->tm_sec - btmp->tm_sec;
1802 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001803}
1804
1805static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07001806time2sub(tmp, funcp, offset, okayp, do_norm_secs, sp) // android-changed: added sp
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001807struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001808struct 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 -07001809const long offset;
1810int * const okayp;
1811const int do_norm_secs;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001812const struct state * sp; // android-changed: added sp
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001813{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001814 register int dir;
1815 register int i, j;
1816 register int saved_seconds;
1817 register long li;
1818 register time_t lo;
1819 register time_t hi;
1820 long y;
1821 time_t newt;
1822 time_t t;
1823 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001824
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001825 *okayp = FALSE;
1826 yourtm = *tmp;
1827 if (do_norm_secs) {
1828 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1829 SECSPERMIN))
1830 return WRONG;
1831 }
1832 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1833 return WRONG;
1834 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1835 return WRONG;
1836 y = yourtm.tm_year;
1837 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1838 return WRONG;
1839 /*
1840 ** Turn y into an actual year number for now.
1841 ** It is converted back to an offset from TM_YEAR_BASE later.
1842 */
1843 if (long_increment_overflow(&y, TM_YEAR_BASE))
1844 return WRONG;
1845 while (yourtm.tm_mday <= 0) {
1846 if (long_increment_overflow(&y, -1))
1847 return WRONG;
1848 li = y + (1 < yourtm.tm_mon);
1849 yourtm.tm_mday += year_lengths[isleap(li)];
1850 }
1851 while (yourtm.tm_mday > DAYSPERLYEAR) {
1852 li = y + (1 < yourtm.tm_mon);
1853 yourtm.tm_mday -= year_lengths[isleap(li)];
1854 if (long_increment_overflow(&y, 1))
1855 return WRONG;
1856 }
1857 for ( ; ; ) {
1858 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1859 if (yourtm.tm_mday <= i)
1860 break;
1861 yourtm.tm_mday -= i;
1862 if (++yourtm.tm_mon >= MONSPERYEAR) {
1863 yourtm.tm_mon = 0;
1864 if (long_increment_overflow(&y, 1))
1865 return WRONG;
1866 }
1867 }
1868 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1869 return WRONG;
1870 yourtm.tm_year = y;
1871 if (yourtm.tm_year != y)
1872 return WRONG;
1873 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1874 saved_seconds = 0;
1875 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1876 /*
1877 ** We can't set tm_sec to 0, because that might push the
1878 ** time below the minimum representable time.
1879 ** Set tm_sec to 59 instead.
1880 ** This assumes that the minimum representable time is
1881 ** not in the same minute that a leap second was deleted from,
1882 ** which is a safer assumption than using 58 would be.
1883 */
1884 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1885 return WRONG;
1886 saved_seconds = yourtm.tm_sec;
1887 yourtm.tm_sec = SECSPERMIN - 1;
1888 } else {
1889 saved_seconds = yourtm.tm_sec;
1890 yourtm.tm_sec = 0;
1891 }
1892 /*
1893 ** Do a binary search (this works whatever time_t's type is).
1894 */
1895 if (!TYPE_SIGNED(time_t)) {
1896 lo = 0;
1897 hi = lo - 1;
1898 } else if (!TYPE_INTEGRAL(time_t)) {
1899 if (sizeof(time_t) > sizeof(float))
1900 hi = (time_t) DBL_MAX;
1901 else hi = (time_t) FLT_MAX;
1902 lo = -hi;
1903 } else {
1904 lo = 1;
1905 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1906 lo *= 2;
1907 hi = -(lo + 1);
1908 }
1909 for ( ; ; ) {
1910 t = lo / 2 + hi / 2;
1911 if (t < lo)
1912 t = lo;
1913 else if (t > hi)
1914 t = hi;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001915 if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001916 /*
1917 ** Assume that t is too extreme to be represented in
1918 ** a struct tm; arrange things so that it is less
1919 ** extreme on the next pass.
1920 */
1921 dir = (t > 0) ? 1 : -1;
1922 } else dir = tmcomp(&mytm, &yourtm);
1923 if (dir != 0) {
1924 if (t == lo) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001925 if (t == TIME_T_MAX)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001926 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001927 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001928 ++lo;
1929 } else if (t == hi) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001930 if (t == TIME_T_MIN)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001931 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001932 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001933 --hi;
1934 }
1935 if (lo > hi)
1936 return WRONG;
1937 if (dir > 0)
1938 hi = t;
1939 else lo = t;
1940 continue;
1941 }
1942 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1943 break;
1944 /*
1945 ** Right time, wrong type.
1946 ** Hunt for right time, right type.
1947 ** It's okay to guess wrong since the guess
1948 ** gets checked.
1949 */
1950 /*
1951 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1952 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07001953 // BEGIN android-changed: support user-supplied sp
1954 if (sp == NULL) {
1955 sp = (const struct state *)
1956 (((void *) funcp == (void *) localsub) ?
1957 lclptr : gmtptr);
1958 }
1959 // END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001960#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001961 if (sp == NULL)
1962 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001963#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001964 for (i = sp->typecnt - 1; i >= 0; --i) {
1965 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1966 continue;
1967 for (j = sp->typecnt - 1; j >= 0; --j) {
1968 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1969 continue;
1970 newt = t + sp->ttis[j].tt_gmtoff -
1971 sp->ttis[i].tt_gmtoff;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001972 if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001973 continue;
1974 if (tmcomp(&mytm, &yourtm) != 0)
1975 continue;
1976 if (mytm.tm_isdst != yourtm.tm_isdst)
1977 continue;
1978 /*
1979 ** We have a match.
1980 */
1981 t = newt;
1982 goto label;
1983 }
1984 }
1985 return WRONG;
1986 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001987label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001988 newt = t + saved_seconds;
1989 if ((newt < t) != (saved_seconds < 0))
1990 return WRONG;
1991 t = newt;
Elliott Hughes3a936a42012-09-11 11:15:53 -07001992 if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001993 *okayp = TRUE;
1994 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001995}
1996
Elliott Hughes3a936a42012-09-11 11:15:53 -07001997// BEGIN android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001998static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07001999time2(tmp, funcp, offset, okayp, sp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002000struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002001struct tm * (* const funcp) P((const time_t*, long, struct tm*, const struct state*));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002002const long offset;
2003int * const okayp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002004const struct state * sp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002005{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002006 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002007
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002008 /*
2009 ** First try without normalization of seconds
2010 ** (in case tm_sec contains a value associated with a leap second).
2011 ** If that fails, try with normalization of seconds.
2012 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07002013 t = time2sub(tmp, funcp, offset, okayp, FALSE, sp);
2014 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002015}
Elliott Hughes3a936a42012-09-11 11:15:53 -07002016// END android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002017
2018static time_t
Elliott Hughes3a936a42012-09-11 11:15:53 -07002019time1(tmp, funcp, offset, sp) // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002020struct tm * const tmp;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002021struct tm * (* const funcp) P((const time_t *, long, struct tm *, const struct state *));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002022const long offset;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002023const struct state * sp; // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002024{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002025 register time_t t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002026 register int samei, otheri;
2027 register int sameind, otherind;
2028 register int i;
2029 register int nseen;
2030 int seen[TZ_MAX_TYPES];
2031 int types[TZ_MAX_TYPES];
2032 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002033
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002034 if (tmp->tm_isdst > 1)
2035 tmp->tm_isdst = 1;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002036 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002038 /*
2039 ** PCTS code courtesy Grant Sullivan.
2040 */
2041 if (okay)
2042 return t;
2043 if (tmp->tm_isdst < 0)
2044 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002045#endif /* defined PCTS */
2046#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002047 if (okay || tmp->tm_isdst < 0)
2048 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002049#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002050 /*
2051 ** We're supposed to assume that somebody took a time of one type
2052 ** and did some math on it that yielded a "struct tm" that's bad.
2053 ** We try to divine the type they started from and adjust to the
2054 ** type they need.
2055 */
2056 /*
2057 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
2058 */
Elliott Hughes3a936a42012-09-11 11:15:53 -07002059 // BEGIN android-changed: support user-supplied sp.
2060 if (sp == NULL) {
2061 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
2062 lclptr : gmtptr);
2063 }
2064 // BEGIN android-changed
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002065#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002066 if (sp == NULL)
2067 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002068#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002069 for (i = 0; i < sp->typecnt; ++i)
2070 seen[i] = FALSE;
2071 nseen = 0;
2072 for (i = sp->timecnt - 1; i >= 0; --i)
2073 if (!seen[sp->types[i]]) {
2074 seen[sp->types[i]] = TRUE;
2075 types[nseen++] = sp->types[i];
2076 }
2077 for (sameind = 0; sameind < nseen; ++sameind) {
2078 samei = types[sameind];
2079 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2080 continue;
2081 for (otherind = 0; otherind < nseen; ++otherind) {
2082 otheri = types[otherind];
2083 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2084 continue;
2085 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2086 sp->ttis[samei].tt_gmtoff;
2087 tmp->tm_isdst = !tmp->tm_isdst;
Elliott Hughes3a936a42012-09-11 11:15:53 -07002088 t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002089 if (okay)
2090 return t;
2091 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2092 sp->ttis[samei].tt_gmtoff;
2093 tmp->tm_isdst = !tmp->tm_isdst;
2094 }
2095 }
2096 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002097}
2098
2099time_t
2100mktime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002101struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002102{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002103 time_t result;
2104 _tzLock();
2105 tzset_locked();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002106 result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002107 _tzUnlock();
2108 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002109}
2110
Elliott Hughes3a936a42012-09-11 11:15:53 -07002111// BEGIN android-added
2112time_t
2113mktime_tz(tmp, tz)
2114struct tm * const tmp;
2115char const * tz;
2116{
2117 struct state st;
2118 if (tzload(tz, &st, TRUE) != 0) {
2119 // TODO: not sure what's best here, but for now, we fall back to gmt.
2120 gmtload(&st);
2121 }
2122 return time1(tmp, localsub, 0L, &st);
2123}
2124
2125void
2126localtime_tz(timep, tmp, tz)
2127const time_t * const timep;
2128struct tm * tmp;
2129const char* tz;
2130{
2131 struct state st;
2132 if (tzload(tz, &st, TRUE) != 0) {
2133 // TODO: not sure what's best here, but for now, we fall back to gmt.
2134 gmtload(&st);
2135 }
2136 localsub(timep, 0L, tmp, &st);
2137}
2138// END android-added
2139
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002140#ifdef STD_INSPIRED
2141
2142time_t
2143timelocal(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002144struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002145{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002146 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2147 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002148}
2149
2150time_t
2151timegm(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002152struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002153{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002154 time_t result;
2155
2156 tmp->tm_isdst = 0;
2157 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002158 result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002159 _tzUnlock();
2160
2161 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002162}
2163
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002164#if 0 /* disable due to lack of clear documentation on this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002165time_t
2166timeoff(tmp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002167struct tm * const tmp;
2168const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002169{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002170 time_t result;
2171
2172 tmp->tm_isdst = 0;
2173 _tzLock();
Elliott Hughes3a936a42012-09-11 11:15:53 -07002174 result = time1(tmp, gmtsub, offset, NULL); // android-changed: extra parameter.
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002175 _tzUnlock();
2176
2177 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002178}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002179#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002180
2181#endif /* defined STD_INSPIRED */
2182
2183#ifdef CMUCS
2184
2185/*
2186** The following is supplied for compatibility with
2187** previous versions of the CMUCS runtime library.
2188*/
2189
2190long
2191gtime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002192struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002193{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002194 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002195
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002196 if (t == WRONG)
2197 return -1;
2198 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002199}
2200
2201#endif /* defined CMUCS */
2202
2203/*
2204** XXX--is the below the right way to conditionalize??
2205*/
2206
2207#ifdef STD_INSPIRED
2208
2209/*
2210** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2211** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2212** is not the case if we are accounting for leap seconds.
2213** So, we provide the following conversion routines for use
2214** when exchanging timestamps with POSIX conforming systems.
2215*/
2216
2217static long
2218leapcorr(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002219time_t * timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002220{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002221 register struct state * sp;
2222 register struct lsinfo * lp;
2223 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002224
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002225 sp = lclptr;
2226 i = sp->leapcnt;
2227 while (--i >= 0) {
2228 lp = &sp->lsis[i];
2229 if (*timep >= lp->ls_trans)
2230 return lp->ls_corr;
2231 }
2232 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002233}
2234
2235time_t
2236time2posix(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002237time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002238{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002239 tzset();
2240 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002241}
2242
2243time_t
2244posix2time(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002245time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002246{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002247 time_t x;
2248 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002249
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002250 tzset();
2251 /*
2252 ** For a positive leap second hit, the result
2253 ** is not unique. For a negative leap second
2254 ** hit, the corresponding time doesn't exist,
2255 ** so we return an adjacent second.
2256 */
2257 x = t + leapcorr(&t);
2258 y = x - leapcorr(&x);
2259 if (y < t) {
2260 do {
2261 x++;
2262 y = x - leapcorr(&x);
2263 } while (y < t);
2264 if (t != y)
2265 return x - 1;
2266 } else if (y > t) {
2267 do {
2268 --x;
2269 y = x - leapcorr(&x);
2270 } while (y > t);
2271 if (t != y)
2272 return x + 1;
2273 }
2274 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002275}
2276
2277#endif /* defined STD_INSPIRED */