]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/time/__tz.c
6 #ifdef __wasilibc_unmodified_upstream // timezone data
12 #include "fork_impl.h"
14 #define malloc __libc_malloc
19 #ifdef __wasilibc_unmodified_upstream // timezone data
22 char *__tzname
[2] = { 0, 0 };
24 weak_alias(__timezone
, timezone
);
25 weak_alias(__daylight
, daylight
);
26 weak_alias(__tzname
, tzname
);
28 static char std_name
[TZNAME_MAX
+1];
29 static char dst_name
[TZNAME_MAX
+1];
31 const char __utc
[] = "UTC";
33 #ifdef __wasilibc_unmodified_upstream // timezone data
35 static int r0
[5], r1
[5];
37 static const unsigned char *zi
, *trans
, *index
, *types
, *abbrevs
, *abbrevs_end
;
38 static size_t map_size
;
40 static char old_tz_buf
[32];
41 static char *old_tz
= old_tz_buf
;
42 static size_t old_tz_size
= sizeof old_tz_buf
;
44 static volatile int lock
[1];
45 volatile int *const __timezone_lockptr
= lock
;
47 static int getint(const char **p
)
50 for (x
=0; **p
-'0'<10U; (*p
)++) x
= **p
-'0' + 10*x
;
54 static int getoff(const char **p
)
60 } else if (**p
== '+') {
63 int off
= 3600*getint(p
);
72 return neg
? -off
: off
;
75 static void getrule(const char **p
, int rule
[5])
77 int r
= rule
[0] = **p
;
84 ++*p
; rule
[1] = getint(p
);
85 ++*p
; rule
[2] = getint(p
);
86 ++*p
; rule
[3] = getint(p
);
97 static void getname(char *d
, const char **p
)
102 for (i
=0; (*p
)[i
] && (*p
)[i
]!='>'; i
++)
103 if (i
<TZNAME_MAX
) d
[i
] = (*p
)[i
];
106 for (i
=0; ((*p
)[i
]|32)-'a'<26U; i
++)
107 if (i
<TZNAME_MAX
) d
[i
] = (*p
)[i
];
110 d
[i
<TZNAME_MAX
?i
:TZNAME_MAX
] = 0;
113 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
115 static uint32_t zi_read32(const unsigned char *z
)
117 return (unsigned)z
[0]<<24 | z
[1]<<16 | z
[2]<<8 | z
[3];
120 static size_t zi_dotprod(const unsigned char *z
, const unsigned char *v
, size_t n
)
124 for (y
=0; n
; n
--, z
+=4, v
++) {
131 static void do_tzset()
133 char buf
[NAME_MAX
+25], *pathname
=buf
+24;
134 const char *try, *s
, *p
;
135 const unsigned char *map
= 0;
137 static const char search
[] =
138 "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
141 if (!s
) s
= "/etc/localtime";
144 if (old_tz
&& !strcmp(s
, old_tz
)) return;
146 for (i
=0; i
<5; i
++) r0
[i
] = r1
[i
] = 0;
148 if (zi
) __munmap((void *)zi
, map_size
);
150 /* Cache the old value of TZ to check if it has changed. Avoid
151 * free so as not to pull it into static programs. Growth
152 * strategy makes it so free would have minimal benefit anyway. */
154 if (i
> PATH_MAX
+1) s
= __utc
, i
= 3;
155 if (i
>= old_tz_size
) {
157 if (i
>= old_tz_size
) old_tz_size
= i
+1;
158 if (old_tz_size
> PATH_MAX
+2) old_tz_size
= PATH_MAX
+2;
159 old_tz
= malloc(old_tz_size
);
161 if (old_tz
) memcpy(old_tz
, s
, i
+1);
166 char dummy_name
[TZNAME_MAX
+1];
167 getname(dummy_name
, &p
);
168 if (p
!=s
&& (*p
== '+' || *p
== '-' || isdigit(*p
)
169 || !strcmp(dummy_name
, "UTC")
170 || !strcmp(dummy_name
, "GMT")))
174 /* Non-suid can use an absolute tzfile pathname or a relative
175 * pathame beginning with "."; in secure mode, only the
176 * standard path will be searched. */
179 if (*s
== '/' || *s
== '.') {
180 if (!libc
.secure
|| !strcmp(s
, "/etc/localtime"))
181 map
= __map_file(s
, &map_size
);
183 size_t l
= strlen(s
);
184 if (l
<= NAME_MAX
&& !strchr(s
, '.')) {
185 memcpy(pathname
, s
, l
+1);
187 for (try=search
; !map
&& *try; try+=l
+1) {
189 memcpy(pathname
-l
, try, l
);
190 map
= __map_file(pathname
-l
, &map_size
);
196 if (map
&& (map_size
< 44 || memcmp(map
, "TZif", 4))) {
197 __munmap((void *)map
, map_size
);
206 size_t skip
= zi_dotprod(zi
+20, VEC(1,1,8,5,6,1), 6);
207 trans
= zi
+skip
+44+44;
212 index
= trans
+ (zi_read32(trans
-12) << scale
);
213 types
= index
+ zi_read32(trans
-12);
214 abbrevs
= types
+ 6*zi_read32(trans
-8);
215 abbrevs_end
= abbrevs
+ zi_read32(trans
-4);
216 if (zi
[map_size
-1] == '\n') {
217 for (s
= (const char *)zi
+map_size
-2; *s
!='\n'; s
--);
220 const unsigned char *p
;
221 __tzname
[0] = __tzname
[1] = 0;
222 __daylight
= __timezone
= dst_off
= 0;
223 for (p
=types
; p
<abbrevs
; p
+=6) {
224 if (!p
[4] && !__tzname
[0]) {
225 __tzname
[0] = (char *)abbrevs
+ p
[5];
226 __timezone
= -zi_read32(p
);
228 if (p
[4] && !__tzname
[1]) {
229 __tzname
[1] = (char *)abbrevs
+ p
[5];
230 dst_off
= -zi_read32(p
);
234 if (!__tzname
[0]) __tzname
[0] = __tzname
[1];
235 if (!__tzname
[0]) __tzname
[0] = (char *)__utc
;
237 __tzname
[1] = __tzname
[0];
238 dst_off
= __timezone
;
245 getname(std_name
, &s
);
246 __tzname
[0] = std_name
;
247 __timezone
= getoff(&s
);
248 getname(dst_name
, &s
);
249 __tzname
[1] = dst_name
;
252 if (*s
== '+' || *s
=='-' || *s
-'0'<10U)
253 dst_off
= getoff(&s
);
255 dst_off
= __timezone
- 3600;
258 dst_off
= __timezone
;
261 if (*s
== ',') s
++, getrule(&s
, r0
);
262 if (*s
== ',') s
++, getrule(&s
, r1
);
265 /* Search zoneinfo rules to find the one that applies to the given time,
266 * and determine alternate opposite-DST-status rule that may be needed. */
268 static size_t scan_trans(long long t
, int local
, size_t *alt
)
270 int scale
= 3 - (trans
== zi
+44);
274 size_t a
= 0, n
= (index
-trans
)>>scale
, m
;
281 /* Binary search for 'most-recent rule before t'. */
284 x
= zi_read32(trans
+ (m
<<scale
));
285 if (scale
== 3) x
= x
<<32 | zi_read32(trans
+ (m
<<scale
) + 4);
287 if (local
) off
= (int32_t)zi_read32(types
+ 6 * index
[m
-1]);
288 if (t
- off
< (int64_t)x
) {
296 /* First and last entry are special. First means to use lowest-index
297 * non-DST type. Last means to apply POSIX-style rule if available. */
298 n
= (index
-trans
)>>scale
;
299 if (a
== n
-1) return -1;
301 x
= zi_read32(trans
);
302 if (scale
== 3) x
= x
<<32 | zi_read32(trans
+ 4);
304 /* Find the lowest non-DST type, or 0 if none. */
306 for (size_t i
=abbrevs
-types
; i
; i
-=6) {
307 if (!types
[i
-6+4]) j
= i
-6;
309 if (local
) off
= (int32_t)zi_read32(types
+ j
);
310 /* If t is before first transition, use the above-found type
311 * and the index-zero (after transition) type as the alt. */
312 if (t
- off
< (int64_t)x
) {
313 if (alt
) *alt
= index
[0];
318 /* Try to find a neighboring opposite-DST-status rule. */
320 if (a
&& types
[6*index
[a
-1]+4] != types
[6*index
[a
]+4])
322 else if (a
+1<n
&& types
[6*index
[a
+1]+4] != types
[6*index
[a
]+4])
331 static int days_in_month(int m
, int is_leap
)
333 if (m
==2) return 28+is_leap
;
334 else return 30+((0xad5>>(m
-1))&1);
337 /* Convert a POSIX DST rule plus year to seconds since epoch. */
339 static long long rule_to_secs(const int *rule
, int year
)
342 long long t
= __year_to_secs(year
, &is_leap
);
346 if (rule
[0]=='J' && (x
< 60 || !is_leap
)) x
--;
352 t
+= __month_to_secs(m
-1, is_leap
);
353 int wday
= (int)((t
+ 4*86400) % (7*86400)) / 86400;
355 if (days
< 0) days
+= 7;
356 if (n
== 5 && days
+28 >= days_in_month(m
, is_leap
)) n
= 4;
357 t
+= 86400 * (days
+ 7*(n
-1));
363 /* Determine the time zone in effect for a given time in seconds since the
364 * epoch. It can be given in local or universal time. The results will
365 * indicate whether DST is in effect at the queried time, and will give both
366 * the GMT offset for the active zone/DST rule and the opposite DST. This
367 * enables a caller to efficiently adjust for the case where an explicit
368 * DST specification mismatches what would be in effect at the time. */
370 void __secs_to_zone(long long t
, int local
, int *isdst
, long *offset
, long *oppoff
, const char **zonename
)
377 size_t alt
, i
= scan_trans(t
, local
, &alt
);
379 *isdst
= types
[6*i
+4];
380 *offset
= (int32_t)zi_read32(types
+6*i
);
381 *zonename
= (const char *)abbrevs
+ types
[6*i
+5];
382 if (oppoff
) *oppoff
= (int32_t)zi_read32(types
+6*alt
);
388 if (!__daylight
) goto std
;
390 /* FIXME: may be broken if DST changes right at year boundary?
391 * Also, this could be more efficient.*/
392 long long y
= t
/ 31556952 + 70;
393 while (__year_to_secs(y
, 0) > t
) y
--;
394 while (__year_to_secs(y
+1, 0) < t
) y
++;
396 long long t0
= rule_to_secs(r0
, y
);
397 long long t1
= rule_to_secs(r1
, y
);
404 if (t
>= t0
&& t
< t1
) goto dst
;
407 if (t
>= t1
&& t
< t0
) goto std
;
412 *offset
= -__timezone
;
413 if (oppoff
) *oppoff
= -dst_off
;
414 *zonename
= __tzname
[0];
420 if (oppoff
) *oppoff
= -__timezone
;
421 *zonename
= __tzname
[1];
425 static void __tzset()
432 weak_alias(__tzset
, tzset
);
434 void __secs_to_zone(long long t
, int local
, int *isdst
, int *offset
, long *oppoff
, const char **zonename
)
436 // Minimalist implementation for now.
444 const char *__tm_to_tzname(const struct tm
*tm
)
446 const void *p
= tm
->__tm_zone
;
447 #ifdef __wasilibc_unmodified_upstream // timezone data
450 if (p
!= __utc
&& p
!= __tzname
[0] && p
!= __tzname
[1] &&
451 (!zi
|| (uintptr_t)p
-(uintptr_t)abbrevs
>= abbrevs_end
- abbrevs
))