__tz.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include "time_impl.h"
  2. #include <stdint.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "libc.h"
  7. long __timezone = 0;
  8. int __daylight = 0;
  9. char *__tzname[2] = { 0, 0 };
  10. weak_alias(__timezone, timezone);
  11. weak_alias(__daylight, daylight);
  12. weak_alias(__tzname, tzname);
  13. static char std_name[TZNAME_MAX+1];
  14. static char dst_name[TZNAME_MAX+1];
  15. const char __gmt[] = "GMT";
  16. static int dst_off;
  17. static int r0[5], r1[5];
  18. static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
  19. static size_t map_size;
  20. static char old_tz_buf[32];
  21. static char *old_tz = old_tz_buf;
  22. static size_t old_tz_size = sizeof old_tz_buf;
  23. static int lock[2];
  24. static int getint(const char **p)
  25. {
  26. unsigned x;
  27. for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
  28. return x;
  29. }
  30. static int getsigned(const char **p)
  31. {
  32. if (**p == '-') {
  33. ++*p;
  34. return -getint(p);
  35. }
  36. if (**p == '+') ++*p;
  37. return getint(p);
  38. }
  39. static int getoff(const char **p)
  40. {
  41. int off = 3600*getsigned(p);
  42. if (**p == ':') {
  43. ++*p;
  44. off += 60*getint(p);
  45. if (**p == ':') {
  46. ++*p;
  47. off += getint(p);
  48. }
  49. }
  50. return off;
  51. }
  52. static void getrule(const char **p, int rule[5])
  53. {
  54. int r = rule[0] = **p;
  55. if (r!='M') {
  56. if (r=='J') ++*p;
  57. else rule[0] = 0;
  58. rule[1] = getint(p);
  59. } else {
  60. ++*p; rule[1] = getint(p);
  61. ++*p; rule[2] = getint(p);
  62. ++*p; rule[3] = getint(p);
  63. }
  64. if (**p=='/') {
  65. ++*p;
  66. rule[4] = getoff(p);
  67. } else {
  68. rule[4] = 7200;
  69. }
  70. }
  71. static void getname(char *d, const char **p)
  72. {
  73. int i;
  74. if (**p == '<') {
  75. ++*p;
  76. for (i=0; **p!='>' && i<TZNAME_MAX; i++)
  77. d[i] = (*p)[i];
  78. ++*p;
  79. } else {
  80. for (i=0; ((*p)[i]|32)-'a'<26U && i<TZNAME_MAX; i++)
  81. d[i] = (*p)[i];
  82. }
  83. *p += i;
  84. d[i] = 0;
  85. }
  86. #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
  87. static uint32_t zi_read32(const unsigned char *z)
  88. {
  89. return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
  90. }
  91. static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
  92. {
  93. size_t y;
  94. uint32_t x;
  95. for (y=0; n; n--, z+=4, v++) {
  96. x = zi_read32(z);
  97. y += x * *v;
  98. }
  99. return y;
  100. }
  101. int __munmap(void *, size_t);
  102. static void do_tzset()
  103. {
  104. char buf[NAME_MAX+25], *pathname=buf+24;
  105. const char *try, *s;
  106. const unsigned char *map = 0;
  107. size_t i;
  108. static const char search[] =
  109. "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
  110. s = getenv("TZ");
  111. if (!s || !*s) s = __gmt;
  112. if (old_tz && !strcmp(s, old_tz)) return;
  113. if (zi) __munmap((void *)zi, map_size);
  114. /* Cache the old value of TZ to check if it has changed. Avoid
  115. * free so as not to pull it into static programs. Growth
  116. * strategy makes it so free would have minimal benefit anyway. */
  117. i = strlen(s);
  118. if (i > PATH_MAX+1) s = "", i = 0;
  119. if (i >= old_tz_size) {
  120. old_tz_size *= 2;
  121. if (i >= old_tz_size) old_tz_size = i+1;
  122. if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
  123. old_tz = malloc(old_tz_size);
  124. }
  125. if (old_tz) memcpy(old_tz, s, i+1);
  126. if (*s == ':') s++;
  127. /* Non-suid can use an absolute tzfile pathname or a relative
  128. * pathame beginning with "."; in secure mode, only the
  129. * standard path will be searched. */
  130. if (*s == '/' || *s == '.') {
  131. if (!libc.secure) map = __map_file(s, &map_size);
  132. } else {
  133. for (i=0; s[i] && s[i]!=','; i++) {
  134. if (s[i]=='/') {
  135. size_t l = strlen(s);
  136. if (l > NAME_MAX || strchr(s, '.'))
  137. break;
  138. memcpy(pathname, s, l+1);
  139. pathname[l] = 0;
  140. for (try=search; !map && *try; try+=l) {
  141. l = strlen(try);
  142. memcpy(pathname-l, try, l);
  143. map = __map_file(pathname-l, &map_size);
  144. }
  145. break;
  146. }
  147. }
  148. }
  149. zi = map;
  150. if (map) {
  151. int scale = 2;
  152. if (sizeof(time_t) > 4 && map[4]=='2') {
  153. size_t skip = zi_dotprod(zi, VEC(1,1,8,5,6,1), 6);
  154. trans = zi+skip+44+20;
  155. scale++;
  156. } else {
  157. trans = zi+44;
  158. }
  159. index = trans + (zi_read32(trans-12) << scale);
  160. types = index + zi_read32(trans-12);
  161. abbrevs = types + 6*zi_read32(trans-8);
  162. abbrevs_end = abbrevs + zi_read32(trans-4);
  163. if (zi[map_size-1] == '\n') {
  164. for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
  165. s++;
  166. } else {
  167. const unsigned char *p;
  168. __tzname[0] = __tzname[1] = 0;
  169. __daylight = __timezone = dst_off = 0;
  170. for (i=0; i<5; i++) r0[i] = r1[i] = 0;
  171. for (p=types; p<abbrevs; p+=6) {
  172. if (!p[4] && !__tzname[0]) {
  173. __tzname[0] = (char *)abbrevs + p[5];
  174. __timezone = -zi_read32(p);
  175. }
  176. if (p[4] && !__tzname[1]) {
  177. __tzname[1] = (char *)abbrevs + p[5];
  178. dst_off = -zi_read32(p);
  179. __daylight = 1;
  180. }
  181. }
  182. if (!__tzname[0]) __tzname[0] = __tzname[1];
  183. if (!__tzname[0]) __tzname[0] = (char *)__gmt;
  184. if (!__daylight) {
  185. __tzname[1] = __tzname[0];
  186. dst_off = __timezone;
  187. }
  188. return;
  189. }
  190. }
  191. if (!s) s = __gmt;
  192. getname(std_name, &s);
  193. __tzname[0] = std_name;
  194. __timezone = getoff(&s);
  195. getname(dst_name, &s);
  196. __tzname[1] = dst_name;
  197. if (dst_name[0]) {
  198. __daylight = 1;
  199. if (*s == '+' || *s=='-' || *s-'0'<10U)
  200. dst_off = getoff(&s);
  201. else
  202. dst_off = __timezone - 3600;
  203. } else {
  204. __daylight = 0;
  205. dst_off = 0;
  206. }
  207. if (*s == ',') s++, getrule(&s, r0);
  208. if (*s == ',') s++, getrule(&s, r1);
  209. }
  210. /* Search zoneinfo rules to find the one that applies to the given time,
  211. * and determine alternate opposite-DST-status rule that may be needed. */
  212. static size_t scan_trans(long long t, int local, size_t *alt)
  213. {
  214. int scale = 3 - (trans == zi+44);
  215. uint64_t x;
  216. int off = 0;
  217. size_t a = 0, n = (index-trans)>>scale, m;
  218. if (!n) {
  219. if (alt) *alt = 0;
  220. return 0;
  221. }
  222. /* Binary search for 'most-recent rule before t'. */
  223. while (n > 1) {
  224. m = a + n/2;
  225. x = zi_read32(trans + (m<<scale));
  226. if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
  227. else x = (int32_t)x;
  228. if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
  229. if (t - off < (int64_t)x) {
  230. n /= 2;
  231. } else {
  232. a = m;
  233. n -= n/2;
  234. }
  235. }
  236. /* First and last entry are special. First means to use lowest-index
  237. * non-DST type. Last means to apply POSIX-style rule if available. */
  238. n = (index-trans)>>scale;
  239. if (a == n-1) return -1;
  240. if (a == 0) {
  241. x = zi_read32(trans + (a<<scale));
  242. if (scale == 3) x = x<<32 | zi_read32(trans + (a<<scale) + 4);
  243. else x = (int32_t)x;
  244. if (local) off = (int32_t)zi_read32(types + 6 * index[a-1]);
  245. if (t - off < (int64_t)x) {
  246. for (a=0; a<(abbrevs-types)/6; a++) {
  247. if (types[6*a+4] != types[4]) break;
  248. }
  249. if (a == (abbrevs-types)/6) a = 0;
  250. if (types[6*a+4]) {
  251. *alt = a;
  252. return 0;
  253. } else {
  254. *alt = 0;
  255. return a;
  256. }
  257. }
  258. }
  259. /* Try to find a neighboring opposite-DST-status rule. */
  260. if (alt) {
  261. if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
  262. *alt = index[a-1];
  263. else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
  264. *alt = index[a+1];
  265. else
  266. *alt = index[a];
  267. }
  268. return index[a];
  269. }
  270. static int days_in_month(int m, int is_leap)
  271. {
  272. if (m==2) return 28+is_leap;
  273. else return 30+((0xad5>>(m-1))&1);
  274. }
  275. /* Convert a POSIX DST rule plus year to seconds since epoch. */
  276. static long long rule_to_secs(const int *rule, int year)
  277. {
  278. int is_leap;
  279. long long t = __year_to_secs(year, &is_leap);
  280. int x, m, n, d;
  281. if (rule[0]!='M') {
  282. x = rule[1];
  283. if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
  284. t += 86400 * x;
  285. } else {
  286. m = rule[1];
  287. n = rule[2];
  288. d = rule[3];
  289. t += __month_to_secs(m-1, is_leap);
  290. int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
  291. int days = d - wday;
  292. if (days < 0) days += 7;
  293. if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
  294. t += 86400 * (days + 7*(n-1));
  295. }
  296. t += rule[4];
  297. return t;
  298. }
  299. /* Determine the time zone in effect for a given time in seconds since the
  300. * epoch. It can be given in local or universal time. The results will
  301. * indicate whether DST is in effect at the queried time, and will give both
  302. * the GMT offset for the active zone/DST rule and the opposite DST. This
  303. * enables a caller to efficiently adjust for the case where an explicit
  304. * DST specification mismatches what would be in effect at the time. */
  305. void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
  306. {
  307. LOCK(lock);
  308. do_tzset();
  309. if (zi) {
  310. size_t alt, i = scan_trans(t, local, &alt);
  311. if (i != -1) {
  312. *isdst = types[6*i+4];
  313. *offset = -(int32_t)zi_read32(types+6*i);
  314. *zonename = (const char *)abbrevs + types[6*i+5];
  315. if (oppoff) *oppoff = -(int32_t)zi_read32(types+6*alt);
  316. UNLOCK(lock);
  317. return;
  318. }
  319. }
  320. if (!__daylight) goto std;
  321. /* FIXME: may be broken if DST changes right at year boundary?
  322. * Also, this could be more efficient.*/
  323. long long y = t / 31556952 + 70;
  324. while (__year_to_secs(y, 0) > t) y--;
  325. while (__year_to_secs(y+1, 0) < t) y++;
  326. long long t0 = rule_to_secs(r0, y);
  327. long long t1 = rule_to_secs(r1, y);
  328. if (t0 < t1) {
  329. if (!local) {
  330. t0 += __timezone;
  331. t1 += dst_off;
  332. }
  333. if (t >= t0 && t < t1) goto dst;
  334. goto std;
  335. } else {
  336. if (!local) {
  337. t1 += __timezone;
  338. t0 += dst_off;
  339. }
  340. if (t >= t1 && t < t0) goto std;
  341. goto dst;
  342. }
  343. std:
  344. *isdst = 0;
  345. *offset = __timezone;
  346. if (oppoff) *oppoff = dst_off;
  347. *zonename = __tzname[0];
  348. UNLOCK(lock);
  349. return;
  350. dst:
  351. *isdst = 1;
  352. *offset = dst_off;
  353. if (oppoff) *oppoff = __timezone;
  354. *zonename = __tzname[1];
  355. UNLOCK(lock);
  356. }
  357. void __tzset()
  358. {
  359. LOCK(lock);
  360. do_tzset();
  361. UNLOCK(lock);
  362. }
  363. weak_alias(__tzset, tzset);
  364. const char *__tm_to_tzname(const struct tm *tm)
  365. {
  366. const void *p = tm->__tm_zone;
  367. LOCK(lock);
  368. do_tzset();
  369. if (p != __gmt && p != __tzname[0] && p != __tzname[1] &&
  370. (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
  371. p = "";
  372. UNLOCK(lock);
  373. return p;
  374. }