1 Add support for use of the system timezone database, rather
2 than embedding a copy. Discussed upstream but was not desired.
5 r12: adapt for upstream changes for new zic
6 r11: use canonical names to avoid more case sensitivity issues
7 round lat/long from zone.tab towards zero per builtin db
8 r10: make timezone case insensitive
9 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
10 r8: fix compile error without --with-system-tzdata configured
11 r7: improve check for valid timezone id to exclude directories
12 r6: fix fd leak in r5, fix country code/BC flag use in
13 timezone_identifiers_list() using system db,
14 fix use of PECL timezonedb to override system db,
15 r5: reverts addition of "System/Localtime" fake tzname.
16 updated for 5.3.0, parses zone.tab to pick up mapping between
17 timezone name, country code and long/lat coords
18 r4: added "System/Localtime" tzname which uses /etc/localtime
19 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
20 r2: add filesystem trawl to set up name alias index
23 --- php-7.0.0RC1/ext/date/lib/parse_tz.c~ 2015-08-27 12:06:04.000000000 +0300
24 +++ php-7.0.0RC1/ext/date/lib/parse_tz.c 2015-08-27 12:08:08.706661229 +0300
29 +#ifdef HAVE_SYSTEM_TZDATA
30 +#include <sys/mman.h>
31 +#include <sys/stat.h>
36 +#include "php_scandir.h"
45 #define TIMELIB_SUPPORTS_V2DATA
46 +#ifndef HAVE_SYSTEM_TZDATA
47 #include "timezonedb.h"
52 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
53 # if defined(__LITTLE_ENDIAN__)
54 @@ -53,6 +68,10 @@ static int read_preamble(const unsigned
58 + if (memcmp(*tzf, "TZif", 4) == 0) {
63 version = (*tzf)[3] - '0';
65 @@ -296,7 +315,418 @@ void timelib_dump_tzinfo(timelib_tzinfo
69 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
70 +#ifdef HAVE_SYSTEM_TZDATA
72 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
73 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
75 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
78 +/* System timezone database pointer. */
79 +static const timelib_tzdb *timezonedb_system;
81 +/* Hash table entry for the cache of the zone.tab mapping table. */
82 +struct location_info {
84 + double latitude, longitude;
87 + struct location_info *next;
90 +/* Cache of zone.tab. */
91 +static struct location_info **system_location_table;
93 +/* Size of the zone.tab hash table; a random-ish prime big enough to
94 + * prevent too many collisions. */
95 +#define LOCINFO_HASH_SIZE (1021)
97 +/* Compute a case insensitive hash of str */
98 +static uint32_t tz_hash(const char *str)
100 + const unsigned char *p = (const unsigned char *)str;
101 + uint32_t hash = 5381;
104 + while ((c = tolower(*p++)) != '\0') {
105 + hash = (hash << 5) ^ hash ^ c;
108 + return hash % LOCINFO_HASH_SIZE;
111 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
112 + * parsed string on success, or NULL on parse error. On success,
113 + * writes the parsed number to *result. */
114 +static char *parse_iso6709(char *p, double *result)
122 + else if (*p == '-')
128 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
131 + /* Annoying encoding used by zone.tab has no decimal point, so use
132 + * the length to determine the format:
140 + if (len < 4 || len > 7) {
145 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
147 + if (len == 5 || len == 7)
148 + v = v * 10.0 + (*p++ - '0');
150 + v += (10.0 * (p[0] - '0')
151 + + p[1] - '0') / 60.0;
155 + v += (10.0 * (p[0] - '0')
156 + + p[1] - '0') / 3600.0;
160 + /* Round to five decimal place, not because it's a good idea,
161 + * but, because the builtin data uses rounded data, so, match
163 + *result = trunc(v * sign * 100000.0) / 100000.0;
168 +/* This function parses the zone.tab file to build up the mapping of
169 + * timezone to country code and geographic location, and returns a
170 + * hash table. The hash table is indexed by the function:
172 + * tz_hash(timezone-name)
174 +static struct location_info **create_location_table(void)
176 + struct location_info **li, *i;
177 + char zone_tab[PATH_MAX];
181 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
183 + fp = fopen(zone_tab, "r");
188 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
190 + while (fgets(line, sizeof line, fp)) {
191 + char *p = line, *code, *name, *comment;
193 + double latitude, longitude;
195 + while (isspace(*p))
198 + if (*p == '#' || *p == '\0' || *p == '\n')
201 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
209 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
210 + p = parse_iso6709(p, &latitude);
214 + p = parse_iso6709(p, &longitude);
219 + if (!p || *p != '\t') {
223 + /* name = string */
225 + while (*p != '\t' && *p && *p != '\n')
230 + /* comment = string */
232 + while (*p != '\t' && *p && *p != '\n')
235 + if (*p == '\n' || *p == '\t')
238 + hash = tz_hash(name);
239 + i = malloc(sizeof *i);
240 + memcpy(i->code, code, 2);
241 + strncpy(i->name, name, sizeof i->name);
242 + i->comment = strdup(comment);
243 + i->longitude = longitude;
244 + i->latitude = latitude;
245 + i->next = li[hash];
247 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
255 +/* Return location info from hash table, using given timezone name.
256 + * Returns NULL if the name could not be found. */
257 +const struct location_info *find_zone_info(struct location_info **li,
260 + uint32_t hash = tz_hash(name);
261 + const struct location_info *l;
267 + for (l = li[hash]; l; l = l->next) {
268 + if (strcasecmp(l->name, name) == 0)
275 +/* Filter out some non-tzdata files and the posix/right databases, if
277 +static int index_filter(const struct dirent *ent)
279 + return strcmp(ent->d_name, ".") != 0
280 + && strcmp(ent->d_name, "..") != 0
281 + && strcmp(ent->d_name, "posix") != 0
282 + && strcmp(ent->d_name, "posixrules") != 0
283 + && strcmp(ent->d_name, "right") != 0
284 + && strstr(ent->d_name, ".tab") == NULL;
287 +static int sysdbcmp(const void *first, const void *second)
289 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
291 + return strcasecmp(alpha->id, beta->id);
295 +/* Create the zone identifier index by trawling the filesystem. */
296 +static void create_zone_index(timelib_tzdb *db)
298 + size_t dirstack_size, dirstack_top;
299 + size_t index_size, index_next;
300 + timelib_tzdb_index_entry *db_index;
303 + /* LIFO stack to hold directory entries to scan; each slot is a
304 + * directory name relative to the zoneinfo prefix. */
305 + dirstack_size = 32;
306 + dirstack = malloc(dirstack_size * sizeof *dirstack);
308 + dirstack[0] = strdup("");
312 + db_index = malloc(index_size * sizeof *db_index);
316 + struct dirent **ents;
317 + char name[PATH_MAX], *top;
320 + /* Pop the top stack entry, and iterate through its contents. */
321 + top = dirstack[--dirstack_top];
322 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
324 + count = php_scandir(name, &ents, index_filter, php_alphasort);
326 + while (count > 0) {
328 + const char *leaf = ents[count - 1]->d_name;
330 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
333 + if (strlen(name) && stat(name, &st) == 0) {
334 + /* Name, relative to the zoneinfo prefix. */
335 + const char *root = top;
337 + if (root[0] == '/') root++;
339 + snprintf(name, sizeof name, "%s%s%s", root,
340 + *root ? "/": "", leaf);
342 + if (S_ISDIR(st.st_mode)) {
343 + if (dirstack_top == dirstack_size) {
344 + dirstack_size *= 2;
345 + dirstack = realloc(dirstack,
346 + dirstack_size * sizeof *dirstack);
348 + dirstack[dirstack_top++] = strdup(name);
351 + if (index_next == index_size) {
353 + db_index = realloc(db_index,
354 + index_size * sizeof *db_index);
357 + db_index[index_next++].id = strdup(name);
361 + free(ents[--count]);
364 + if (count != -1) free(ents);
366 + } while (dirstack_top);
368 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
370 + db->index = db_index;
371 + db->index_size = index_next;
376 +#define FAKE_HEADER "1234\0??\1??"
377 +#define FAKE_UTC_POS (7 - 4)
379 +/* Create a fake data segment for database 'sysdb'. */
380 +static void fake_data_segment(timelib_tzdb *sysdb,
381 + struct location_info **info)
386 + data = malloc(3 * sysdb->index_size + 7);
388 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
390 + for (n = 0; n < sysdb->index_size; n++) {
391 + const struct location_info *li;
392 + timelib_tzdb_index_entry *ent;
394 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
396 + /* Lookup the timezone name in the hash table. */
397 + if (strcmp(ent->id, "UTC") == 0) {
398 + ent->pos = FAKE_UTC_POS;
402 + li = find_zone_info(info, ent->id);
404 + /* If found, append the BC byte and the
405 + * country code; set the position for this
406 + * section of timezone data. */
407 + ent->pos = (p - data) - 4;
409 + *p++ = li->code[0];
410 + *p++ = li->code[1];
413 + /* If not found, the timezone data can
414 + * point at the header. */
419 + sysdb->data = (unsigned char *)data;
422 +/* Returns true if the passed-in stat structure describes a
423 + * probably-valid timezone file. */
424 +static int is_valid_tzfile(const struct stat *st)
426 + return S_ISREG(st->st_mode) && st->st_size > 20;
429 +/* To allow timezone names to be used case-insensitively, find the
430 + * canonical name for this timezone, if possible. */
431 +static const char *canonical_tzname(const char *timezone)
433 + if (timezonedb_system) {
434 + timelib_tzdb_index_entry *ent, lookup;
436 + lookup.id = (char *)timezone;
438 + ent = bsearch(&lookup, timezonedb_system->index,
439 + timezonedb_system->index_size, sizeof lookup,
449 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
450 + * length of the mapped data is placed in *length. */
451 +static char *map_tzfile(const char *timezone, size_t *length)
453 + char fname[PATH_MAX];
458 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
462 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
464 + fd = open(fname, O_RDONLY);
467 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
472 + *length = st.st_size;
473 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
476 + return p != MAP_FAILED ? p : NULL;
481 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
483 int left = 0, right = tzdb->index_size - 1;
484 #ifdef HAVE_SETLOCALE
485 @@ -335,21 +765,87 @@ static int seek_to_tz_position(const uns
489 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
490 + char **map, size_t *maplen,
491 + const timelib_tzdb *tzdb)
493 +#ifdef HAVE_SYSTEM_TZDATA
494 + if (tzdb == timezonedb_system) {
497 + orig = map_tzfile(timezone, maplen);
498 + if (orig == NULL) {
502 + (*tzf) = (unsigned char *)orig ;
509 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
513 const timelib_tzdb *timelib_builtin_db(void)
515 +#ifdef HAVE_SYSTEM_TZDATA
516 + if (timezonedb_system == NULL) {
517 + timelib_tzdb *tmp = malloc(sizeof *tmp);
519 + tmp->version = "0.system";
521 + create_zone_index(tmp);
522 + system_location_table = create_location_table();
523 + fake_data_segment(tmp, system_location_table);
524 + timezonedb_system = tmp;
527 + return timezonedb_system;
529 return &timezonedb_builtin;
533 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
535 +#ifdef HAVE_SYSTEM_TZDATA
536 + *count = timezonedb_system->index_size;
537 + return timezonedb_system->index;
539 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
540 return timezonedb_idx_builtin;
544 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
546 const unsigned char *tzf;
547 - return (seek_to_tz_position(&tzf, timezone, tzdb));
549 +#ifdef HAVE_SYSTEM_TZDATA
550 + if (tzdb == timezonedb_system) {
551 + char fname[PATH_MAX];
554 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
558 + if (system_location_table) {
559 + if (find_zone_info(system_location_table, timezone) != NULL) {
560 + /* found in cache */
565 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
567 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
570 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
573 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
574 @@ -374,24 +870,54 @@ static void read_64bit_header(const unsi
575 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
577 const unsigned char *tzf;
578 + char *memmap = NULL;
583 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
584 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
585 tmp = timelib_tzinfo_ctor(timezone);
587 version = read_preamble(&tzf, tmp);
588 read_header(&tzf, tmp);
589 read_transistions(&tzf, tmp);
590 read_types(&tzf, tmp);
591 - if (version == 2) {
592 - skip_64bit_preamble(&tzf, tmp);
593 - read_64bit_header(&tzf, tmp);
594 - skip_64bit_transistions(&tzf, tmp);
595 - skip_64bit_types(&tzf, tmp);
596 - skip_posix_string(&tzf, tmp);
598 - read_location(&tzf, tmp);
600 +#ifdef HAVE_SYSTEM_TZDATA
602 + const struct location_info *li;
604 + /* TZif-style - grok the location info from the system database,
607 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
608 + tmp->location.comments = strdup(li->comment);
609 + strncpy(tmp->location.country_code, li->code, 2);
610 + tmp->location.longitude = li->longitude;
611 + tmp->location.latitude = li->latitude;
615 + strcpy(tmp->location.country_code, "??");
617 + tmp->location.comments = strdup("");
620 + /* Now done with the mmap segment - discard it. */
621 + munmap(memmap, maplen);
625 + if (version == 2) {
626 + skip_64bit_preamble(&tzf, tmp);
627 + read_64bit_header(&tzf, tmp);
628 + skip_64bit_transistions(&tzf, tmp);
629 + skip_64bit_types(&tzf, tmp);
630 + skip_posix_string(&tzf, tmp);
632 + /* PHP-style - use the embedded info. */
633 + read_location(&tzf, tmp);
638 diff -up php-5.6.9RC1/ext/date/lib/timelib.m4.systzdata php-5.6.9RC1/ext/date/lib/timelib.m4
639 --- php-5.6.9RC1/ext/date/lib/timelib.m4.systzdata 2015-04-30 00:00:18.000000000 +0200
640 +++ php-5.6.9RC1/ext/date/lib/timelib.m4 2015-04-30 06:32:08.549500385 +0200
641 @@ -78,3 +78,17 @@ stdlib.h
643 dnl Check for strtoll, atoll
644 AC_CHECK_FUNCS(strtoll atoll strftime)
646 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
647 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
650 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
651 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
653 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
654 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
655 + [Define for location of system timezone data])