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