sys/stat.h: fix typo in statx member name stx_dio_offset_align
[musl.git] / src / time / __tz.c
blob54ed4cf6a783da821286fdffa6d0afb895708efb
1 #include "time_impl.h"
2 #include <stdint.h>
3 #include <limits.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <ctype.h>
8 #include "libc.h"
9 #include "lock.h"
10 #include "fork_impl.h"
12 #define malloc __libc_malloc
13 #define calloc undef
14 #define realloc undef
15 #define free undef
17 long __timezone = 0;
18 int __daylight = 0;
19 char *__tzname[2] = { 0, 0 };
21 weak_alias(__timezone, timezone);
22 weak_alias(__daylight, daylight);
23 weak_alias(__tzname, tzname);
25 static char std_name[TZNAME_MAX+1];
26 static char dst_name[TZNAME_MAX+1];
28 static int dst_off;
29 static int r0[5], r1[5];
31 static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
32 static size_t map_size;
34 static char old_tz_buf[32];
35 static char *old_tz = old_tz_buf;
36 static size_t old_tz_size = sizeof old_tz_buf;
38 static volatile int lock[1];
39 volatile int *const __timezone_lockptr = lock;
41 static int getint(const char **p)
43 unsigned x;
44 for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
45 return x;
48 static int getoff(const char **p)
50 int neg = 0;
51 if (**p == '-') {
52 ++*p;
53 neg = 1;
54 } else if (**p == '+') {
55 ++*p;
57 int off = 3600*getint(p);
58 if (**p == ':') {
59 ++*p;
60 off += 60*getint(p);
61 if (**p == ':') {
62 ++*p;
63 off += getint(p);
66 return neg ? -off : off;
69 static void getrule(const char **p, int rule[5])
71 int r = rule[0] = **p;
73 if (r!='M') {
74 if (r=='J') ++*p;
75 else rule[0] = 0;
76 rule[1] = getint(p);
77 } else {
78 ++*p; rule[1] = getint(p);
79 ++*p; rule[2] = getint(p);
80 ++*p; rule[3] = getint(p);
83 if (**p=='/') {
84 ++*p;
85 rule[4] = getoff(p);
86 } else {
87 rule[4] = 7200;
91 static void getname(char *d, const char **p)
93 int i;
94 if (**p == '<') {
95 ++*p;
96 for (i=0; (*p)[i] && (*p)[i]!='>'; i++)
97 if (i<TZNAME_MAX) d[i] = (*p)[i];
98 if ((*p)[i]) ++*p;
99 } else {
100 for (i=0; ((*p)[i]|32)-'a'<26U; i++)
101 if (i<TZNAME_MAX) d[i] = (*p)[i];
103 *p += i;
104 d[i<TZNAME_MAX?i:TZNAME_MAX] = 0;
107 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
109 static uint32_t zi_read32(const unsigned char *z)
111 return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
114 static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
116 size_t y;
117 uint32_t x;
118 for (y=0; n; n--, z+=4, v++) {
119 x = zi_read32(z);
120 y += x * *v;
122 return y;
125 static void do_tzset()
127 char buf[NAME_MAX+25], *pathname=buf+24;
128 const char *try, *s, *p;
129 const unsigned char *map = 0;
130 size_t i;
131 static const char search[] =
132 "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
134 s = getenv("TZ");
135 if (!s) s = "/etc/localtime";
136 if (!*s) s = __utc;
138 if (old_tz && !strcmp(s, old_tz)) return;
140 for (i=0; i<5; i++) r0[i] = r1[i] = 0;
142 if (zi) __munmap((void *)zi, map_size);
144 /* Cache the old value of TZ to check if it has changed. Avoid
145 * free so as not to pull it into static programs. Growth
146 * strategy makes it so free would have minimal benefit anyway. */
147 i = strlen(s);
148 if (i > PATH_MAX+1) s = __utc, i = 3;
149 if (i >= old_tz_size) {
150 old_tz_size *= 2;
151 if (i >= old_tz_size) old_tz_size = i+1;
152 if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
153 old_tz = malloc(old_tz_size);
155 if (old_tz) memcpy(old_tz, s, i+1);
157 int posix_form = 0;
158 if (*s != ':') {
159 p = s;
160 char dummy_name[TZNAME_MAX+1];
161 getname(dummy_name, &p);
162 if (p!=s && (*p == '+' || *p == '-' || isdigit(*p)
163 || !strcmp(dummy_name, "UTC")
164 || !strcmp(dummy_name, "GMT")))
165 posix_form = 1;
168 /* Non-suid can use an absolute tzfile pathname or a relative
169 * pathame beginning with "."; in secure mode, only the
170 * standard path will be searched. */
171 if (!posix_form) {
172 if (*s == ':') s++;
173 if (*s == '/' || *s == '.') {
174 if (!libc.secure || !strcmp(s, "/etc/localtime"))
175 map = __map_file(s, &map_size);
176 } else {
177 size_t l = strlen(s);
178 if (l <= NAME_MAX && !strchr(s, '.')) {
179 memcpy(pathname, s, l+1);
180 pathname[l] = 0;
181 for (try=search; !map && *try; try+=l+1) {
182 l = strlen(try);
183 memcpy(pathname-l, try, l);
184 map = __map_file(pathname-l, &map_size);
188 if (!map) s = __utc;
190 if (map && (map_size < 44 || memcmp(map, "TZif", 4))) {
191 __munmap((void *)map, map_size);
192 map = 0;
193 s = __utc;
196 zi = map;
197 if (map) {
198 int scale = 2;
199 if (map[4]!='1') {
200 size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
201 trans = zi+skip+44+44;
202 scale++;
203 } else {
204 trans = zi+44;
206 index = trans + (zi_read32(trans-12) << scale);
207 types = index + zi_read32(trans-12);
208 abbrevs = types + 6*zi_read32(trans-8);
209 abbrevs_end = abbrevs + zi_read32(trans-4);
210 if (zi[map_size-1] == '\n') {
211 for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
212 s++;
213 } else {
214 const unsigned char *p;
215 __tzname[0] = __tzname[1] = 0;
216 __daylight = __timezone = dst_off = 0;
217 for (p=types; p<abbrevs; p+=6) {
218 if (!p[4] && !__tzname[0]) {
219 __tzname[0] = (char *)abbrevs + p[5];
220 __timezone = -zi_read32(p);
222 if (p[4] && !__tzname[1]) {
223 __tzname[1] = (char *)abbrevs + p[5];
224 dst_off = -zi_read32(p);
225 __daylight = 1;
228 if (!__tzname[0]) __tzname[0] = __tzname[1];
229 if (!__tzname[0]) __tzname[0] = (char *)__utc;
230 if (!__daylight) {
231 __tzname[1] = __tzname[0];
232 dst_off = __timezone;
234 return;
238 if (!s) s = __utc;
239 getname(std_name, &s);
240 __tzname[0] = std_name;
241 __timezone = getoff(&s);
242 getname(dst_name, &s);
243 __tzname[1] = dst_name;
244 if (dst_name[0]) {
245 __daylight = 1;
246 if (*s == '+' || *s=='-' || *s-'0'<10U)
247 dst_off = getoff(&s);
248 else
249 dst_off = __timezone - 3600;
250 } else {
251 __daylight = 0;
252 dst_off = __timezone;
255 if (*s == ',') s++, getrule(&s, r0);
256 if (*s == ',') s++, getrule(&s, r1);
259 /* Search zoneinfo rules to find the one that applies to the given time,
260 * and determine alternate opposite-DST-status rule that may be needed. */
262 static size_t scan_trans(long long t, int local, size_t *alt)
264 int scale = 3 - (trans == zi+44);
265 uint64_t x;
266 int off = 0;
268 size_t a = 0, n = (index-trans)>>scale, m;
270 if (!n) {
271 if (alt) *alt = 0;
272 return 0;
275 /* Binary search for 'most-recent rule before t'. */
276 while (n > 1) {
277 m = a + n/2;
278 x = zi_read32(trans + (m<<scale));
279 if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
280 else x = (int32_t)x;
281 if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
282 if (t - off < (int64_t)x) {
283 n /= 2;
284 } else {
285 a = m;
286 n -= n/2;
290 /* First and last entry are special. First means to use lowest-index
291 * non-DST type. Last means to apply POSIX-style rule if available. */
292 n = (index-trans)>>scale;
293 if (a == n-1) return -1;
294 if (a == 0) {
295 x = zi_read32(trans);
296 if (scale == 3) x = x<<32 | zi_read32(trans + 4);
297 else x = (int32_t)x;
298 /* Find the lowest non-DST type, or 0 if none. */
299 size_t j = 0;
300 for (size_t i=abbrevs-types; i; i-=6) {
301 if (!types[i-6+4]) j = i-6;
303 if (local) off = (int32_t)zi_read32(types + j);
304 /* If t is before first transition, use the above-found type
305 * and the index-zero (after transition) type as the alt. */
306 if (t - off < (int64_t)x) {
307 if (alt) *alt = index[0];
308 return j/6;
312 /* Try to find a neighboring opposite-DST-status rule. */
313 if (alt) {
314 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
315 *alt = index[a-1];
316 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
317 *alt = index[a+1];
318 else
319 *alt = index[a];
322 return index[a];
325 static int days_in_month(int m, int is_leap)
327 if (m==2) return 28+is_leap;
328 else return 30+((0xad5>>(m-1))&1);
331 /* Convert a POSIX DST rule plus year to seconds since epoch. */
333 static long long rule_to_secs(const int *rule, int year)
335 int is_leap;
336 long long t = __year_to_secs(year, &is_leap);
337 int x, m, n, d;
338 if (rule[0]!='M') {
339 x = rule[1];
340 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
341 t += 86400 * x;
342 } else {
343 m = rule[1];
344 n = rule[2];
345 d = rule[3];
346 t += __month_to_secs(m-1, is_leap);
347 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
348 int days = d - wday;
349 if (days < 0) days += 7;
350 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
351 t += 86400 * (days + 7*(n-1));
353 t += rule[4];
354 return t;
357 /* Determine the time zone in effect for a given time in seconds since the
358 * epoch. It can be given in local or universal time. The results will
359 * indicate whether DST is in effect at the queried time, and will give both
360 * the GMT offset for the active zone/DST rule and the opposite DST. This
361 * enables a caller to efficiently adjust for the case where an explicit
362 * DST specification mismatches what would be in effect at the time. */
364 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
366 LOCK(lock);
368 do_tzset();
370 if (zi) {
371 size_t alt, i = scan_trans(t, local, &alt);
372 if (i != -1) {
373 *isdst = types[6*i+4];
374 *offset = (int32_t)zi_read32(types+6*i);
375 *zonename = (const char *)abbrevs + types[6*i+5];
376 if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
377 UNLOCK(lock);
378 return;
382 if (!__daylight) goto std;
384 /* FIXME: may be broken if DST changes right at year boundary?
385 * Also, this could be more efficient.*/
386 long long y = t / 31556952 + 70;
387 while (__year_to_secs(y, 0) > t) y--;
388 while (__year_to_secs(y+1, 0) < t) y++;
390 long long t0 = rule_to_secs(r0, y);
391 long long t1 = rule_to_secs(r1, y);
393 if (!local) {
394 t0 += __timezone;
395 t1 += dst_off;
397 if (t0 < t1) {
398 if (t >= t0 && t < t1) goto dst;
399 goto std;
400 } else {
401 if (t >= t1 && t < t0) goto std;
402 goto dst;
404 std:
405 *isdst = 0;
406 *offset = -__timezone;
407 if (oppoff) *oppoff = -dst_off;
408 *zonename = __tzname[0];
409 UNLOCK(lock);
410 return;
411 dst:
412 *isdst = 1;
413 *offset = -dst_off;
414 if (oppoff) *oppoff = -__timezone;
415 *zonename = __tzname[1];
416 UNLOCK(lock);
419 static void __tzset()
421 LOCK(lock);
422 do_tzset();
423 UNLOCK(lock);
426 weak_alias(__tzset, tzset);
428 const char *__tm_to_tzname(const struct tm *tm)
430 const void *p = tm->__tm_zone;
431 LOCK(lock);
432 do_tzset();
433 if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
434 (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
435 p = "";
436 UNLOCK(lock);
437 return p;