__tz.c 8.4 KB

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