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