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