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 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 6 | /* |
| 7 | ** Leap second handling from Bradley White. |
| 8 | ** POSIX-style TZ environment variable handling from Guy Harris. |
| 9 | */ |
| 10 | |
| 11 | /*LINTLIBRARY*/ |
| 12 | |
| 13 | #include "private.h" |
| 14 | #include "tzfile.h" |
| 15 | #include "fcntl.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 16 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 17 | #ifndef TZ_ABBR_MAX_LEN |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 18 | #define TZ_ABBR_MAX_LEN 16 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 19 | #endif /* !defined TZ_ABBR_MAX_LEN */ |
| 20 | |
| 21 | #ifndef TZ_ABBR_CHAR_SET |
| 22 | #define TZ_ABBR_CHAR_SET \ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 23 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 24 | #endif /* !defined TZ_ABBR_CHAR_SET */ |
| 25 | |
| 26 | #ifndef TZ_ABBR_ERR_CHAR |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 27 | #define TZ_ABBR_ERR_CHAR '_' |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 28 | #endif /* !defined TZ_ABBR_ERR_CHAR */ |
| 29 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | /* |
| 31 | ** SunOS 4.1.1 headers lack O_BINARY. |
| 32 | */ |
| 33 | |
| 34 | #ifdef O_BINARY |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 35 | #define OPEN_MODE (O_RDONLY | O_BINARY) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | #endif /* defined O_BINARY */ |
| 37 | #ifndef O_BINARY |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 38 | #define OPEN_MODE O_RDONLY |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | #endif /* !defined O_BINARY */ |
| 40 | |
| 41 | #if 0 |
| 42 | # define XLOG(xx) printf xx , fflush(stdout) |
| 43 | #else |
| 44 | # define XLOG(x) do{}while (0) |
| 45 | #endif |
| 46 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 47 | /* BEGIN android-added: thread-safety. */ |
| 48 | #include <pthread.h> |
| 49 | static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER; |
| 50 | static inline void _tzLock(void) { pthread_mutex_lock(&_tzMutex); } |
| 51 | static inline void _tzUnlock(void) { pthread_mutex_unlock(&_tzMutex); } |
| 52 | /* END android-added */ |
David 'Digit' Turner | 2093d35 | 2009-09-09 17:41:59 -0700 | [diff] [blame] | 53 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | #ifndef WILDABBR |
| 55 | /* |
| 56 | ** 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] | 57 | ** 1. They might reference tzname[0] before calling tzset (explicitly |
| 58 | ** or implicitly). |
| 59 | ** 2. They might reference tzname[1] before calling tzset (explicitly |
| 60 | ** or implicitly). |
| 61 | ** 3. They might reference tzname[1] after setting to a time zone |
| 62 | ** in which Daylight Saving Time is never observed. |
| 63 | ** 4. They might reference tzname[0] after setting to a time zone |
| 64 | ** in which Standard Time is never observed. |
| 65 | ** 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] | 66 | ** What's best to do in the above cases is open to debate; |
| 67 | ** for now, we just set things up so that in any of the five cases |
| 68 | ** WILDABBR is used. Another possibility: initialize tzname[0] to the |
| 69 | ** string "tzname[0] used before set", and similarly for the other cases. |
| 70 | ** And another: initialize tzname[0] to "ERA", with an explanation in the |
| 71 | ** manual page of what this "time zone abbreviation" means (doing this so |
| 72 | ** that tzname[0] has the "normal" length of three characters). |
| 73 | */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 74 | #define WILDABBR " " |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 75 | #endif /* !defined WILDABBR */ |
| 76 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 77 | static char wildabbr[] = WILDABBR; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 79 | static const char gmt[] = "GMT"; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES. |
| 83 | ** We default to US rules as of 1999-08-17. |
| 84 | ** POSIX 1003.1 section 8.1.1 says that the default DST rules are |
| 85 | ** implementation dependent; for historical reasons, US rules are a |
| 86 | ** common default. |
| 87 | */ |
| 88 | #ifndef TZDEFRULESTRING |
| 89 | #define TZDEFRULESTRING ",M4.1.0,M10.5.0" |
| 90 | #endif /* !defined TZDEFDST */ |
| 91 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 92 | struct ttinfo { /* time type information */ |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 93 | int_fast32_t tt_gmtoff; /* UT offset in seconds */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 94 | int tt_isdst; /* used to set tm_isdst */ |
| 95 | int tt_abbrind; /* abbreviation list index */ |
| 96 | int tt_ttisstd; /* TRUE if transition is std time */ |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 97 | int tt_ttisgmt; /* TRUE if transition is UT */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 100 | struct lsinfo { /* leap second information */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 101 | time_t ls_trans; /* transition time */ |
| 102 | int_fast64_t ls_corr; /* correction to apply */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 105 | #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | |
| 107 | #ifdef TZNAME_MAX |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 108 | #define MY_TZNAME_MAX TZNAME_MAX |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 109 | #endif /* defined TZNAME_MAX */ |
| 110 | #ifndef TZNAME_MAX |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 111 | #define MY_TZNAME_MAX 255 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | #endif /* !defined TZNAME_MAX */ |
| 113 | |
| 114 | struct state { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 115 | int leapcnt; |
| 116 | int timecnt; |
| 117 | int typecnt; |
| 118 | int charcnt; |
| 119 | int goback; |
| 120 | int goahead; |
| 121 | time_t ats[TZ_MAX_TIMES]; |
| 122 | unsigned char types[TZ_MAX_TIMES]; |
| 123 | struct ttinfo ttis[TZ_MAX_TYPES]; |
| 124 | char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), |
| 125 | (2 * (MY_TZNAME_MAX + 1)))]; |
| 126 | struct lsinfo lsis[TZ_MAX_LEAPS]; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 127 | int defaulttype; /* for early times or if no transitions */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct rule { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 131 | int r_type; /* type of rule--see below */ |
| 132 | int r_day; /* day number of rule */ |
| 133 | int r_week; /* week number of rule */ |
| 134 | int r_mon; /* month number of rule */ |
| 135 | int_fast32_t r_time; /* transition time of rule */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 138 | #define JULIAN_DAY 0 /* Jn - Julian day */ |
| 139 | #define DAY_OF_YEAR 1 /* n - day of year */ |
| 140 | #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] | 141 | |
| 142 | /* |
| 143 | ** Prototypes for static functions. |
| 144 | */ |
| 145 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 146 | /* NOTE: all internal functions assume that _tzLock() was already called */ |
| 147 | |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 148 | static int __bionic_open_tzdata(const char*, int*); |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 149 | static int_fast32_t detzcode(const char * codep); |
| 150 | static time_t detzcode64(const char * codep); |
| 151 | static int differ_by_repeat(time_t t1, time_t t0); |
| 152 | static const char * getzname(const char * strp) ATTRIBUTE_PURE; |
| 153 | static const char * getqzname(const char * strp, const int delim) |
| 154 | ATTRIBUTE_PURE; |
| 155 | static const char * getnum(const char * strp, int * nump, int min, |
| 156 | int max); |
| 157 | static const char * getsecs(const char * strp, int_fast32_t * secsp); |
| 158 | static const char * getoffset(const char * strp, int_fast32_t * offsetp); |
| 159 | static const char * getrule(const char * strp, struct rule * rulep); |
| 160 | static void gmtload(struct state * sp); |
| 161 | static struct tm * gmtsub(const time_t * timep, const int_fast32_t offset, |
| 162 | struct tm * tmp, const struct state * sp); // android-changed: added sp. |
| 163 | static struct tm * localsub(const time_t * timep, int_fast32_t offset, |
| 164 | struct tm * tmp, const struct state * sp); // android-changed: added sp. |
| 165 | static int increment_overflow(int * number, int delta); |
| 166 | static int leaps_thru_end_of(int y) ATTRIBUTE_PURE; |
| 167 | static int increment_overflow32(int_fast32_t * number, int delta); |
| 168 | static int normalize_overflow32(int_fast32_t * tensptr, |
| 169 | int * unitsptr, int base); |
| 170 | static int normalize_overflow(int * tensptr, int * unitsptr, |
| 171 | int base); |
| 172 | static void settzname(void); |
| 173 | static time_t time1(struct tm * tmp, |
| 174 | struct tm * (*funcp)(const time_t *, |
| 175 | int_fast32_t, struct tm *, const struct state *), // android-changed: added state*. |
| 176 | int_fast32_t offset, const struct state * sp); // android-changed: added sp. |
| 177 | static time_t time2(struct tm * const tmp, |
| 178 | struct tm * (*const funcp)(const time_t *, |
| 179 | int_fast32_t, struct tm*, const struct state *), // android-changed: added state*. |
| 180 | int_fast32_t offset, int * okayp, const struct state * sp); // android-changed: added sp. |
| 181 | static time_t time2sub(struct tm *tmp, |
| 182 | struct tm * (*funcp) (const time_t *, |
| 183 | int_fast32_t, struct tm*, const struct state *), // android-changed: added state*. |
| 184 | int_fast32_t offset, int * okayp, int do_norm_secs, const struct state * sp); // android-change: added sp. |
| 185 | static struct tm * timesub(const time_t * timep, int_fast32_t offset, |
| 186 | const struct state * sp, struct tm * tmp); |
| 187 | static int tmcomp(const struct tm * atmp, |
| 188 | const struct tm * btmp); |
| 189 | static time_t transtime(time_t janfirst, int year, |
| 190 | const struct rule * rulep, int_fast32_t offset) |
| 191 | ATTRIBUTE_PURE; |
| 192 | static int typesequiv(const struct state * sp, int a, int b); |
| 193 | static int tzload(const char * name, struct state * sp, |
| 194 | int doextend); |
| 195 | static int tzparse(const char * name, struct state * sp, |
| 196 | int lastditch); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 197 | |
| 198 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 199 | static struct state * lclptr; |
| 200 | static struct state * gmtptr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | #endif /* defined ALL_STATE */ |
| 202 | |
| 203 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 204 | static struct state lclmem; |
| 205 | static struct state gmtmem; |
| 206 | #define lclptr (&lclmem) |
| 207 | #define gmtptr (&gmtmem) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 | #endif /* State Farm */ |
| 209 | |
| 210 | #ifndef TZ_STRLEN_MAX |
| 211 | #define TZ_STRLEN_MAX 255 |
| 212 | #endif /* !defined TZ_STRLEN_MAX */ |
| 213 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 214 | static char lcl_TZname[TZ_STRLEN_MAX + 1]; |
| 215 | static int lcl_is_set; |
| 216 | static int gmt_is_set; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 217 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 218 | char * tzname[2] = { |
| 219 | wildabbr, |
| 220 | wildabbr |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | /* |
| 224 | ** 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] | 225 | ** Except for the strftime function, these functions [asctime, |
| 226 | ** ctime, gmtime, localtime] return values in one of two static |
| 227 | ** 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] | 228 | ** Thanks to Paul Eggert for noting this. |
| 229 | */ |
| 230 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 231 | static struct tm tmGlobal; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 232 | |
| 233 | #ifdef USG_COMPAT |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 234 | long timezone = 0; |
| 235 | int daylight = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 236 | #endif /* defined USG_COMPAT */ |
| 237 | |
| 238 | #ifdef ALTZONE |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 239 | long altzone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | #endif /* defined ALTZONE */ |
| 241 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 242 | static int_fast32_t |
| 243 | detzcode(const char *const codep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 244 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 245 | register int_fast32_t result; |
| 246 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 247 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 248 | result = (codep[0] & 0x80) ? -1 : 0; |
| 249 | for (i = 0; i < 4; ++i) |
| 250 | result = (result << 8) | (codep[i] & 0xff); |
| 251 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 255 | detzcode64(const char *const codep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 256 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 257 | register time_t result; |
| 258 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 259 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 260 | result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0; |
| 261 | for (i = 0; i < 8; ++i) |
| 262 | result = result * 256 + (codep[i] & 0xff); |
| 263 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 267 | settzname(void) |
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 struct state * const sp = lclptr; |
| 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 | tzname[0] = wildabbr; |
| 273 | tzname[1] = wildabbr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 275 | daylight = 0; |
| 276 | timezone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 277 | #endif /* defined USG_COMPAT */ |
| 278 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 279 | altzone = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 280 | #endif /* defined ALTZONE */ |
| 281 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 282 | if (sp == NULL) { |
| 283 | tzname[0] = tzname[1] = gmt; |
| 284 | return; |
| 285 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 286 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 287 | /* |
| 288 | ** And to get the latest zone names into tzname. . . |
| 289 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 290 | for (i = 0; i < sp->typecnt; ++i) { |
| 291 | register const struct ttinfo * const ttisp = &sp->ttis[i]; |
| 292 | |
| 293 | tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; |
| 294 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 295 | for (i = 0; i < sp->timecnt; ++i) { |
| 296 | register const struct ttinfo * const ttisp = |
| 297 | &sp->ttis[ |
| 298 | sp->types[i]]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 299 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 300 | tzname[ttisp->tt_isdst] = |
| 301 | &sp->chars[ttisp->tt_abbrind]; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 302 | #ifdef USG_COMPAT |
| 303 | if (ttisp->tt_isdst) |
| 304 | daylight = 1; |
| 305 | if (!ttisp->tt_isdst) |
| 306 | timezone = -(ttisp->tt_gmtoff); |
| 307 | #endif /* defined USG_COMPAT */ |
| 308 | #ifdef ALTZONE |
| 309 | if (ttisp->tt_isdst) |
| 310 | altzone = -(ttisp->tt_gmtoff); |
| 311 | #endif /* defined ALTZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 312 | } |
| 313 | /* |
| 314 | ** Finally, scrub the abbreviations. |
| 315 | ** First, replace bogus characters. |
| 316 | */ |
| 317 | for (i = 0; i < sp->charcnt; ++i) |
| 318 | if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) |
| 319 | sp->chars[i] = TZ_ABBR_ERR_CHAR; |
| 320 | /* |
| 321 | ** Second, truncate long abbreviations. |
| 322 | */ |
| 323 | for (i = 0; i < sp->typecnt; ++i) { |
| 324 | register const struct ttinfo * const ttisp = &sp->ttis[i]; |
| 325 | register char * cp = &sp->chars[ttisp->tt_abbrind]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 327 | if (strlen(cp) > TZ_ABBR_MAX_LEN && |
| 328 | strcmp(cp, GRANDPARENTED) != 0) |
| 329 | *(cp + TZ_ABBR_MAX_LEN) = '\0'; |
| 330 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 334 | differ_by_repeat(const time_t t1, const time_t t0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 335 | { |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 336 | if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 337 | return 0; |
Elliott Hughes | 51aeff7 | 2013-10-08 18:30:44 -0700 | [diff] [blame] | 338 | #if __LP64__ // 32-bit Android only has a signed 32-bit time_t; 64-bit Android is fixed. |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 339 | return t1 - t0 == SECSPERREPEAT; |
Elliott Hughes | 51aeff7 | 2013-10-08 18:30:44 -0700 | [diff] [blame] | 340 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 344 | tzload(register const char* name, register struct state* const sp, |
| 345 | register const int doextend) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 346 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 347 | register const char * p; |
| 348 | register int i; |
| 349 | register int fid; |
| 350 | register int stored; |
| 351 | register int nread; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 352 | typedef union { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 353 | struct tzhead tzhead; |
| 354 | char buf[2 * sizeof(struct tzhead) + |
| 355 | 2 * sizeof *sp + |
| 356 | 4 * TZ_MAX_TIMES]; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 357 | } u_t; |
| 358 | #ifdef ALL_STATE |
| 359 | register u_t * up; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 360 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 361 | up = (u_t *) calloc(1, sizeof *up); |
| 362 | if (up == NULL) |
| 363 | return -1; |
| 364 | #else /* !defined ALL_STATE */ |
| 365 | u_t u; |
| 366 | register u_t * const up = &u; |
| 367 | #endif /* !defined ALL_STATE */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 368 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 369 | sp->goback = sp->goahead = FALSE; |
| 370 | if (name == NULL && (name = TZDEFAULT) == NULL) |
| 371 | goto oops; |
| 372 | int toread; |
| 373 | fid = __bionic_open_tzdata(name, &toread); |
| 374 | if (fid < 0) { |
| 375 | return -1; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 376 | } |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 377 | nread = read(fid, up->buf, toread); |
| 378 | if (close(fid) < 0 || nread <= 0) |
| 379 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 380 | for (stored = 4; stored <= 8; stored *= 2) { |
| 381 | int ttisstdcnt; |
| 382 | int ttisgmtcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 383 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 384 | ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt); |
| 385 | ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt); |
| 386 | sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt); |
| 387 | sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt); |
| 388 | sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt); |
| 389 | sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt); |
| 390 | p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 391 | if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || |
| 392 | sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || |
| 393 | sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || |
| 394 | sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || |
| 395 | (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || |
| 396 | (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 397 | goto oops; |
| 398 | if (nread - (p - up->buf) < |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 399 | sp->timecnt * stored + /* ats */ |
| 400 | sp->timecnt + /* types */ |
| 401 | sp->typecnt * 6 + /* ttinfos */ |
| 402 | sp->charcnt + /* chars */ |
| 403 | sp->leapcnt * (stored + 4) + /* lsinfos */ |
| 404 | ttisstdcnt + /* ttisstds */ |
| 405 | ttisgmtcnt) /* ttisgmts */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 406 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 407 | for (i = 0; i < sp->timecnt; ++i) { |
| 408 | sp->ats[i] = (stored == 4) ? |
| 409 | detzcode(p) : detzcode64(p); |
| 410 | p += stored; |
| 411 | } |
| 412 | for (i = 0; i < sp->timecnt; ++i) { |
| 413 | sp->types[i] = (unsigned char) *p++; |
| 414 | if (sp->types[i] >= sp->typecnt) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 415 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 416 | } |
| 417 | for (i = 0; i < sp->typecnt; ++i) { |
| 418 | register struct ttinfo * ttisp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 419 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 420 | ttisp = &sp->ttis[i]; |
| 421 | ttisp->tt_gmtoff = detzcode(p); |
| 422 | p += 4; |
| 423 | ttisp->tt_isdst = (unsigned char) *p++; |
| 424 | if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 425 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 426 | ttisp->tt_abbrind = (unsigned char) *p++; |
| 427 | if (ttisp->tt_abbrind < 0 || |
| 428 | ttisp->tt_abbrind > sp->charcnt) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 429 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 430 | } |
| 431 | for (i = 0; i < sp->charcnt; ++i) |
| 432 | sp->chars[i] = *p++; |
| 433 | sp->chars[i] = '\0'; /* ensure '\0' at end */ |
| 434 | for (i = 0; i < sp->leapcnt; ++i) { |
| 435 | register struct lsinfo * lsisp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 436 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 437 | lsisp = &sp->lsis[i]; |
| 438 | lsisp->ls_trans = (stored == 4) ? |
| 439 | detzcode(p) : detzcode64(p); |
| 440 | p += stored; |
| 441 | lsisp->ls_corr = detzcode(p); |
| 442 | p += 4; |
| 443 | } |
| 444 | for (i = 0; i < sp->typecnt; ++i) { |
| 445 | register struct ttinfo * ttisp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 446 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 447 | ttisp = &sp->ttis[i]; |
| 448 | if (ttisstdcnt == 0) |
| 449 | ttisp->tt_ttisstd = FALSE; |
| 450 | else { |
| 451 | ttisp->tt_ttisstd = *p++; |
| 452 | if (ttisp->tt_ttisstd != TRUE && |
| 453 | ttisp->tt_ttisstd != FALSE) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 454 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | for (i = 0; i < sp->typecnt; ++i) { |
| 458 | register struct ttinfo * ttisp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 459 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 460 | ttisp = &sp->ttis[i]; |
| 461 | if (ttisgmtcnt == 0) |
| 462 | ttisp->tt_ttisgmt = FALSE; |
| 463 | else { |
| 464 | ttisp->tt_ttisgmt = *p++; |
| 465 | if (ttisp->tt_ttisgmt != TRUE && |
| 466 | ttisp->tt_ttisgmt != FALSE) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 467 | goto oops; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | /* |
| 471 | ** Out-of-sort ats should mean we're running on a |
| 472 | ** signed time_t system but using a data file with |
| 473 | ** unsigned values (or vice versa). |
| 474 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 475 | for (i = 0; i < sp->timecnt; ++i) |
| 476 | if ((i < sp->timecnt - 1 && |
| 477 | sp->ats[i] > sp->ats[i + 1]) || |
| 478 | (i == sp->timecnt - 1 && !TYPE_SIGNED(time_t) && |
| 479 | sp->ats[i] > |
| 480 | ((stored == 4) ? INT32_MAX : INT64_MAX))) { |
| 481 | if (TYPE_SIGNED(time_t)) { |
| 482 | /* |
| 483 | ** Ignore the end (easy). |
| 484 | */ |
| 485 | sp->timecnt = i + 1; |
| 486 | } else { |
| 487 | /* |
| 488 | ** Ignore the beginning (harder). |
| 489 | */ |
| 490 | register int j; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 491 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 492 | /* |
| 493 | ** Keep the record right before the |
| 494 | ** epoch boundary, |
| 495 | ** but tweak it so that it starts |
| 496 | ** right with the epoch |
| 497 | ** (thanks to Doug Bailey). |
| 498 | */ |
| 499 | sp->ats[i] = 0; |
| 500 | for (j = 0; j + i < sp->timecnt; ++j) { |
| 501 | sp->ats[j] = sp->ats[j + i]; |
| 502 | sp->types[j] = sp->types[j + i]; |
| 503 | } |
| 504 | sp->timecnt = j; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 505 | } |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 506 | break; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 507 | } |
| 508 | /* |
| 509 | ** If this is an old file, we're done. |
| 510 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 511 | if (up->tzhead.tzh_version[0] == '\0') |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 512 | break; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 513 | nread -= p - up->buf; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 514 | for (i = 0; i < nread; ++i) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 515 | up->buf[i] = p[i]; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 516 | /* |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 517 | ** If this is a narrow time_t system, we're done. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 518 | */ |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 519 | if (stored >= (int) sizeof(time_t)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 520 | break; |
| 521 | } |
| 522 | if (doextend && nread > 2 && |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 523 | up->buf[0] == '\n' && up->buf[nread - 1] == '\n' && |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 524 | sp->typecnt + 2 <= TZ_MAX_TYPES) { |
| 525 | struct state ts; |
| 526 | register int result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 527 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 528 | up->buf[nread - 1] = '\0'; |
| 529 | result = tzparse(&up->buf[1], &ts, FALSE); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 530 | if (result == 0 && ts.typecnt == 2 && |
| 531 | sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) { |
| 532 | for (i = 0; i < 2; ++i) |
| 533 | ts.ttis[i].tt_abbrind += |
| 534 | sp->charcnt; |
| 535 | for (i = 0; i < ts.charcnt; ++i) |
| 536 | sp->chars[sp->charcnt++] = |
| 537 | ts.chars[i]; |
| 538 | i = 0; |
| 539 | while (i < ts.timecnt && |
| 540 | ts.ats[i] <= |
| 541 | sp->ats[sp->timecnt - 1]) |
| 542 | ++i; |
| 543 | while (i < ts.timecnt && |
| 544 | sp->timecnt < TZ_MAX_TIMES) { |
| 545 | sp->ats[sp->timecnt] = |
| 546 | ts.ats[i]; |
| 547 | sp->types[sp->timecnt] = |
| 548 | sp->typecnt + |
| 549 | ts.types[i]; |
| 550 | ++sp->timecnt; |
| 551 | ++i; |
| 552 | } |
| 553 | sp->ttis[sp->typecnt++] = ts.ttis[0]; |
| 554 | sp->ttis[sp->typecnt++] = ts.ttis[1]; |
| 555 | } |
| 556 | } |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 557 | if (sp->timecnt > 1) { |
| 558 | for (i = 1; i < sp->timecnt; ++i) |
| 559 | if (typesequiv(sp, sp->types[i], sp->types[0]) && |
| 560 | differ_by_repeat(sp->ats[i], sp->ats[0])) { |
| 561 | sp->goback = TRUE; |
| 562 | break; |
| 563 | } |
| 564 | for (i = sp->timecnt - 2; i >= 0; --i) |
| 565 | if (typesequiv(sp, sp->types[sp->timecnt - 1], |
| 566 | sp->types[i]) && |
| 567 | differ_by_repeat(sp->ats[sp->timecnt - 1], |
| 568 | sp->ats[i])) { |
| 569 | sp->goahead = TRUE; |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | /* |
| 574 | ** If type 0 is is unused in transitions, |
| 575 | ** it's the type to use for early times. |
| 576 | */ |
| 577 | for (i = 0; i < sp->typecnt; ++i) |
| 578 | if (sp->types[i] == 0) |
| 579 | break; |
| 580 | i = (i >= sp->typecnt) ? 0 : -1; |
| 581 | /* |
| 582 | ** Absent the above, |
| 583 | ** if there are transition times |
| 584 | ** and the first transition is to a daylight time |
| 585 | ** find the standard type less than and closest to |
| 586 | ** the type of the first transition. |
| 587 | */ |
| 588 | if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) { |
| 589 | i = sp->types[0]; |
| 590 | while (--i >= 0) |
| 591 | if (!sp->ttis[i].tt_isdst) |
| 592 | break; |
| 593 | } |
| 594 | /* |
| 595 | ** If no result yet, find the first standard type. |
| 596 | ** If there is none, punt to type zero. |
| 597 | */ |
| 598 | if (i < 0) { |
| 599 | i = 0; |
| 600 | while (sp->ttis[i].tt_isdst) |
| 601 | if (++i >= sp->typecnt) { |
| 602 | i = 0; |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | sp->defaulttype = i; |
| 607 | #ifdef ALL_STATE |
| 608 | free(up); |
| 609 | #endif /* defined ALL_STATE */ |
| 610 | return 0; |
| 611 | oops: |
| 612 | #ifdef ALL_STATE |
| 613 | free(up); |
| 614 | #endif /* defined ALL_STATE */ |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | static int |
| 619 | typesequiv(const struct state *const sp, const int a, const int b) |
| 620 | { |
| 621 | register int result; |
| 622 | |
| 623 | if (sp == NULL || |
| 624 | a < 0 || a >= sp->typecnt || |
| 625 | b < 0 || b >= sp->typecnt) |
| 626 | result = FALSE; |
| 627 | else { |
| 628 | register const struct ttinfo * ap = &sp->ttis[a]; |
| 629 | register const struct ttinfo * bp = &sp->ttis[b]; |
| 630 | result = ap->tt_gmtoff == bp->tt_gmtoff && |
| 631 | ap->tt_isdst == bp->tt_isdst && |
| 632 | ap->tt_ttisstd == bp->tt_ttisstd && |
| 633 | ap->tt_ttisgmt == bp->tt_ttisgmt && |
| 634 | strcmp(&sp->chars[ap->tt_abbrind], |
| 635 | &sp->chars[bp->tt_abbrind]) == 0; |
| 636 | } |
| 637 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 638 | } |
| 639 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 640 | static const int mon_lengths[2][MONSPERYEAR] = { |
| 641 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
| 642 | { 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] | 643 | }; |
| 644 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 645 | static const int year_lengths[2] = { |
| 646 | DAYSPERNYEAR, DAYSPERLYEAR |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 647 | }; |
| 648 | |
| 649 | /* |
| 650 | ** Given a pointer into a time zone string, scan until a character that is not |
| 651 | ** a valid character in a zone name is found. Return a pointer to that |
| 652 | ** character. |
| 653 | */ |
| 654 | |
| 655 | static const char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 656 | getzname(register const char * strp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 657 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 658 | register char c; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 659 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 660 | while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && |
| 661 | c != '+') |
| 662 | ++strp; |
| 663 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | ** Given a pointer into an extended time zone string, scan until the ending |
| 668 | ** delimiter of the zone name is located. Return a pointer to the delimiter. |
| 669 | ** |
| 670 | ** As with getzname above, the legal character set is actually quite |
| 671 | ** restricted, with other characters producing undefined results. |
| 672 | ** We don't do any checking here; checking is done later in common-case code. |
| 673 | */ |
| 674 | |
| 675 | static const char * |
| 676 | getqzname(register const char *strp, const int delim) |
| 677 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 678 | register int c; |
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 | while ((c = *strp) != '\0' && c != delim) |
| 681 | ++strp; |
| 682 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | ** Given a pointer into a time zone string, extract a number from that string. |
| 687 | ** Check that the number is within a specified range; if it is not, return |
| 688 | ** NULL. |
| 689 | ** Otherwise, return a pointer to the first character not part of the number. |
| 690 | */ |
| 691 | |
| 692 | static const char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 693 | getnum(register const char * strp, int * const nump, const int min, const int max) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 694 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 695 | register char c; |
| 696 | register int num; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 697 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 698 | if (strp == NULL || !is_digit(c = *strp)) |
| 699 | return NULL; |
| 700 | num = 0; |
| 701 | do { |
| 702 | num = num * 10 + (c - '0'); |
| 703 | if (num > max) |
| 704 | return NULL; /* illegal value */ |
| 705 | c = *++strp; |
| 706 | } while (is_digit(c)); |
| 707 | if (num < min) |
| 708 | return NULL; /* illegal value */ |
| 709 | *nump = num; |
| 710 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* |
| 714 | ** Given a pointer into a time zone string, extract a number of seconds, |
| 715 | ** in hh[:mm[:ss]] form, from the string. |
| 716 | ** If any error occurs, return NULL. |
| 717 | ** Otherwise, return a pointer to the first character not part of the number |
| 718 | ** of seconds. |
| 719 | */ |
| 720 | |
| 721 | static const char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 722 | getsecs(register const char *strp, int_fast32_t *const secsp) |
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 | int num; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 725 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 726 | /* |
| 727 | ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like |
| 728 | ** "M10.4.6/26", which does not conform to Posix, |
| 729 | ** but which specifies the equivalent of |
| 730 | ** ``02:00 on the first Sunday on or after 23 Oct''. |
| 731 | */ |
| 732 | strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); |
| 733 | if (strp == NULL) |
| 734 | return NULL; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 735 | *secsp = num * (int_fast32_t) SECSPERHOUR; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 736 | if (*strp == ':') { |
| 737 | ++strp; |
| 738 | strp = getnum(strp, &num, 0, MINSPERHOUR - 1); |
| 739 | if (strp == NULL) |
| 740 | return NULL; |
| 741 | *secsp += num * SECSPERMIN; |
| 742 | if (*strp == ':') { |
| 743 | ++strp; |
| 744 | /* `SECSPERMIN' allows for leap seconds. */ |
| 745 | strp = getnum(strp, &num, 0, SECSPERMIN); |
| 746 | if (strp == NULL) |
| 747 | return NULL; |
| 748 | *secsp += num; |
| 749 | } |
| 750 | } |
| 751 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* |
| 755 | ** Given a pointer into a time zone string, extract an offset, in |
| 756 | ** [+-]hh[:mm[:ss]] form, from the string. |
| 757 | ** If any error occurs, return NULL. |
| 758 | ** Otherwise, return a pointer to the first character not part of the time. |
| 759 | */ |
| 760 | |
| 761 | static const char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 762 | getoffset(register const char *strp, int_fast32_t *const offsetp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 763 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 764 | register int neg = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 765 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 766 | if (*strp == '-') { |
| 767 | neg = 1; |
| 768 | ++strp; |
| 769 | } else if (*strp == '+') |
| 770 | ++strp; |
| 771 | strp = getsecs(strp, offsetp); |
| 772 | if (strp == NULL) |
| 773 | return NULL; /* illegal time */ |
| 774 | if (neg) |
| 775 | *offsetp = -*offsetp; |
| 776 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
| 780 | ** Given a pointer into a time zone string, extract a rule in the form |
| 781 | ** date[/time]. See POSIX section 8 for the format of "date" and "time". |
| 782 | ** If a valid rule is not found, return NULL. |
| 783 | ** Otherwise, return a pointer to the first character not part of the rule. |
| 784 | */ |
| 785 | |
| 786 | static const char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 787 | getrule(const char * strp, register struct rule * const rulep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 788 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 789 | if (*strp == 'J') { |
| 790 | /* |
| 791 | ** Julian day. |
| 792 | */ |
| 793 | rulep->r_type = JULIAN_DAY; |
| 794 | ++strp; |
| 795 | strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); |
| 796 | } else if (*strp == 'M') { |
| 797 | /* |
| 798 | ** Month, week, day. |
| 799 | */ |
| 800 | rulep->r_type = MONTH_NTH_DAY_OF_WEEK; |
| 801 | ++strp; |
| 802 | strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); |
| 803 | if (strp == NULL) |
| 804 | return NULL; |
| 805 | if (*strp++ != '.') |
| 806 | return NULL; |
| 807 | strp = getnum(strp, &rulep->r_week, 1, 5); |
| 808 | if (strp == NULL) |
| 809 | return NULL; |
| 810 | if (*strp++ != '.') |
| 811 | return NULL; |
| 812 | strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); |
| 813 | } else if (is_digit(*strp)) { |
| 814 | /* |
| 815 | ** Day of year. |
| 816 | */ |
| 817 | rulep->r_type = DAY_OF_YEAR; |
| 818 | strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); |
| 819 | } else return NULL; /* invalid format */ |
| 820 | if (strp == NULL) |
| 821 | return NULL; |
| 822 | if (*strp == '/') { |
| 823 | /* |
| 824 | ** Time specified. |
| 825 | */ |
| 826 | ++strp; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 827 | strp = getoffset(strp, &rulep->r_time); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 828 | } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ |
| 829 | return strp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* |
| 833 | ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 834 | ** year, a rule, and the offset from UT at the time that rule takes effect, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 835 | ** calculate the Epoch-relative time that rule takes effect. |
| 836 | */ |
| 837 | |
| 838 | static time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 839 | transtime(const time_t janfirst, const int year, |
| 840 | register const struct rule *const rulep, const int_fast32_t offset) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 841 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 842 | register int leapyear; |
| 843 | register time_t value; |
| 844 | register int i; |
| 845 | int d, m1, yy0, yy1, yy2, dow; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 846 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 847 | INITIALIZE(value); |
| 848 | leapyear = isleap(year); |
| 849 | switch (rulep->r_type) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 850 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 851 | case JULIAN_DAY: |
| 852 | /* |
| 853 | ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap |
| 854 | ** years. |
| 855 | ** In non-leap years, or if the day number is 59 or less, just |
| 856 | ** add SECSPERDAY times the day number-1 to the time of |
| 857 | ** January 1, midnight, to get the day. |
| 858 | */ |
| 859 | value = janfirst + (rulep->r_day - 1) * SECSPERDAY; |
| 860 | if (leapyear && rulep->r_day >= 60) |
| 861 | value += SECSPERDAY; |
| 862 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 863 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 864 | case DAY_OF_YEAR: |
| 865 | /* |
| 866 | ** n - day of year. |
| 867 | ** Just add SECSPERDAY times the day number to the time of |
| 868 | ** January 1, midnight, to get the day. |
| 869 | */ |
| 870 | value = janfirst + rulep->r_day * SECSPERDAY; |
| 871 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 872 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 873 | case MONTH_NTH_DAY_OF_WEEK: |
| 874 | /* |
| 875 | ** Mm.n.d - nth "dth day" of month m. |
| 876 | */ |
| 877 | value = janfirst; |
| 878 | for (i = 0; i < rulep->r_mon - 1; ++i) |
| 879 | value += mon_lengths[leapyear][i] * SECSPERDAY; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 880 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 881 | /* |
| 882 | ** Use Zeller's Congruence to get day-of-week of first day of |
| 883 | ** month. |
| 884 | */ |
| 885 | m1 = (rulep->r_mon + 9) % 12 + 1; |
| 886 | yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; |
| 887 | yy1 = yy0 / 100; |
| 888 | yy2 = yy0 % 100; |
| 889 | dow = ((26 * m1 - 2) / 10 + |
| 890 | 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; |
| 891 | if (dow < 0) |
| 892 | dow += DAYSPERWEEK; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 893 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 894 | /* |
| 895 | ** "dow" is the day-of-week of the first day of the month. Get |
| 896 | ** the day-of-month (zero-origin) of the first "dow" day of the |
| 897 | ** month. |
| 898 | */ |
| 899 | d = rulep->r_day - dow; |
| 900 | if (d < 0) |
| 901 | d += DAYSPERWEEK; |
| 902 | for (i = 1; i < rulep->r_week; ++i) { |
| 903 | if (d + DAYSPERWEEK >= |
| 904 | mon_lengths[leapyear][rulep->r_mon - 1]) |
| 905 | break; |
| 906 | d += DAYSPERWEEK; |
| 907 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 908 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 909 | /* |
| 910 | ** "d" is the day-of-month (zero-origin) of the day we want. |
| 911 | */ |
| 912 | value += d * SECSPERDAY; |
| 913 | break; |
| 914 | } |
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 | /* |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 917 | ** "value" is the Epoch-relative time of 00:00:00 UT on the day in |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 918 | ** question. To get the Epoch-relative time of the specified local |
| 919 | ** time on that day, add the transition time and the current offset |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 920 | ** from UT. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 921 | */ |
| 922 | return value + rulep->r_time + offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
| 926 | ** Given a POSIX section 8-style TZ string, fill in the rule tables as |
| 927 | ** appropriate. |
| 928 | */ |
| 929 | |
| 930 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 931 | tzparse(const char * name, register struct state * const sp, |
| 932 | const int lastditch) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 933 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 934 | const char * stdname; |
| 935 | const char * dstname; |
| 936 | size_t stdlen; |
| 937 | size_t dstlen; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 938 | int_fast32_t stdoffset; |
| 939 | int_fast32_t dstoffset; |
| 940 | register time_t * atp; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 941 | register unsigned char * typep; |
| 942 | register char * cp; |
| 943 | register int load_result; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 944 | static struct ttinfo zttinfo; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 945 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 946 | INITIALIZE(dstname); |
| 947 | stdname = name; |
| 948 | if (lastditch) { |
| 949 | stdlen = strlen(name); /* length of standard zone name */ |
| 950 | name += stdlen; |
| 951 | if (stdlen >= sizeof sp->chars) |
| 952 | stdlen = (sizeof sp->chars) - 1; |
| 953 | stdoffset = 0; |
| 954 | } else { |
| 955 | if (*name == '<') { |
| 956 | name++; |
| 957 | stdname = name; |
| 958 | name = getqzname(name, '>'); |
| 959 | if (*name != '>') |
| 960 | return (-1); |
| 961 | stdlen = name - stdname; |
| 962 | name++; |
| 963 | } else { |
| 964 | name = getzname(name); |
| 965 | stdlen = name - stdname; |
| 966 | } |
| 967 | if (*name == '\0') |
| 968 | return -1; |
| 969 | name = getoffset(name, &stdoffset); |
| 970 | if (name == NULL) |
| 971 | return -1; |
| 972 | } |
| 973 | load_result = tzload(TZDEFRULES, sp, FALSE); |
| 974 | if (load_result != 0) |
| 975 | sp->leapcnt = 0; /* so, we're off a little */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 976 | if (*name != '\0') { |
| 977 | if (*name == '<') { |
| 978 | dstname = ++name; |
| 979 | name = getqzname(name, '>'); |
| 980 | if (*name != '>') |
| 981 | return -1; |
| 982 | dstlen = name - dstname; |
| 983 | name++; |
| 984 | } else { |
| 985 | dstname = name; |
| 986 | name = getzname(name); |
| 987 | dstlen = name - dstname; /* length of DST zone name */ |
| 988 | } |
| 989 | if (*name != '\0' && *name != ',' && *name != ';') { |
| 990 | name = getoffset(name, &dstoffset); |
| 991 | if (name == NULL) |
| 992 | return -1; |
| 993 | } else dstoffset = stdoffset - SECSPERHOUR; |
| 994 | if (*name == '\0' && load_result != 0) |
| 995 | name = TZDEFRULESTRING; |
| 996 | if (*name == ',' || *name == ';') { |
| 997 | struct rule start; |
| 998 | struct rule end; |
| 999 | register int year; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1000 | register int yearlim; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1001 | register time_t janfirst; |
| 1002 | time_t starttime; |
| 1003 | time_t endtime; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1004 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1005 | ++name; |
| 1006 | if ((name = getrule(name, &start)) == NULL) |
| 1007 | return -1; |
| 1008 | if (*name++ != ',') |
| 1009 | return -1; |
| 1010 | if ((name = getrule(name, &end)) == NULL) |
| 1011 | return -1; |
| 1012 | if (*name != '\0') |
| 1013 | return -1; |
| 1014 | sp->typecnt = 2; /* standard time and DST */ |
| 1015 | /* |
| 1016 | ** Two transitions per year, from EPOCH_YEAR forward. |
| 1017 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1018 | sp->ttis[0] = sp->ttis[1] = zttinfo; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1019 | sp->ttis[0].tt_gmtoff = -dstoffset; |
| 1020 | sp->ttis[0].tt_isdst = 1; |
| 1021 | sp->ttis[0].tt_abbrind = stdlen + 1; |
| 1022 | sp->ttis[1].tt_gmtoff = -stdoffset; |
| 1023 | sp->ttis[1].tt_isdst = 0; |
| 1024 | sp->ttis[1].tt_abbrind = 0; |
| 1025 | atp = sp->ats; |
| 1026 | typep = sp->types; |
| 1027 | janfirst = 0; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1028 | yearlim = EPOCH_YEAR + YEARSPERREPEAT; |
| 1029 | for (year = EPOCH_YEAR; year < yearlim; year++) { |
| 1030 | int_fast32_t yearsecs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1031 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1032 | starttime = transtime(janfirst, year, &start, |
| 1033 | stdoffset); |
| 1034 | endtime = transtime(janfirst, year, &end, |
| 1035 | dstoffset); |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1036 | yearsecs = (year_lengths[isleap(year)] |
| 1037 | * SECSPERDAY); |
| 1038 | if (starttime > endtime |
| 1039 | || (starttime < endtime |
| 1040 | && (endtime - starttime |
| 1041 | < (yearsecs |
| 1042 | + (stdoffset - dstoffset))))) { |
| 1043 | if (&sp->ats[TZ_MAX_TIMES - 2] < atp) |
| 1044 | break; |
| 1045 | yearlim = year + YEARSPERREPEAT + 1; |
| 1046 | if (starttime > endtime) { |
| 1047 | *atp++ = endtime; |
| 1048 | *typep++ = 1; /* DST ends */ |
| 1049 | *atp++ = starttime; |
| 1050 | *typep++ = 0; /* DST begins */ |
| 1051 | } else { |
| 1052 | *atp++ = starttime; |
| 1053 | *typep++ = 0; /* DST begins */ |
| 1054 | *atp++ = endtime; |
| 1055 | *typep++ = 1; /* DST ends */ |
| 1056 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1057 | } |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1058 | if (time_t_max - janfirst < yearsecs) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1059 | break; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1060 | janfirst += yearsecs; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1061 | } |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1062 | sp->timecnt = atp - sp->ats; |
| 1063 | if (!sp->timecnt) |
| 1064 | sp->typecnt = 1; /* Perpetual DST. */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1065 | } else { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1066 | register int_fast32_t theirstdoffset; |
| 1067 | register int_fast32_t theirdstoffset; |
| 1068 | register int_fast32_t theiroffset; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1069 | register int isdst; |
| 1070 | register int i; |
| 1071 | register int j; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1072 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1073 | if (*name != '\0') |
| 1074 | return -1; |
| 1075 | /* |
| 1076 | ** Initial values of theirstdoffset and theirdstoffset. |
| 1077 | */ |
| 1078 | theirstdoffset = 0; |
| 1079 | for (i = 0; i < sp->timecnt; ++i) { |
| 1080 | j = sp->types[i]; |
| 1081 | if (!sp->ttis[j].tt_isdst) { |
| 1082 | theirstdoffset = |
| 1083 | -sp->ttis[j].tt_gmtoff; |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | theirdstoffset = 0; |
| 1088 | for (i = 0; i < sp->timecnt; ++i) { |
| 1089 | j = sp->types[i]; |
| 1090 | if (sp->ttis[j].tt_isdst) { |
| 1091 | theirdstoffset = |
| 1092 | -sp->ttis[j].tt_gmtoff; |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | /* |
| 1097 | ** Initially we're assumed to be in standard time. |
| 1098 | */ |
| 1099 | isdst = FALSE; |
| 1100 | theiroffset = theirstdoffset; |
| 1101 | /* |
| 1102 | ** Now juggle transition times and types |
| 1103 | ** tracking offsets as you do. |
| 1104 | */ |
| 1105 | for (i = 0; i < sp->timecnt; ++i) { |
| 1106 | j = sp->types[i]; |
| 1107 | sp->types[i] = sp->ttis[j].tt_isdst; |
| 1108 | if (sp->ttis[j].tt_ttisgmt) { |
| 1109 | /* No adjustment to transition time */ |
| 1110 | } else { |
| 1111 | /* |
| 1112 | ** If summer time is in effect, and the |
| 1113 | ** transition time was not specified as |
| 1114 | ** standard time, add the summer time |
| 1115 | ** offset to the transition time; |
| 1116 | ** otherwise, add the standard time |
| 1117 | ** offset to the transition time. |
| 1118 | */ |
| 1119 | /* |
| 1120 | ** Transitions from DST to DDST |
| 1121 | ** will effectively disappear since |
| 1122 | ** POSIX provides for only one DST |
| 1123 | ** offset. |
| 1124 | */ |
| 1125 | if (isdst && !sp->ttis[j].tt_ttisstd) { |
| 1126 | sp->ats[i] += dstoffset - |
| 1127 | theirdstoffset; |
| 1128 | } else { |
| 1129 | sp->ats[i] += stdoffset - |
| 1130 | theirstdoffset; |
| 1131 | } |
| 1132 | } |
| 1133 | theiroffset = -sp->ttis[j].tt_gmtoff; |
| 1134 | if (sp->ttis[j].tt_isdst) |
| 1135 | theirdstoffset = theiroffset; |
| 1136 | else theirstdoffset = theiroffset; |
| 1137 | } |
| 1138 | /* |
| 1139 | ** Finally, fill in ttis. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1140 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1141 | sp->ttis[0] = sp->ttis[1] = zttinfo; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1142 | sp->ttis[0].tt_gmtoff = -stdoffset; |
| 1143 | sp->ttis[0].tt_isdst = FALSE; |
| 1144 | sp->ttis[0].tt_abbrind = 0; |
| 1145 | sp->ttis[1].tt_gmtoff = -dstoffset; |
| 1146 | sp->ttis[1].tt_isdst = TRUE; |
| 1147 | sp->ttis[1].tt_abbrind = stdlen + 1; |
| 1148 | sp->typecnt = 2; |
| 1149 | } |
| 1150 | } else { |
| 1151 | dstlen = 0; |
| 1152 | sp->typecnt = 1; /* only standard time */ |
| 1153 | sp->timecnt = 0; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1154 | sp->ttis[0] = zttinfo; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1155 | sp->ttis[0].tt_gmtoff = -stdoffset; |
| 1156 | sp->ttis[0].tt_isdst = 0; |
| 1157 | sp->ttis[0].tt_abbrind = 0; |
| 1158 | } |
| 1159 | sp->charcnt = stdlen + 1; |
| 1160 | if (dstlen != 0) |
| 1161 | sp->charcnt += dstlen + 1; |
| 1162 | if ((size_t) sp->charcnt > sizeof sp->chars) |
| 1163 | return -1; |
| 1164 | cp = sp->chars; |
| 1165 | (void) strncpy(cp, stdname, stdlen); |
| 1166 | cp += stdlen; |
| 1167 | *cp++ = '\0'; |
| 1168 | if (dstlen != 0) { |
| 1169 | (void) strncpy(cp, dstname, dstlen); |
| 1170 | *(cp + dstlen) = '\0'; |
| 1171 | } |
| 1172 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | static void |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1176 | gmtload(struct state * const sp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1177 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1178 | if (tzload(gmt, sp, TRUE) != 0) |
| 1179 | (void) tzparse(gmt, sp, TRUE); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1182 | #ifndef STD_INSPIRED |
| 1183 | /* |
| 1184 | ** A non-static declaration of tzsetwall in a system header file |
| 1185 | ** may cause a warning about this upcoming static declaration... |
| 1186 | */ |
| 1187 | static |
| 1188 | #endif /* !defined STD_INSPIRED */ |
| 1189 | void |
| 1190 | tzsetwall(void) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1191 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1192 | if (lcl_is_set < 0) |
| 1193 | return; |
| 1194 | lcl_is_set = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1195 | |
| 1196 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1197 | if (lclptr == NULL) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1198 | lclptr = calloc(1, sizeof *lclptr); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1199 | if (lclptr == NULL) { |
| 1200 | settzname(); /* all we can do */ |
| 1201 | return; |
| 1202 | } |
| 1203 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1204 | #endif /* defined ALL_STATE */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1205 | if (tzload(NULL, lclptr, TRUE) != 0) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1206 | gmtload(lclptr); |
| 1207 | settzname(); |
| 1208 | } |
| 1209 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1210 | #include <sys/system_properties.h> // For __system_property_get. |
| 1211 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1212 | static void |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1213 | tzset_locked(void) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1214 | { |
| 1215 | register const char * name = NULL; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1216 | |
| 1217 | name = getenv("TZ"); |
| 1218 | |
| 1219 | // try the "persist.sys.timezone" system property first |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1220 | static char buf[PROP_VALUE_MAX]; |
| 1221 | if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1222 | name = buf; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1223 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1224 | |
| 1225 | if (name == NULL) { |
| 1226 | tzsetwall(); |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) |
| 1231 | return; |
| 1232 | lcl_is_set = strlen(name) < sizeof lcl_TZname; |
| 1233 | if (lcl_is_set) |
| 1234 | (void) strcpy(lcl_TZname, name); |
| 1235 | |
| 1236 | #ifdef ALL_STATE |
| 1237 | if (lclptr == NULL) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1238 | lclptr = calloc(1, sizeof *lclptr); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1239 | if (lclptr == NULL) { |
| 1240 | settzname(); /* all we can do */ |
| 1241 | return; |
| 1242 | } |
| 1243 | } |
| 1244 | #endif /* defined ALL_STATE */ |
| 1245 | if (*name == '\0') { |
| 1246 | /* |
| 1247 | ** User wants it fast rather than right. |
| 1248 | */ |
| 1249 | lclptr->leapcnt = 0; /* so, we're off a little */ |
| 1250 | lclptr->timecnt = 0; |
| 1251 | lclptr->typecnt = 0; |
| 1252 | lclptr->ttis[0].tt_isdst = 0; |
| 1253 | lclptr->ttis[0].tt_gmtoff = 0; |
| 1254 | lclptr->ttis[0].tt_abbrind = 0; |
| 1255 | (void) strcpy(lclptr->chars, gmt); |
| 1256 | } else if (tzload(name, lclptr, TRUE) != 0) |
| 1257 | if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0) |
| 1258 | (void) gmtload(lclptr); |
| 1259 | settzname(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | void |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1263 | tzset(void) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1264 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1265 | _tzLock(); |
| 1266 | tzset_locked(); |
| 1267 | _tzUnlock(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | ** The easy way to behave "as if no library function calls" localtime |
| 1272 | ** is to not call it--so we drop its guts into "localsub", which can be |
| 1273 | ** freely called. (And no, the PANS doesn't require the above behavior-- |
| 1274 | ** but it *is* desirable.) |
| 1275 | ** |
| 1276 | ** The unused offset argument is for the benefit of mktime variants. |
| 1277 | */ |
| 1278 | |
| 1279 | /*ARGSUSED*/ |
| 1280 | static struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1281 | localsub(const time_t * const timep, const int_fast32_t offset, |
| 1282 | struct tm * const tmp, const struct state * sp) // android-changed: added sp. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1283 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1284 | register const struct ttinfo * ttisp; |
| 1285 | register int i; |
| 1286 | register struct tm * result; |
| 1287 | const time_t t = *timep; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1288 | |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1289 | // BEGIN android-changed: support user-supplied sp. |
| 1290 | if (sp == NULL) { |
| 1291 | sp = lclptr; |
| 1292 | } |
| 1293 | // END android-changed |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1294 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1295 | if (sp == NULL) |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1296 | return gmtsub(timep, offset, tmp, sp); // android-changed: added sp. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1297 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1298 | if ((sp->goback && t < sp->ats[0]) || |
| 1299 | (sp->goahead && t > sp->ats[sp->timecnt - 1])) { |
| 1300 | time_t newt = t; |
| 1301 | register time_t seconds; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1302 | register time_t years; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1303 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1304 | if (t < sp->ats[0]) |
| 1305 | seconds = sp->ats[0] - t; |
| 1306 | else seconds = t - sp->ats[sp->timecnt - 1]; |
| 1307 | --seconds; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1308 | years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT; |
| 1309 | seconds = years * AVGSECSPERYEAR; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1310 | if (t < sp->ats[0]) |
| 1311 | newt += seconds; |
| 1312 | else newt -= seconds; |
| 1313 | if (newt < sp->ats[0] || |
| 1314 | newt > sp->ats[sp->timecnt - 1]) |
| 1315 | return NULL; /* "cannot happen" */ |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1316 | result = localsub(&newt, offset, tmp, sp); // android-changed: added sp. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1317 | if (result == tmp) { |
| 1318 | register time_t newy; |
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 | newy = tmp->tm_year; |
| 1321 | if (t < sp->ats[0]) |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1322 | newy -= years; |
| 1323 | else newy += years; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1324 | tmp->tm_year = newy; |
| 1325 | if (tmp->tm_year != newy) |
| 1326 | return NULL; |
| 1327 | } |
| 1328 | return result; |
| 1329 | } |
| 1330 | if (sp->timecnt == 0 || t < sp->ats[0]) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1331 | i = sp->defaulttype; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1332 | } else { |
| 1333 | register int lo = 1; |
| 1334 | register int hi = sp->timecnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1335 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1336 | while (lo < hi) { |
| 1337 | register int mid = (lo + hi) >> 1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1338 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1339 | if (t < sp->ats[mid]) |
| 1340 | hi = mid; |
| 1341 | else lo = mid + 1; |
| 1342 | } |
| 1343 | i = (int) sp->types[lo - 1]; |
| 1344 | } |
| 1345 | ttisp = &sp->ttis[i]; |
| 1346 | /* |
| 1347 | ** To get (wrong) behavior that's compatible with System V Release 2.0 |
| 1348 | ** you'd replace the statement below with |
| 1349 | ** t += ttisp->tt_gmtoff; |
| 1350 | ** timesub(&t, 0L, sp, tmp); |
| 1351 | */ |
| 1352 | result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); |
| 1353 | tmp->tm_isdst = ttisp->tt_isdst; |
| 1354 | tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1355 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1356 | tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1357 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1358 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1362 | localtime(const time_t * const timep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1363 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1364 | return localtime_r(timep, &tmGlobal); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | ** Re-entrant version of localtime. |
| 1369 | */ |
| 1370 | |
| 1371 | struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1372 | localtime_r(const time_t * const timep, struct tm * tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1373 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1374 | struct tm* result; |
| 1375 | |
| 1376 | _tzLock(); |
| 1377 | tzset_locked(); |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1378 | result = localsub(timep, 0L, tmp, NULL); // android-changed: extra parameter. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1379 | _tzUnlock(); |
| 1380 | |
| 1381 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | ** gmtsub is to gmtime as localsub is to localtime. |
| 1386 | */ |
| 1387 | |
| 1388 | static struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1389 | gmtsub(const time_t * const timep, const int_fast32_t offset, |
| 1390 | struct tm *const tmp, const struct state * sp) // android-changed: added sp. |
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 | register struct tm * result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1393 | |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1394 | (void) sp; // android-added: unused. |
| 1395 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1396 | if (!gmt_is_set) { |
| 1397 | gmt_is_set = TRUE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1398 | #ifdef ALL_STATE |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1399 | gmtptr = calloc(1, sizeof *gmtptr); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1400 | if (gmtptr != NULL) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1401 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1402 | gmtload(gmtptr); |
| 1403 | } |
| 1404 | result = timesub(timep, offset, gmtptr, tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1405 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | ** Could get fancy here and deliver something such as |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1408 | ** "UT+xxxx" or "UT-xxxx" if offset is non-zero, |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1409 | ** but this is no time for a treasure hunt. |
| 1410 | */ |
| 1411 | if (offset != 0) |
| 1412 | tmp->TM_ZONE = wildabbr; |
| 1413 | else { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1414 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1415 | if (gmtptr == NULL) |
| 1416 | tmp->TM_ZONE = gmt; |
| 1417 | else tmp->TM_ZONE = gmtptr->chars; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1418 | #endif /* defined ALL_STATE */ |
| 1419 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1420 | tmp->TM_ZONE = gmtptr->chars; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1421 | #endif /* State Farm */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1422 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1423 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1424 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1428 | gmtime(const time_t * const timep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1429 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1430 | return gmtime_r(timep, &tmGlobal); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Re-entrant version of gmtime. |
| 1435 | */ |
| 1436 | |
| 1437 | struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1438 | gmtime_r(const time_t * const timep, struct tm * tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1439 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1440 | struct tm* result; |
| 1441 | |
| 1442 | _tzLock(); |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1443 | result = gmtsub(timep, 0L, tmp, NULL); // android-changed: extra parameter. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1444 | _tzUnlock(); |
| 1445 | |
| 1446 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1447 | } |
| 1448 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1449 | /* |
| 1450 | ** Return the number of leap years through the end of the given year |
| 1451 | ** where, to make the math easy, the answer for year zero is defined as zero. |
| 1452 | */ |
| 1453 | |
| 1454 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1455 | leaps_thru_end_of(register const int y) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1456 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1457 | return (y >= 0) ? (y / 4 - y / 100 + y / 400) : |
| 1458 | -(leaps_thru_end_of(-(y + 1)) + 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | static struct tm * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1462 | timesub(const time_t *const timep, const int_fast32_t offset, |
| 1463 | register const struct state *const sp, |
| 1464 | register struct tm *const tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1465 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1466 | register const struct lsinfo * lp; |
| 1467 | register time_t tdays; |
| 1468 | register int idays; /* unsigned would be so 2003 */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1469 | register int_fast64_t rem; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1470 | int y; |
| 1471 | register const int * ip; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1472 | register int_fast64_t corr; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1473 | register int hit; |
| 1474 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1475 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1476 | corr = 0; |
| 1477 | hit = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1478 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1479 | i = (sp == NULL) ? 0 : sp->leapcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1480 | #endif /* defined ALL_STATE */ |
| 1481 | #ifndef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1482 | i = sp->leapcnt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1483 | #endif /* State Farm */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1484 | while (--i >= 0) { |
| 1485 | lp = &sp->lsis[i]; |
| 1486 | if (*timep >= lp->ls_trans) { |
| 1487 | if (*timep == lp->ls_trans) { |
| 1488 | hit = ((i == 0 && lp->ls_corr > 0) || |
| 1489 | lp->ls_corr > sp->lsis[i - 1].ls_corr); |
| 1490 | if (hit) |
| 1491 | while (i > 0 && |
| 1492 | sp->lsis[i].ls_trans == |
| 1493 | sp->lsis[i - 1].ls_trans + 1 && |
| 1494 | sp->lsis[i].ls_corr == |
| 1495 | sp->lsis[i - 1].ls_corr + 1) { |
| 1496 | ++hit; |
| 1497 | --i; |
| 1498 | } |
| 1499 | } |
| 1500 | corr = lp->ls_corr; |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | y = EPOCH_YEAR; |
| 1505 | tdays = *timep / SECSPERDAY; |
| 1506 | rem = *timep - tdays * SECSPERDAY; |
| 1507 | while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { |
| 1508 | int newy; |
| 1509 | register time_t tdelta; |
| 1510 | register int idelta; |
| 1511 | register int leapdays; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1512 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1513 | tdelta = tdays / DAYSPERLYEAR; |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1514 | if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta) |
| 1515 | && tdelta <= INT_MAX)) |
| 1516 | return NULL; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1517 | idelta = tdelta; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1518 | if (idelta == 0) |
| 1519 | idelta = (tdays < 0) ? -1 : 1; |
| 1520 | newy = y; |
| 1521 | if (increment_overflow(&newy, idelta)) |
| 1522 | return NULL; |
| 1523 | leapdays = leaps_thru_end_of(newy - 1) - |
| 1524 | leaps_thru_end_of(y - 1); |
| 1525 | tdays -= ((time_t) newy - y) * DAYSPERNYEAR; |
| 1526 | tdays -= leapdays; |
| 1527 | y = newy; |
| 1528 | } |
| 1529 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1530 | register int_fast32_t seconds; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1531 | |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1532 | seconds = tdays * SECSPERDAY; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1533 | tdays = seconds / SECSPERDAY; |
| 1534 | rem += seconds - tdays * SECSPERDAY; |
| 1535 | } |
| 1536 | /* |
| 1537 | ** Given the range, we can now fearlessly cast... |
| 1538 | */ |
| 1539 | idays = tdays; |
| 1540 | rem += offset - corr; |
| 1541 | while (rem < 0) { |
| 1542 | rem += SECSPERDAY; |
| 1543 | --idays; |
| 1544 | } |
| 1545 | while (rem >= SECSPERDAY) { |
| 1546 | rem -= SECSPERDAY; |
| 1547 | ++idays; |
| 1548 | } |
| 1549 | while (idays < 0) { |
| 1550 | if (increment_overflow(&y, -1)) |
| 1551 | return NULL; |
| 1552 | idays += year_lengths[isleap(y)]; |
| 1553 | } |
| 1554 | while (idays >= year_lengths[isleap(y)]) { |
| 1555 | idays -= year_lengths[isleap(y)]; |
| 1556 | if (increment_overflow(&y, 1)) |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | tmp->tm_year = y; |
| 1560 | if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE)) |
| 1561 | return NULL; |
| 1562 | tmp->tm_yday = idays; |
| 1563 | /* |
| 1564 | ** The "extra" mods below avoid overflow problems. |
| 1565 | */ |
| 1566 | tmp->tm_wday = EPOCH_WDAY + |
| 1567 | ((y - EPOCH_YEAR) % DAYSPERWEEK) * |
| 1568 | (DAYSPERNYEAR % DAYSPERWEEK) + |
| 1569 | leaps_thru_end_of(y - 1) - |
| 1570 | leaps_thru_end_of(EPOCH_YEAR - 1) + |
| 1571 | idays; |
| 1572 | tmp->tm_wday %= DAYSPERWEEK; |
| 1573 | if (tmp->tm_wday < 0) |
| 1574 | tmp->tm_wday += DAYSPERWEEK; |
| 1575 | tmp->tm_hour = (int) (rem / SECSPERHOUR); |
| 1576 | rem %= SECSPERHOUR; |
| 1577 | tmp->tm_min = (int) (rem / SECSPERMIN); |
| 1578 | /* |
| 1579 | ** A positive leap second requires a special |
| 1580 | ** representation. This uses "... ??:59:60" et seq. |
| 1581 | */ |
| 1582 | tmp->tm_sec = (int) (rem % SECSPERMIN) + hit; |
| 1583 | ip = mon_lengths[isleap(y)]; |
| 1584 | for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon)) |
| 1585 | idays -= ip[tmp->tm_mon]; |
| 1586 | tmp->tm_mday = (int) (idays + 1); |
| 1587 | tmp->tm_isdst = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1588 | #ifdef TM_GMTOFF |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1589 | tmp->TM_GMTOFF = offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1590 | #endif /* defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1591 | return tmp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1595 | ctime(const time_t * const timep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1596 | { |
| 1597 | /* |
| 1598 | ** 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] | 1599 | ** The ctime function converts the calendar time pointed to by timer |
| 1600 | ** to local time in the form of a string. It is equivalent to |
| 1601 | ** asctime(localtime(timer)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1602 | */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1603 | return asctime(localtime(timep)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | char * |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1607 | ctime_r(const time_t * const timep, char * buf) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1608 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1609 | struct tm mytm; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1610 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1611 | return asctime_r(localtime_r(timep, &mytm), buf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | ** Adapted from code provided by Robert Elz, who writes: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1616 | ** The "best" way to do mktime I think is based on an idea of Bob |
| 1617 | ** Kridle's (so its said...) from a long time ago. |
| 1618 | ** It does a binary search of the time_t space. Since time_t's are |
| 1619 | ** just 32 bits, its a max of 32 iterations (even at 64 bits it |
| 1620 | ** would still be very reasonable). |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1621 | */ |
| 1622 | |
| 1623 | #ifndef WRONG |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1624 | #define WRONG (-1) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1625 | #endif /* !defined WRONG */ |
| 1626 | |
| 1627 | /* |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1628 | ** Normalize logic courtesy Paul Eggert. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1629 | */ |
| 1630 | |
| 1631 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1632 | increment_overflow(int *const ip, int j) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1633 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1634 | register int const i = *ip; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1635 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1636 | /* |
| 1637 | ** If i >= 0 there can only be overflow if i + j > INT_MAX |
| 1638 | ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow. |
| 1639 | ** If i < 0 there can only be overflow if i + j < INT_MIN |
| 1640 | ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow. |
| 1641 | */ |
| 1642 | if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i)) |
| 1643 | return TRUE; |
| 1644 | *ip += j; |
| 1645 | return FALSE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1649 | increment_overflow32(int_fast32_t *const lp, int const m) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1650 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1651 | register int_fast32_t const l = *lp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1652 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1653 | if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l)) |
| 1654 | return TRUE; |
| 1655 | *lp += m; |
| 1656 | return FALSE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1660 | normalize_overflow(int *const tensptr, int *const unitsptr, const int base) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1661 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1662 | register int tensdelta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1663 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1664 | tensdelta = (*unitsptr >= 0) ? |
| 1665 | (*unitsptr / base) : |
| 1666 | (-1 - (-1 - *unitsptr) / base); |
| 1667 | *unitsptr -= tensdelta * base; |
| 1668 | return increment_overflow(tensptr, tensdelta); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1672 | normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr, |
| 1673 | const int base) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1674 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1675 | register int tensdelta; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1676 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1677 | tensdelta = (*unitsptr >= 0) ? |
| 1678 | (*unitsptr / base) : |
| 1679 | (-1 - (-1 - *unitsptr) / base); |
| 1680 | *unitsptr -= tensdelta * base; |
| 1681 | return increment_overflow32(tensptr, tensdelta); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | static int |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1685 | tmcomp(register const struct tm * const atmp, |
| 1686 | register const struct tm * const btmp) |
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 | register int result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1689 | |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 1690 | if (atmp->tm_year != btmp->tm_year) |
| 1691 | return atmp->tm_year < btmp->tm_year ? -1 : 1; |
| 1692 | if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 && |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1693 | (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && |
| 1694 | (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && |
| 1695 | (result = (atmp->tm_min - btmp->tm_min)) == 0) |
| 1696 | result = atmp->tm_sec - btmp->tm_sec; |
| 1697 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | static time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1701 | time2sub(struct tm * const tmp, |
| 1702 | struct tm *(*const funcp)(const time_t*, int_fast32_t, struct tm*, const struct state*), |
| 1703 | const int_fast32_t offset, |
| 1704 | int * const okayp, |
| 1705 | const int do_norm_secs, const struct state * sp) // android-changed: added sp |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1706 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1707 | register int dir; |
| 1708 | register int i, j; |
| 1709 | register int saved_seconds; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1710 | register int_fast32_t li; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1711 | register time_t lo; |
| 1712 | register time_t hi; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1713 | int_fast32_t y; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1714 | time_t newt; |
| 1715 | time_t t; |
| 1716 | struct tm yourtm, mytm; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1717 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1718 | *okayp = FALSE; |
| 1719 | yourtm = *tmp; |
| 1720 | if (do_norm_secs) { |
| 1721 | if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, |
| 1722 | SECSPERMIN)) |
| 1723 | return WRONG; |
| 1724 | } |
| 1725 | if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) |
| 1726 | return WRONG; |
| 1727 | if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) |
| 1728 | return WRONG; |
| 1729 | y = yourtm.tm_year; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1730 | if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1731 | return WRONG; |
| 1732 | /* |
| 1733 | ** Turn y into an actual year number for now. |
| 1734 | ** It is converted back to an offset from TM_YEAR_BASE later. |
| 1735 | */ |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1736 | if (increment_overflow32(&y, TM_YEAR_BASE)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1737 | return WRONG; |
| 1738 | while (yourtm.tm_mday <= 0) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1739 | if (increment_overflow32(&y, -1)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1740 | return WRONG; |
| 1741 | li = y + (1 < yourtm.tm_mon); |
| 1742 | yourtm.tm_mday += year_lengths[isleap(li)]; |
| 1743 | } |
| 1744 | while (yourtm.tm_mday > DAYSPERLYEAR) { |
| 1745 | li = y + (1 < yourtm.tm_mon); |
| 1746 | yourtm.tm_mday -= year_lengths[isleap(li)]; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1747 | if (increment_overflow32(&y, 1)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1748 | return WRONG; |
| 1749 | } |
| 1750 | for ( ; ; ) { |
| 1751 | i = mon_lengths[isleap(y)][yourtm.tm_mon]; |
| 1752 | if (yourtm.tm_mday <= i) |
| 1753 | break; |
| 1754 | yourtm.tm_mday -= i; |
| 1755 | if (++yourtm.tm_mon >= MONSPERYEAR) { |
| 1756 | yourtm.tm_mon = 0; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1757 | if (increment_overflow32(&y, 1)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1758 | return WRONG; |
| 1759 | } |
| 1760 | } |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1761 | if (increment_overflow32(&y, -TM_YEAR_BASE)) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1762 | return WRONG; |
| 1763 | yourtm.tm_year = y; |
| 1764 | if (yourtm.tm_year != y) |
| 1765 | return WRONG; |
| 1766 | if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) |
| 1767 | saved_seconds = 0; |
| 1768 | else if (y + TM_YEAR_BASE < EPOCH_YEAR) { |
| 1769 | /* |
| 1770 | ** We can't set tm_sec to 0, because that might push the |
| 1771 | ** time below the minimum representable time. |
| 1772 | ** Set tm_sec to 59 instead. |
| 1773 | ** This assumes that the minimum representable time is |
| 1774 | ** not in the same minute that a leap second was deleted from, |
| 1775 | ** which is a safer assumption than using 58 would be. |
| 1776 | */ |
| 1777 | if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) |
| 1778 | return WRONG; |
| 1779 | saved_seconds = yourtm.tm_sec; |
| 1780 | yourtm.tm_sec = SECSPERMIN - 1; |
| 1781 | } else { |
| 1782 | saved_seconds = yourtm.tm_sec; |
| 1783 | yourtm.tm_sec = 0; |
| 1784 | } |
| 1785 | /* |
| 1786 | ** Do a binary search (this works whatever time_t's type is). |
| 1787 | */ |
| 1788 | if (!TYPE_SIGNED(time_t)) { |
| 1789 | lo = 0; |
| 1790 | hi = lo - 1; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1791 | } else { |
| 1792 | lo = 1; |
| 1793 | for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) |
| 1794 | lo *= 2; |
| 1795 | hi = -(lo + 1); |
| 1796 | } |
| 1797 | for ( ; ; ) { |
| 1798 | t = lo / 2 + hi / 2; |
| 1799 | if (t < lo) |
| 1800 | t = lo; |
| 1801 | else if (t > hi) |
| 1802 | t = hi; |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1803 | if ((*funcp)(&t, offset, &mytm, sp) == NULL) { // android-changed: added sp. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1804 | /* |
| 1805 | ** Assume that t is too extreme to be represented in |
| 1806 | ** a struct tm; arrange things so that it is less |
| 1807 | ** extreme on the next pass. |
| 1808 | */ |
| 1809 | dir = (t > 0) ? 1 : -1; |
| 1810 | } else dir = tmcomp(&mytm, &yourtm); |
| 1811 | if (dir != 0) { |
| 1812 | if (t == lo) { |
Elliott Hughes | 713fe64 | 2013-08-22 14:13:50 -0700 | [diff] [blame] | 1813 | if (t == time_t_max) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1814 | return WRONG; |
Elliott Hughes | 713fe64 | 2013-08-22 14:13:50 -0700 | [diff] [blame] | 1815 | ++t; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1816 | ++lo; |
| 1817 | } else if (t == hi) { |
Elliott Hughes | 713fe64 | 2013-08-22 14:13:50 -0700 | [diff] [blame] | 1818 | if (t == time_t_min) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1819 | return WRONG; |
Elliott Hughes | 713fe64 | 2013-08-22 14:13:50 -0700 | [diff] [blame] | 1820 | --t; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1821 | --hi; |
| 1822 | } |
| 1823 | if (lo > hi) |
| 1824 | return WRONG; |
| 1825 | if (dir > 0) |
| 1826 | hi = t; |
| 1827 | else lo = t; |
| 1828 | continue; |
| 1829 | } |
| 1830 | if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) |
| 1831 | break; |
| 1832 | /* |
| 1833 | ** Right time, wrong type. |
| 1834 | ** Hunt for right time, right type. |
| 1835 | ** It's okay to guess wrong since the guess |
| 1836 | ** gets checked. |
| 1837 | */ |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1838 | // BEGIN android-changed: support user-supplied sp |
| 1839 | if (sp == NULL) { |
| 1840 | sp = (const struct state *) |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1841 | ((funcp == localsub) ? lclptr : gmtptr); |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1842 | } |
| 1843 | // END android-changed |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1844 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1845 | if (sp == NULL) |
| 1846 | return WRONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1847 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1848 | for (i = sp->typecnt - 1; i >= 0; --i) { |
| 1849 | if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) |
| 1850 | continue; |
| 1851 | for (j = sp->typecnt - 1; j >= 0; --j) { |
| 1852 | if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) |
| 1853 | continue; |
| 1854 | newt = t + sp->ttis[j].tt_gmtoff - |
| 1855 | sp->ttis[i].tt_gmtoff; |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1856 | if ((*funcp)(&newt, offset, &mytm, sp) == NULL) // android-changed: added sp. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1857 | continue; |
| 1858 | if (tmcomp(&mytm, &yourtm) != 0) |
| 1859 | continue; |
| 1860 | if (mytm.tm_isdst != yourtm.tm_isdst) |
| 1861 | continue; |
| 1862 | /* |
| 1863 | ** We have a match. |
| 1864 | */ |
| 1865 | t = newt; |
| 1866 | goto label; |
| 1867 | } |
| 1868 | } |
| 1869 | return WRONG; |
| 1870 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1871 | label: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1872 | newt = t + saved_seconds; |
| 1873 | if ((newt < t) != (saved_seconds < 0)) |
| 1874 | return WRONG; |
| 1875 | t = newt; |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1876 | if ((*funcp)(&t, offset, tmp, sp)) // android-changed: added sp. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1877 | *okayp = TRUE; |
| 1878 | return t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1882 | time2(struct tm * const tmp, |
| 1883 | struct tm * (*const funcp)(const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp. |
| 1884 | const int_fast32_t offset, |
| 1885 | int *const okayp, const struct state* sp) // android-changed: added sp. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1886 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1887 | time_t t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1888 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1889 | /* |
| 1890 | ** First try without normalization of seconds |
| 1891 | ** (in case tm_sec contains a value associated with a leap second). |
| 1892 | ** If that fails, try with normalization of seconds. |
| 1893 | */ |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1894 | t = time2sub(tmp, funcp, offset, okayp, FALSE, sp); |
| 1895 | return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, sp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | static time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1899 | time1(struct tm * const tmp, |
| 1900 | struct tm * (* const funcp) (const time_t *, int_fast32_t, struct tm *, const struct state *), // android-changed: added sp. |
| 1901 | const int_fast32_t offset, const struct state * sp) // android-changed: added sp. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1902 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1903 | register time_t t; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1904 | register int samei, otheri; |
| 1905 | register int sameind, otherind; |
| 1906 | register int i; |
| 1907 | register int nseen; |
| 1908 | int seen[TZ_MAX_TYPES]; |
| 1909 | int types[TZ_MAX_TYPES]; |
| 1910 | int okay; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1911 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1912 | if (tmp == NULL) { |
| 1913 | errno = EINVAL; |
| 1914 | return WRONG; |
| 1915 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1916 | if (tmp->tm_isdst > 1) |
| 1917 | tmp->tm_isdst = 1; |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1918 | t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1919 | #ifdef PCTS |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1920 | /* |
| 1921 | ** PCTS code courtesy Grant Sullivan. |
| 1922 | */ |
| 1923 | if (okay) |
| 1924 | return t; |
| 1925 | if (tmp->tm_isdst < 0) |
| 1926 | 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] | 1927 | #endif /* defined PCTS */ |
| 1928 | #ifndef PCTS |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1929 | if (okay || tmp->tm_isdst < 0) |
| 1930 | return t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1931 | #endif /* !defined PCTS */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1932 | /* |
| 1933 | ** We're supposed to assume that somebody took a time of one type |
| 1934 | ** and did some math on it that yielded a "struct tm" that's bad. |
| 1935 | ** We try to divine the type they started from and adjust to the |
| 1936 | ** type they need. |
| 1937 | */ |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1938 | // BEGIN android-changed: support user-supplied sp. |
| 1939 | if (sp == NULL) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1940 | sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1941 | } |
| 1942 | // BEGIN android-changed |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1943 | #ifdef ALL_STATE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1944 | if (sp == NULL) |
| 1945 | return WRONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1946 | #endif /* defined ALL_STATE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1947 | for (i = 0; i < sp->typecnt; ++i) |
| 1948 | seen[i] = FALSE; |
| 1949 | nseen = 0; |
| 1950 | for (i = sp->timecnt - 1; i >= 0; --i) |
| 1951 | if (!seen[sp->types[i]]) { |
| 1952 | seen[sp->types[i]] = TRUE; |
| 1953 | types[nseen++] = sp->types[i]; |
| 1954 | } |
| 1955 | for (sameind = 0; sameind < nseen; ++sameind) { |
| 1956 | samei = types[sameind]; |
| 1957 | if (sp->ttis[samei].tt_isdst != tmp->tm_isdst) |
| 1958 | continue; |
| 1959 | for (otherind = 0; otherind < nseen; ++otherind) { |
| 1960 | otheri = types[otherind]; |
| 1961 | if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst) |
| 1962 | continue; |
| 1963 | tmp->tm_sec += sp->ttis[otheri].tt_gmtoff - |
| 1964 | sp->ttis[samei].tt_gmtoff; |
| 1965 | tmp->tm_isdst = !tmp->tm_isdst; |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 1966 | t = time2(tmp, funcp, offset, &okay, sp); // android-changed: added sp. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1967 | if (okay) |
| 1968 | return t; |
| 1969 | tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - |
| 1970 | sp->ttis[samei].tt_gmtoff; |
| 1971 | tmp->tm_isdst = !tmp->tm_isdst; |
| 1972 | } |
| 1973 | } |
| 1974 | return WRONG; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1978 | mktime(struct tm * const tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1979 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1980 | _tzLock(); |
| 1981 | tzset_locked(); |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1982 | time_t result = time1(tmp, localsub, 0L, NULL); // android-changed: extra parameter. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1983 | _tzUnlock(); |
| 1984 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | #ifdef STD_INSPIRED |
| 1988 | |
| 1989 | time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1990 | timelocal(struct tm * const tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1991 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1992 | if (tmp != NULL) |
| 1993 | tmp->tm_isdst = -1; /* in case it wasn't initialized */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1994 | return mktime(tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 1998 | timegm(struct tm * const tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1999 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2000 | time_t result; |
| 2001 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2002 | if (tmp != NULL) |
| 2003 | tmp->tm_isdst = 0; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2004 | _tzLock(); |
Elliott Hughes | b989c9c | 2013-01-16 10:34:33 -0800 | [diff] [blame] | 2005 | result = time1(tmp, gmtsub, 0L, NULL); // android-changed: extra parameter. |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2006 | _tzUnlock(); |
| 2007 | |
| 2008 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2009 | } |
| 2010 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2011 | #endif /* defined STD_INSPIRED */ |
| 2012 | |
| 2013 | #ifdef CMUCS |
| 2014 | |
| 2015 | /* |
| 2016 | ** The following is supplied for compatibility with |
| 2017 | ** previous versions of the CMUCS runtime library. |
| 2018 | */ |
| 2019 | |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 2020 | long |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2021 | gtime(struct tm * const tmp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2022 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2023 | const time_t t = mktime(tmp); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2024 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2025 | if (t == WRONG) |
| 2026 | return -1; |
| 2027 | return t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | #endif /* defined CMUCS */ |
| 2031 | |
| 2032 | /* |
| 2033 | ** XXX--is the below the right way to conditionalize?? |
| 2034 | */ |
| 2035 | |
| 2036 | #ifdef STD_INSPIRED |
| 2037 | |
| 2038 | /* |
| 2039 | ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 |
| 2040 | ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which |
| 2041 | ** is not the case if we are accounting for leap seconds. |
| 2042 | ** So, we provide the following conversion routines for use |
| 2043 | ** when exchanging timestamps with POSIX conforming systems. |
| 2044 | */ |
| 2045 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2046 | static int_fast64_t |
| 2047 | leapcorr(time_t * timep) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2048 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2049 | register struct state * sp; |
| 2050 | register struct lsinfo * lp; |
| 2051 | register int i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2052 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2053 | sp = lclptr; |
| 2054 | i = sp->leapcnt; |
| 2055 | while (--i >= 0) { |
| 2056 | lp = &sp->lsis[i]; |
| 2057 | if (*timep >= lp->ls_trans) |
| 2058 | return lp->ls_corr; |
| 2059 | } |
| 2060 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2064 | time2posix(time_t t) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2065 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2066 | tzset(); |
| 2067 | return t - leapcorr(&t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | time_t |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2071 | posix2time(time_t t) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2072 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2073 | time_t x; |
| 2074 | time_t y; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2075 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 2076 | tzset(); |
| 2077 | /* |
| 2078 | ** For a positive leap second hit, the result |
| 2079 | ** is not unique. For a negative leap second |
| 2080 | ** hit, the corresponding time doesn't exist, |
| 2081 | ** so we return an adjacent second. |
| 2082 | */ |
| 2083 | x = t + leapcorr(&t); |
| 2084 | y = x - leapcorr(&x); |
| 2085 | if (y < t) { |
| 2086 | do { |
| 2087 | x++; |
| 2088 | y = x - leapcorr(&x); |
| 2089 | } while (y < t); |
| 2090 | if (t != y) |
| 2091 | return x - 1; |
| 2092 | } else if (y > t) { |
| 2093 | do { |
| 2094 | --x; |
| 2095 | y = x - leapcorr(&x); |
| 2096 | } while (y > t); |
| 2097 | if (t != y) |
| 2098 | return x + 1; |
| 2099 | } |
| 2100 | return x; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | #endif /* defined STD_INSPIRED */ |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2104 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2105 | // BEGIN android-added |
| 2106 | |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2107 | #include <assert.h> |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2108 | #include <stdint.h> |
Elliott Hughes | 8b95404 | 2012-10-18 13:42:59 -0700 | [diff] [blame] | 2109 | #include <arpa/inet.h> // For ntohl(3). |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2110 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2111 | static int to_int(unsigned char* s) { |
| 2112 | return (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; |
| 2113 | } |
| 2114 | |
Elliott Hughes | cf178bf | 2013-09-18 19:25:28 -0700 | [diff] [blame] | 2115 | static int __bionic_open_tzdata_path(const char* path_prefix_variable, const char* path_suffix, |
| 2116 | const char* olson_id, int* data_size) { |
| 2117 | const char* path_prefix = getenv(path_prefix_variable); |
| 2118 | if (path_prefix == NULL) { |
| 2119 | fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable); |
| 2120 | return -1; |
| 2121 | } |
| 2122 | char path[PATH_MAX]; |
| 2123 | snprintf(path, sizeof(path), "%s/%s", path_prefix, path_suffix); |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2124 | int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE)); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2125 | if (fd == -1) { |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2126 | XLOG(("%s: could not open \"%s\": %s\n", __FUNCTION__, path, strerror(errno))); |
| 2127 | return -2; // Distinguish failure to find any data from failure to find a specific id. |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | // byte[12] tzdata_version -- "tzdata2012f\0" |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2131 | // int index_offset |
| 2132 | // int data_offset |
| 2133 | // int zonetab_offset |
| 2134 | struct bionic_tzdata_header { |
| 2135 | char tzdata_version[12]; |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2136 | int32_t index_offset; |
| 2137 | int32_t data_offset; |
| 2138 | int32_t zonetab_offset; |
| 2139 | } header; |
Elliott Hughes | e7aaad8 | 2013-04-25 14:02:59 -0700 | [diff] [blame] | 2140 | memset(&header, 0, sizeof(header)); |
| 2141 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))); |
| 2142 | if (bytes_read != sizeof(header)) { |
| 2143 | fprintf(stderr, "%s: could not read header of \"%s\": %s\n", |
| 2144 | __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read"); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2145 | close(fd); |
| 2146 | return -1; |
| 2147 | } |
| 2148 | |
| 2149 | if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) { |
Elliott Hughes | e7aaad8 | 2013-04-25 14:02:59 -0700 | [diff] [blame] | 2150 | fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n", |
| 2151 | __FUNCTION__, path, header.tzdata_version); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2152 | close(fd); |
| 2153 | return -1; |
| 2154 | } |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2155 | |
| 2156 | #if 0 |
Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 2157 | fprintf(stderr, "version: %s\n", header.tzdata_version); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2158 | fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset)); |
| 2159 | fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset)); |
| 2160 | fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset)); |
| 2161 | #endif |
| 2162 | |
| 2163 | if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) { |
Elliott Hughes | e7aaad8 | 2013-04-25 14:02:59 -0700 | [diff] [blame] | 2164 | fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n", |
| 2165 | __FUNCTION__, path, strerror(errno)); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2166 | close(fd); |
| 2167 | return -1; |
| 2168 | } |
| 2169 | |
| 2170 | off_t specific_zone_offset = -1; |
| 2171 | |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2172 | static const size_t NAME_LENGTH = 40; |
| 2173 | unsigned char buf[NAME_LENGTH + 3 * sizeof(int32_t)]; |
Elliott Hughes | e0175ca | 2013-03-14 14:38:08 -0700 | [diff] [blame] | 2174 | |
| 2175 | size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(buf); |
| 2176 | for (size_t i = 0; i < id_count; ++i) { |
| 2177 | if (TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf))) != (ssize_t) sizeof(buf)) { |
| 2178 | break; |
| 2179 | } |
| 2180 | |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2181 | char this_id[NAME_LENGTH + 1]; |
| 2182 | memcpy(this_id, buf, NAME_LENGTH); |
| 2183 | this_id[NAME_LENGTH] = '\0'; |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2184 | |
| 2185 | if (strcmp(this_id, olson_id) == 0) { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2186 | specific_zone_offset = to_int(buf + NAME_LENGTH) + ntohl(header.data_offset); |
| 2187 | *data_size = to_int(buf + NAME_LENGTH + sizeof(int32_t)); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2188 | break; |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | if (specific_zone_offset == -1) { |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2193 | XLOG(("%s: couldn't find zone \"%s\"\n", __FUNCTION__, olson_id)); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2194 | close(fd); |
| 2195 | return -1; |
| 2196 | } |
| 2197 | |
| 2198 | if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) { |
Elliott Hughes | e7aaad8 | 2013-04-25 14:02:59 -0700 | [diff] [blame] | 2199 | fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n", |
| 2200 | __FUNCTION__, specific_zone_offset, path, strerror(errno)); |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2201 | close(fd); |
| 2202 | return -1; |
| 2203 | } |
| 2204 | |
Elliott Hughes | e7aaad8 | 2013-04-25 14:02:59 -0700 | [diff] [blame] | 2205 | // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not. |
| 2206 | |
Elliott Hughes | d23af23 | 2012-10-17 16:30:47 -0700 | [diff] [blame] | 2207 | return fd; |
| 2208 | } |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2209 | |
| 2210 | static int __bionic_open_tzdata(const char* olson_id, int* data_size) { |
Elliott Hughes | cf178bf | 2013-09-18 19:25:28 -0700 | [diff] [blame] | 2211 | int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/tzdata", olson_id, data_size); |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2212 | if (fd < 0) { |
Elliott Hughes | cf178bf | 2013-09-18 19:25:28 -0700 | [diff] [blame] | 2213 | fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size); |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2214 | if (fd == -2) { |
Elliott Hughes | 49271d8 | 2012-10-25 14:38:51 -0700 | [diff] [blame] | 2215 | // The first thing that 'recovery' does is try to format the current time. It doesn't have |
| 2216 | // any tzdata available, so we must not abort here --- doing so breaks the recovery image! |
| 2217 | fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id); |
Elliott Hughes | 1c29572 | 2012-10-19 18:13:15 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | return fd; |
| 2221 | } |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 2222 | |
| 2223 | // Caches the most recent timezone (http://b/8270865). |
| 2224 | static int __bionic_tzload_cached(const char* name, struct state* const sp, const int doextend) { |
| 2225 | _tzLock(); |
| 2226 | |
| 2227 | // Our single-item cache. |
| 2228 | static char* gCachedTimeZoneName; |
| 2229 | static struct state gCachedTimeZone; |
| 2230 | |
| 2231 | // Do we already have this timezone cached? |
| 2232 | if (gCachedTimeZoneName != NULL && strcmp(name, gCachedTimeZoneName) == 0) { |
| 2233 | *sp = gCachedTimeZone; |
| 2234 | _tzUnlock(); |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
| 2238 | // Can we load it? |
| 2239 | int rc = tzload(name, sp, doextend); |
| 2240 | if (rc == 0) { |
| 2241 | // Update the cache. |
| 2242 | free(gCachedTimeZoneName); |
| 2243 | gCachedTimeZoneName = strdup(name); |
| 2244 | gCachedTimeZone = *sp; |
| 2245 | } |
| 2246 | |
| 2247 | _tzUnlock(); |
| 2248 | return rc; |
| 2249 | } |
| 2250 | |
| 2251 | // Non-standard API: mktime(3) but with an explicit timezone parameter. |
| 2252 | time_t mktime_tz(struct tm* const tmp, const char* tz) { |
| 2253 | struct state st; |
| 2254 | if (__bionic_tzload_cached(tz, &st, TRUE) != 0) { |
| 2255 | // TODO: not sure what's best here, but for now, we fall back to gmt. |
| 2256 | gmtload(&st); |
| 2257 | } |
| 2258 | return time1(tmp, localsub, 0L, &st); |
| 2259 | } |
| 2260 | |
| 2261 | // Non-standard API: localtime(3) but with an explicit timezone parameter. |
| 2262 | void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz) { |
| 2263 | struct state st; |
| 2264 | if (__bionic_tzload_cached(tz, &st, TRUE) != 0) { |
| 2265 | // TODO: not sure what's best here, but for now, we fall back to gmt. |
| 2266 | gmtload(&st); |
| 2267 | } |
| 2268 | localsub(timep, 0L, tmp, &st); |
| 2269 | } |
| 2270 | |
| 2271 | // END android-added |