1 Add support for use of the system timezone database, rather
2 than embedding a copy. Discussed upstream but was not desired.
5 r13: adapt for upstream changes to use PHP allocator
6 r12: adapt for upstream changes for new zic
7 r11: use canonical names to avoid more case sensitivity issues
8 round lat/long from zone.tab towards zero per builtin db
9 r10: make timezone case insensitive
10 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
11 r8: fix compile error without --with-system-tzdata configured
12 r7: improve check for valid timezone id to exclude directories
13 r6: fix fd leak in r5, fix country code/BC flag use in
14 timezone_identifiers_list() using system db,
15 fix use of PECL timezonedb to override system db,
16 r5: reverts addition of "System/Localtime" fake tzname.
17 updated for 5.3.0, parses zone.tab to pick up mapping between
18 timezone name, country code and long/lat coords
19 r4: added "System/Localtime" tzname which uses /etc/localtime
20 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
21 r2: add filesystem trawl to set up name alias index
24 diff -up php-7.0.0RC1/ext/date/lib/parse_tz.c.systzdata php-7.0.0RC1/ext/date/lib/parse_tz.c
25 --- php-7.0.0RC1/ext/date/lib/parse_tz.c.systzdata 2015-08-18 23:39:24.000000000 +0200
26 +++ php-7.0.0RC1/ext/date/lib/parse_tz.c 2015-08-22 07:54:38.097258458 +0200
31 +#ifdef HAVE_SYSTEM_TZDATA
32 +#include <sys/mman.h>
33 +#include <sys/stat.h>
38 +#include "php_scandir.h"
48 +#ifndef HAVE_SYSTEM_TZDATA
49 #define TIMELIB_SUPPORTS_V2DATA
50 #include "timezonedb.h"
55 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
56 # if defined(__LITTLE_ENDIAN__)
57 @@ -55,6 +69,11 @@ static int read_preamble(const unsigned
61 + if (memcmp(*tzf, "TZif", 4) == 0) {
67 version = (*tzf)[3] - '0';
69 @@ -298,7 +317,418 @@ void timelib_dump_tzinfo(timelib_tzinfo
73 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
74 +#ifdef HAVE_SYSTEM_TZDATA
76 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
77 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
79 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
82 +/* System timezone database pointer. */
83 +static const timelib_tzdb *timezonedb_system;
85 +/* Hash table entry for the cache of the zone.tab mapping table. */
86 +struct location_info {
88 + double latitude, longitude;
91 + struct location_info *next;
94 +/* Cache of zone.tab. */
95 +static struct location_info **system_location_table;
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)
101 +/* Compute a case insensitive hash of str */
102 +static uint32_t tz_hash(const char *str)
104 + const unsigned char *p = (const unsigned char *)str;
105 + uint32_t hash = 5381;
108 + while ((c = tolower(*p++)) != '\0') {
109 + hash = (hash << 5) ^ hash ^ c;
112 + return hash % LOCINFO_HASH_SIZE;
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)
126 + else if (*p == '-')
132 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
135 + /* Annoying encoding used by zone.tab has no decimal point, so use
136 + * the length to determine the format:
144 + if (len < 4 || len > 7) {
149 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
151 + if (len == 5 || len == 7)
152 + v = v * 10.0 + (*p++ - '0');
154 + v += (10.0 * (p[0] - '0')
155 + + p[1] - '0') / 60.0;
159 + v += (10.0 * (p[0] - '0')
160 + + p[1] - '0') / 3600.0;
164 + /* Round to five decimal place, not because it's a good idea,
165 + * but, because the builtin data uses rounded data, so, match
167 + *result = trunc(v * sign * 100000.0) / 100000.0;
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:
176 + * tz_hash(timezone-name)
178 +static struct location_info **create_location_table(void)
180 + struct location_info **li, *i;
181 + char zone_tab[PATH_MAX];
185 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
187 + fp = fopen(zone_tab, "r");
192 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
194 + while (fgets(line, sizeof line, fp)) {
195 + char *p = line, *code, *name, *comment;
197 + double latitude, longitude;
199 + while (isspace(*p))
202 + if (*p == '#' || *p == '\0' || *p == '\n')
205 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
213 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
214 + p = parse_iso6709(p, &latitude);
218 + p = parse_iso6709(p, &longitude);
223 + if (!p || *p != '\t') {
227 + /* name = string */
229 + while (*p != '\t' && *p && *p != '\n')
234 + /* comment = string */
236 + while (*p != '\t' && *p && *p != '\n')
239 + if (*p == '\n' || *p == '\t')
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];
251 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
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,
264 + uint32_t hash = tz_hash(name);
265 + const struct location_info *l;
271 + for (l = li[hash]; l; l = l->next) {
272 + if (strcasecmp(l->name, name) == 0)
279 +/* Filter out some non-tzdata files and the posix/right databases, if
281 +static int index_filter(const struct dirent *ent)
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, ".tab") == NULL;
291 +static int sysdbcmp(const void *first, const void *second)
293 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
295 + return strcasecmp(alpha->id, beta->id);
299 +/* Create the zone identifier index by trawling the filesystem. */
300 +static void create_zone_index(timelib_tzdb *db)
302 + size_t dirstack_size, dirstack_top;
303 + size_t index_size, index_next;
304 + timelib_tzdb_index_entry *db_index;
307 + /* LIFO stack to hold directory entries to scan; each slot is a
308 + * directory name relative to the zoneinfo prefix. */
309 + dirstack_size = 32;
310 + dirstack = malloc(dirstack_size * sizeof *dirstack);
312 + dirstack[0] = strdup("");
316 + db_index = malloc(index_size * sizeof *db_index);
320 + struct dirent **ents;
321 + char name[PATH_MAX], *top;
324 + /* Pop the top stack entry, and iterate through its contents. */
325 + top = dirstack[--dirstack_top];
326 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
328 + count = php_scandir(name, &ents, index_filter, php_alphasort);
330 + while (count > 0) {
332 + const char *leaf = ents[count - 1]->d_name;
334 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
337 + if (strlen(name) && stat(name, &st) == 0) {
338 + /* Name, relative to the zoneinfo prefix. */
339 + const char *root = top;
341 + if (root[0] == '/') root++;
343 + snprintf(name, sizeof name, "%s%s%s", root,
344 + *root ? "/": "", leaf);
346 + if (S_ISDIR(st.st_mode)) {
347 + if (dirstack_top == dirstack_size) {
348 + dirstack_size *= 2;
349 + dirstack = realloc(dirstack,
350 + dirstack_size * sizeof *dirstack);
352 + dirstack[dirstack_top++] = strdup(name);
355 + if (index_next == index_size) {
357 + db_index = realloc(db_index,
358 + index_size * sizeof *db_index);
361 + db_index[index_next++].id = strdup(name);
365 + free(ents[--count]);
368 + if (count != -1) free(ents);
370 + } while (dirstack_top);
372 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
374 + db->index = db_index;
375 + db->index_size = index_next;
380 +#define FAKE_HEADER "1234\0??\1??"
381 +#define FAKE_UTC_POS (7 - 4)
383 +/* Create a fake data segment for database 'sysdb'. */
384 +static void fake_data_segment(timelib_tzdb *sysdb,
385 + struct location_info **info)
390 + data = malloc(3 * sysdb->index_size + 7);
392 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
394 + for (n = 0; n < sysdb->index_size; n++) {
395 + const struct location_info *li;
396 + timelib_tzdb_index_entry *ent;
398 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
400 + /* Lookup the timezone name in the hash table. */
401 + if (strcmp(ent->id, "UTC") == 0) {
402 + ent->pos = FAKE_UTC_POS;
406 + li = find_zone_info(info, ent->id);
408 + /* If found, append the BC byte and the
409 + * country code; set the position for this
410 + * section of timezone data. */
411 + ent->pos = (p - data) - 4;
413 + *p++ = li->code[0];
414 + *p++ = li->code[1];
417 + /* If not found, the timezone data can
418 + * point at the header. */
423 + sysdb->data = (unsigned char *)data;
426 +/* Returns true if the passed-in stat structure describes a
427 + * probably-valid timezone file. */
428 +static int is_valid_tzfile(const struct stat *st)
430 + return S_ISREG(st->st_mode) && st->st_size > 20;
433 +/* To allow timezone names to be used case-insensitively, find the
434 + * canonical name for this timezone, if possible. */
435 +static const char *canonical_tzname(const char *timezone)
437 + if (timezonedb_system) {
438 + timelib_tzdb_index_entry *ent, lookup;
440 + lookup.id = (char *)timezone;
442 + ent = bsearch(&lookup, timezonedb_system->index,
443 + timezonedb_system->index_size, sizeof lookup,
453 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
454 + * length of the mapped data is placed in *length. */
455 +static char *map_tzfile(const char *timezone, size_t *length)
457 + char fname[PATH_MAX];
462 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
466 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
468 + fd = open(fname, O_RDONLY);
471 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
476 + *length = st.st_size;
477 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
480 + return p != MAP_FAILED ? p : NULL;
485 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
487 int left = 0, right = tzdb->index_size - 1;
488 #ifdef HAVE_SETLOCALE
489 @@ -337,21 +767,88 @@ static int seek_to_tz_position(const uns
493 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
494 + char **map, size_t *maplen,
495 + const timelib_tzdb *tzdb)
497 +#ifdef HAVE_SYSTEM_TZDATA
498 + if (tzdb == timezonedb_system) {
501 + orig = map_tzfile(timezone, maplen);
502 + if (orig == NULL) {
506 + (*tzf) = (unsigned char *)orig;
513 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
517 const timelib_tzdb *timelib_builtin_db(void)
519 +#ifdef HAVE_SYSTEM_TZDATA
520 + if (timezonedb_system == NULL) {
521 + timelib_tzdb *tmp = malloc(sizeof *tmp);
523 + tmp->version = "0.system";
525 + create_zone_index(tmp);
526 + system_location_table = create_location_table();
527 + fake_data_segment(tmp, system_location_table);
528 + timezonedb_system = tmp;
531 + return timezonedb_system;
533 return &timezonedb_builtin;
537 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
539 +#ifdef HAVE_SYSTEM_TZDATA
540 + *count = timezonedb_system->index_size;
541 + return timezonedb_system->index;
543 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
544 return timezonedb_idx_builtin;
548 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
550 const unsigned char *tzf;
551 - return (seek_to_tz_position(&tzf, timezone, tzdb));
553 +#ifdef HAVE_SYSTEM_TZDATA
554 + if (tzdb == timezonedb_system) {
555 + char fname[PATH_MAX];
558 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
562 + if (system_location_table) {
563 + if (find_zone_info(system_location_table, timezone) != NULL) {
564 + /* found in cache */
569 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
571 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
575 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
578 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
579 @@ -376,24 +873,54 @@ static void read_64bit_header(const unsi
580 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
582 const unsigned char *tzf;
583 + char *memmap = NULL;
588 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
589 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
590 tmp = timelib_tzinfo_ctor(timezone);
592 version = read_preamble(&tzf, tmp);
593 read_header(&tzf, tmp);
594 read_transistions(&tzf, tmp);
595 read_types(&tzf, tmp);
596 - if (version == 2) {
597 - skip_64bit_preamble(&tzf, tmp);
598 - read_64bit_header(&tzf, tmp);
599 - skip_64bit_transistions(&tzf, tmp);
600 - skip_64bit_types(&tzf, tmp);
601 - skip_posix_string(&tzf, tmp);
603 - read_location(&tzf, tmp);
605 +#ifdef HAVE_SYSTEM_TZDATA
607 + const struct location_info *li;
609 + /* TZif-style - grok the location info from the system database,
612 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
613 + tmp->location.comments = timelib_strdup(li->comment);
614 + strncpy(tmp->location.country_code, li->code, 2);
615 + tmp->location.longitude = li->longitude;
616 + tmp->location.latitude = li->latitude;
620 + strcpy(tmp->location.country_code, "??");
622 + tmp->location.comments = timelib_strdup("");
625 + /* Now done with the mmap segment - discard it. */
626 + munmap(memmap, maplen);
630 + /* PHP-style - use the embedded info. */
631 + if (version == 2) {
632 + skip_64bit_preamble(&tzf, tmp);
633 + read_64bit_header(&tzf, tmp);
634 + skip_64bit_transistions(&tzf, tmp);
635 + skip_64bit_types(&tzf, tmp);
636 + skip_posix_string(&tzf, tmp);
638 + read_location(&tzf, tmp);
643 diff -up php-7.0.0RC1/ext/date/lib/timelib.m4.systzdata php-7.0.0RC1/ext/date/lib/timelib.m4
644 --- php-7.0.0RC1/ext/date/lib/timelib.m4.systzdata 2015-08-18 23:39:24.000000000 +0200
645 +++ php-7.0.0RC1/ext/date/lib/timelib.m4 2015-08-22 07:47:34.854055364 +0200
646 @@ -78,3 +78,17 @@ stdlib.h
648 dnl Check for strtoll, atoll
649 AC_CHECK_FUNCS(strtoll atoll strftime)
651 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
652 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
655 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
656 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
658 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
659 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
660 + [Define for location of system timezone data])