10 char *__tzname[2] = { 0, 0 };
12 weak_alias(__timezone, timezone);
13 weak_alias(__daylight, daylight);
14 weak_alias(__tzname, tzname);
16 static char std_name[TZNAME_MAX+1];
17 static char dst_name[TZNAME_MAX+1];
18 const char __gmt[] = "GMT";
21 static int r0[5], r1[5];
23 static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
24 static size_t map_size;
26 static char old_tz_buf[32];
27 static char *old_tz = old_tz_buf;
28 static size_t old_tz_size = sizeof old_tz_buf;
32 static int getint(const char **p)
35 for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
39 static int getsigned(const char **p)
49 static int getoff(const char **p)
51 int off = 3600*getsigned(p);
63 static void getrule(const char **p, int rule[5])
65 int r = rule[0] = **p;
72 ++*p; rule[1] = getint(p);
73 ++*p; rule[2] = getint(p);
74 ++*p; rule[3] = getint(p);
85 static void getname(char *d, const char **p)
90 for (i=0; **p!='>' && i<TZNAME_MAX; i++)
94 for (i=0; ((*p)[i]|32)-'a'<26U && i<TZNAME_MAX; i++)
101 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
103 static uint32_t zi_read32(const unsigned char *z)
105 return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
108 static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
112 for (y=0; n; n--, z+=4, v++) {
119 int __munmap(void *, size_t);
121 static void do_tzset()
123 char buf[NAME_MAX+25], *pathname=buf+24;
125 const unsigned char *map = 0;
127 static const char search[] =
128 "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
131 if (!s || !*s) s = __gmt;
133 if (old_tz && !strcmp(s, old_tz)) return;
135 if (zi) __munmap((void *)zi, map_size);
137 /* Cache the old value of TZ to check if it has changed. Avoid
138 * free so as not to pull it into static programs. Growth
139 * strategy makes it so free would have minimal benefit anyway. */
141 if (i > PATH_MAX+1) s = __gmt, i = 3;
142 if (i >= old_tz_size) {
144 if (i >= old_tz_size) old_tz_size = i+1;
145 if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
146 old_tz = malloc(old_tz_size);
148 if (old_tz) memcpy(old_tz, s, i+1);
152 /* Non-suid can use an absolute tzfile pathname or a relative
153 * pathame beginning with "."; in secure mode, only the
154 * standard path will be searched. */
155 if (*s == '/' || *s == '.') {
156 if (!libc.secure) map = __map_file(s, &map_size);
158 for (i=0; s[i] && s[i]!=','; i++) {
160 size_t l = strlen(s);
161 if (l > NAME_MAX || strchr(s, '.'))
163 memcpy(pathname, s, l+1);
165 for (try=search; !map && *try; try+=l+1) {
167 memcpy(pathname-l, try, l);
168 map = __map_file(pathname-l, &map_size);
178 if (sizeof(time_t) > 4 && map[4]=='2') {
179 size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
180 trans = zi+skip+44+44;
185 index = trans + (zi_read32(trans-12) << scale);
186 types = index + zi_read32(trans-12);
187 abbrevs = types + 6*zi_read32(trans-8);
188 abbrevs_end = abbrevs + zi_read32(trans-4);
189 if (zi[map_size-1] == '\n') {
190 for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
193 const unsigned char *p;
194 __tzname[0] = __tzname[1] = 0;
195 __daylight = __timezone = dst_off = 0;
196 for (i=0; i<5; i++) r0[i] = r1[i] = 0;
197 for (p=types; p<abbrevs; p+=6) {
198 if (!p[4] && !__tzname[0]) {
199 __tzname[0] = (char *)abbrevs + p[5];
200 __timezone = -zi_read32(p);
202 if (p[4] && !__tzname[1]) {
203 __tzname[1] = (char *)abbrevs + p[5];
204 dst_off = -zi_read32(p);
208 if (!__tzname[0]) __tzname[0] = __tzname[1];
209 if (!__tzname[0]) __tzname[0] = (char *)__gmt;
211 __tzname[1] = __tzname[0];
212 dst_off = __timezone;
219 getname(std_name, &s);
220 __tzname[0] = std_name;
221 __timezone = getoff(&s);
222 getname(dst_name, &s);
223 __tzname[1] = dst_name;
226 if (*s == '+' || *s=='-' || *s-'0'<10U)
227 dst_off = getoff(&s);
229 dst_off = __timezone - 3600;
235 if (*s == ',') s++, getrule(&s, r0);
236 if (*s == ',') s++, getrule(&s, r1);
239 /* Search zoneinfo rules to find the one that applies to the given time,
240 * and determine alternate opposite-DST-status rule that may be needed. */
242 static size_t scan_trans(long long t, int local, size_t *alt)
244 int scale = 3 - (trans == zi+44);
248 size_t a = 0, n = (index-trans)>>scale, m;
255 /* Binary search for 'most-recent rule before t'. */
258 x = zi_read32(trans + (m<<scale));
259 if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
261 if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
262 if (t - off < (int64_t)x) {
270 /* First and last entry are special. First means to use lowest-index
271 * non-DST type. Last means to apply POSIX-style rule if available. */
272 n = (index-trans)>>scale;
273 if (a == n-1) return -1;
275 x = zi_read32(trans + (a<<scale));
276 if (scale == 3) x = x<<32 | zi_read32(trans + (a<<scale) + 4);
278 if (local) off = (int32_t)zi_read32(types + 6 * index[a-1]);
279 if (t - off < (int64_t)x) {
280 for (a=0; a<(abbrevs-types)/6; a++) {
281 if (types[6*a+4] != types[4]) break;
283 if (a == (abbrevs-types)/6) a = 0;
294 /* Try to find a neighboring opposite-DST-status rule. */
296 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
298 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
307 static int days_in_month(int m, int is_leap)
309 if (m==2) return 28+is_leap;
310 else return 30+((0xad5>>(m-1))&1);
313 /* Convert a POSIX DST rule plus year to seconds since epoch. */
315 static long long rule_to_secs(const int *rule, int year)
318 long long t = __year_to_secs(year, &is_leap);
322 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
328 t += __month_to_secs(m-1, is_leap);
329 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
331 if (days < 0) days += 7;
332 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
333 t += 86400 * (days + 7*(n-1));
339 /* Determine the time zone in effect for a given time in seconds since the
340 * epoch. It can be given in local or universal time. The results will
341 * indicate whether DST is in effect at the queried time, and will give both
342 * the GMT offset for the active zone/DST rule and the opposite DST. This
343 * enables a caller to efficiently adjust for the case where an explicit
344 * DST specification mismatches what would be in effect at the time. */
346 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
353 size_t alt, i = scan_trans(t, local, &alt);
355 *isdst = types[6*i+4];
356 *offset = -(int32_t)zi_read32(types+6*i);
357 *zonename = (const char *)abbrevs + types[6*i+5];
358 if (oppoff) *oppoff = -(int32_t)zi_read32(types+6*alt);
364 if (!__daylight) goto std;
366 /* FIXME: may be broken if DST changes right at year boundary?
367 * Also, this could be more efficient.*/
368 long long y = t / 31556952 + 70;
369 while (__year_to_secs(y, 0) > t) y--;
370 while (__year_to_secs(y+1, 0) < t) y++;
372 long long t0 = rule_to_secs(r0, y);
373 long long t1 = rule_to_secs(r1, y);
380 if (t >= t0 && t < t1) goto dst;
387 if (t >= t1 && t < t0) goto std;
392 *offset = __timezone;
393 if (oppoff) *oppoff = dst_off;
394 *zonename = __tzname[0];
400 if (oppoff) *oppoff = __timezone;
401 *zonename = __tzname[1];
412 weak_alias(__tzset, tzset);
414 const char *__tm_to_tzname(const struct tm *tm)
416 const void *p = tm->__tm_zone;
419 if (p != __gmt && p != __tzname[0] && p != __tzname[1] &&
420 (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))