blob: 9c5f2188ad365d4c7cecc133cb640fba38248481 [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
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070063/* THREAD-SAFETY SUPPORT GOES HERE */
64static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER;
65
66static __inline__ void _tzLock(void)
67{
68 if (__isthreaded)
69 pthread_mutex_lock(&_tzMutex);
70}
71
72static __inline__ void _tzUnlock(void)
73{
74 if (__isthreaded)
75 pthread_mutex_unlock(&_tzMutex);
76}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
78#ifndef WILDABBR
79/*
80** Someone might make incorrect use of a time zone abbreviation:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070081** 1. They might reference tzname[0] before calling tzset (explicitly
82** or implicitly).
83** 2. They might reference tzname[1] before calling tzset (explicitly
84** or implicitly).
85** 3. They might reference tzname[1] after setting to a time zone
86** in which Daylight Saving Time is never observed.
87** 4. They might reference tzname[0] after setting to a time zone
88** in which Standard Time is never observed.
89** 5. They might reference tm.TM_ZONE after calling offtime.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090** What's best to do in the above cases is open to debate;
91** for now, we just set things up so that in any of the five cases
92** WILDABBR is used. Another possibility: initialize tzname[0] to the
93** string "tzname[0] used before set", and similarly for the other cases.
94** And another: initialize tzname[0] to "ERA", with an explanation in the
95** manual page of what this "time zone abbreviation" means (doing this so
96** that tzname[0] has the "normal" length of three characters).
97*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070098#define WILDABBR " "
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif /* !defined WILDABBR */
100
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700101static char wildabbr[] = WILDABBR;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700103static const char gmt[] = "GMT";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
105/*
106** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
107** We default to US rules as of 1999-08-17.
108** POSIX 1003.1 section 8.1.1 says that the default DST rules are
109** implementation dependent; for historical reasons, US rules are a
110** common default.
111*/
112#ifndef TZDEFRULESTRING
113#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
114#endif /* !defined TZDEFDST */
115
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700116struct ttinfo { /* time type information */
117 long tt_gmtoff; /* UTC offset in seconds */
118 int tt_isdst; /* used to set tm_isdst */
119 int tt_abbrind; /* abbreviation list index */
120 int tt_ttisstd; /* TRUE if transition is std time */
121 int tt_ttisgmt; /* TRUE if transition is UTC */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122};
123
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700124struct lsinfo { /* leap second information */
125 time_t ls_trans; /* transition time */
126 long ls_corr; /* correction to apply */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127};
128
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700129#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
131#ifdef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700132#define MY_TZNAME_MAX TZNAME_MAX
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133#endif /* defined TZNAME_MAX */
134#ifndef TZNAME_MAX
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700135#define MY_TZNAME_MAX 255
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136#endif /* !defined TZNAME_MAX */
137
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700138/* XXX: This code should really use time64_t instead of time_t
139 * but we can't change it without re-generating the index
140 * file first with the correct data.
141 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142struct state {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700143 int leapcnt;
144 int timecnt;
145 int typecnt;
146 int charcnt;
147 int goback;
148 int goahead;
149 time_t ats[TZ_MAX_TIMES];
150 unsigned char types[TZ_MAX_TIMES];
151 struct ttinfo ttis[TZ_MAX_TYPES];
152 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
153 (2 * (MY_TZNAME_MAX + 1)))];
154 struct lsinfo lsis[TZ_MAX_LEAPS];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155};
156
157struct rule {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700158 int r_type; /* type of rule--see below */
159 int r_day; /* day number of rule */
160 int r_week; /* week number of rule */
161 int r_mon; /* month number of rule */
162 long r_time; /* transition time of rule */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163};
164
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700165#define JULIAN_DAY 0 /* Jn - Julian day */
166#define DAY_OF_YEAR 1 /* n - day of year */
167#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 -0800168
169/*
170** Prototypes for static functions.
171*/
172
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700173/* NOTE: all internal functions assume that _tzLock() was already called */
174
175static long detzcode P((const char * codep));
176static time_t detzcode64 P((const char * codep));
177static int differ_by_repeat P((time_t t1, time_t t0));
178static const char * getzname P((const char * strp));
179static const char * getqzname P((const char * strp, const int delim));
180static const char * getnum P((const char * strp, int * nump, int min,
181 int max));
182static const char * getsecs P((const char * strp, long * secsp));
183static const char * getoffset P((const char * strp, long * offsetp));
184static const char * getrule P((const char * strp, struct rule * rulep));
185static void gmtload P((struct state * sp));
186static struct tm * gmtsub P((const time_t * timep, long offset,
187 struct tm * tmp));
188static struct tm * localsub P((const time_t * timep, long offset,
189 struct tm * tmp));
190static int increment_overflow P((int * number, int delta));
191static int leaps_thru_end_of P((int y));
192static int long_increment_overflow P((long * number, int delta));
193static int long_normalize_overflow P((long * tensptr,
194 int * unitsptr, int base));
195static int normalize_overflow P((int * tensptr, int * unitsptr,
196 int base));
197static void settzname P((void));
198static time_t time1 P((struct tm * tmp,
199 struct tm * (*funcp) P((const time_t *,
200 long, struct tm *)),
201 long offset));
202static time_t time2 P((struct tm *tmp,
203 struct tm * (*funcp) P((const time_t *,
204 long, struct tm*)),
205 long offset, int * okayp));
206static time_t time2sub P((struct tm *tmp,
207 struct tm * (*funcp) P((const time_t *,
208 long, struct tm*)),
209 long offset, int * okayp, int do_norm_secs));
210static struct tm * timesub P((const time_t * timep, long offset,
211 const struct state * sp, struct tm * tmp));
212static int tmcomp P((const struct tm * atmp,
213 const struct tm * btmp));
214static time_t transtime P((time_t janfirst, int year,
215 const struct rule * rulep, long offset));
216static int tzload P((const char * name, struct state * sp,
217 int doextend));
218static int tzparse P((const char * name, struct state * sp,
219 int lastditch));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
221#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700222static struct state * lclptr;
223static struct state * gmtptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224#endif /* defined ALL_STATE */
225
226#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700227static struct state lclmem;
228static struct state gmtmem;
229#define lclptr (&lclmem)
230#define gmtptr (&gmtmem)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231#endif /* State Farm */
232
233#ifndef TZ_STRLEN_MAX
234#define TZ_STRLEN_MAX 255
235#endif /* !defined TZ_STRLEN_MAX */
236
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700237static char lcl_TZname[TZ_STRLEN_MAX + 1];
238static int lcl_is_set;
239static int gmt_is_set;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700241char * tzname[2] = {
242 wildabbr,
243 wildabbr
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244};
245
246/*
247** Section 4.12.3 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700248** Except for the strftime function, these functions [asctime,
249** ctime, gmtime, localtime] return values in one of two static
250** objects: a broken-down time structure and an array of char.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800251** Thanks to Paul Eggert for noting this.
252*/
253
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700254static struct tm tmGlobal;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255
256#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700257time_t timezone = 0;
258int daylight = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259#endif /* defined USG_COMPAT */
260
261#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700262time_t altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263#endif /* defined ALTZONE */
264
265static long
266detzcode(codep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700267const char * const codep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700269 register long result;
270 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700272 result = (codep[0] & 0x80) ? ~0L : 0;
273 for (i = 0; i < 4; ++i)
274 result = (result << 8) | (codep[i] & 0xff);
275 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276}
277
278static time_t
279detzcode64(codep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700280const char * const codep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700282 register time_t result;
283 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700285 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
286 for (i = 0; i < 8; ++i)
287 result = result * 256 + (codep[i] & 0xff);
288 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289}
290
291static void
292settzname P((void))
293{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700294 register struct state * const sp = lclptr;
295 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700297 tzname[0] = wildabbr;
298 tzname[1] = wildabbr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700300 daylight = 0;
301 timezone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302#endif /* defined USG_COMPAT */
303#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700304 altzone = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305#endif /* defined ALTZONE */
306#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700307 if (sp == NULL) {
308 tzname[0] = tzname[1] = gmt;
309 return;
310 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700312 for (i = 0; i < sp->typecnt; ++i) {
313 register const struct ttinfo * const ttisp = &sp->ttis[i];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700315 tzname[ttisp->tt_isdst] =
316 &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700318 if (ttisp->tt_isdst)
319 daylight = 1;
320 if (i == 0 || !ttisp->tt_isdst)
321 timezone = -(ttisp->tt_gmtoff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322#endif /* defined USG_COMPAT */
323#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700324 if (i == 0 || ttisp->tt_isdst)
325 altzone = -(ttisp->tt_gmtoff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326#endif /* defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700327 }
328 /*
329 ** And to get the latest zone names into tzname. . .
330 */
331 for (i = 0; i < sp->timecnt; ++i) {
332 register const struct ttinfo * const ttisp =
333 &sp->ttis[
334 sp->types[i]];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700336 tzname[ttisp->tt_isdst] =
337 &sp->chars[ttisp->tt_abbrind];
338 }
339 /*
340 ** Finally, scrub the abbreviations.
341 ** First, replace bogus characters.
342 */
343 for (i = 0; i < sp->charcnt; ++i)
344 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
345 sp->chars[i] = TZ_ABBR_ERR_CHAR;
346 /*
347 ** Second, truncate long abbreviations.
348 */
349 for (i = 0; i < sp->typecnt; ++i) {
350 register const struct ttinfo * const ttisp = &sp->ttis[i];
351 register char * cp = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700353 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
354 strcmp(cp, GRANDPARENTED) != 0)
355 *(cp + TZ_ABBR_MAX_LEN) = '\0';
356 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357}
358
359static int
360differ_by_repeat(t1, t0)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700361const time_t t1;
362const time_t t0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700364 if (TYPE_INTEGRAL(time_t) &&
365 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
366 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367 return (t1 - t0) == SECSPERREPEAT;
368}
369
370static int toint(unsigned char *s) {
371 return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
372}
373
374static int
375tzload(name, sp, doextend)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700376register const char * name;
377register struct state * const sp;
378register const int doextend;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700380 register const char * p;
381 register int i;
382 register int fid;
383 register int stored;
384 register int nread;
385 union {
386 struct tzhead tzhead;
387 char buf[2 * sizeof(struct tzhead) +
388 2 * sizeof *sp +
389 4 * TZ_MAX_TIMES];
390 } u;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391 int toread = sizeof u.buf;
392
393 if (name == NULL && (name = TZDEFAULT) == NULL) {
394 XLOG(("tzload: null 'name' parameter\n" ));
395 return -1;
396 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700397 {
398 register int doaccess;
399 /*
400 ** Section 4.9.1 of the C standard says that
401 ** "FILENAME_MAX expands to an integral constant expression
402 ** that is the size needed for an array of char large enough
403 ** to hold the longest file name string that the implementation
404 ** guarantees can be opened."
405 */
406 char fullname[FILENAME_MAX + 1];
407 char *origname = (char*) name;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700409 if (name[0] == ':')
410 ++name;
411 doaccess = name[0] == '/';
412 if (!doaccess) {
413 if ((p = TZDIR) == NULL) {
414 XLOG(("tzload: null TZDIR macro ?\n" ));
415 return -1;
416 }
417 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) {
418 XLOG(( "tzload: path too long: %s/%s\n", p, name ));
419 return -1;
420 }
421 (void) strcpy(fullname, p);
422 (void) strcat(fullname, "/");
423 (void) strcat(fullname, name);
424 /*
425 ** Set doaccess if '.' (as in "../") shows up in name.
426 */
427 if (strchr(name, '.') != NULL)
428 doaccess = TRUE;
429 name = fullname;
430 }
431 if (doaccess && access(name, R_OK) != 0) {
432 XLOG(( "tzload: could not find '%s'\n", name ));
433 return -1;
434 }
435 if ((fid = open(name, OPEN_MODE)) == -1) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436 char buf[READLEN];
437 char name[NAMELEN + 1];
438 int fidix = open(INDEXFILE, OPEN_MODE);
439 int off = -1;
440
441 XLOG(( "tzload: could not open '%s', trying '%s'\n", fullname, INDEXFILE ));
442 if (fidix < 0) {
443 XLOG(( "tzload: could not find '%s'\n", INDEXFILE ));
444 return -1;
445 }
446
447 while (read(fidix, buf, sizeof(buf)) == sizeof(buf)) {
448 memcpy(name, buf, NAMELEN);
449 name[NAMELEN] = '\0';
450
451 if (strcmp(name, origname) == 0) {
452 off = toint((unsigned char *) buf + NAMELEN);
453 toread = toint((unsigned char *) buf + NAMELEN + INTLEN);
454 break;
455 }
456 }
457
458 close(fidix);
459
460 if (off < 0) {
461 XLOG(( "tzload: invalid offset (%d)\n", off ));
462 return -1;
463 }
464
465 fid = open(DATAFILE, OPEN_MODE);
466
467 if (fid < 0) {
468 XLOG(( "tzload: could not open '%s'\n", DATAFILE ));
469 return -1;
470 }
471
472 if (lseek(fid, off, SEEK_SET) < 0) {
473 XLOG(( "tzload: could not seek to %d in '%s'\n", off, DATAFILE ));
474 return -1;
475 }
476 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700477 }
478 nread = read(fid, u.buf, toread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800479 if (close(fid) < 0 || nread <= 0) {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700480 XLOG(( "tzload: could not read content of '%s'\n", DATAFILE ));
481 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700483 for (stored = 4; stored <= 8; stored *= 2) {
484 int ttisstdcnt;
485 int ttisgmtcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800486
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700487 ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
488 ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
489 sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
490 sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
491 sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
492 sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
493 p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
494 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
495 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
496 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
497 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
498 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
499 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
500 return -1;
501 if (nread - (p - u.buf) <
502 sp->timecnt * stored + /* ats */
503 sp->timecnt + /* types */
504 sp->typecnt * 6 + /* ttinfos */
505 sp->charcnt + /* chars */
506 sp->leapcnt * (stored + 4) + /* lsinfos */
507 ttisstdcnt + /* ttisstds */
508 ttisgmtcnt) /* ttisgmts */
509 return -1;
510 for (i = 0; i < sp->timecnt; ++i) {
511 sp->ats[i] = (stored == 4) ?
512 detzcode(p) : detzcode64(p);
513 p += stored;
514 }
515 for (i = 0; i < sp->timecnt; ++i) {
516 sp->types[i] = (unsigned char) *p++;
517 if (sp->types[i] >= sp->typecnt)
518 return -1;
519 }
520 for (i = 0; i < sp->typecnt; ++i) {
521 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700523 ttisp = &sp->ttis[i];
524 ttisp->tt_gmtoff = detzcode(p);
525 p += 4;
526 ttisp->tt_isdst = (unsigned char) *p++;
527 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
528 return -1;
529 ttisp->tt_abbrind = (unsigned char) *p++;
530 if (ttisp->tt_abbrind < 0 ||
531 ttisp->tt_abbrind > sp->charcnt)
532 return -1;
533 }
534 for (i = 0; i < sp->charcnt; ++i)
535 sp->chars[i] = *p++;
536 sp->chars[i] = '\0'; /* ensure '\0' at end */
537 for (i = 0; i < sp->leapcnt; ++i) {
538 register struct lsinfo * lsisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800539
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700540 lsisp = &sp->lsis[i];
541 lsisp->ls_trans = (stored == 4) ?
542 detzcode(p) : detzcode64(p);
543 p += stored;
544 lsisp->ls_corr = detzcode(p);
545 p += 4;
546 }
547 for (i = 0; i < sp->typecnt; ++i) {
548 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700550 ttisp = &sp->ttis[i];
551 if (ttisstdcnt == 0)
552 ttisp->tt_ttisstd = FALSE;
553 else {
554 ttisp->tt_ttisstd = *p++;
555 if (ttisp->tt_ttisstd != TRUE &&
556 ttisp->tt_ttisstd != FALSE)
557 return -1;
558 }
559 }
560 for (i = 0; i < sp->typecnt; ++i) {
561 register struct ttinfo * ttisp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700563 ttisp = &sp->ttis[i];
564 if (ttisgmtcnt == 0)
565 ttisp->tt_ttisgmt = FALSE;
566 else {
567 ttisp->tt_ttisgmt = *p++;
568 if (ttisp->tt_ttisgmt != TRUE &&
569 ttisp->tt_ttisgmt != FALSE)
570 return -1;
571 }
572 }
573 /*
574 ** Out-of-sort ats should mean we're running on a
575 ** signed time_t system but using a data file with
576 ** unsigned values (or vice versa).
577 */
578 for (i = 0; i < sp->timecnt - 2; ++i)
579 if (sp->ats[i] > sp->ats[i + 1]) {
580 ++i;
581 if (TYPE_SIGNED(time_t)) {
582 /*
583 ** Ignore the end (easy).
584 */
585 sp->timecnt = i;
586 } else {
587 /*
588 ** Ignore the beginning (harder).
589 */
590 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800591
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700592 for (j = 0; j + i < sp->timecnt; ++j) {
593 sp->ats[j] = sp->ats[j + i];
594 sp->types[j] = sp->types[j + i];
595 }
596 sp->timecnt = j;
597 }
598 break;
599 }
600 /*
601 ** If this is an old file, we're done.
602 */
603 if (u.tzhead.tzh_version[0] == '\0')
604 break;
605 nread -= p - u.buf;
606 for (i = 0; i < nread; ++i)
607 u.buf[i] = p[i];
608 /*
609 ** If this is a narrow integer time_t system, we're done.
610 */
611 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
612 break;
613 }
614 if (doextend && nread > 2 &&
615 u.buf[0] == '\n' && u.buf[nread - 1] == '\n' &&
616 sp->typecnt + 2 <= TZ_MAX_TYPES) {
617 struct state ts;
618 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800619
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700620 u.buf[nread - 1] = '\0';
621 result = tzparse(&u.buf[1], &ts, FALSE);
622 if (result == 0 && ts.typecnt == 2 &&
623 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
624 for (i = 0; i < 2; ++i)
625 ts.ttis[i].tt_abbrind +=
626 sp->charcnt;
627 for (i = 0; i < ts.charcnt; ++i)
628 sp->chars[sp->charcnt++] =
629 ts.chars[i];
630 i = 0;
631 while (i < ts.timecnt &&
632 ts.ats[i] <=
633 sp->ats[sp->timecnt - 1])
634 ++i;
635 while (i < ts.timecnt &&
636 sp->timecnt < TZ_MAX_TIMES) {
637 sp->ats[sp->timecnt] =
638 ts.ats[i];
639 sp->types[sp->timecnt] =
640 sp->typecnt +
641 ts.types[i];
642 ++sp->timecnt;
643 ++i;
644 }
645 sp->ttis[sp->typecnt++] = ts.ttis[0];
646 sp->ttis[sp->typecnt++] = ts.ttis[1];
647 }
648 }
649 i = 2 * YEARSPERREPEAT;
650 sp->goback = sp->goahead = sp->timecnt > i;
651 sp->goback &= sp->types[i] == sp->types[0] &&
652 differ_by_repeat(sp->ats[i], sp->ats[0]);
653 sp->goahead &=
654 sp->types[sp->timecnt - 1] == sp->types[sp->timecnt - 1 - i] &&
655 differ_by_repeat(sp->ats[sp->timecnt - 1],
656 sp->ats[sp->timecnt - 1 - i]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657 XLOG(( "tzload: load ok !!\n" ));
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700658 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659}
660
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700661static const int mon_lengths[2][MONSPERYEAR] = {
662 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
663 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800664};
665
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700666static const int year_lengths[2] = {
667 DAYSPERNYEAR, DAYSPERLYEAR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668};
669
670/*
671** Given a pointer into a time zone string, scan until a character that is not
672** a valid character in a zone name is found. Return a pointer to that
673** character.
674*/
675
676static const char *
677getzname(strp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700678register const char * strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800679{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700680 register char c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700682 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
683 c != '+')
684 ++strp;
685 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686}
687
688/*
689** Given a pointer into an extended time zone string, scan until the ending
690** delimiter of the zone name is located. Return a pointer to the delimiter.
691**
692** As with getzname above, the legal character set is actually quite
693** restricted, with other characters producing undefined results.
694** We don't do any checking here; checking is done later in common-case code.
695*/
696
697static const char *
698getqzname(register const char *strp, const int delim)
699{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700700 register int c;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700702 while ((c = *strp) != '\0' && c != delim)
703 ++strp;
704 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705}
706
707/*
708** Given a pointer into a time zone string, extract a number from that string.
709** Check that the number is within a specified range; if it is not, return
710** NULL.
711** Otherwise, return a pointer to the first character not part of the number.
712*/
713
714static const char *
715getnum(strp, nump, min, max)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700716register const char * strp;
717int * const nump;
718const int min;
719const int max;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700721 register char c;
722 register int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800723
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700724 if (strp == NULL || !is_digit(c = *strp))
725 return NULL;
726 num = 0;
727 do {
728 num = num * 10 + (c - '0');
729 if (num > max)
730 return NULL; /* illegal value */
731 c = *++strp;
732 } while (is_digit(c));
733 if (num < min)
734 return NULL; /* illegal value */
735 *nump = num;
736 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737}
738
739/*
740** Given a pointer into a time zone string, extract a number of seconds,
741** in hh[:mm[:ss]] form, from the string.
742** If any error occurs, return NULL.
743** Otherwise, return a pointer to the first character not part of the number
744** of seconds.
745*/
746
747static const char *
748getsecs(strp, secsp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700749register const char * strp;
750long * const secsp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700752 int num;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700754 /*
755 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
756 ** "M10.4.6/26", which does not conform to Posix,
757 ** but which specifies the equivalent of
758 ** ``02:00 on the first Sunday on or after 23 Oct''.
759 */
760 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
761 if (strp == NULL)
762 return NULL;
763 *secsp = num * (long) SECSPERHOUR;
764 if (*strp == ':') {
765 ++strp;
766 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
767 if (strp == NULL)
768 return NULL;
769 *secsp += num * SECSPERMIN;
770 if (*strp == ':') {
771 ++strp;
772 /* `SECSPERMIN' allows for leap seconds. */
773 strp = getnum(strp, &num, 0, SECSPERMIN);
774 if (strp == NULL)
775 return NULL;
776 *secsp += num;
777 }
778 }
779 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780}
781
782/*
783** Given a pointer into a time zone string, extract an offset, in
784** [+-]hh[:mm[:ss]] form, from the string.
785** If any error occurs, return NULL.
786** Otherwise, return a pointer to the first character not part of the time.
787*/
788
789static const char *
790getoffset(strp, offsetp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700791register const char * strp;
792long * const offsetp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700794 register int neg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800795
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700796 if (*strp == '-') {
797 neg = 1;
798 ++strp;
799 } else if (*strp == '+')
800 ++strp;
801 strp = getsecs(strp, offsetp);
802 if (strp == NULL)
803 return NULL; /* illegal time */
804 if (neg)
805 *offsetp = -*offsetp;
806 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800807}
808
809/*
810** Given a pointer into a time zone string, extract a rule in the form
811** date[/time]. See POSIX section 8 for the format of "date" and "time".
812** If a valid rule is not found, return NULL.
813** Otherwise, return a pointer to the first character not part of the rule.
814*/
815
816static const char *
817getrule(strp, rulep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700818const char * strp;
819register struct rule * const rulep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800820{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700821 if (*strp == 'J') {
822 /*
823 ** Julian day.
824 */
825 rulep->r_type = JULIAN_DAY;
826 ++strp;
827 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
828 } else if (*strp == 'M') {
829 /*
830 ** Month, week, day.
831 */
832 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
833 ++strp;
834 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
835 if (strp == NULL)
836 return NULL;
837 if (*strp++ != '.')
838 return NULL;
839 strp = getnum(strp, &rulep->r_week, 1, 5);
840 if (strp == NULL)
841 return NULL;
842 if (*strp++ != '.')
843 return NULL;
844 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
845 } else if (is_digit(*strp)) {
846 /*
847 ** Day of year.
848 */
849 rulep->r_type = DAY_OF_YEAR;
850 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
851 } else return NULL; /* invalid format */
852 if (strp == NULL)
853 return NULL;
854 if (*strp == '/') {
855 /*
856 ** Time specified.
857 */
858 ++strp;
859 strp = getsecs(strp, &rulep->r_time);
860 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
861 return strp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800862}
863
864/*
865** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
866** year, a rule, and the offset from UTC at the time that rule takes effect,
867** calculate the Epoch-relative time that rule takes effect.
868*/
869
870static time_t
871transtime(janfirst, year, rulep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700872const time_t janfirst;
873const int year;
874register const struct rule * const rulep;
875const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700877 register int leapyear;
878 register time_t value;
879 register int i;
880 int d, m1, yy0, yy1, yy2, dow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800881
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700882 INITIALIZE(value);
883 leapyear = isleap(year);
884 switch (rulep->r_type) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800885
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700886 case JULIAN_DAY:
887 /*
888 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
889 ** years.
890 ** In non-leap years, or if the day number is 59 or less, just
891 ** add SECSPERDAY times the day number-1 to the time of
892 ** January 1, midnight, to get the day.
893 */
894 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
895 if (leapyear && rulep->r_day >= 60)
896 value += SECSPERDAY;
897 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800898
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700899 case DAY_OF_YEAR:
900 /*
901 ** n - day of year.
902 ** Just add SECSPERDAY times the day number to the time of
903 ** January 1, midnight, to get the day.
904 */
905 value = janfirst + rulep->r_day * SECSPERDAY;
906 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800907
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700908 case MONTH_NTH_DAY_OF_WEEK:
909 /*
910 ** Mm.n.d - nth "dth day" of month m.
911 */
912 value = janfirst;
913 for (i = 0; i < rulep->r_mon - 1; ++i)
914 value += mon_lengths[leapyear][i] * SECSPERDAY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700916 /*
917 ** Use Zeller's Congruence to get day-of-week of first day of
918 ** month.
919 */
920 m1 = (rulep->r_mon + 9) % 12 + 1;
921 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
922 yy1 = yy0 / 100;
923 yy2 = yy0 % 100;
924 dow = ((26 * m1 - 2) / 10 +
925 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
926 if (dow < 0)
927 dow += DAYSPERWEEK;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800928
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700929 /*
930 ** "dow" is the day-of-week of the first day of the month. Get
931 ** the day-of-month (zero-origin) of the first "dow" day of the
932 ** month.
933 */
934 d = rulep->r_day - dow;
935 if (d < 0)
936 d += DAYSPERWEEK;
937 for (i = 1; i < rulep->r_week; ++i) {
938 if (d + DAYSPERWEEK >=
939 mon_lengths[leapyear][rulep->r_mon - 1])
940 break;
941 d += DAYSPERWEEK;
942 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700944 /*
945 ** "d" is the day-of-month (zero-origin) of the day we want.
946 */
947 value += d * SECSPERDAY;
948 break;
949 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800950
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700951 /*
952 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
953 ** question. To get the Epoch-relative time of the specified local
954 ** time on that day, add the transition time and the current offset
955 ** from UTC.
956 */
957 return value + rulep->r_time + offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800958}
959
960/*
961** Given a POSIX section 8-style TZ string, fill in the rule tables as
962** appropriate.
963*/
964
965static int
966tzparse(name, sp, lastditch)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700967const char * name;
968register struct state * const sp;
969const int lastditch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800970{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700971 const char * stdname;
972 const char * dstname;
973 size_t stdlen;
974 size_t dstlen;
975 long stdoffset;
976 long dstoffset;
977 register time_t * atp;
978 register unsigned char * typep;
979 register char * cp;
980 register int load_result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800981
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700982 INITIALIZE(dstname);
983 stdname = name;
984 if (lastditch) {
985 stdlen = strlen(name); /* length of standard zone name */
986 name += stdlen;
987 if (stdlen >= sizeof sp->chars)
988 stdlen = (sizeof sp->chars) - 1;
989 stdoffset = 0;
990 } else {
991 if (*name == '<') {
992 name++;
993 stdname = name;
994 name = getqzname(name, '>');
995 if (*name != '>')
996 return (-1);
997 stdlen = name - stdname;
998 name++;
999 } else {
1000 name = getzname(name);
1001 stdlen = name - stdname;
1002 }
1003 if (*name == '\0')
1004 return -1;
1005 name = getoffset(name, &stdoffset);
1006 if (name == NULL)
1007 return -1;
1008 }
1009 load_result = tzload(TZDEFRULES, sp, FALSE);
1010 if (load_result != 0)
1011 sp->leapcnt = 0; /* so, we're off a little */
1012 sp->timecnt = 0;
1013 if (*name != '\0') {
1014 if (*name == '<') {
1015 dstname = ++name;
1016 name = getqzname(name, '>');
1017 if (*name != '>')
1018 return -1;
1019 dstlen = name - dstname;
1020 name++;
1021 } else {
1022 dstname = name;
1023 name = getzname(name);
1024 dstlen = name - dstname; /* length of DST zone name */
1025 }
1026 if (*name != '\0' && *name != ',' && *name != ';') {
1027 name = getoffset(name, &dstoffset);
1028 if (name == NULL)
1029 return -1;
1030 } else dstoffset = stdoffset - SECSPERHOUR;
1031 if (*name == '\0' && load_result != 0)
1032 name = TZDEFRULESTRING;
1033 if (*name == ',' || *name == ';') {
1034 struct rule start;
1035 struct rule end;
1036 register int year;
1037 register time_t janfirst;
1038 time_t starttime;
1039 time_t endtime;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001040
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001041 ++name;
1042 if ((name = getrule(name, &start)) == NULL)
1043 return -1;
1044 if (*name++ != ',')
1045 return -1;
1046 if ((name = getrule(name, &end)) == NULL)
1047 return -1;
1048 if (*name != '\0')
1049 return -1;
1050 sp->typecnt = 2; /* standard time and DST */
1051 /*
1052 ** Two transitions per year, from EPOCH_YEAR forward.
1053 */
1054 sp->ttis[0].tt_gmtoff = -dstoffset;
1055 sp->ttis[0].tt_isdst = 1;
1056 sp->ttis[0].tt_abbrind = stdlen + 1;
1057 sp->ttis[1].tt_gmtoff = -stdoffset;
1058 sp->ttis[1].tt_isdst = 0;
1059 sp->ttis[1].tt_abbrind = 0;
1060 atp = sp->ats;
1061 typep = sp->types;
1062 janfirst = 0;
1063 for (year = EPOCH_YEAR;
1064 sp->timecnt + 2 <= TZ_MAX_TIMES;
1065 ++year) {
1066 time_t newfirst;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001067
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001068 starttime = transtime(janfirst, year, &start,
1069 stdoffset);
1070 endtime = transtime(janfirst, year, &end,
1071 dstoffset);
1072 if (starttime > endtime) {
1073 *atp++ = endtime;
1074 *typep++ = 1; /* DST ends */
1075 *atp++ = starttime;
1076 *typep++ = 0; /* DST begins */
1077 } else {
1078 *atp++ = starttime;
1079 *typep++ = 0; /* DST begins */
1080 *atp++ = endtime;
1081 *typep++ = 1; /* DST ends */
1082 }
1083 sp->timecnt += 2;
1084 newfirst = janfirst;
1085 newfirst += year_lengths[isleap(year)] *
1086 SECSPERDAY;
1087 if (newfirst <= janfirst)
1088 break;
1089 janfirst = newfirst;
1090 }
1091 } else {
1092 register long theirstdoffset;
1093 register long theirdstoffset;
1094 register long theiroffset;
1095 register int isdst;
1096 register int i;
1097 register int j;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001098
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001099 if (*name != '\0')
1100 return -1;
1101 /*
1102 ** Initial values of theirstdoffset and theirdstoffset.
1103 */
1104 theirstdoffset = 0;
1105 for (i = 0; i < sp->timecnt; ++i) {
1106 j = sp->types[i];
1107 if (!sp->ttis[j].tt_isdst) {
1108 theirstdoffset =
1109 -sp->ttis[j].tt_gmtoff;
1110 break;
1111 }
1112 }
1113 theirdstoffset = 0;
1114 for (i = 0; i < sp->timecnt; ++i) {
1115 j = sp->types[i];
1116 if (sp->ttis[j].tt_isdst) {
1117 theirdstoffset =
1118 -sp->ttis[j].tt_gmtoff;
1119 break;
1120 }
1121 }
1122 /*
1123 ** Initially we're assumed to be in standard time.
1124 */
1125 isdst = FALSE;
1126 theiroffset = theirstdoffset;
1127 /*
1128 ** Now juggle transition times and types
1129 ** tracking offsets as you do.
1130 */
1131 for (i = 0; i < sp->timecnt; ++i) {
1132 j = sp->types[i];
1133 sp->types[i] = sp->ttis[j].tt_isdst;
1134 if (sp->ttis[j].tt_ttisgmt) {
1135 /* No adjustment to transition time */
1136 } else {
1137 /*
1138 ** If summer time is in effect, and the
1139 ** transition time was not specified as
1140 ** standard time, add the summer time
1141 ** offset to the transition time;
1142 ** otherwise, add the standard time
1143 ** offset to the transition time.
1144 */
1145 /*
1146 ** Transitions from DST to DDST
1147 ** will effectively disappear since
1148 ** POSIX provides for only one DST
1149 ** offset.
1150 */
1151 if (isdst && !sp->ttis[j].tt_ttisstd) {
1152 sp->ats[i] += dstoffset -
1153 theirdstoffset;
1154 } else {
1155 sp->ats[i] += stdoffset -
1156 theirstdoffset;
1157 }
1158 }
1159 theiroffset = -sp->ttis[j].tt_gmtoff;
1160 if (sp->ttis[j].tt_isdst)
1161 theirdstoffset = theiroffset;
1162 else theirstdoffset = theiroffset;
1163 }
1164 /*
1165 ** Finally, fill in ttis.
1166 ** ttisstd and ttisgmt need not be handled.
1167 */
1168 sp->ttis[0].tt_gmtoff = -stdoffset;
1169 sp->ttis[0].tt_isdst = FALSE;
1170 sp->ttis[0].tt_abbrind = 0;
1171 sp->ttis[1].tt_gmtoff = -dstoffset;
1172 sp->ttis[1].tt_isdst = TRUE;
1173 sp->ttis[1].tt_abbrind = stdlen + 1;
1174 sp->typecnt = 2;
1175 }
1176 } else {
1177 dstlen = 0;
1178 sp->typecnt = 1; /* only standard time */
1179 sp->timecnt = 0;
1180 sp->ttis[0].tt_gmtoff = -stdoffset;
1181 sp->ttis[0].tt_isdst = 0;
1182 sp->ttis[0].tt_abbrind = 0;
1183 }
1184 sp->charcnt = stdlen + 1;
1185 if (dstlen != 0)
1186 sp->charcnt += dstlen + 1;
1187 if ((size_t) sp->charcnt > sizeof sp->chars)
1188 return -1;
1189 cp = sp->chars;
1190 (void) strncpy(cp, stdname, stdlen);
1191 cp += stdlen;
1192 *cp++ = '\0';
1193 if (dstlen != 0) {
1194 (void) strncpy(cp, dstname, dstlen);
1195 *(cp + dstlen) = '\0';
1196 }
1197 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198}
1199
1200static void
1201gmtload(sp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001202struct state * const sp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001204 if (tzload(gmt, sp, TRUE) != 0)
1205 (void) tzparse(gmt, sp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001206}
1207
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001208static void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001209tzsetwall P((void))
1210{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001211 if (lcl_is_set < 0)
1212 return;
1213 lcl_is_set = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001214
1215#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001216 if (lclptr == NULL) {
1217 lclptr = (struct state *) malloc(sizeof *lclptr);
1218 if (lclptr == NULL) {
1219 settzname(); /* all we can do */
1220 return;
1221 }
1222 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001223#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001224 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1225 gmtload(lclptr);
1226 settzname();
1227}
1228
1229static void
1230tzset_locked P((void))
1231{
1232 register const char * name = NULL;
1233 static char buf[PROP_VALUE_MAX];
1234
1235 name = getenv("TZ");
1236
1237 // try the "persist.sys.timezone" system property first
1238 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0)
1239 name = buf;
1240
1241 if (name == NULL) {
1242 tzsetwall();
1243 return;
1244 }
1245
1246 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1247 return;
1248 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1249 if (lcl_is_set)
1250 (void) strcpy(lcl_TZname, name);
1251
1252#ifdef ALL_STATE
1253 if (lclptr == NULL) {
1254 lclptr = (struct state *) malloc(sizeof *lclptr);
1255 if (lclptr == NULL) {
1256 settzname(); /* all we can do */
1257 return;
1258 }
1259 }
1260#endif /* defined ALL_STATE */
1261 if (*name == '\0') {
1262 /*
1263 ** User wants it fast rather than right.
1264 */
1265 lclptr->leapcnt = 0; /* so, we're off a little */
1266 lclptr->timecnt = 0;
1267 lclptr->typecnt = 0;
1268 lclptr->ttis[0].tt_isdst = 0;
1269 lclptr->ttis[0].tt_gmtoff = 0;
1270 lclptr->ttis[0].tt_abbrind = 0;
1271 (void) strcpy(lclptr->chars, gmt);
1272 } else if (tzload(name, lclptr, TRUE) != 0)
1273 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1274 (void) gmtload(lclptr);
1275 settzname();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001276}
1277
1278void
1279tzset P((void))
1280{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001281 _tzLock();
1282 tzset_locked();
1283 _tzUnlock();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001284}
1285
1286/*
1287** The easy way to behave "as if no library function calls" localtime
1288** is to not call it--so we drop its guts into "localsub", which can be
1289** freely called. (And no, the PANS doesn't require the above behavior--
1290** but it *is* desirable.)
1291**
1292** The unused offset argument is for the benefit of mktime variants.
1293*/
1294
1295/*ARGSUSED*/
1296static struct tm *
1297localsub(timep, offset, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001298const time_t * const timep;
1299const long offset;
1300struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001301{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001302 register struct state * sp;
1303 register const struct ttinfo * ttisp;
1304 register int i;
1305 register struct tm * result;
1306 const time_t t = *timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001307
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001308 sp = lclptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001309#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001310 if (sp == NULL)
1311 return gmtsub(timep, offset, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001312#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001313 if ((sp->goback && t < sp->ats[0]) ||
1314 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1315 time_t newt = t;
1316 register time_t seconds;
1317 register time_t tcycles;
1318 register int_fast64_t icycles;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001319
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001320 if (t < sp->ats[0])
1321 seconds = sp->ats[0] - t;
1322 else seconds = t - sp->ats[sp->timecnt - 1];
1323 --seconds;
1324 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1325 ++tcycles;
1326 icycles = tcycles;
1327 if (tcycles - icycles >= 1 || icycles - tcycles >= 1)
1328 return NULL;
1329 seconds = icycles;
1330 seconds *= YEARSPERREPEAT;
1331 seconds *= AVGSECSPERYEAR;
1332 if (t < sp->ats[0])
1333 newt += seconds;
1334 else newt -= seconds;
1335 if (newt < sp->ats[0] ||
1336 newt > sp->ats[sp->timecnt - 1])
1337 return NULL; /* "cannot happen" */
1338 result = localsub(&newt, offset, tmp);
1339 if (result == tmp) {
1340 register time_t newy;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001341
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001342 newy = tmp->tm_year;
1343 if (t < sp->ats[0])
1344 newy -= icycles * YEARSPERREPEAT;
1345 else newy += icycles * YEARSPERREPEAT;
1346 tmp->tm_year = newy;
1347 if (tmp->tm_year != newy)
1348 return NULL;
1349 }
1350 return result;
1351 }
1352 if (sp->timecnt == 0 || t < sp->ats[0]) {
1353 i = 0;
1354 while (sp->ttis[i].tt_isdst)
1355 if (++i >= sp->typecnt) {
1356 i = 0;
1357 break;
1358 }
1359 } else {
1360 register int lo = 1;
1361 register int hi = sp->timecnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001362
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001363 while (lo < hi) {
1364 register int mid = (lo + hi) >> 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001366 if (t < sp->ats[mid])
1367 hi = mid;
1368 else lo = mid + 1;
1369 }
1370 i = (int) sp->types[lo - 1];
1371 }
1372 ttisp = &sp->ttis[i];
1373 /*
1374 ** To get (wrong) behavior that's compatible with System V Release 2.0
1375 ** you'd replace the statement below with
1376 ** t += ttisp->tt_gmtoff;
1377 ** timesub(&t, 0L, sp, tmp);
1378 */
1379 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1380 tmp->tm_isdst = ttisp->tt_isdst;
1381 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001382#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001383 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001384#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001385 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386}
1387
1388struct tm *
1389localtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001390const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001392 return localtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001393}
1394
1395/*
1396** Re-entrant version of localtime.
1397*/
1398
1399struct tm *
1400localtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001401const time_t * const timep;
1402struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001403{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001404 struct tm* result;
1405
1406 _tzLock();
1407 tzset_locked();
1408 result = localsub(timep, 0L, tmp);
1409 _tzUnlock();
1410
1411 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001412}
1413
1414/*
1415** gmtsub is to gmtime as localsub is to localtime.
1416*/
1417
1418static struct tm *
1419gmtsub(timep, offset, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001420const time_t * const timep;
1421const long offset;
1422struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001423{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001424 register struct tm * result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001425
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001426 if (!gmt_is_set) {
1427 gmt_is_set = TRUE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001428#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001429 gmtptr = (struct state *) malloc(sizeof *gmtptr);
1430 if (gmtptr != NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001431#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001432 gmtload(gmtptr);
1433 }
1434 result = timesub(timep, offset, gmtptr, tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001436 /*
1437 ** Could get fancy here and deliver something such as
1438 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1439 ** but this is no time for a treasure hunt.
1440 */
1441 if (offset != 0)
1442 tmp->TM_ZONE = wildabbr;
1443 else {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001444#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001445 if (gmtptr == NULL)
1446 tmp->TM_ZONE = gmt;
1447 else tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001448#endif /* defined ALL_STATE */
1449#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001450 tmp->TM_ZONE = gmtptr->chars;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001451#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001452 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001453#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001454 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001455}
1456
1457struct tm *
1458gmtime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001459const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001460{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001461 return gmtime_r(timep, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462}
1463
1464/*
1465* Re-entrant version of gmtime.
1466*/
1467
1468struct tm *
1469gmtime_r(timep, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001470const time_t * const timep;
1471struct tm * tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001472{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001473 struct tm* result;
1474
1475 _tzLock();
1476 result = gmtsub(timep, 0L, tmp);
1477 _tzUnlock();
1478
1479 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001480}
1481
1482#ifdef STD_INSPIRED
1483
1484struct tm *
1485offtime(timep, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001486const time_t * const timep;
1487const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001488{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001489 return gmtsub(timep, offset, &tmGlobal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001490}
1491
1492#endif /* defined STD_INSPIRED */
1493
1494/*
1495** Return the number of leap years through the end of the given year
1496** where, to make the math easy, the answer for year zero is defined as zero.
1497*/
1498
1499static int
1500leaps_thru_end_of(y)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001501register const int y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001502{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001503 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1504 -(leaps_thru_end_of(-(y + 1)) + 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001505}
1506
1507static struct tm *
1508timesub(timep, offset, sp, tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001509const time_t * const timep;
1510const long offset;
1511register const struct state * const sp;
1512register struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001513{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001514 register const struct lsinfo * lp;
1515 register time_t tdays;
1516 register int idays; /* unsigned would be so 2003 */
1517 register long rem;
1518 int y;
1519 register const int * ip;
1520 register long corr;
1521 register int hit;
1522 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001523
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001524 corr = 0;
1525 hit = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001526#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001527 i = (sp == NULL) ? 0 : sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001528#endif /* defined ALL_STATE */
1529#ifndef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001530 i = sp->leapcnt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001531#endif /* State Farm */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001532 while (--i >= 0) {
1533 lp = &sp->lsis[i];
1534 if (*timep >= lp->ls_trans) {
1535 if (*timep == lp->ls_trans) {
1536 hit = ((i == 0 && lp->ls_corr > 0) ||
1537 lp->ls_corr > sp->lsis[i - 1].ls_corr);
1538 if (hit)
1539 while (i > 0 &&
1540 sp->lsis[i].ls_trans ==
1541 sp->lsis[i - 1].ls_trans + 1 &&
1542 sp->lsis[i].ls_corr ==
1543 sp->lsis[i - 1].ls_corr + 1) {
1544 ++hit;
1545 --i;
1546 }
1547 }
1548 corr = lp->ls_corr;
1549 break;
1550 }
1551 }
1552 y = EPOCH_YEAR;
1553 tdays = *timep / SECSPERDAY;
1554 rem = *timep - tdays * SECSPERDAY;
1555 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
1556 int newy;
1557 register time_t tdelta;
1558 register int idelta;
1559 register int leapdays;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001560
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001561 tdelta = tdays / DAYSPERLYEAR;
1562 idelta = tdelta;
1563 if (tdelta - idelta >= 1 || idelta - tdelta >= 1)
1564 return NULL;
1565 if (idelta == 0)
1566 idelta = (tdays < 0) ? -1 : 1;
1567 newy = y;
1568 if (increment_overflow(&newy, idelta))
1569 return NULL;
1570 leapdays = leaps_thru_end_of(newy - 1) -
1571 leaps_thru_end_of(y - 1);
1572 tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
1573 tdays -= leapdays;
1574 y = newy;
1575 }
1576 {
1577 register long seconds;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001578
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001579 seconds = tdays * SECSPERDAY + 0.5;
1580 tdays = seconds / SECSPERDAY;
1581 rem += seconds - tdays * SECSPERDAY;
1582 }
1583 /*
1584 ** Given the range, we can now fearlessly cast...
1585 */
1586 idays = tdays;
1587 rem += offset - corr;
1588 while (rem < 0) {
1589 rem += SECSPERDAY;
1590 --idays;
1591 }
1592 while (rem >= SECSPERDAY) {
1593 rem -= SECSPERDAY;
1594 ++idays;
1595 }
1596 while (idays < 0) {
1597 if (increment_overflow(&y, -1))
1598 return NULL;
1599 idays += year_lengths[isleap(y)];
1600 }
1601 while (idays >= year_lengths[isleap(y)]) {
1602 idays -= year_lengths[isleap(y)];
1603 if (increment_overflow(&y, 1))
1604 return NULL;
1605 }
1606 tmp->tm_year = y;
1607 if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
1608 return NULL;
1609 tmp->tm_yday = idays;
1610 /*
1611 ** The "extra" mods below avoid overflow problems.
1612 */
1613 tmp->tm_wday = EPOCH_WDAY +
1614 ((y - EPOCH_YEAR) % DAYSPERWEEK) *
1615 (DAYSPERNYEAR % DAYSPERWEEK) +
1616 leaps_thru_end_of(y - 1) -
1617 leaps_thru_end_of(EPOCH_YEAR - 1) +
1618 idays;
1619 tmp->tm_wday %= DAYSPERWEEK;
1620 if (tmp->tm_wday < 0)
1621 tmp->tm_wday += DAYSPERWEEK;
1622 tmp->tm_hour = (int) (rem / SECSPERHOUR);
1623 rem %= SECSPERHOUR;
1624 tmp->tm_min = (int) (rem / SECSPERMIN);
1625 /*
1626 ** A positive leap second requires a special
1627 ** representation. This uses "... ??:59:60" et seq.
1628 */
1629 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1630 ip = mon_lengths[isleap(y)];
1631 for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
1632 idays -= ip[tmp->tm_mon];
1633 tmp->tm_mday = (int) (idays + 1);
1634 tmp->tm_isdst = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001635#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001636 tmp->TM_GMTOFF = offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001637#endif /* defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001638 return tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001639}
1640
1641char *
1642ctime(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001643const time_t * const timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001644{
1645/*
1646** Section 4.12.3.2 of X3.159-1989 requires that
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001647** The ctime function converts the calendar time pointed to by timer
1648** to local time in the form of a string. It is equivalent to
1649** asctime(localtime(timer))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001651 return asctime(localtime(timep));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001652}
1653
1654char *
1655ctime_r(timep, buf)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001656const time_t * const timep;
1657char * buf;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001658{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001659 struct tm mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001660
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001661 return asctime_r(localtime_r(timep, &mytm), buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001662}
1663
1664/*
1665** Adapted from code provided by Robert Elz, who writes:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001666** The "best" way to do mktime I think is based on an idea of Bob
1667** Kridle's (so its said...) from a long time ago.
1668** It does a binary search of the time_t space. Since time_t's are
1669** just 32 bits, its a max of 32 iterations (even at 64 bits it
1670** would still be very reasonable).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001671*/
1672
1673#ifndef WRONG
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001674#define WRONG (-1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001675#endif /* !defined WRONG */
1676
1677/*
1678** Simplified normalize logic courtesy Paul Eggert.
1679*/
1680
1681static int
1682increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001683int * number;
1684int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001685{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001686 int number0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001687
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001688 number0 = *number;
1689 *number += delta;
1690 return (*number < number0) != (delta < 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001691}
1692
1693static int
1694long_increment_overflow(number, delta)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001695long * number;
1696int delta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001698 long number0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001699
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001700 number0 = *number;
1701 *number += delta;
1702 return (*number < number0) != (delta < 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001703}
1704
1705static int
1706normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001707int * const tensptr;
1708int * const unitsptr;
1709const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001710{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001711 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001712
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001713 tensdelta = (*unitsptr >= 0) ?
1714 (*unitsptr / base) :
1715 (-1 - (-1 - *unitsptr) / base);
1716 *unitsptr -= tensdelta * base;
1717 return increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001718}
1719
1720static int
1721long_normalize_overflow(tensptr, unitsptr, base)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001722long * const tensptr;
1723int * const unitsptr;
1724const int base;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001725{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001726 register int tensdelta;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001727
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001728 tensdelta = (*unitsptr >= 0) ?
1729 (*unitsptr / base) :
1730 (-1 - (-1 - *unitsptr) / base);
1731 *unitsptr -= tensdelta * base;
1732 return long_increment_overflow(tensptr, tensdelta);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001733}
1734
1735static int
1736tmcomp(atmp, btmp)
1737register const struct tm * const atmp;
1738register const struct tm * const btmp;
1739{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001740 register int result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001741
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001742 if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1743 (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1744 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1745 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1746 (result = (atmp->tm_min - btmp->tm_min)) == 0)
1747 result = atmp->tm_sec - btmp->tm_sec;
1748 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001749}
1750
1751static time_t
1752time2sub(tmp, funcp, offset, okayp, do_norm_secs)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001753struct tm * const tmp;
1754struct tm * (* const funcp) P((const time_t*, long, struct tm*));
1755const long offset;
1756int * const okayp;
1757const int do_norm_secs;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001758{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001759 register const struct state * sp;
1760 register int dir;
1761 register int i, j;
1762 register int saved_seconds;
1763 register long li;
1764 register time_t lo;
1765 register time_t hi;
1766 long y;
1767 time_t newt;
1768 time_t t;
1769 struct tm yourtm, mytm;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001770
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001771 *okayp = FALSE;
1772 yourtm = *tmp;
1773 if (do_norm_secs) {
1774 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1775 SECSPERMIN))
1776 return WRONG;
1777 }
1778 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1779 return WRONG;
1780 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1781 return WRONG;
1782 y = yourtm.tm_year;
1783 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
1784 return WRONG;
1785 /*
1786 ** Turn y into an actual year number for now.
1787 ** It is converted back to an offset from TM_YEAR_BASE later.
1788 */
1789 if (long_increment_overflow(&y, TM_YEAR_BASE))
1790 return WRONG;
1791 while (yourtm.tm_mday <= 0) {
1792 if (long_increment_overflow(&y, -1))
1793 return WRONG;
1794 li = y + (1 < yourtm.tm_mon);
1795 yourtm.tm_mday += year_lengths[isleap(li)];
1796 }
1797 while (yourtm.tm_mday > DAYSPERLYEAR) {
1798 li = y + (1 < yourtm.tm_mon);
1799 yourtm.tm_mday -= year_lengths[isleap(li)];
1800 if (long_increment_overflow(&y, 1))
1801 return WRONG;
1802 }
1803 for ( ; ; ) {
1804 i = mon_lengths[isleap(y)][yourtm.tm_mon];
1805 if (yourtm.tm_mday <= i)
1806 break;
1807 yourtm.tm_mday -= i;
1808 if (++yourtm.tm_mon >= MONSPERYEAR) {
1809 yourtm.tm_mon = 0;
1810 if (long_increment_overflow(&y, 1))
1811 return WRONG;
1812 }
1813 }
1814 if (long_increment_overflow(&y, -TM_YEAR_BASE))
1815 return WRONG;
1816 yourtm.tm_year = y;
1817 if (yourtm.tm_year != y)
1818 return WRONG;
1819 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1820 saved_seconds = 0;
1821 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
1822 /*
1823 ** We can't set tm_sec to 0, because that might push the
1824 ** time below the minimum representable time.
1825 ** Set tm_sec to 59 instead.
1826 ** This assumes that the minimum representable time is
1827 ** not in the same minute that a leap second was deleted from,
1828 ** which is a safer assumption than using 58 would be.
1829 */
1830 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1831 return WRONG;
1832 saved_seconds = yourtm.tm_sec;
1833 yourtm.tm_sec = SECSPERMIN - 1;
1834 } else {
1835 saved_seconds = yourtm.tm_sec;
1836 yourtm.tm_sec = 0;
1837 }
1838 /*
1839 ** Do a binary search (this works whatever time_t's type is).
1840 */
1841 if (!TYPE_SIGNED(time_t)) {
1842 lo = 0;
1843 hi = lo - 1;
1844 } else if (!TYPE_INTEGRAL(time_t)) {
1845 if (sizeof(time_t) > sizeof(float))
1846 hi = (time_t) DBL_MAX;
1847 else hi = (time_t) FLT_MAX;
1848 lo = -hi;
1849 } else {
1850 lo = 1;
1851 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
1852 lo *= 2;
1853 hi = -(lo + 1);
1854 }
1855 for ( ; ; ) {
1856 t = lo / 2 + hi / 2;
1857 if (t < lo)
1858 t = lo;
1859 else if (t > hi)
1860 t = hi;
1861 if ((*funcp)(&t, offset, &mytm) == NULL) {
1862 /*
1863 ** Assume that t is too extreme to be represented in
1864 ** a struct tm; arrange things so that it is less
1865 ** extreme on the next pass.
1866 */
1867 dir = (t > 0) ? 1 : -1;
1868 } else dir = tmcomp(&mytm, &yourtm);
1869 if (dir != 0) {
1870 if (t == lo) {
1871 ++t;
1872 if (t <= lo)
1873 return WRONG;
1874 ++lo;
1875 } else if (t == hi) {
1876 --t;
1877 if (t >= hi)
1878 return WRONG;
1879 --hi;
1880 }
1881 if (lo > hi)
1882 return WRONG;
1883 if (dir > 0)
1884 hi = t;
1885 else lo = t;
1886 continue;
1887 }
1888 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1889 break;
1890 /*
1891 ** Right time, wrong type.
1892 ** Hunt for right time, right type.
1893 ** It's okay to guess wrong since the guess
1894 ** gets checked.
1895 */
1896 /*
1897 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1898 */
1899 sp = (const struct state *)
1900 (((void *) funcp == (void *) localsub) ?
1901 lclptr : gmtptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001902#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001903 if (sp == NULL)
1904 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001905#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001906 for (i = sp->typecnt - 1; i >= 0; --i) {
1907 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1908 continue;
1909 for (j = sp->typecnt - 1; j >= 0; --j) {
1910 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1911 continue;
1912 newt = t + sp->ttis[j].tt_gmtoff -
1913 sp->ttis[i].tt_gmtoff;
1914 if ((*funcp)(&newt, offset, &mytm) == NULL)
1915 continue;
1916 if (tmcomp(&mytm, &yourtm) != 0)
1917 continue;
1918 if (mytm.tm_isdst != yourtm.tm_isdst)
1919 continue;
1920 /*
1921 ** We have a match.
1922 */
1923 t = newt;
1924 goto label;
1925 }
1926 }
1927 return WRONG;
1928 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001929label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001930 newt = t + saved_seconds;
1931 if ((newt < t) != (saved_seconds < 0))
1932 return WRONG;
1933 t = newt;
1934 if ((*funcp)(&t, offset, tmp))
1935 *okayp = TRUE;
1936 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001937}
1938
1939static time_t
1940time2(tmp, funcp, offset, okayp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001941struct tm * const tmp;
1942struct tm * (* const funcp) P((const time_t*, long, struct tm*));
1943const long offset;
1944int * const okayp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001945{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001946 time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001947
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001948 /*
1949 ** First try without normalization of seconds
1950 ** (in case tm_sec contains a value associated with a leap second).
1951 ** If that fails, try with normalization of seconds.
1952 */
1953 t = time2sub(tmp, funcp, offset, okayp, FALSE);
1954 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001955}
1956
1957static time_t
1958time1(tmp, funcp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001959struct tm * const tmp;
1960struct tm * (* const funcp) P((const time_t *, long, struct tm *));
1961const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001962{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001963 register time_t t;
1964 register const struct state * sp;
1965 register int samei, otheri;
1966 register int sameind, otherind;
1967 register int i;
1968 register int nseen;
1969 int seen[TZ_MAX_TYPES];
1970 int types[TZ_MAX_TYPES];
1971 int okay;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001972
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001973 if (tmp->tm_isdst > 1)
1974 tmp->tm_isdst = 1;
1975 t = time2(tmp, funcp, offset, &okay);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001976#ifdef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001977 /*
1978 ** PCTS code courtesy Grant Sullivan.
1979 */
1980 if (okay)
1981 return t;
1982 if (tmp->tm_isdst < 0)
1983 tmp->tm_isdst = 0; /* reset to std and try again */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001984#endif /* defined PCTS */
1985#ifndef PCTS
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001986 if (okay || tmp->tm_isdst < 0)
1987 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001988#endif /* !defined PCTS */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07001989 /*
1990 ** We're supposed to assume that somebody took a time of one type
1991 ** and did some math on it that yielded a "struct tm" that's bad.
1992 ** We try to divine the type they started from and adjust to the
1993 ** type they need.
1994 */
1995 /*
1996 ** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1997 */
1998 sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1999 lclptr : gmtptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002000#ifdef ALL_STATE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002001 if (sp == NULL)
2002 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002003#endif /* defined ALL_STATE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002004 for (i = 0; i < sp->typecnt; ++i)
2005 seen[i] = FALSE;
2006 nseen = 0;
2007 for (i = sp->timecnt - 1; i >= 0; --i)
2008 if (!seen[sp->types[i]]) {
2009 seen[sp->types[i]] = TRUE;
2010 types[nseen++] = sp->types[i];
2011 }
2012 for (sameind = 0; sameind < nseen; ++sameind) {
2013 samei = types[sameind];
2014 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2015 continue;
2016 for (otherind = 0; otherind < nseen; ++otherind) {
2017 otheri = types[otherind];
2018 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2019 continue;
2020 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2021 sp->ttis[samei].tt_gmtoff;
2022 tmp->tm_isdst = !tmp->tm_isdst;
2023 t = time2(tmp, funcp, offset, &okay);
2024 if (okay)
2025 return t;
2026 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2027 sp->ttis[samei].tt_gmtoff;
2028 tmp->tm_isdst = !tmp->tm_isdst;
2029 }
2030 }
2031 return WRONG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002032}
2033
2034time_t
2035mktime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002036struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002037{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002038 time_t result;
2039 _tzLock();
2040 tzset_locked();
2041 result = time1(tmp, localsub, 0L);
2042 _tzUnlock();
2043 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002044}
2045
2046#ifdef STD_INSPIRED
2047
2048time_t
2049timelocal(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002050struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002051{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002052 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2053 return mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002054}
2055
2056time_t
2057timegm(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002058struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002059{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002060 time_t result;
2061
2062 tmp->tm_isdst = 0;
2063 _tzLock();
2064 result = time1(tmp, gmtsub, 0L);
2065 _tzUnlock();
2066
2067 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002068}
2069
2070time_t
2071timeoff(tmp, offset)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002072struct tm * const tmp;
2073const long offset;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002074{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002075 time_t result;
2076
2077 tmp->tm_isdst = 0;
2078 _tzLock();
2079 result = time1(tmp, gmtsub, offset);
2080 _tzUnlock();
2081
2082 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002083}
2084
2085#endif /* defined STD_INSPIRED */
2086
2087#ifdef CMUCS
2088
2089/*
2090** The following is supplied for compatibility with
2091** previous versions of the CMUCS runtime library.
2092*/
2093
2094long
2095gtime(tmp)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002096struct tm * const tmp;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002097{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002098 const time_t t = mktime(tmp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002099
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002100 if (t == WRONG)
2101 return -1;
2102 return t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002103}
2104
2105#endif /* defined CMUCS */
2106
2107/*
2108** XXX--is the below the right way to conditionalize??
2109*/
2110
2111#ifdef STD_INSPIRED
2112
2113/*
2114** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2115** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2116** is not the case if we are accounting for leap seconds.
2117** So, we provide the following conversion routines for use
2118** when exchanging timestamps with POSIX conforming systems.
2119*/
2120
2121static long
2122leapcorr(timep)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002123time_t * timep;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002124{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002125 register struct state * sp;
2126 register struct lsinfo * lp;
2127 register int i;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002128
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002129 sp = lclptr;
2130 i = sp->leapcnt;
2131 while (--i >= 0) {
2132 lp = &sp->lsis[i];
2133 if (*timep >= lp->ls_trans)
2134 return lp->ls_corr;
2135 }
2136 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002137}
2138
2139time_t
2140time2posix(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002141time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002142{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002143 tzset();
2144 return t - leapcorr(&t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002145}
2146
2147time_t
2148posix2time(t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002149time_t t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002150{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002151 time_t x;
2152 time_t y;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002153
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07002154 tzset();
2155 /*
2156 ** For a positive leap second hit, the result
2157 ** is not unique. For a negative leap second
2158 ** hit, the corresponding time doesn't exist,
2159 ** so we return an adjacent second.
2160 */
2161 x = t + leapcorr(&t);
2162 y = x - leapcorr(&x);
2163 if (y < t) {
2164 do {
2165 x++;
2166 y = x - leapcorr(&x);
2167 } while (y < t);
2168 if (t != y)
2169 return x - 1;
2170 } else if (y > t) {
2171 do {
2172 --x;
2173 y = x - leapcorr(&x);
2174 } while (y > t);
2175 if (t != y)
2176 return x + 1;
2177 }
2178 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002179}
2180
2181#endif /* defined STD_INSPIRED */