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 + (a<<scale));
86 if (scale == 3) x = x<<32 | zi_read32(trans + (a<<scale) + 4);
87 else x = (int32_t)x;
88 if (local) off = (int32_t)zi_read32(types + 6 * index[a-1]);
89 if (t - off < (int64_t)x) {
90 for (a=0; a<(abbrevs-types)/6; a++) {
91 if (types[6*a+4] != types[4]) break;
92 }
93 if (a == (abbrevs-types)/6) a = 0;
94 if (types[6*a+4]) {
95 *alt = a;
96 return 0;
97 } else {
98 *alt = 0;
99 return a;
100 }
101 }
102 }
103
104 /* Try to find a neighboring opposite-DST-status rule. */
105 if (alt) {
106 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
107 *alt = index[a-1];
108 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
109 *alt = index[a+1];
110 else
111 *alt = index[a];
112 }
113
114 return index[a];
115 }
116
days_in_month(int m,int is_leap)117 static int days_in_month(int m, int is_leap)
118 {
119 if (m==2) return 28+is_leap;
120 else return 30+((0xad5>>(m-1))&1);
121 }
122
123 /* Convert a POSIX DST rule plus year to seconds since epoch. */
124
rule_to_secs(const int * rule,int year)125 static long long rule_to_secs(const int *rule, int year)
126 {
127 int is_leap;
128 long long t = __year_to_secs(year, &is_leap);
129 int x, m, n, d;
130 if (rule[0]!='M') {
131 x = rule[1];
132 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
133 t += 86400 * x;
134 } else {
135 m = rule[1];
136 n = rule[2];
137 d = rule[3];
138 t += __month_to_secs(m-1, is_leap);
139 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
140 int days = d - wday;
141 if (days < 0) days += 7;
142 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
143 t += 86400 * (days + 7*(n-1));
144 }
145 t += rule[4];
146 return t;
147 }
148
149 /* Determine the time zone in effect for a given time in seconds since the
150 * epoch. It can be given in local or universal time. The results will
151 * indicate whether DST is in effect at the queried time, and will give both
152 * the GMT offset for the active zone/DST rule and the opposite DST. This
153 * enables a caller to efficiently adjust for the case where an explicit
154 * DST specification mismatches what would be in effect at the time. */
155
__secs_to_zone(long long t,int local,int * isdst,long * offset,long * oppoff,const char ** zonename)156 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
157 {
158 LOCK();
159
160 do_tzset();
161
162 if (zi) {
163 size_t alt, i = scan_trans(t, local, &alt);
164 if (i != -1) {
165 *isdst = types[6*i+4];
166 *offset = (int32_t)zi_read32(types+6*i);
167 *zonename = (const char *)abbrevs + types[6*i+5];
168 if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
169 UNLOCK();
170 return;
171 }
172 }
173
174 if (!__daylight) goto std;
175
176 /* FIXME: may be broken if DST changes right at year boundary?
177 * Also, this could be more efficient.*/
178 long long y = t / 31556952 + 70;
179 while (__year_to_secs(y, 0) > t) y--;
180 while (__year_to_secs(y+1, 0) < t) y++;
181
182 long long t0 = rule_to_secs(r0, y);
183 long long t1 = rule_to_secs(r1, y);
184
185 if (!local) {
186 t0 += __timezone;
187 t1 += dst_off;
188 }
189 if (t0 < t1) {
190 if (t >= t0 && t < t1) goto dst;
191 goto std;
192 } else {
193 if (t >= t1 && t < t0) goto std;
194 goto dst;
195 }
196 std:
197 *isdst = 0;
198 *offset = -__timezone;
199 if (oppoff) *oppoff = -dst_off;
200 *zonename = __tzname[0];
201 UNLOCK();
202 return;
203 dst:
204 *isdst = 1;
205 *offset = -dst_off;
206 if (oppoff) *oppoff = -__timezone;
207 *zonename = __tzname[1];
208 UNLOCK();
209 }
210
__tzset(void)211 static void __tzset(void)
212 {
213 LOCK();
214 do_tzset();
215 UNLOCK();
216 }
217
218 weak_alias(__tzset, tzset);
219
__tm_to_tzname(const struct tm * tm)220 const char *__tm_to_tzname(const struct tm *tm)
221 {
222 const void *p = tm->__tm_zone;
223 LOCK();
224 do_tzset();
225 if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
226 (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
227 p = "";
228 UNLOCK();
229 return p;
230 }
231