parse_tz.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Derick Rethans
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "timelib.h"
  25. #include <stdio.h>
  26. #ifdef HAVE_LOCALE_H
  27. #include <locale.h>
  28. #endif
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #else
  32. #include <strings.h>
  33. #endif
  34. #define TIMELIB_SUPPORTS_V2DATA
  35. #include "timezonedb.h"
  36. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  37. # if defined(__LITTLE_ENDIAN__)
  38. # undef WORDS_BIGENDIAN
  39. # else
  40. # if defined(__BIG_ENDIAN__)
  41. # define WORDS_BIGENDIAN
  42. # endif
  43. # endif
  44. #endif
  45. #ifdef WORDS_BIGENDIAN
  46. #define timelib_conv_int(l) (l)
  47. #else
  48. #define timelib_conv_int(l) ((l & 0x000000ff) << 24) + ((l & 0x0000ff00) << 8) + ((l & 0x00ff0000) >> 8) + ((l & 0xff000000) >> 24)
  49. #endif
  50. static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  51. {
  52. uint32_t version;
  53. /* read ID */
  54. version = (*tzf)[3] - '0';
  55. *tzf += 4;
  56. /* read BC flag */
  57. tz->bc = (**tzf == '\1');
  58. *tzf += 1;
  59. /* read country code */
  60. memcpy(tz->location.country_code, *tzf, 2);
  61. tz->location.country_code[2] = '\0';
  62. *tzf += 2;
  63. /* skip rest of preamble */
  64. *tzf += 13;
  65. return version;
  66. }
  67. static void read_header(const unsigned char **tzf, timelib_tzinfo *tz)
  68. {
  69. uint32_t buffer[6];
  70. memcpy(&buffer, *tzf, sizeof(buffer));
  71. tz->bit32.ttisgmtcnt = timelib_conv_int(buffer[0]);
  72. tz->bit32.ttisstdcnt = timelib_conv_int(buffer[1]);
  73. tz->bit32.leapcnt = timelib_conv_int(buffer[2]);
  74. tz->bit32.timecnt = timelib_conv_int(buffer[3]);
  75. tz->bit32.typecnt = timelib_conv_int(buffer[4]);
  76. tz->bit32.charcnt = timelib_conv_int(buffer[5]);
  77. *tzf += sizeof(buffer);
  78. }
  79. static void skip_64bit_transistions(const unsigned char **tzf, timelib_tzinfo *tz)
  80. {
  81. if (tz->bit64.timecnt) {
  82. *tzf += (sizeof(int64_t) * tz->bit64.timecnt);
  83. *tzf += (sizeof(unsigned char) * tz->bit64.timecnt);
  84. }
  85. }
  86. static void read_transistions(const unsigned char **tzf, timelib_tzinfo *tz)
  87. {
  88. int32_t *buffer = NULL;
  89. uint32_t i;
  90. unsigned char *cbuffer = NULL;
  91. if (tz->bit32.timecnt) {
  92. buffer = (int32_t*) timelib_malloc(tz->bit32.timecnt * sizeof(int32_t));
  93. if (!buffer) {
  94. return;
  95. }
  96. memcpy(buffer, *tzf, sizeof(int32_t) * tz->bit32.timecnt);
  97. *tzf += (sizeof(int32_t) * tz->bit32.timecnt);
  98. for (i = 0; i < tz->bit32.timecnt; i++) {
  99. buffer[i] = timelib_conv_int(buffer[i]);
  100. }
  101. cbuffer = (unsigned char*) timelib_malloc(tz->bit32.timecnt * sizeof(unsigned char));
  102. if (!cbuffer) {
  103. timelib_free(buffer);
  104. return;
  105. }
  106. memcpy(cbuffer, *tzf, sizeof(unsigned char) * tz->bit32.timecnt);
  107. *tzf += sizeof(unsigned char) * tz->bit32.timecnt;
  108. }
  109. tz->trans = buffer;
  110. tz->trans_idx = cbuffer;
  111. }
  112. static void skip_64bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
  113. {
  114. *tzf += sizeof(unsigned char) * 6 * tz->bit64.typecnt;
  115. *tzf += sizeof(char) * tz->bit64.charcnt;
  116. if (tz->bit64.leapcnt) {
  117. *tzf += sizeof(int64_t) * tz->bit64.leapcnt * 2;
  118. }
  119. if (tz->bit64.ttisstdcnt) {
  120. *tzf += sizeof(unsigned char) * tz->bit64.ttisstdcnt;
  121. }
  122. if (tz->bit64.ttisgmtcnt) {
  123. *tzf += sizeof(unsigned char) * tz->bit64.ttisgmtcnt;
  124. }
  125. }
  126. static void read_types(const unsigned char **tzf, timelib_tzinfo *tz)
  127. {
  128. unsigned char *buffer;
  129. int32_t *leap_buffer;
  130. unsigned int i, j;
  131. buffer = (unsigned char*) timelib_malloc(tz->bit32.typecnt * sizeof(unsigned char) * 6);
  132. if (!buffer) {
  133. return;
  134. }
  135. memcpy(buffer, *tzf, sizeof(unsigned char) * 6 * tz->bit32.typecnt);
  136. *tzf += sizeof(unsigned char) * 6 * tz->bit32.typecnt;
  137. tz->type = (ttinfo*) timelib_malloc(tz->bit32.typecnt * sizeof(struct ttinfo));
  138. if (!tz->type) {
  139. timelib_free(buffer);
  140. return;
  141. }
  142. for (i = 0; i < tz->bit32.typecnt; i++) {
  143. j = i * 6;
  144. tz->type[i].offset = (buffer[j] * 16777216) + (buffer[j + 1] * 65536) + (buffer[j + 2] * 256) + buffer[j + 3];
  145. tz->type[i].isdst = buffer[j + 4];
  146. tz->type[i].abbr_idx = buffer[j + 5];
  147. }
  148. timelib_free(buffer);
  149. tz->timezone_abbr = (char*) timelib_malloc(tz->bit32.charcnt);
  150. if (!tz->timezone_abbr) {
  151. return;
  152. }
  153. memcpy(tz->timezone_abbr, *tzf, sizeof(char) * tz->bit32.charcnt);
  154. *tzf += sizeof(char) * tz->bit32.charcnt;
  155. if (tz->bit32.leapcnt) {
  156. leap_buffer = (int32_t *) timelib_malloc(tz->bit32.leapcnt * 2 * sizeof(int32_t));
  157. if (!leap_buffer) {
  158. return;
  159. }
  160. memcpy(leap_buffer, *tzf, sizeof(int32_t) * tz->bit32.leapcnt * 2);
  161. *tzf += sizeof(int32_t) * tz->bit32.leapcnt * 2;
  162. tz->leap_times = (tlinfo*) timelib_malloc(tz->bit32.leapcnt * sizeof(tlinfo));
  163. if (!tz->leap_times) {
  164. timelib_free(leap_buffer);
  165. return;
  166. }
  167. for (i = 0; i < tz->bit32.leapcnt; i++) {
  168. tz->leap_times[i].trans = timelib_conv_int(leap_buffer[i * 2]);
  169. tz->leap_times[i].offset = timelib_conv_int(leap_buffer[i * 2 + 1]);
  170. }
  171. timelib_free(leap_buffer);
  172. }
  173. if (tz->bit32.ttisstdcnt) {
  174. buffer = (unsigned char*) timelib_malloc(tz->bit32.ttisstdcnt * sizeof(unsigned char));
  175. if (!buffer) {
  176. return;
  177. }
  178. memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit32.ttisstdcnt);
  179. *tzf += sizeof(unsigned char) * tz->bit32.ttisstdcnt;
  180. for (i = 0; i < tz->bit32.ttisstdcnt; i++) {
  181. tz->type[i].isstdcnt = buffer[i];
  182. }
  183. timelib_free(buffer);
  184. }
  185. if (tz->bit32.ttisgmtcnt) {
  186. buffer = (unsigned char*) timelib_malloc(tz->bit32.ttisgmtcnt * sizeof(unsigned char));
  187. if (!buffer) {
  188. return;
  189. }
  190. memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit32.ttisgmtcnt);
  191. *tzf += sizeof(unsigned char) * tz->bit32.ttisgmtcnt;
  192. for (i = 0; i < tz->bit32.ttisgmtcnt; i++) {
  193. tz->type[i].isgmtcnt = buffer[i];
  194. }
  195. timelib_free(buffer);
  196. }
  197. }
  198. static void skip_posix_string(const unsigned char **tzf, timelib_tzinfo *tz)
  199. {
  200. int n_count = 0;
  201. do {
  202. if (*tzf[0] == '\n') {
  203. n_count++;
  204. }
  205. (*tzf)++;
  206. } while (n_count < 2);
  207. }
  208. static void read_location(const unsigned char **tzf, timelib_tzinfo *tz)
  209. {
  210. uint32_t buffer[3];
  211. uint32_t comments_len;
  212. memcpy(&buffer, *tzf, sizeof(buffer));
  213. tz->location.latitude = timelib_conv_int(buffer[0]);
  214. tz->location.latitude = (tz->location.latitude / 100000) - 90;
  215. tz->location.longitude = timelib_conv_int(buffer[1]);
  216. tz->location.longitude = (tz->location.longitude / 100000) - 180;
  217. comments_len = timelib_conv_int(buffer[2]);
  218. *tzf += sizeof(buffer);
  219. tz->location.comments = timelib_malloc(comments_len + 1);
  220. memcpy(tz->location.comments, *tzf, comments_len);
  221. tz->location.comments[comments_len] = '\0';
  222. *tzf += comments_len;
  223. }
  224. void timelib_dump_tzinfo(timelib_tzinfo *tz)
  225. {
  226. uint32_t i;
  227. printf("Country Code: %s\n", tz->location.country_code);
  228. printf("Geo Location: %f,%f\n", tz->location.latitude, tz->location.longitude);
  229. printf("Comments:\n%s\n", tz->location.comments);
  230. printf("BC: %s\n", tz->bc ? "" : "yes");
  231. printf("UTC/Local count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.ttisgmtcnt);
  232. printf("Std/Wall count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.ttisstdcnt);
  233. printf("Leap.sec. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.leapcnt);
  234. printf("Trans. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.timecnt);
  235. printf("Local types count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.typecnt);
  236. printf("Zone Abbr. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit32.charcnt);
  237. printf ("%8s (%12s) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
  238. "", "", 0,
  239. (long int) tz->type[0].offset,
  240. tz->type[0].isdst,
  241. tz->type[0].abbr_idx,
  242. &tz->timezone_abbr[tz->type[0].abbr_idx],
  243. tz->type[0].isstdcnt,
  244. tz->type[0].isgmtcnt
  245. );
  246. for (i = 0; i < tz->bit32.timecnt; i++) {
  247. printf ("%08X (%12d) = %3d [%5ld %1d %3d '%s' (%d,%d)]\n",
  248. tz->trans[i], tz->trans[i], tz->trans_idx[i],
  249. (long int) tz->type[tz->trans_idx[i]].offset,
  250. tz->type[tz->trans_idx[i]].isdst,
  251. tz->type[tz->trans_idx[i]].abbr_idx,
  252. &tz->timezone_abbr[tz->type[tz->trans_idx[i]].abbr_idx],
  253. tz->type[tz->trans_idx[i]].isstdcnt,
  254. tz->type[tz->trans_idx[i]].isgmtcnt
  255. );
  256. }
  257. for (i = 0; i < tz->bit32.leapcnt; i++) {
  258. printf ("%08X (%12ld) = %d\n",
  259. tz->leap_times[i].trans,
  260. (long) tz->leap_times[i].trans,
  261. tz->leap_times[i].offset);
  262. }
  263. }
  264. static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
  265. {
  266. int left = 0, right = tzdb->index_size - 1;
  267. #ifdef HAVE_SETLOCALE
  268. char *cur_locale = NULL, *tmp;
  269. tmp = setlocale(LC_CTYPE, NULL);
  270. if (tmp) {
  271. cur_locale = timelib_strdup(tmp);
  272. }
  273. setlocale(LC_CTYPE, "C");
  274. #endif
  275. do {
  276. int mid = ((unsigned)left + right) >> 1;
  277. int cmp = strcasecmp(timezone, tzdb->index[mid].id);
  278. if (cmp < 0) {
  279. right = mid - 1;
  280. } else if (cmp > 0) {
  281. left = mid + 1;
  282. } else { /* (cmp == 0) */
  283. (*tzf) = &(tzdb->data[tzdb->index[mid].pos]);
  284. #ifdef HAVE_SETLOCALE
  285. setlocale(LC_CTYPE, cur_locale);
  286. if (cur_locale) timelib_free(cur_locale);
  287. #endif
  288. return 1;
  289. }
  290. } while (left <= right);
  291. #ifdef HAVE_SETLOCALE
  292. setlocale(LC_CTYPE, cur_locale);
  293. if (cur_locale) timelib_free(cur_locale);
  294. #endif
  295. return 0;
  296. }
  297. const timelib_tzdb *timelib_builtin_db(void)
  298. {
  299. return &timezonedb_builtin;
  300. }
  301. const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
  302. {
  303. *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
  304. return timezonedb_idx_builtin;
  305. }
  306. int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
  307. {
  308. const unsigned char *tzf;
  309. return (seek_to_tz_position(&tzf, timezone, tzdb));
  310. }
  311. static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  312. {
  313. *tzf += 20;
  314. }
  315. static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
  316. {
  317. uint32_t buffer[6];
  318. memcpy(&buffer, *tzf, sizeof(buffer));
  319. tz->bit64.ttisgmtcnt = timelib_conv_int(buffer[0]);
  320. tz->bit64.ttisstdcnt = timelib_conv_int(buffer[1]);
  321. tz->bit64.leapcnt = timelib_conv_int(buffer[2]);
  322. tz->bit64.timecnt = timelib_conv_int(buffer[3]);
  323. tz->bit64.typecnt = timelib_conv_int(buffer[4]);
  324. tz->bit64.charcnt = timelib_conv_int(buffer[5]);
  325. *tzf += sizeof(buffer);
  326. }
  327. timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
  328. {
  329. const unsigned char *tzf;
  330. timelib_tzinfo *tmp;
  331. int version;
  332. if (seek_to_tz_position(&tzf, timezone, tzdb)) {
  333. tmp = timelib_tzinfo_ctor(timezone);
  334. version = read_preamble(&tzf, tmp);
  335. read_header(&tzf, tmp);
  336. read_transistions(&tzf, tmp);
  337. read_types(&tzf, tmp);
  338. if (version == 2) {
  339. skip_64bit_preamble(&tzf, tmp);
  340. read_64bit_header(&tzf, tmp);
  341. skip_64bit_transistions(&tzf, tmp);
  342. skip_64bit_types(&tzf, tmp);
  343. skip_posix_string(&tzf, tmp);
  344. }
  345. read_location(&tzf, tmp);
  346. } else {
  347. tmp = NULL;
  348. }
  349. return tmp;
  350. }
  351. static ttinfo* fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
  352. {
  353. uint32_t i;
  354. /* If there is no transition time, we pick the first one, if that doesn't
  355. * exist we return NULL */
  356. if (!tz->bit32.timecnt || !tz->trans) {
  357. *transition_time = 0;
  358. if (tz->bit32.typecnt == 1) {
  359. return &(tz->type[0]);
  360. }
  361. return NULL;
  362. }
  363. /* If the TS is lower than the first transition time, then we scan over
  364. * all the transition times to find the first non-DST one, or the first
  365. * one in case there are only DST entries. Not sure which smartass came up
  366. * with this idea in the first though :) */
  367. if (ts < tz->trans[0]) {
  368. uint32_t j;
  369. *transition_time = 0;
  370. j = 0;
  371. while (j < tz->bit32.timecnt && tz->type[tz->trans_idx[j]].isdst) {
  372. ++j;
  373. }
  374. if (j == tz->bit32.timecnt) {
  375. j = 0;
  376. }
  377. return &(tz->type[tz->trans_idx[j]]);
  378. }
  379. /* In all other cases we loop through the available transtion times to find
  380. * the correct entry */
  381. for (i = 0; i < tz->bit32.timecnt; i++) {
  382. if (ts < tz->trans[i]) {
  383. *transition_time = tz->trans[i - 1];
  384. return &(tz->type[tz->trans_idx[i - 1]]);
  385. }
  386. }
  387. *transition_time = tz->trans[tz->bit32.timecnt - 1];
  388. return &(tz->type[tz->trans_idx[tz->bit32.timecnt - 1]]);
  389. }
  390. static tlinfo* fetch_leaptime_offset(timelib_tzinfo *tz, timelib_sll ts)
  391. {
  392. int i;
  393. if (!tz->bit32.leapcnt || !tz->leap_times) {
  394. return NULL;
  395. }
  396. for (i = tz->bit32.leapcnt - 1; i > 0; i--) {
  397. if (ts > tz->leap_times[i].trans) {
  398. return &(tz->leap_times[i]);
  399. }
  400. }
  401. return NULL;
  402. }
  403. int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz)
  404. {
  405. ttinfo *to;
  406. timelib_sll dummy;
  407. if ((to = fetch_timezone_offset(tz, ts, &dummy))) {
  408. return to->isdst;
  409. }
  410. return -1;
  411. }
  412. timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
  413. {
  414. ttinfo *to;
  415. tlinfo *tl;
  416. int32_t offset = 0, leap_secs = 0;
  417. char *abbr;
  418. timelib_time_offset *tmp = timelib_time_offset_ctor();
  419. timelib_sll transistion_time;
  420. if ((to = fetch_timezone_offset(tz, ts, &transistion_time))) {
  421. offset = to->offset;
  422. abbr = &(tz->timezone_abbr[to->abbr_idx]);
  423. tmp->is_dst = to->isdst;
  424. tmp->transistion_time = transistion_time;
  425. } else {
  426. offset = 0;
  427. abbr = tz->timezone_abbr;
  428. tmp->is_dst = 0;
  429. tmp->transistion_time = 0;
  430. }
  431. if ((tl = fetch_leaptime_offset(tz, ts))) {
  432. leap_secs = -tl->offset;
  433. }
  434. tmp->offset = offset;
  435. tmp->leap_secs = leap_secs;
  436. tmp->abbr = abbr ? timelib_strdup(abbr) : timelib_strdup("GMT");
  437. return tmp;
  438. }
  439. timelib_sll timelib_get_current_offset(timelib_time *t)
  440. {
  441. timelib_time_offset *gmt_offset;
  442. timelib_sll retval;
  443. switch (t->zone_type) {
  444. case TIMELIB_ZONETYPE_ABBR:
  445. case TIMELIB_ZONETYPE_OFFSET:
  446. return (t->z + t->dst) * -60;
  447. case TIMELIB_ZONETYPE_ID:
  448. gmt_offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  449. retval = gmt_offset->offset;
  450. timelib_time_offset_dtor(gmt_offset);
  451. return retval;
  452. default:
  453. return 0;
  454. }
  455. }