1
0

0013-Add-support-for-use-of-the-system-timezone-database.patch 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. From: Joe Orton <jorton@redhat.com>
  2. Date: Thu, 20 Oct 2016 11:44:14 +0200
  3. Subject: Add support for use of the system timezone database
  4. Add support for use of the system timezone database, rather
  5. than embedding a copy. Discussed upstream but was not desired.
  6. History:
  7. r14: improve check for valid tz file
  8. r13: adapt for upstream changes to use PHP allocator
  9. r12: adapt for upstream changes for new zic
  10. r11: use canonical names to avoid more case sensitivity issues
  11. round lat/long from zone.tab towards zero per builtin db
  12. r10: make timezone case insensitive
  13. r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
  14. r8: fix compile error without --with-system-tzdata configured
  15. r7: improve check for valid timezone id to exclude directories
  16. r6: fix fd leak in r5, fix country code/BC flag use in
  17. timezone_identifiers_list() using system db,
  18. fix use of PECL timezonedb to override system db,
  19. r5: reverts addition of "System/Localtime" fake tzname.
  20. updated for 5.3.0, parses zone.tab to pick up mapping between
  21. timezone name, country code and long/lat coords
  22. r4: added "System/Localtime" tzname which uses /etc/localtime
  23. r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
  24. r2: add filesystem trawl to set up name alias index
  25. r1: initial revision
  26. ---
  27. ext/date/lib/parse_tz.c | 560 +++++++++++++++++++++++++++++++++++++++++++++++-
  28. ext/date/lib/timelib.m4 | 13 ++
  29. 2 files changed, 562 insertions(+), 11 deletions(-)
  30. diff --git a/ext/date/lib/parse_tz.c b/ext/date/lib/parse_tz.c
  31. index 20d7eea..ed7717e 100644
  32. --- a/ext/date/lib/parse_tz.c
  33. +++ b/ext/date/lib/parse_tz.c
  34. @@ -24,6 +24,16 @@
  35. #include "timelib.h"
  36. +#ifdef HAVE_SYSTEM_TZDATA
  37. +#include <sys/mman.h>
  38. +#include <sys/stat.h>
  39. +#include <limits.h>
  40. +#include <fcntl.h>
  41. +#include <unistd.h>
  42. +
  43. +#include "php_scandir.h"
  44. +#endif
  45. +
  46. #include <stdio.h>
  47. #ifdef HAVE_LOCALE_H
  48. @@ -36,8 +46,12 @@
  49. #include <strings.h>
  50. #endif
  51. +#ifndef HAVE_SYSTEM_TZDATA
  52. #define TIMELIB_SUPPORTS_V2DATA
  53. #include "timezonedb.h"
  54. +#endif
  55. +
  56. +#include <ctype.h>
  57. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  58. # if defined(__LITTLE_ENDIAN__)
  59. @@ -59,6 +73,11 @@ static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  60. {
  61. uint32_t version;
  62. + if (memcmp(*tzf, "TZif", 4) == 0) {
  63. + *tzf += 20;
  64. + return 0;
  65. + }
  66. +
  67. /* read ID */
  68. version = (*tzf)[3] - '0';
  69. *tzf += 4;
  70. @@ -302,7 +321,429 @@ void timelib_dump_tzinfo(timelib_tzinfo *tz)
  71. }
  72. }
  73. -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
  74. +#ifdef HAVE_SYSTEM_TZDATA
  75. +
  76. +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
  77. +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
  78. +#else
  79. +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
  80. +#endif
  81. +
  82. +/* System timezone database pointer. */
  83. +static const timelib_tzdb *timezonedb_system;
  84. +
  85. +/* Hash table entry for the cache of the zone.tab mapping table. */
  86. +struct location_info {
  87. + char code[2];
  88. + double latitude, longitude;
  89. + char name[64];
  90. + char *comment;
  91. + struct location_info *next;
  92. +};
  93. +
  94. +/* Cache of zone.tab. */
  95. +static struct location_info **system_location_table;
  96. +
  97. +/* Size of the zone.tab hash table; a random-ish prime big enough to
  98. + * prevent too many collisions. */
  99. +#define LOCINFO_HASH_SIZE (1021)
  100. +
  101. +/* Compute a case insensitive hash of str */
  102. +static uint32_t tz_hash(const char *str)
  103. +{
  104. + const unsigned char *p = (const unsigned char *)str;
  105. + uint32_t hash = 5381;
  106. + int c;
  107. +
  108. + while ((c = tolower(*p++)) != '\0') {
  109. + hash = (hash << 5) ^ hash ^ c;
  110. + }
  111. +
  112. + return hash % LOCINFO_HASH_SIZE;
  113. +}
  114. +
  115. +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
  116. + * parsed string on success, or NULL on parse error. On success,
  117. + * writes the parsed number to *result. */
  118. +static char *parse_iso6709(char *p, double *result)
  119. +{
  120. + double v, sign;
  121. + char *pend;
  122. + size_t len;
  123. +
  124. + if (*p == '+')
  125. + sign = 1.0;
  126. + else if (*p == '-')
  127. + sign = -1.0;
  128. + else
  129. + return NULL;
  130. +
  131. + p++;
  132. + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
  133. + ;;
  134. +
  135. + /* Annoying encoding used by zone.tab has no decimal point, so use
  136. + * the length to determine the format:
  137. + *
  138. + * 4 = DDMM
  139. + * 5 = DDDMM
  140. + * 6 = DDMMSS
  141. + * 7 = DDDMMSS
  142. + */
  143. + len = pend - p;
  144. + if (len < 4 || len > 7) {
  145. + return NULL;
  146. + }
  147. +
  148. + /* p => [D]DD */
  149. + v = (p[0] - '0') * 10.0 + (p[1] - '0');
  150. + p += 2;
  151. + if (len == 5 || len == 7)
  152. + v = v * 10.0 + (*p++ - '0');
  153. + /* p => MM[SS] */
  154. + v += (10.0 * (p[0] - '0')
  155. + + p[1] - '0') / 60.0;
  156. + p += 2;
  157. + /* p => [SS] */
  158. + if (len > 5) {
  159. + v += (10.0 * (p[0] - '0')
  160. + + p[1] - '0') / 3600.0;
  161. + p += 2;
  162. + }
  163. +
  164. + /* Round to five decimal place, not because it's a good idea,
  165. + * but, because the builtin data uses rounded data, so, match
  166. + * that. */
  167. + *result = trunc(v * sign * 100000.0) / 100000.0;
  168. +
  169. + return p;
  170. +}
  171. +
  172. +/* This function parses the zone.tab file to build up the mapping of
  173. + * timezone to country code and geographic location, and returns a
  174. + * hash table. The hash table is indexed by the function:
  175. + *
  176. + * tz_hash(timezone-name)
  177. + */
  178. +static struct location_info **create_location_table(void)
  179. +{
  180. + struct location_info **li, *i;
  181. + char zone_tab[PATH_MAX];
  182. + char line[512];
  183. + FILE *fp;
  184. +
  185. + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
  186. +
  187. + fp = fopen(zone_tab, "r");
  188. + if (!fp) {
  189. + return NULL;
  190. + }
  191. +
  192. + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
  193. +
  194. + while (fgets(line, sizeof line, fp)) {
  195. + char *p = line, *code, *name, *comment;
  196. + uint32_t hash;
  197. + double latitude, longitude;
  198. +
  199. + while (isspace(*p))
  200. + p++;
  201. +
  202. + if (*p == '#' || *p == '\0' || *p == '\n')
  203. + continue;
  204. +
  205. + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
  206. + continue;
  207. +
  208. + /* code => AA */
  209. + code = p;
  210. + p[2] = 0;
  211. + p += 3;
  212. +
  213. + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
  214. + p = parse_iso6709(p, &latitude);
  215. + if (!p) {
  216. + continue;
  217. + }
  218. + p = parse_iso6709(p, &longitude);
  219. + if (!p) {
  220. + continue;
  221. + }
  222. +
  223. + if (!p || *p != '\t') {
  224. + continue;
  225. + }
  226. +
  227. + /* name = string */
  228. + name = ++p;
  229. + while (*p != '\t' && *p && *p != '\n')
  230. + p++;
  231. +
  232. + *p++ = '\0';
  233. +
  234. + /* comment = string */
  235. + comment = p;
  236. + while (*p != '\t' && *p && *p != '\n')
  237. + p++;
  238. +
  239. + if (*p == '\n' || *p == '\t')
  240. + *p = '\0';
  241. +
  242. + hash = tz_hash(name);
  243. + i = malloc(sizeof *i);
  244. + memcpy(i->code, code, 2);
  245. + strncpy(i->name, name, sizeof i->name);
  246. + i->comment = strdup(comment);
  247. + i->longitude = longitude;
  248. + i->latitude = latitude;
  249. + i->next = li[hash];
  250. + li[hash] = i;
  251. + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
  252. + }
  253. +
  254. + fclose(fp);
  255. +
  256. + return li;
  257. +}
  258. +
  259. +/* Return location info from hash table, using given timezone name.
  260. + * Returns NULL if the name could not be found. */
  261. +const struct location_info *find_zone_info(struct location_info **li,
  262. + const char *name)
  263. +{
  264. + uint32_t hash = tz_hash(name);
  265. + const struct location_info *l;
  266. +
  267. + if (!li) {
  268. + return NULL;
  269. + }
  270. +
  271. + for (l = li[hash]; l; l = l->next) {
  272. + if (strcasecmp(l->name, name) == 0)
  273. + return l;
  274. + }
  275. +
  276. + return NULL;
  277. +}
  278. +
  279. +/* Filter out some non-tzdata files and the posix/right databases, if
  280. + * present. */
  281. +static int index_filter(const struct dirent *ent)
  282. +{
  283. + return strcmp(ent->d_name, ".") != 0
  284. + && strcmp(ent->d_name, "..") != 0
  285. + && strcmp(ent->d_name, "posix") != 0
  286. + && strcmp(ent->d_name, "posixrules") != 0
  287. + && strcmp(ent->d_name, "right") != 0
  288. + && strstr(ent->d_name, ".list") == NULL
  289. + && strstr(ent->d_name, ".tab") == NULL;
  290. +}
  291. +
  292. +static int sysdbcmp(const void *first, const void *second)
  293. +{
  294. + const timelib_tzdb_index_entry *alpha = first, *beta = second;
  295. +
  296. + return strcasecmp(alpha->id, beta->id);
  297. +}
  298. +
  299. +
  300. +/* Create the zone identifier index by trawling the filesystem. */
  301. +static void create_zone_index(timelib_tzdb *db)
  302. +{
  303. + size_t dirstack_size, dirstack_top;
  304. + size_t index_size, index_next;
  305. + timelib_tzdb_index_entry *db_index;
  306. + char **dirstack;
  307. +
  308. + /* LIFO stack to hold directory entries to scan; each slot is a
  309. + * directory name relative to the zoneinfo prefix. */
  310. + dirstack_size = 32;
  311. + dirstack = malloc(dirstack_size * sizeof *dirstack);
  312. + dirstack_top = 1;
  313. + dirstack[0] = strdup("");
  314. +
  315. + /* Index array. */
  316. + index_size = 64;
  317. + db_index = malloc(index_size * sizeof *db_index);
  318. + index_next = 0;
  319. +
  320. + do {
  321. + struct dirent **ents;
  322. + char name[PATH_MAX], *top;
  323. + int count;
  324. +
  325. + /* Pop the top stack entry, and iterate through its contents. */
  326. + top = dirstack[--dirstack_top];
  327. + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
  328. +
  329. + count = php_scandir(name, &ents, index_filter, php_alphasort);
  330. +
  331. + while (count > 0) {
  332. + struct stat st;
  333. + const char *leaf = ents[count - 1]->d_name;
  334. +
  335. + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
  336. + top, leaf);
  337. +
  338. + if (strlen(name) && stat(name, &st) == 0) {
  339. + /* Name, relative to the zoneinfo prefix. */
  340. + const char *root = top;
  341. +
  342. + if (root[0] == '/') root++;
  343. +
  344. + snprintf(name, sizeof name, "%s%s%s", root,
  345. + *root ? "/": "", leaf);
  346. +
  347. + if (S_ISDIR(st.st_mode)) {
  348. + if (dirstack_top == dirstack_size) {
  349. + dirstack_size *= 2;
  350. + dirstack = realloc(dirstack,
  351. + dirstack_size * sizeof *dirstack);
  352. + }
  353. + dirstack[dirstack_top++] = strdup(name);
  354. + }
  355. + else {
  356. + if (index_next == index_size) {
  357. + index_size *= 2;
  358. + db_index = realloc(db_index,
  359. + index_size * sizeof *db_index);
  360. + }
  361. +
  362. + db_index[index_next++].id = strdup(name);
  363. + }
  364. + }
  365. +
  366. + free(ents[--count]);
  367. + }
  368. +
  369. + if (count != -1) free(ents);
  370. + free(top);
  371. + } while (dirstack_top);
  372. +
  373. + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
  374. +
  375. + db->index = db_index;
  376. + db->index_size = index_next;
  377. +
  378. + free(dirstack);
  379. +}
  380. +
  381. +#define FAKE_HEADER "1234\0??\1??"
  382. +#define FAKE_UTC_POS (7 - 4)
  383. +
  384. +/* Create a fake data segment for database 'sysdb'. */
  385. +static void fake_data_segment(timelib_tzdb *sysdb,
  386. + struct location_info **info)
  387. +{
  388. + size_t n;
  389. + char *data, *p;
  390. +
  391. + data = malloc(3 * sysdb->index_size + 7);
  392. +
  393. + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
  394. +
  395. + for (n = 0; n < sysdb->index_size; n++) {
  396. + const struct location_info *li;
  397. + timelib_tzdb_index_entry *ent;
  398. +
  399. + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
  400. +
  401. + /* Lookup the timezone name in the hash table. */
  402. + if (strcmp(ent->id, "UTC") == 0) {
  403. + ent->pos = FAKE_UTC_POS;
  404. + continue;
  405. + }
  406. +
  407. + li = find_zone_info(info, ent->id);
  408. + if (li) {
  409. + /* If found, append the BC byte and the
  410. + * country code; set the position for this
  411. + * section of timezone data. */
  412. + ent->pos = (p - data) - 4;
  413. + *p++ = '\1';
  414. + *p++ = li->code[0];
  415. + *p++ = li->code[1];
  416. + }
  417. + else {
  418. + /* If not found, the timezone data can
  419. + * point at the header. */
  420. + ent->pos = 0;
  421. + }
  422. + }
  423. +
  424. + sysdb->data = (unsigned char *)data;
  425. +}
  426. +
  427. +/* Returns true if the passed-in stat structure describes a
  428. + * probably-valid timezone file. */
  429. +static int is_valid_tzfile(const struct stat *st, int fd)
  430. +{
  431. + if (fd) {
  432. + char buf[20];
  433. + if (read(fd, buf, 20)!=20) {
  434. + return 0;
  435. + }
  436. + lseek(fd, SEEK_SET, 0);
  437. + if (memcmp(buf, "TZif", 4)) {
  438. + return 0;
  439. + }
  440. + }
  441. + return S_ISREG(st->st_mode) && st->st_size > 20;
  442. +}
  443. +
  444. +/* To allow timezone names to be used case-insensitively, find the
  445. + * canonical name for this timezone, if possible. */
  446. +static const char *canonical_tzname(const char *timezone)
  447. +{
  448. + if (timezonedb_system) {
  449. + timelib_tzdb_index_entry *ent, lookup;
  450. +
  451. + lookup.id = (char *)timezone;
  452. +
  453. + ent = bsearch(&lookup, timezonedb_system->index,
  454. + timezonedb_system->index_size, sizeof lookup,
  455. + sysdbcmp);
  456. + if (ent) {
  457. + return ent->id;
  458. + }
  459. + }
  460. +
  461. + return timezone;
  462. +}
  463. +
  464. +/* Return the mmap()ed tzfile if found, else NULL. On success, the
  465. + * length of the mapped data is placed in *length. */
  466. +static char *map_tzfile(const char *timezone, size_t *length)
  467. +{
  468. + char fname[PATH_MAX];
  469. + struct stat st;
  470. + char *p;
  471. + int fd;
  472. +
  473. + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
  474. + return NULL;
  475. + }
  476. +
  477. + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
  478. +
  479. + fd = open(fname, O_RDONLY);
  480. + if (fd == -1) {
  481. + return NULL;
  482. + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
  483. + close(fd);
  484. + return NULL;
  485. + }
  486. +
  487. + *length = st.st_size;
  488. + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  489. + close(fd);
  490. +
  491. + return p != MAP_FAILED ? p : NULL;
  492. +}
  493. +
  494. +#endif
  495. +
  496. +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
  497. {
  498. int left = 0, right = tzdb->index_size - 1;
  499. #ifdef HAVE_SETLOCALE
  500. @@ -341,21 +782,88 @@ static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const
  501. return 0;
  502. }
  503. +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
  504. + char **map, size_t *maplen,
  505. + const timelib_tzdb *tzdb)
  506. +{
  507. +#ifdef HAVE_SYSTEM_TZDATA
  508. + if (tzdb == timezonedb_system) {
  509. + char *orig;
  510. +
  511. + orig = map_tzfile(timezone, maplen);
  512. + if (orig == NULL) {
  513. + return 0;
  514. + }
  515. +
  516. + (*tzf) = (unsigned char *)orig;
  517. + *map = orig;
  518. + return 1;
  519. + }
  520. + else
  521. +#endif
  522. + {
  523. + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
  524. + }
  525. +}
  526. +
  527. const timelib_tzdb *timelib_builtin_db(void)
  528. {
  529. +#ifdef HAVE_SYSTEM_TZDATA
  530. + if (timezonedb_system == NULL) {
  531. + timelib_tzdb *tmp = malloc(sizeof *tmp);
  532. +
  533. + tmp->version = "0.system";
  534. + tmp->data = NULL;
  535. + create_zone_index(tmp);
  536. + system_location_table = create_location_table();
  537. + fake_data_segment(tmp, system_location_table);
  538. + timezonedb_system = tmp;
  539. + }
  540. +
  541. + return timezonedb_system;
  542. +#else
  543. return &timezonedb_builtin;
  544. +#endif
  545. }
  546. const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
  547. {
  548. +#ifdef HAVE_SYSTEM_TZDATA
  549. + *count = timezonedb_system->index_size;
  550. + return timezonedb_system->index;
  551. +#else
  552. *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
  553. return timezonedb_idx_builtin;
  554. +#endif
  555. }
  556. int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
  557. {
  558. const unsigned char *tzf;
  559. - return (seek_to_tz_position(&tzf, timezone, tzdb));
  560. +
  561. +#ifdef HAVE_SYSTEM_TZDATA
  562. + if (tzdb == timezonedb_system) {
  563. + char fname[PATH_MAX];
  564. + struct stat st;
  565. +
  566. + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
  567. + return 0;
  568. + }
  569. +
  570. + if (system_location_table) {
  571. + if (find_zone_info(system_location_table, timezone) != NULL) {
  572. + /* found in cache */
  573. + return 1;
  574. + }
  575. + }
  576. +
  577. + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
  578. +
  579. + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
  580. + }
  581. +#endif
  582. +
  583. + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
  584. }
  585. static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  586. @@ -380,24 +888,54 @@ static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
  587. timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
  588. {
  589. const unsigned char *tzf;
  590. + char *memmap = NULL;
  591. + size_t maplen;
  592. timelib_tzinfo *tmp;
  593. int version;
  594. - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
  595. + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
  596. tmp = timelib_tzinfo_ctor(timezone);
  597. version = read_preamble(&tzf, tmp);
  598. read_header(&tzf, tmp);
  599. read_transistions(&tzf, tmp);
  600. read_types(&tzf, tmp);
  601. - if (version == 2) {
  602. - skip_64bit_preamble(&tzf, tmp);
  603. - read_64bit_header(&tzf, tmp);
  604. - skip_64bit_transistions(&tzf, tmp);
  605. - skip_64bit_types(&tzf, tmp);
  606. - skip_posix_string(&tzf, tmp);
  607. - }
  608. - read_location(&tzf, tmp);
  609. +
  610. +#ifdef HAVE_SYSTEM_TZDATA
  611. + if (memmap) {
  612. + const struct location_info *li;
  613. +
  614. + /* TZif-style - grok the location info from the system database,
  615. + * if possible. */
  616. +
  617. + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
  618. + tmp->location.comments = timelib_strdup(li->comment);
  619. + strncpy(tmp->location.country_code, li->code, 2);
  620. + tmp->location.longitude = li->longitude;
  621. + tmp->location.latitude = li->latitude;
  622. + tmp->bc = 1;
  623. + }
  624. + else {
  625. + strcpy(tmp->location.country_code, "??");
  626. + tmp->bc = 0;
  627. + tmp->location.comments = timelib_strdup("");
  628. + }
  629. +
  630. + /* Now done with the mmap segment - discard it. */
  631. + munmap(memmap, maplen);
  632. + } else
  633. +#endif
  634. + {
  635. + /* PHP-style - use the embedded info. */
  636. + if (version == 2) {
  637. + skip_64bit_preamble(&tzf, tmp);
  638. + read_64bit_header(&tzf, tmp);
  639. + skip_64bit_transistions(&tzf, tmp);
  640. + skip_64bit_types(&tzf, tmp);
  641. + skip_posix_string(&tzf, tmp);
  642. + }
  643. + read_location(&tzf, tmp);
  644. + }
  645. } else {
  646. tmp = NULL;
  647. }
  648. diff --git a/ext/date/lib/timelib.m4 b/ext/date/lib/timelib.m4
  649. index 99bf9fa..4bf7e46 100644
  650. --- a/ext/date/lib/timelib.m4
  651. +++ b/ext/date/lib/timelib.m4
  652. @@ -78,3 +78,16 @@ stdlib.h
  653. dnl Check for strtoll, atoll
  654. AC_CHECK_FUNCS(strtoll atoll strftime gettimeofday)
  655. +
  656. +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
  657. +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
  658. +no, no)
  659. +
  660. +if test "$PHP_SYSTEM_TZDATA" != "no"; then
  661. + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
  662. +
  663. + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
  664. + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
  665. + [Define for location of system timezone data])
  666. + fi
  667. +fi