blob: e442a03a5a3320f2e28f9ef5d9a4aebf1930ed72 [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,
220 struct tm * tmp));
221static struct tm * localsub P((const time_t * timep, long offset,
222 struct tm * tmp));
223static 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 *,
233 long, struct tm *)),
234 long offset));
235static time_t time2 P((struct tm *tmp,
236 struct tm * (*funcp) P((const time_t *,
237 long, struct tm*)),
238 long offset, int * okayp));
239static time_t time2sub P((struct tm *tmp,
240 struct tm * (*funcp) P((const time_t *,
241 long, struct tm*)),
242 long offset, int * okayp, int do_norm_secs));
243static 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 *
1334localsub(timep, offset, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001335const time_t * const timep;
1336const long offset;
1337struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001338{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001339 register struct state * sp;
1340 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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001345 sp = lclptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001347 if (sp == NULL)
1348 return gmtsub(timep, offset, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001349#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001350 if ((sp->goback && t < sp->ats[0]) ||
1351 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1352 time_t newt = t;
1353 register time_t seconds;
1354 register time_t tcycles;
1355 register int_fast64_t icycles;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001356
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001357 if (t < sp->ats[0])
1358 seconds = sp->ats[0] - t;
1359 else seconds = t - sp->ats[sp->timecnt - 1];
1360 --seconds;
1361 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1362 ++tcycles;
1363 icycles = tcycles;
1364 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1365 return NULL;
1366 seconds = icycles;
1367 seconds *= YEARSPERREPEAT;
1368 seconds *= AVGSECSPERYEAR;
1369 if (t < sp->ats[0])
1370 newt += seconds;
1371 else newt -= seconds;
1372 if (newt < sp->ats[0] ||
1373 newt > sp->ats[sp->timecnt - 1])
1374 return NULL; /* "cannot happen" */
1375 result = localsub(&newt, offset, tmp);
1376 if (result == tmp) {
1377 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001378
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001379 newy = tmp->tm_year;
1380 if (t < sp->ats[0])
1381 newy -= icycles * YEARSPERREPEAT;
1382 else newy += icycles * YEARSPERREPEAT;
1383 tmp->tm_year = newy;
1384 if (tmp->tm_year != newy)
1385 return NULL;
1386 }
1387 return result;
1388 }
1389 if (sp->timecnt == 0 || t < sp->ats[0]) {
1390 i = 0;
1391 while (sp->ttis[i].tt_isdst)
1392 if (++i >= sp->typecnt) {
1393 i = 0;
1394 break;
1395 }
1396 } else {
1397 register int lo = 1;
1398 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001399
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001400 while (lo < hi) {
1401 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001402
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001403 if (t < sp->ats[mid])
1404 hi = mid;
1405 else lo = mid + 1;
1406 }
1407 i = (int) sp->types[lo - 1];
1408 }
1409 ttisp = &sp->ttis[i];
1410 /*
1411 ** To get (wrong) behavior that's compatible with System V Release 2.0
1412 ** you'd replace the statement below with
1413 ** t += ttisp->tt_gmtoff;
1414 ** timesub(&t, 0L, sp, tmp);
1415 */
1416 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1417 tmp->tm_isdst = ttisp->tt_isdst;
1418 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001420 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001421#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001422 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423}
1424
1425struct tm *
1426localtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001427const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001429 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001430}
1431
1432/*
1433** Re-entrant version of localtime.
1434*/
1435
1436struct tm *
1437localtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001438const time_t * const timep;
1439struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001440{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001441 struct tm* result;
1442
1443 _tzLock();
1444 tzset_locked();
1445 result = localsub(timep, 0L, tmp);
1446 _tzUnlock();
1447
1448 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001449}
1450
1451/*
1452** gmtsub is to gmtime as localsub is to localtime.
1453*/
1454
1455static struct tm *
1456gmtsub(timep, offset, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001457const time_t * const timep;
1458const long offset;
1459struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001460{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001461 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001463 if (!gmt_is_set) {
1464 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001465#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001466 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1467 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001468#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001469 gmtload(gmtptr);
1470 }
1471 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001473 /*
1474 ** Could get fancy here and deliver something such as
1475 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1476 ** but this is no time for a treasure hunt.
1477 */
1478 if (offset != 0)
1479 tmp->TM_ZONE = wildabbr;
1480 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001481#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001482 if (gmtptr == NULL)
1483 tmp->TM_ZONE = gmt;
1484 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001485#endif /* defined ALL_STATE */
1486#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001487 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001489 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001491 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001492}
1493
1494struct tm *
1495gmtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001496const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001497{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001498 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001499}
1500
1501/*
1502* Re-entrant version of gmtime.
1503*/
1504
1505struct tm *
1506gmtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001507const time_t * const timep;
1508struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001509{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001510 struct tm* result;
1511
1512 _tzLock();
1513 result = gmtsub(timep, 0L, tmp);
1514 _tzUnlock();
1515
1516 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001517}
1518
1519#ifdef STD_INSPIRED
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001520#if 0 /* disabled because there is no good documentation for this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001521struct tm *
1522offtime(timep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001523const time_t * const timep;
1524const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001525{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001526 return gmtsub(timep, offset, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001527}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01001528#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001529#endif /* defined STD_INSPIRED */
1530
1531/*
1532** Return the number of leap years through the end of the given year
1533** where, to make the math easy, the answer for year zero is defined as zero.
1534*/
1535
1536static int
1537leaps_thru_end_of(y)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001538register const int y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001539{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001540 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1541 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001542}
1543
1544static struct tm *
1545timesub(timep, offset, sp, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001546const time_t * const timep;
1547const long offset;
1548register const struct state * const sp;
1549register struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001550{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001551 register const struct lsinfo * lp;
1552 register time_t tdays;
1553 register int idays; /* unsigned would be so 2003 */
1554 register long rem;
1555 int y;
1556 register const int * ip;
1557 register long corr;
1558 register int hit;
1559 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001560
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001561 corr = 0;
1562 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001563#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001564 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001565#endif /* defined ALL_STATE */
1566#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001567 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001568#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001569 while (--i >= 0) {
1570 lp = &sp->lsis[i];
1571 if (*timep >= lp->ls_trans) {
1572 if (*timep == lp->ls_trans) {
1573 hit = ((i == 0 && lp->ls_corr > 0) ||
1574 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1575 if (hit)
1576 while (i > 0 &&
1577 sp->lsis[i].ls_trans ==
1578 sp->lsis[i - 1].ls_trans + 1 &&
1579 sp->lsis[i].ls_corr ==
1580 sp->lsis[i - 1].ls_corr + 1) {
1581 ++hit;
1582 --i;
1583 }
1584 }
1585 corr = lp->ls_corr;
1586 break;
1587 }
1588 }
1589 y = EPOCH_YEAR;
1590 tdays = *timep / SECSPERDAY;
1591 rem = *timep - tdays * SECSPERDAY;
1592 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1593 int newy;
1594 register time_t tdelta;
1595 register int idelta;
1596 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001597
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001598 tdelta = tdays / DAYSPERLYEAR;
1599 idelta = tdelta;
1600 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1601 return NULL;
1602 if (idelta == 0)
1603 idelta = (tdays < 0) ? -1 : 1;
1604 newy = y;
1605 if (increment_overflow(&newy, idelta))
1606 return NULL;
1607 leapdays = leaps_thru_end_of(newy - 1) -
1608 leaps_thru_end_of(y - 1);
1609 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1610 tdays -= leapdays;
1611 y = newy;
1612 }
1613 {
1614 register long seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001615
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001616 seconds = tdays * SECSPERDAY + 0.5;
1617 tdays = seconds / SECSPERDAY;
1618 rem += seconds - tdays * SECSPERDAY;
1619 }
1620 /*
1621 ** Given the range, we can now fearlessly cast...
1622 */
1623 idays = tdays;
1624 rem += offset - corr;
1625 while (rem < 0) {
1626 rem += SECSPERDAY;
1627 --idays;
1628 }
1629 while (rem >= SECSPERDAY) {
1630 rem -= SECSPERDAY;
1631 ++idays;
1632 }
1633 while (idays < 0) {
1634 if (increment_overflow(&y, -1))
1635 return NULL;
1636 idays += year_lengths[isleap(y)];
1637 }
1638 while (idays >= year_lengths[isleap(y)]) {
1639 idays -= year_lengths[isleap(y)];
1640 if (increment_overflow(&y, 1))
1641 return NULL;
1642 }
1643 tmp->tm_year = y;
1644 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1645 return NULL;
1646 tmp->tm_yday = idays;
1647 /*
1648 ** The "extra" mods below avoid overflow problems.
1649 */
1650 tmp->tm_wday = EPOCH_WDAY +
1651 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1652 (DAYSPERNYEAR % DAYSPERWEEK) +
1653 leaps_thru_end_of(y - 1) -
1654 leaps_thru_end_of(EPOCH_YEAR - 1) +
1655 idays;
1656 tmp->tm_wday %= DAYSPERWEEK;
1657 if (tmp->tm_wday < 0)
1658 tmp->tm_wday += DAYSPERWEEK;
1659 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1660 rem %= SECSPERHOUR;
1661 tmp->tm_min = (int) (rem / SECSPERMIN);
1662 /*
1663 ** A positive leap second requires a special
1664 ** representation. This uses "... ??:59:60" et seq.
1665 */
1666 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1667 ip = mon_lengths[isleap(y)];
1668 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1669 idays -= ip[tmp->tm_mon];
1670 tmp->tm_mday = (int) (idays + 1);
1671 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001672#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001673 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001674#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001675 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001676}
1677
1678char *
1679ctime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001680const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681{
1682/*
1683** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001684** The ctime function converts the calendar time pointed to by timer
1685** to local time in the form of a string. It is equivalent to
1686** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001688 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001689}
1690
1691char *
1692ctime_r(timep, buf)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001693const time_t * const timep;
1694char * buf;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001695{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001696 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001698 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699}
1700
1701/*
1702** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001703** The "best" way to do mktime I think is based on an idea of Bob
1704** Kridle's (so its said...) from a long time ago.
1705** It does a binary search of the time_t space. Since time_t's are
1706** just 32 bits, its a max of 32 iterations (even at 64 bits it
1707** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001708*/
1709
1710#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001711#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712#endif /* !defined WRONG */
1713
1714/*
1715** Simplified normalize logic courtesy Paul Eggert.
1716*/
1717
1718static int
1719increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001720int * number;
1721int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001722{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001723 unsigned number0 = (unsigned)*number;
1724 unsigned number1 = (unsigned)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001725
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001726 *number = (int)number1;
1727
1728 if (delta >= 0) {
1729 return ((int)number1 < (int)number0);
1730 } else {
1731 return ((int)number1 > (int)number0);
1732 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733}
1734
1735static int
1736long_increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001737long * number;
1738int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001739{
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001740 unsigned long number0 = (unsigned long)*number;
1741 unsigned long number1 = (unsigned long)(number0 + delta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001742
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001743 *number = (long)number1;
1744
1745 if (delta >= 0) {
1746 return ((long)number1 < (long)number0);
1747 } else {
1748 return ((long)number1 > (long)number0);
1749 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001750}
1751
1752static int
1753normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001754int * const tensptr;
1755int * const unitsptr;
1756const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001757{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001758 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001759
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001760 tensdelta = (*unitsptr >= 0) ?
1761 (*unitsptr / base) :
1762 (-1 - (-1 - *unitsptr) / base);
1763 *unitsptr -= tensdelta * base;
1764 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001765}
1766
1767static int
1768long_normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001769long * const tensptr;
1770int * const unitsptr;
1771const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001772{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001773 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001774
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001775 tensdelta = (*unitsptr >= 0) ?
1776 (*unitsptr / base) :
1777 (-1 - (-1 - *unitsptr) / base);
1778 *unitsptr -= tensdelta * base;
1779 return long_increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001780}
1781
1782static int
1783tmcomp(atmp, btmp)
1784register const struct tm * const atmp;
1785register const struct tm * const btmp;
1786{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001787 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001788
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001789 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1790 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1791 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1792 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1793 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1794 result = atmp->tm_sec - btmp->tm_sec;
1795 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001796}
1797
1798static time_t
1799time2sub(tmp, funcp, offset, okayp, do_norm_secs)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001800struct tm * const tmp;
1801struct tm * (* const funcp) P((const time_t*, long, struct tm*));
1802const long offset;
1803int * const okayp;
1804const int do_norm_secs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001805{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001806 register const struct state * sp;
1807 register int dir;
1808 register int i, j;
1809 register int saved_seconds;
1810 register long li;
1811 register time_t lo;
1812 register time_t hi;
1813 long y;
1814 time_t newt;
1815 time_t t;
1816 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001817
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001818 *okayp = FALSE;
1819 yourtm = *tmp;
1820 if (do_norm_secs) {
1821 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1822 SECSPERMIN))
1823 return WRONG;
1824 }
1825 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1826 return WRONG;
1827 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1828 return WRONG;
1829 y = yourtm.tm_year;
1830 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1831 return WRONG;
1832 /*
1833 ** Turn y into an actual year number for now.
1834 ** It is converted back to an offset from TM_YEAR_BASE later.
1835 */
1836 if (long_increment_overflow(&y, TM_YEAR_BASE))
1837 return WRONG;
1838 while (yourtm.tm_mday <= 0) {
1839 if (long_increment_overflow(&y, -1))
1840 return WRONG;
1841 li = y + (1 < yourtm.tm_mon);
1842 yourtm.tm_mday += year_lengths[isleap(li)];
1843 }
1844 while (yourtm.tm_mday > DAYSPERLYEAR) {
1845 li = y + (1 < yourtm.tm_mon);
1846 yourtm.tm_mday -= year_lengths[isleap(li)];
1847 if (long_increment_overflow(&y, 1))
1848 return WRONG;
1849 }
1850 for ( ; ; ) {
1851 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1852 if (yourtm.tm_mday <= i)
1853 break;
1854 yourtm.tm_mday -= i;
1855 if (++yourtm.tm_mon >= MONSPERYEAR) {
1856 yourtm.tm_mon = 0;
1857 if (long_increment_overflow(&y, 1))
1858 return WRONG;
1859 }
1860 }
1861 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1862 return WRONG;
1863 yourtm.tm_year = y;
1864 if (yourtm.tm_year != y)
1865 return WRONG;
1866 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1867 saved_seconds = 0;
1868 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1869 /*
1870 ** We can't set tm_sec to 0, because that might push the
1871 ** time below the minimum representable time.
1872 ** Set tm_sec to 59 instead.
1873 ** This assumes that the minimum representable time is
1874 ** not in the same minute that a leap second was deleted from,
1875 ** which is a safer assumption than using 58 would be.
1876 */
1877 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1878 return WRONG;
1879 saved_seconds = yourtm.tm_sec;
1880 yourtm.tm_sec = SECSPERMIN - 1;
1881 } else {
1882 saved_seconds = yourtm.tm_sec;
1883 yourtm.tm_sec = 0;
1884 }
1885 /*
1886 ** Do a binary search (this works whatever time_t's type is).
1887 */
1888 if (!TYPE_SIGNED(time_t)) {
1889 lo = 0;
1890 hi = lo - 1;
1891 } else if (!TYPE_INTEGRAL(time_t)) {
1892 if (sizeof(time_t) > sizeof(float))
1893 hi = (time_t) DBL_MAX;
1894 else hi = (time_t) FLT_MAX;
1895 lo = -hi;
1896 } else {
1897 lo = 1;
1898 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1899 lo *= 2;
1900 hi = -(lo + 1);
1901 }
1902 for ( ; ; ) {
1903 t = lo / 2 + hi / 2;
1904 if (t < lo)
1905 t = lo;
1906 else if (t > hi)
1907 t = hi;
1908 if ((*funcp)(&t, offset, &mytm) == NULL) {
1909 /*
1910 ** Assume that t is too extreme to be represented in
1911 ** a struct tm; arrange things so that it is less
1912 ** extreme on the next pass.
1913 */
1914 dir = (t > 0) ? 1 : -1;
1915 } else dir = tmcomp(&mytm, &yourtm);
1916 if (dir != 0) {
1917 if (t == lo) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001918 if (t == TIME_T_MAX)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001919 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001920 ++t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001921 ++lo;
1922 } else if (t == hi) {
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001923 if (t == TIME_T_MIN)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001924 return WRONG;
David 'Digit' Turner2093d352009-09-09 17:41:59 -07001925 --t;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001926 --hi;
1927 }
1928 if (lo > hi)
1929 return WRONG;
1930 if (dir > 0)
1931 hi = t;
1932 else lo = t;
1933 continue;
1934 }
1935 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1936 break;
1937 /*
1938 ** Right time, wrong type.
1939 ** Hunt for right time, right type.
1940 ** It's okay to guess wrong since the guess
1941 ** gets checked.
1942 */
1943 /*
1944 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1945 */
1946 sp = (const struct state *)
1947 (((void *) funcp == (void *) localsub) ?
1948 lclptr : gmtptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001949#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001950 if (sp == NULL)
1951 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001952#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001953 for (i = sp->typecnt - 1; i >= 0; --i) {
1954 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1955 continue;
1956 for (j = sp->typecnt - 1; j >= 0; --j) {
1957 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1958 continue;
1959 newt = t + sp->ttis[j].tt_gmtoff -
1960 sp->ttis[i].tt_gmtoff;
1961 if ((*funcp)(&newt, offset, &mytm) == NULL)
1962 continue;
1963 if (tmcomp(&mytm, &yourtm) != 0)
1964 continue;
1965 if (mytm.tm_isdst != yourtm.tm_isdst)
1966 continue;
1967 /*
1968 ** We have a match.
1969 */
1970 t = newt;
1971 goto label;
1972 }
1973 }
1974 return WRONG;
1975 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001976label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001977 newt = t + saved_seconds;
1978 if ((newt < t) != (saved_seconds < 0))
1979 return WRONG;
1980 t = newt;
1981 if ((*funcp)(&t, offset, tmp))
1982 *okayp = TRUE;
1983 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984}
1985
1986static time_t
1987time2(tmp, funcp, offset, okayp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001988struct tm * const tmp;
1989struct tm * (* const funcp) P((const time_t*, long, struct tm*));
1990const long offset;
1991int * const okayp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001992{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001993 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001994
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001995 /*
1996 ** First try without normalization of seconds
1997 ** (in case tm_sec contains a value associated with a leap second).
1998 ** If that fails, try with normalization of seconds.
1999 */
2000 t = time2sub(tmp, funcp, offset, okayp, FALSE);
2001 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002002}
2003
2004static time_t
2005time1(tmp, funcp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002006struct tm * const tmp;
2007struct tm * (* const funcp) P((const time_t *, long, struct tm *));
2008const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002009{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002010 register time_t t;
2011 register const struct state * sp;
2012 register int samei, otheri;
2013 register int sameind, otherind;
2014 register int i;
2015 register int nseen;
2016 int seen[TZ_MAX_TYPES];
2017 int types[TZ_MAX_TYPES];
2018 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002019
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002020 if (tmp->tm_isdst > 1)
2021 tmp->tm_isdst = 1;
2022 t = time2(tmp, funcp, offset, &okay);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002023#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002024 /*
2025 ** PCTS code courtesy Grant Sullivan.
2026 */
2027 if (okay)
2028 return t;
2029 if (tmp->tm_isdst < 0)
2030 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002031#endif /* defined PCTS */
2032#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002033 if (okay || tmp->tm_isdst < 0)
2034 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002035#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002036 /*
2037 ** We're supposed to assume that somebody took a time of one type
2038 ** and did some math on it that yielded a "struct tm" that's bad.
2039 ** We try to divine the type they started from and adjust to the
2040 ** type they need.
2041 */
2042 /*
2043 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
2044 */
2045 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
2046 lclptr : gmtptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002047#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002048 if (sp == NULL)
2049 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002050#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002051 for (i = 0; i < sp->typecnt; ++i)
2052 seen[i] = FALSE;
2053 nseen = 0;
2054 for (i = sp->timecnt - 1; i >= 0; --i)
2055 if (!seen[sp->types[i]]) {
2056 seen[sp->types[i]] = TRUE;
2057 types[nseen++] = sp->types[i];
2058 }
2059 for (sameind = 0; sameind < nseen; ++sameind) {
2060 samei = types[sameind];
2061 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2062 continue;
2063 for (otherind = 0; otherind < nseen; ++otherind) {
2064 otheri = types[otherind];
2065 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2066 continue;
2067 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2068 sp->ttis[samei].tt_gmtoff;
2069 tmp->tm_isdst = !tmp->tm_isdst;
2070 t = time2(tmp, funcp, offset, &okay);
2071 if (okay)
2072 return t;
2073 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2074 sp->ttis[samei].tt_gmtoff;
2075 tmp->tm_isdst = !tmp->tm_isdst;
2076 }
2077 }
2078 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002079}
2080
2081time_t
2082mktime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002083struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002084{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002085 time_t result;
2086 _tzLock();
2087 tzset_locked();
2088 result = time1(tmp, localsub, 0L);
2089 _tzUnlock();
2090 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002091}
2092
2093#ifdef STD_INSPIRED
2094
2095time_t
2096timelocal(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002097struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002098{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002099 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2100 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002101}
2102
2103time_t
2104timegm(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002105struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002106{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002107 time_t result;
2108
2109 tmp->tm_isdst = 0;
2110 _tzLock();
2111 result = time1(tmp, gmtsub, 0L);
2112 _tzUnlock();
2113
2114 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002115}
2116
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002117#if 0 /* disable due to lack of clear documentation on this function */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002118time_t
2119timeoff(tmp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002120struct tm * const tmp;
2121const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002122{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002123 time_t result;
2124
2125 tmp->tm_isdst = 0;
2126 _tzLock();
2127 result = time1(tmp, gmtsub, offset);
2128 _tzUnlock();
2129
2130 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002131}
David 'Digit' Turner6481b912010-12-06 12:23:16 +01002132#endif /* 0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002133
2134#endif /* defined STD_INSPIRED */
2135
2136#ifdef CMUCS
2137
2138/*
2139** The following is supplied for compatibility with
2140** previous versions of the CMUCS runtime library.
2141*/
2142
2143long
2144gtime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002145struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002146{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002147 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002148
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002149 if (t == WRONG)
2150 return -1;
2151 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002152}
2153
2154#endif /* defined CMUCS */
2155
2156/*
2157** XXX--is the below the right way to conditionalize??
2158*/
2159
2160#ifdef STD_INSPIRED
2161
2162/*
2163** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2164** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2165** is not the case if we are accounting for leap seconds.
2166** So, we provide the following conversion routines for use
2167** when exchanging timestamps with POSIX conforming systems.
2168*/
2169
2170static long
2171leapcorr(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002172time_t * timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002173{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002174 register struct state * sp;
2175 register struct lsinfo * lp;
2176 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002177
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002178 sp = lclptr;
2179 i = sp->leapcnt;
2180 while (--i >= 0) {
2181 lp = &sp->lsis[i];
2182 if (*timep >= lp->ls_trans)
2183 return lp->ls_corr;
2184 }
2185 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002186}
2187
2188time_t
2189time2posix(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002190time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002191{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002192 tzset();
2193 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002194}
2195
2196time_t
2197posix2time(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002198time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002199{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002200 time_t x;
2201 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002202
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002203 tzset();
2204 /*
2205 ** For a positive leap second hit, the result
2206 ** is not unique. For a negative leap second
2207 ** hit, the corresponding time doesn't exist,
2208 ** so we return an adjacent second.
2209 */
2210 x = t + leapcorr(&t);
2211 y = x - leapcorr(&x);
2212 if (y < t) {
2213 do {
2214 x++;
2215 y = x - leapcorr(&x);
2216 } while (y < t);
2217 if (t != y)
2218 return x - 1;
2219 } else if (y > t) {
2220 do {
2221 --x;
2222 y = x - leapcorr(&x);
2223 } while (y > t);
2224 if (t != y)
2225 return x + 1;
2226 }
2227 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002228}
2229
2230#endif /* defined STD_INSPIRED */