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 "libc.h"
8 #include <pthread.h>
9
10 long __timezone = 0;
11 int __daylight = 0;
12 char *__tzname[2] = { 0, 0 };
13
14 weak_alias(__timezone, timezone);
15 weak_alias(__daylight, daylight);
16 weak_alias(__tzname, tzname);
17
18 static pthread_mutex_t locallock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
19
LOCK(void)20 static int LOCK(void)
21 {
22 return pthread_mutex_lock(&locallock);
23 }
24
UNLOCK(void)25 static void UNLOCK(void)
26 {
27 (void)pthread_mutex_unlock(&locallock);
28 }
29
30
31 const char __utc[] = "UTC";
32
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
38 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
39
zi_read32(const unsigned char * z)40 static uint32_t zi_read32(const unsigned char *z)
41 {
42 return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
43 }
44
do_tzset(void)45 static void do_tzset(void)
46 {
47 }
48
49 /* Search zoneinfo rules to find the one that applies to the given time,
50 * and determine alternate opposite-DST-status rule that may be needed. */
51
scan_trans(long long t,int local,size_t * alt)52 static size_t scan_trans(long long t, int local, size_t *alt)
53 {
54 int scale = 3 - (trans == zi+44);
55 uint64_t x;
56 int off = 0;
57
58 size_t a = 0, n = (index-trans)>>scale, m;
59
60 if (!n) {
61 if (alt) *alt = 0;
62 return 0;
63 }
64
65 /* Binary search for 'most-recent rule before t'. */
66 while (n > 1) {
67 m = a + n/2;
68 x = zi_read32(trans + (m<<scale));
69 if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
70 else x = (int32_t)x;
71 if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
72 if (t - off < (int64_t)x) {
73 n /= 2;
74 } else {
75 a = m;
76 n -= n/2;
77 }
78 }
79
80 /* First and last entry are special. First means to use lowest-index
81 * non-DST type. Last means to apply POSIX-style rule if available. */
82 n = (index-trans)>>scale;
83 if (a == n-1) return -1;
84 if (a == 0) {
85 x = zi_read32(trans);
86 if (scale == 3) x = x<<32 | zi_read32(trans + 4);
87 else x = (int32_t)x;
88 /* Find the lowest non-DST type, or 0 if none. */
89 size_t j = 0;
90 for (size_t i=abbrevs-types; i; i-=6) {
91 if (!types[i-6+4]) j = i-6;
92 }
93 if (local) off = (int32_t)zi_read32(types + j);
94 /* If t is before first transition, use the above-found type
95 * and the index-zero (after transition) type as the alt. */
96 if (t - off < (int64_t)x) {
97 if (alt) *alt = index[0];
98 return j/6;
99 }
100 }
101
102 /* Try to find a neighboring opposite-DST-status rule. */
103 if (alt) {
104 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
105 *alt = index[a-1];
106 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
107 *alt = index[a+1];
108 else
109 *alt = index[a];
110 }
111
112 return index[a];
113 }
114
days_in_month(int m,int is_leap)115 static int days_in_month(int m, int is_leap)
116 {
117 if (m==2) return 28+is_leap;
118 else return 30+((0xad5>>(m-1))&1);
119 }
120
121 /* Convert a POSIX DST rule plus year to seconds since epoch. */
122
rule_to_secs(const int * rule,int year)123 static long long rule_to_secs(const int *rule, int year)
124 {
125 int is_leap;
126 long long t = __year_to_secs(year, &is_leap);
127 int x, m, n, d;
128 if (rule[0]!='M') {
129 x = rule[1];
130 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
131 t += 86400 * x;
132 } else {
133 m = rule[1];
134 n = rule[2];
135 d = rule[3];
136 t += __month_to_secs(m-1, is_leap);
137 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
138 int days = d - wday;
139 if (days < 0) days += 7;
140 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
141 t += 86400 * (days + 7*(n-1));
142 }
143 t += rule[4];
144 return t;
145 }
146
147 /* Determine the time zone in effect for a given time in seconds since the
148 * epoch. It can be given in local or universal time. The results will
149 * indicate whether DST is in effect at the queried time, and will give both
150 * the GMT offset for the active zone/DST rule and the opposite DST. This
151 * enables a caller to efficiently adjust for the case where an explicit
152 * DST specification mismatches what would be in effect at the time. */
153
__secs_to_zone(long long t,int local,int * isdst,long * offset,long * oppoff,const char ** zonename)154 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
155 {
156 LOCK();
157
158 do_tzset();
159
160 if (zi) {
161 size_t alt, i = scan_trans(t, local, &alt);
162 if (i != -1) {
163 *isdst = types[6*i+4];
164 *offset = (int32_t)zi_read32(types+6*i);
165 *zonename = (const char *)abbrevs + types[6*i+5];
166 if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
167 UNLOCK();
168 return;
169 }
170 }
171
172 if (!__daylight) goto std;
173
174 /* FIXME: may be broken if DST changes right at year boundary?
175 * Also, this could be more efficient.*/
176 long long y = t / 31556952 + 70;
177 while (__year_to_secs(y, 0) > t) y--;
178 while (__year_to_secs(y+1, 0) < t) y++;
179
180 long long t0 = rule_to_secs(r0, y);
181 long long t1 = rule_to_secs(r1, y);
182
183 if (!local) {
184 t0 += __timezone;
185 t1 += dst_off;
186 }
187 if (t0 < t1) {
188 if (t >= t0 && t < t1) goto dst;
189 goto std;
190 } else {
191 if (t >= t1 && t < t0) goto std;
192 goto dst;
193 }
194 std:
195 *isdst = 0;
196 *offset = -__timezone;
197 if (oppoff) *oppoff = -dst_off;
198 *zonename = __tzname[0];
199 UNLOCK();
200 return;
201 dst:
202 *isdst = 1;
203 *offset = -dst_off;
204 if (oppoff) *oppoff = -__timezone;
205 *zonename = __tzname[1];
206 UNLOCK();
207 }
208
__tzset(void)209 static void __tzset(void)
210 {
211 LOCK();
212 do_tzset();
213 UNLOCK();
214 }
215
216 weak_alias(__tzset, tzset);
217
__tm_to_tzname(const struct tm * tm)218 const char *__tm_to_tzname(const struct tm *tm)
219 {
220 const void *p = tm->__tm_zone;
221 LOCK();
222 do_tzset();
223 if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
224 (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
225 p = "";
226 UNLOCK();
227 return p;
228 }
229