]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/time/__tz.c
092d343df3300bb605222ff194ed72dbd4b1287d
6 #ifdef __wasilibc_unmodified_upstream // timezone data
11 #include "fork_impl.h"
13 #define malloc __libc_malloc
18 #ifdef __wasilibc_unmodified_upstream // timezone data
21 char *__tzname
[2] = { 0, 0 };
23 weak_alias(__timezone
, timezone
);
24 weak_alias(__daylight
, daylight
);
25 weak_alias(__tzname
, tzname
);
27 static char std_name
[TZNAME_MAX
+1];
28 static char dst_name
[TZNAME_MAX
+1];
30 const char __utc
[] = "UTC";
32 #ifdef __wasilibc_unmodified_upstream // timezone data
34 static int r0
[5], r1
[5];
36 static const unsigned char *zi
, *trans
, *index
, *types
, *abbrevs
, *abbrevs_end
;
37 static size_t map_size
;
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
;
43 static volatile int lock
[1];
44 volatile int *const __timezone_lockptr
= lock
;
46 static int getint(const char **p
)
49 for (x
=0; **p
-'0'<10U; (*p
)++) x
= **p
-'0' + 10*x
;
53 static int getoff(const char **p
)
59 } else if (**p
== '+') {
62 int off
= 3600*getint(p
);
71 return neg
? -off
: off
;
74 static void getrule(const char **p
, int rule
[5])
76 int r
= rule
[0] = **p
;
83 ++*p
; rule
[1] = getint(p
);
84 ++*p
; rule
[2] = getint(p
);
85 ++*p
; rule
[3] = getint(p
);
96 static void getname(char *d
, const char **p
)
101 for (i
=0; (*p
)[i
] && (*p
)[i
]!='>'; i
++)
102 if (i
<TZNAME_MAX
) d
[i
] = (*p
)[i
];
105 for (i
=0; ((*p
)[i
]|32)-'a'<26U; i
++)
106 if (i
<TZNAME_MAX
) d
[i
] = (*p
)[i
];
109 d
[i
<TZNAME_MAX
?i
:TZNAME_MAX
] = 0;
112 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
114 static uint32_t zi_read32(const unsigned char *z
)
116 return (unsigned)z
[0]<<24 | z
[1]<<16 | z
[2]<<8 | z
[3];
119 static size_t zi_dotprod(const unsigned char *z
, const unsigned char *v
, size_t n
)
123 for (y
=0; n
; n
--, z
+=4, v
++) {
130 static void do_tzset()
132 char buf
[NAME_MAX
+25], *pathname
=buf
+24;
133 const char *try, *s
, *p
;
134 const unsigned char *map
= 0;
136 static const char search
[] =
137 "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
140 if (!s
) s
= "/etc/localtime";
143 if (old_tz
&& !strcmp(s
, old_tz
)) return;
145 for (i
=0; i
<5; i
++) r0
[i
] = r1
[i
] = 0;
147 if (zi
) __munmap((void *)zi
, map_size
);
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. */
153 if (i
> PATH_MAX
+1) s
= __utc
, i
= 3;
154 if (i
>= old_tz_size
) {
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
);
160 if (old_tz
) memcpy(old_tz
, s
, i
+1);
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
))) {
167 if (*s
== '/' || *s
== '.') {
168 if (!libc
.secure
|| !strcmp(s
, "/etc/localtime"))
169 map
= __map_file(s
, &map_size
);
171 size_t l
= strlen(s
);
172 if (l
<= NAME_MAX
&& !strchr(s
, '.')) {
173 memcpy(pathname
, s
, l
+1);
175 for (try=search
; !map
&& *try; try+=l
+1) {
177 memcpy(pathname
-l
, try, l
);
178 map
= __map_file(pathname
-l
, &map_size
);
184 if (map
&& (map_size
< 44 || memcmp(map
, "TZif", 4))) {
185 __munmap((void *)map
, map_size
);
194 size_t skip
= zi_dotprod(zi
+20, VEC(1,1,8,5,6,1), 6);
195 trans
= zi
+skip
+44+44;
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
--);
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
);
216 if (p
[4] && !__tzname
[1]) {
217 __tzname
[1] = (char *)abbrevs
+ p
[5];
218 dst_off
= -zi_read32(p
);
222 if (!__tzname
[0]) __tzname
[0] = __tzname
[1];
223 if (!__tzname
[0]) __tzname
[0] = (char *)__utc
;
225 __tzname
[1] = __tzname
[0];
226 dst_off
= __timezone
;
233 getname(std_name
, &s
);
234 __tzname
[0] = std_name
;
235 __timezone
= getoff(&s
);
236 getname(dst_name
, &s
);
237 __tzname
[1] = dst_name
;
240 if (*s
== '+' || *s
=='-' || *s
-'0'<10U)
241 dst_off
= getoff(&s
);
243 dst_off
= __timezone
- 3600;
246 dst_off
= __timezone
;
249 if (*s
== ',') s
++, getrule(&s
, r0
);
250 if (*s
== ',') s
++, getrule(&s
, r1
);
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. */
256 static size_t scan_trans(long long t
, int local
, size_t *alt
)
258 int scale
= 3 - (trans
== zi
+44);
262 size_t a
= 0, n
= (index
-trans
)>>scale
, m
;
269 /* Binary search for 'most-recent rule before t'. */
272 x
= zi_read32(trans
+ (m
<<scale
));
273 if (scale
== 3) x
= x
<<32 | zi_read32(trans
+ (m
<<scale
) + 4);
275 if (local
) off
= (int32_t)zi_read32(types
+ 6 * index
[m
-1]);
276 if (t
- off
< (int64_t)x
) {
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;
289 x
= zi_read32(trans
+ (a
<<scale
));
290 if (scale
== 3) x
= x
<<32 | zi_read32(trans
+ (a
<<scale
) + 4);
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;
297 if (a
== (abbrevs
-types
)/6) a
= 0;
308 /* Try to find a neighboring opposite-DST-status rule. */
310 if (a
&& types
[6*index
[a
-1]+4] != types
[6*index
[a
]+4])
312 else if (a
+1<n
&& types
[6*index
[a
+1]+4] != types
[6*index
[a
]+4])
321 static int days_in_month(int m
, int is_leap
)
323 if (m
==2) return 28+is_leap
;
324 else return 30+((0xad5>>(m
-1))&1);
327 /* Convert a POSIX DST rule plus year to seconds since epoch. */
329 static long long rule_to_secs(const int *rule
, int year
)
332 long long t
= __year_to_secs(year
, &is_leap
);
336 if (rule
[0]=='J' && (x
< 60 || !is_leap
)) x
--;
342 t
+= __month_to_secs(m
-1, is_leap
);
343 int wday
= (int)((t
+ 4*86400) % (7*86400)) / 86400;
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));
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. */
360 void __secs_to_zone(long long t
, int local
, int *isdst
, long *offset
, long *oppoff
, const char **zonename
)
367 size_t alt
, i
= scan_trans(t
, local
, &alt
);
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
);
378 if (!__daylight
) goto std
;
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
++;
386 long long t0
= rule_to_secs(r0
, y
);
387 long long t1
= rule_to_secs(r1
, y
);
394 if (t
>= t0
&& t
< t1
) goto dst
;
397 if (t
>= t1
&& t
< t0
) goto std
;
402 *offset
= -__timezone
;
403 if (oppoff
) *oppoff
= -dst_off
;
404 *zonename
= __tzname
[0];
410 if (oppoff
) *oppoff
= -__timezone
;
411 *zonename
= __tzname
[1];
415 static void __tzset()
422 weak_alias(__tzset
, tzset
);
424 void __secs_to_zone(long long t
, int local
, int *isdst
, int *offset
, long *oppoff
, const char **zonename
)
426 // Minimalist implementation for now.
434 const char *__tm_to_tzname(const struct tm
*tm
)
436 const void *p
= tm
->__tm_zone
;
437 #ifdef __wasilibc_unmodified_upstream // timezone data
440 if (p
!= __utc
&& p
!= __tzname
[0] && p
!= __tzname
[1] &&
441 (!zi
|| (uintptr_t)p
-(uintptr_t)abbrevs
>= abbrevs_end
- abbrevs
))