The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 8 | static char elsieid[] = "@(#)localtime.c 8.3"; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 9 | #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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 22 | #include "float.h" /* for FLT_MAX and DBL_MAX */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 23 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 24 | #include "thread_private.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 25 | #include <sys/system_properties.h> |
| 26 | |
| 27 | #ifndef TZ_ABBR_MAX_LEN |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 28 | #define TZ_ABBR_MAX_LEN 16 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #endif /* !defined TZ_ABBR_MAX_LEN */ |
| 30 | |
| 31 | #ifndef TZ_ABBR_CHAR_SET |
| 32 | #define TZ_ABBR_CHAR_SET \ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 33 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | #endif /* !defined TZ_ABBR_CHAR_SET */ |
| 35 | |
| 36 | #ifndef TZ_ABBR_ERR_CHAR |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 37 | #define TZ_ABBR_ERR_CHAR '_' |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | #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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 51 | #define OPEN_MODE (O_RDONLY | O_BINARY) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | #endif /* defined O_BINARY */ |
| 53 | #ifndef O_BINARY |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 54 | #define OPEN_MODE O_RDONLY |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 55 | #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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 63 | /* THREAD-SAFETY SUPPORT GOES HERE */ |
| 64 | static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER; |
| 65 | |
| 66 | static __inline__ void _tzLock(void) |
| 67 | { |
| 68 | if (__isthreaded) |
| 69 | pthread_mutex_lock(&_tzMutex); |
| 70 | } |
| 71 | |
| 72 | static __inline__ void _tzUnlock(void) |
| 73 | { |
| 74 | if (__isthreaded) |
| 75 | pthread_mutex_unlock(&_tzMutex); |
| 76 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | |
| 78 | #ifndef WILDABBR |
| 79 | /* |
| 80 | ** Someone might make incorrect use of a time zone abbreviation: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 81 | ** 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 90 | ** 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 98 | #define WILDABBR " " |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | #endif /* !defined WILDABBR */ |
| 100 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 101 | static char wildabbr[] = WILDABBR; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 102 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 103 | static const char gmt[] = "GMT"; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | |
| 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 116 | struct 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 124 | struct lsinfo { /* leap second information */ |
| 125 | time_t ls_trans; /* transition time */ |
| 126 | long ls_corr; /* correction to apply */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 129 | #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | |
| 131 | #ifdef TZNAME_MAX |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 132 | #define MY_TZNAME_MAX TZNAME_MAX |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 133 | #endif /* defined TZNAME_MAX */ |
| 134 | #ifndef TZNAME_MAX |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 135 | #define MY_TZNAME_MAX 255 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | #endif /* !defined TZNAME_MAX */ |
| 137 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 138 | /* 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | struct state { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 143 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | struct rule { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 158 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 165 | #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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | ** Prototypes for static functions. |
| 171 | */ |
| 172 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 173 | /* NOTE: all internal functions assume that _tzLock() was already called */ |
| 174 | |
| 175 | static long detzcode P((const char * codep)); |
| 176 | static time_t detzcode64 P((const char * codep)); |
| 177 | static int differ_by_repeat P((time_t t1, time_t t0)); |
| 178 | static const char * getzname P((const char * strp)); |
| 179 | static const char * getqzname P((const char * strp, const int delim)); |
| 180 | static const char * getnum P((const char * strp, int * nump, int min, |
| 181 | int max)); |
| 182 | static const char * getsecs P((const char * strp, long * secsp)); |
| 183 | static const char * getoffset P((const char * strp, long * offsetp)); |
| 184 | static const char * getrule P((const char * strp, struct rule * rulep)); |
| 185 | static void gmtload P((struct state * sp)); |
| 186 | static struct tm * gmtsub P((const time_t * timep, long offset, |
| 187 | struct tm * tmp)); |
| 188 | static struct tm * localsub P((const time_t * timep, long offset, |
| 189 | struct tm * tmp)); |
| 190 | static int increment_overflow P((int * number, int delta)); |
| 191 | static int leaps_thru_end_of P((int y)); |
| 192 | static int long_increment_overflow P((long * number, int delta)); |
| 193 | static int long_normalize_overflow P((long * tensptr, |
| 194 | int * unitsptr, int base)); |
| 195 | static int normalize_overflow P((int * tensptr, int * unitsptr, |
| 196 | int base)); |
| 197 | static void settzname P((void)); |
| 198 | static time_t time1 P((struct tm * tmp, |
| 199 | struct tm * (*funcp) P((const time_t *, |
| 200 | long, struct tm *)), |
| 201 | long offset)); |
| 202 | static time_t time2 P((struct tm *tmp, |
| 203 | struct tm * (*funcp) P((const time_t *, |
| 204 | long, struct tm*)), |
| 205 | long offset, int * okayp)); |
| 206 | static 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)); |
| 210 | static struct tm * timesub P((const time_t * timep, long offset, |
| 211 | const struct state * sp, struct tm * tmp)); |
| 212 | static int tmcomp P((const struct tm * atmp, |
| 213 | const struct tm * btmp)); |
| 214 | static time_t transtime P((time_t janfirst, int year, |
| 215 | const struct rule * rulep, long offset)); |
| 216 | static int tzload P((const char * name, struct state * sp, |
| 217 | int doextend)); |
| 218 | static int tzparse P((const char * name, struct state * sp, |
| 219 | int lastditch)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 220 | |
| 221 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 222 | static struct state * lclptr; |
| 223 | static struct state * gmtptr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 224 | #endif /* defined ALL_STATE */ |
| 225 | |
| 226 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 227 | static struct state lclmem; |
| 228 | static struct state gmtmem; |
| 229 | #define lclptr (&lclmem) |
| 230 | #define gmtptr (&gmtmem) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 231 | #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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 237 | static char lcl_TZname[TZ_STRLEN_MAX + 1]; |
| 238 | static int lcl_is_set; |
| 239 | static int gmt_is_set; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 241 | char * tzname[2] = { |
| 242 | wildabbr, |
| 243 | wildabbr |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | /* |
| 247 | ** Section 4.12.3 of X3.159-1989 requires that |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 248 | ** 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 251 | ** Thanks to Paul Eggert for noting this. |
| 252 | */ |
| 253 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 254 | static struct tm tmGlobal; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 255 | |
| 256 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 257 | time_t timezone = 0; |
| 258 | int daylight = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 259 | #endif /* defined USG_COMPAT */ |
| 260 | |
| 261 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 262 | time_t altzone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 263 | #endif /* defined ALTZONE */ |
| 264 | |
| 265 | static long |
| 266 | detzcode(codep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 267 | const char * const codep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 268 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 269 | register long result; |
| 270 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 271 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 272 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static time_t |
| 279 | detzcode64(codep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 280 | const char * const codep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 281 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 282 | register time_t result; |
| 283 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 284 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 285 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void |
| 292 | settzname P((void)) |
| 293 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 294 | register struct state * const sp = lclptr; |
| 295 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 296 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 297 | tzname[0] = wildabbr; |
| 298 | tzname[1] = wildabbr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 299 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 300 | daylight = 0; |
| 301 | timezone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 302 | #endif /* defined USG_COMPAT */ |
| 303 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 304 | altzone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 305 | #endif /* defined ALTZONE */ |
| 306 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 307 | if (sp == NULL) { |
| 308 | tzname[0] = tzname[1] = gmt; |
| 309 | return; |
| 310 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 312 | for (i = 0; i < sp->typecnt; ++i) { |
| 313 | register const struct ttinfo * const ttisp = &sp->ttis[i]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 314 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 315 | tzname[ttisp->tt_isdst] = |
| 316 | &sp->chars[ttisp->tt_abbrind]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 317 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 318 | if (ttisp->tt_isdst) |
| 319 | daylight = 1; |
| 320 | if (i == 0 || !ttisp->tt_isdst) |
| 321 | timezone = -(ttisp->tt_gmtoff); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 322 | #endif /* defined USG_COMPAT */ |
| 323 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 324 | if (i == 0 || ttisp->tt_isdst) |
| 325 | altzone = -(ttisp->tt_gmtoff); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | #endif /* defined ALTZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 327 | } |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 335 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 336 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 352 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 353 | if (strlen(cp) > TZ_ABBR_MAX_LEN && |
| 354 | strcmp(cp, GRANDPARENTED) != 0) |
| 355 | *(cp + TZ_ABBR_MAX_LEN) = '\0'; |
| 356 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int |
| 360 | differ_by_repeat(t1, t0) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 361 | const time_t t1; |
| 362 | const time_t t0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 363 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 364 | if (TYPE_INTEGRAL(time_t) && |
| 365 | TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) |
| 366 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 367 | return (t1 - t0) == SECSPERREPEAT; |
| 368 | } |
| 369 | |
| 370 | static int toint(unsigned char *s) { |
| 371 | return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; |
| 372 | } |
| 373 | |
| 374 | static int |
| 375 | tzload(name, sp, doextend) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 376 | register const char * name; |
| 377 | register struct state * const sp; |
| 378 | register const int doextend; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 379 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 380 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 391 | 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 397 | { |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 408 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 409 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 436 | 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 477 | } |
| 478 | nread = read(fid, u.buf, toread); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 479 | if (close(fid) < 0 || nread <= 0) { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 480 | XLOG(( "tzload: could not read content of '%s'\n", DATAFILE )); |
| 481 | return -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 482 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 483 | for (stored = 4; stored <= 8; stored *= 2) { |
| 484 | int ttisstdcnt; |
| 485 | int ttisgmtcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 486 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 487 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 522 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 523 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 539 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 540 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 549 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 550 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 562 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 563 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 591 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 592 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 619 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 620 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 657 | XLOG(( "tzload: load ok !!\n" )); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 658 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 659 | } |
| 660 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 661 | static 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 664 | }; |
| 665 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 666 | static const int year_lengths[2] = { |
| 667 | DAYSPERNYEAR, DAYSPERLYEAR |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 668 | }; |
| 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 | |
| 676 | static const char * |
| 677 | getzname(strp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 678 | register const char * strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 679 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 680 | register char c; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 681 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 682 | while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && |
| 683 | c != '+') |
| 684 | ++strp; |
| 685 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 686 | } |
| 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 | |
| 697 | static const char * |
| 698 | getqzname(register const char *strp, const int delim) |
| 699 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 700 | register int c; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 701 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 702 | while ((c = *strp) != '\0' && c != delim) |
| 703 | ++strp; |
| 704 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 705 | } |
| 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 | |
| 714 | static const char * |
| 715 | getnum(strp, nump, min, max) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 716 | register const char * strp; |
| 717 | int * const nump; |
| 718 | const int min; |
| 719 | const int max; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 720 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 721 | register char c; |
| 722 | register int num; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 723 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 724 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 737 | } |
| 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 | |
| 747 | static const char * |
| 748 | getsecs(strp, secsp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 749 | register const char * strp; |
| 750 | long * const secsp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 751 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 752 | int num; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 753 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 754 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 780 | } |
| 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 | |
| 789 | static const char * |
| 790 | getoffset(strp, offsetp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 791 | register const char * strp; |
| 792 | long * const offsetp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 793 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 794 | register int neg = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 795 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 796 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 807 | } |
| 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 | |
| 816 | static const char * |
| 817 | getrule(strp, rulep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 818 | const char * strp; |
| 819 | register struct rule * const rulep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 820 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 821 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 862 | } |
| 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 | |
| 870 | static time_t |
| 871 | transtime(janfirst, year, rulep, offset) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 872 | const time_t janfirst; |
| 873 | const int year; |
| 874 | register const struct rule * const rulep; |
| 875 | const long offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 876 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 877 | register int leapyear; |
| 878 | register time_t value; |
| 879 | register int i; |
| 880 | int d, m1, yy0, yy1, yy2, dow; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 881 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 882 | INITIALIZE(value); |
| 883 | leapyear = isleap(year); |
| 884 | switch (rulep->r_type) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 885 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 886 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 898 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 899 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 907 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 908 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 915 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 916 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 928 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 929 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 943 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 944 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 950 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 951 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | ** Given a POSIX section 8-style TZ string, fill in the rule tables as |
| 962 | ** appropriate. |
| 963 | */ |
| 964 | |
| 965 | static int |
| 966 | tzparse(name, sp, lastditch) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 967 | const char * name; |
| 968 | register struct state * const sp; |
| 969 | const int lastditch; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 970 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 971 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 981 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 982 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1040 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1041 | ++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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1067 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1068 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1098 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1099 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static void |
| 1201 | gmtload(sp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1202 | struct state * const sp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1203 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1204 | if (tzload(gmt, sp, TRUE) != 0) |
| 1205 | (void) tzparse(gmt, sp, TRUE); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1208 | static void |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1209 | tzsetwall P((void)) |
| 1210 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1211 | if (lcl_is_set < 0) |
| 1212 | return; |
| 1213 | lcl_is_set = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1214 | |
| 1215 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1216 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1223 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1224 | if (tzload((char *) NULL, lclptr, TRUE) != 0) |
| 1225 | gmtload(lclptr); |
| 1226 | settzname(); |
| 1227 | } |
| 1228 | |
| 1229 | static void |
| 1230 | tzset_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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | void |
| 1279 | tzset P((void)) |
| 1280 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1281 | _tzLock(); |
| 1282 | tzset_locked(); |
| 1283 | _tzUnlock(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1284 | } |
| 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*/ |
| 1296 | static struct tm * |
| 1297 | localsub(timep, offset, tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1298 | const time_t * const timep; |
| 1299 | const long offset; |
| 1300 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1301 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1302 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1307 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1308 | sp = lclptr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1309 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1310 | if (sp == NULL) |
| 1311 | return gmtsub(timep, offset, tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1312 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1313 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1319 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1320 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1341 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1342 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1362 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1363 | while (lo < hi) { |
| 1364 | register int mid = (lo + hi) >> 1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1365 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1366 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1382 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1383 | tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1384 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1385 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | struct tm * |
| 1389 | localtime(timep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1390 | const time_t * const timep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1391 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1392 | return localtime_r(timep, &tmGlobal); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | ** Re-entrant version of localtime. |
| 1397 | */ |
| 1398 | |
| 1399 | struct tm * |
| 1400 | localtime_r(timep, tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1401 | const time_t * const timep; |
| 1402 | struct tm * tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1403 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1404 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | ** gmtsub is to gmtime as localsub is to localtime. |
| 1416 | */ |
| 1417 | |
| 1418 | static struct tm * |
| 1419 | gmtsub(timep, offset, tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1420 | const time_t * const timep; |
| 1421 | const long offset; |
| 1422 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1423 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1424 | register struct tm * result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1425 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1426 | if (!gmt_is_set) { |
| 1427 | gmt_is_set = TRUE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1428 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1429 | gmtptr = (struct state *) malloc(sizeof *gmtptr); |
| 1430 | if (gmtptr != NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1431 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1432 | gmtload(gmtptr); |
| 1433 | } |
| 1434 | result = timesub(timep, offset, gmtptr, tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1435 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1436 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1444 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1445 | if (gmtptr == NULL) |
| 1446 | tmp->TM_ZONE = gmt; |
| 1447 | else tmp->TM_ZONE = gmtptr->chars; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1448 | #endif /* defined ALL_STATE */ |
| 1449 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1450 | tmp->TM_ZONE = gmtptr->chars; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1451 | #endif /* State Farm */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1452 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1453 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1454 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | struct tm * |
| 1458 | gmtime(timep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1459 | const time_t * const timep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1460 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1461 | return gmtime_r(timep, &tmGlobal); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * Re-entrant version of gmtime. |
| 1466 | */ |
| 1467 | |
| 1468 | struct tm * |
| 1469 | gmtime_r(timep, tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1470 | const time_t * const timep; |
| 1471 | struct tm * tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1472 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1473 | struct tm* result; |
| 1474 | |
| 1475 | _tzLock(); |
| 1476 | result = gmtsub(timep, 0L, tmp); |
| 1477 | _tzUnlock(); |
| 1478 | |
| 1479 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | #ifdef STD_INSPIRED |
| 1483 | |
| 1484 | struct tm * |
| 1485 | offtime(timep, offset) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1486 | const time_t * const timep; |
| 1487 | const long offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1488 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1489 | return gmtsub(timep, offset, &tmGlobal); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1490 | } |
| 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 | |
| 1499 | static int |
| 1500 | leaps_thru_end_of(y) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1501 | register const int y; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1502 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1503 | return (y >= 0) ? (y / 4 - y / 100 + y / 400) : |
| 1504 | -(leaps_thru_end_of(-(y + 1)) + 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | static struct tm * |
| 1508 | timesub(timep, offset, sp, tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1509 | const time_t * const timep; |
| 1510 | const long offset; |
| 1511 | register const struct state * const sp; |
| 1512 | register struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1513 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1514 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1523 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1524 | corr = 0; |
| 1525 | hit = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1526 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1527 | i = (sp == NULL) ? 0 : sp->leapcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1528 | #endif /* defined ALL_STATE */ |
| 1529 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1530 | i = sp->leapcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1531 | #endif /* State Farm */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1532 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1560 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1561 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1578 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1579 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1635 | #ifdef TM_GMTOFF |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1636 | tmp->TM_GMTOFF = offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1637 | #endif /* defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1638 | return tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | char * |
| 1642 | ctime(timep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1643 | const time_t * const timep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1644 | { |
| 1645 | /* |
| 1646 | ** Section 4.12.3.2 of X3.159-1989 requires that |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1647 | ** 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1650 | */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1651 | return asctime(localtime(timep)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | char * |
| 1655 | ctime_r(timep, buf) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1656 | const time_t * const timep; |
| 1657 | char * buf; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1658 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1659 | struct tm mytm; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1660 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1661 | return asctime_r(localtime_r(timep, &mytm), buf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | ** Adapted from code provided by Robert Elz, who writes: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1666 | ** 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1671 | */ |
| 1672 | |
| 1673 | #ifndef WRONG |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1674 | #define WRONG (-1) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1675 | #endif /* !defined WRONG */ |
| 1676 | |
| 1677 | /* |
| 1678 | ** Simplified normalize logic courtesy Paul Eggert. |
| 1679 | */ |
| 1680 | |
| 1681 | static int |
| 1682 | increment_overflow(number, delta) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1683 | int * number; |
| 1684 | int delta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1685 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1686 | int number0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1687 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1688 | number0 = *number; |
| 1689 | *number += delta; |
| 1690 | return (*number < number0) != (delta < 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | static int |
| 1694 | long_increment_overflow(number, delta) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1695 | long * number; |
| 1696 | int delta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1697 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1698 | long number0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1699 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1700 | number0 = *number; |
| 1701 | *number += delta; |
| 1702 | return (*number < number0) != (delta < 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static int |
| 1706 | normalize_overflow(tensptr, unitsptr, base) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1707 | int * const tensptr; |
| 1708 | int * const unitsptr; |
| 1709 | const int base; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1710 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1711 | register int tensdelta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1712 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1713 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | static int |
| 1721 | long_normalize_overflow(tensptr, unitsptr, base) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1722 | long * const tensptr; |
| 1723 | int * const unitsptr; |
| 1724 | const int base; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1725 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1726 | register int tensdelta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1727 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1728 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static int |
| 1736 | tmcomp(atmp, btmp) |
| 1737 | register const struct tm * const atmp; |
| 1738 | register const struct tm * const btmp; |
| 1739 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1740 | register int result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1741 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1742 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | static time_t |
| 1752 | time2sub(tmp, funcp, offset, okayp, do_norm_secs) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1753 | struct tm * const tmp; |
| 1754 | struct tm * (* const funcp) P((const time_t*, long, struct tm*)); |
| 1755 | const long offset; |
| 1756 | int * const okayp; |
| 1757 | const int do_norm_secs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1758 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1759 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1770 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1771 | *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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1902 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1903 | if (sp == NULL) |
| 1904 | return WRONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1905 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1906 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1929 | label: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1930 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | static time_t |
| 1940 | time2(tmp, funcp, offset, okayp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1941 | struct tm * const tmp; |
| 1942 | struct tm * (* const funcp) P((const time_t*, long, struct tm*)); |
| 1943 | const long offset; |
| 1944 | int * const okayp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1945 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1946 | time_t t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1947 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1948 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | static time_t |
| 1958 | time1(tmp, funcp, offset) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1959 | struct tm * const tmp; |
| 1960 | struct tm * (* const funcp) P((const time_t *, long, struct tm *)); |
| 1961 | const long offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1962 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1963 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1972 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1973 | if (tmp->tm_isdst > 1) |
| 1974 | tmp->tm_isdst = 1; |
| 1975 | t = time2(tmp, funcp, offset, &okay); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1976 | #ifdef PCTS |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1977 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1984 | #endif /* defined PCTS */ |
| 1985 | #ifndef PCTS |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1986 | if (okay || tmp->tm_isdst < 0) |
| 1987 | return t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1988 | #endif /* !defined PCTS */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1989 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2000 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2001 | if (sp == NULL) |
| 2002 | return WRONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2003 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2004 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | time_t |
| 2035 | mktime(tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2036 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2037 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2038 | time_t result; |
| 2039 | _tzLock(); |
| 2040 | tzset_locked(); |
| 2041 | result = time1(tmp, localsub, 0L); |
| 2042 | _tzUnlock(); |
| 2043 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | #ifdef STD_INSPIRED |
| 2047 | |
| 2048 | time_t |
| 2049 | timelocal(tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2050 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2051 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2052 | tmp->tm_isdst = -1; /* in case it wasn't initialized */ |
| 2053 | return mktime(tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | time_t |
| 2057 | timegm(tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2058 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2059 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2060 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | time_t |
| 2071 | timeoff(tmp, offset) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2072 | struct tm * const tmp; |
| 2073 | const long offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2074 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2075 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2083 | } |
| 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 | |
| 2094 | long |
| 2095 | gtime(tmp) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2096 | struct tm * const tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2097 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2098 | const time_t t = mktime(tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2099 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2100 | if (t == WRONG) |
| 2101 | return -1; |
| 2102 | return t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2103 | } |
| 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 | |
| 2121 | static long |
| 2122 | leapcorr(timep) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2123 | time_t * timep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2124 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2125 | register struct state * sp; |
| 2126 | register struct lsinfo * lp; |
| 2127 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2128 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2129 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | time_t |
| 2140 | time2posix(t) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2141 | time_t t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2142 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2143 | tzset(); |
| 2144 | return t - leapcorr(&t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | time_t |
| 2148 | posix2time(t) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2149 | time_t t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2150 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2151 | time_t x; |
| 2152 | time_t y; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2153 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2154 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | #endif /* defined STD_INSPIRED */ |