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