1 Add support for use of the system timezone database, rather
2 than embedding a copy. Discussed upstream but was not desired.
4 (Few) upstream reports:
5 https://bugs.php.net/bug.php?id=53320
6 https://bugs.php.net/bug.php?id=54250
9 r13: adapt for upstream changes to use PHP allocator
10 r12: adapt for upstream changes for new zic
11 r11: use canonical names to avoid more case sensitivity issues
12 round lat/long from zone.tab towards zero per builtin db
13 r10: make timezone case insensitive
14 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
15 r8: fix compile error without --with-system-tzdata configured
16 r7: improve check for valid timezone id to exclude directories
17 r6: fix fd leak in r5, fix country code/BC flag use in
18 timezone_identifiers_list() using system db,
19 fix use of PECL timezonedb to override system db,
20 r5: reverts addition of "System/Localtime" fake tzname.
21 updated for 5.3.0, parses zone.tab to pick up mapping between
22 timezone name, country code and long/lat coords
23 r4: added "System/Localtime" tzname which uses /etc/localtime
24 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
25 r2: add filesystem trawl to set up name alias index
28 diff -up php-7.0.0RC1/ext/date/lib/parse_tz.c.systzdata php-7.0.0RC1/ext/date/lib/parse_tz.c
29 --- php-7.0.0RC1/ext/date/lib/parse_tz.c.systzdata 2015-08-18 23:39:24.000000000 +0200
30 +++ php-7.0.0RC1/ext/date/lib/parse_tz.c 2015-08-22 07:54:38.097258458 +0200
35 +#ifdef HAVE_SYSTEM_TZDATA
36 +#include <sys/mman.h>
37 +#include <sys/stat.h>
42 +#include "php_scandir.h"
52 +#ifndef HAVE_SYSTEM_TZDATA
53 #define TIMELIB_SUPPORTS_V2DATA
54 #include "timezonedb.h"
59 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
60 # if defined(__LITTLE_ENDIAN__)
61 @@ -55,6 +69,11 @@ static int read_preamble(const unsigned
65 + if (memcmp(*tzf, "TZif", 4) == 0) {
71 version = (*tzf)[3] - '0';
73 @@ -298,7 +317,418 @@ void timelib_dump_tzinfo(timelib_tzinfo
77 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
78 +#ifdef HAVE_SYSTEM_TZDATA
80 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
81 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
83 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
86 +/* System timezone database pointer. */
87 +static const timelib_tzdb *timezonedb_system;
89 +/* Hash table entry for the cache of the zone.tab mapping table. */
90 +struct location_info {
92 + double latitude, longitude;
95 + struct location_info *next;
98 +/* Cache of zone.tab. */
99 +static struct location_info **system_location_table;
101 +/* Size of the zone.tab hash table; a random-ish prime big enough to
102 + * prevent too many collisions. */
103 +#define LOCINFO_HASH_SIZE (1021)
105 +/* Compute a case insensitive hash of str */
106 +static uint32_t tz_hash(const char *str)
108 + const unsigned char *p = (const unsigned char *)str;
109 + uint32_t hash = 5381;
112 + while ((c = tolower(*p++)) != '\0') {
113 + hash = (hash << 5) ^ hash ^ c;
116 + return hash % LOCINFO_HASH_SIZE;
119 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
120 + * parsed string on success, or NULL on parse error. On success,
121 + * writes the parsed number to *result. */
122 +static char *parse_iso6709(char *p, double *result)
130 + else if (*p == '-')
136 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
139 + /* Annoying encoding used by zone.tab has no decimal point, so use
140 + * the length to determine the format:
148 + if (len < 4 || len > 7) {
153 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
155 + if (len == 5 || len == 7)
156 + v = v * 10.0 + (*p++ - '0');
158 + v += (10.0 * (p[0] - '0')
159 + + p[1] - '0') / 60.0;
163 + v += (10.0 * (p[0] - '0')
164 + + p[1] - '0') / 3600.0;
168 + /* Round to five decimal place, not because it's a good idea,
169 + * but, because the builtin data uses rounded data, so, match
171 + *result = trunc(v * sign * 100000.0) / 100000.0;
176 +/* This function parses the zone.tab file to build up the mapping of
177 + * timezone to country code and geographic location, and returns a
178 + * hash table. The hash table is indexed by the function:
180 + * tz_hash(timezone-name)
182 +static struct location_info **create_location_table(void)
184 + struct location_info **li, *i;
185 + char zone_tab[PATH_MAX];
189 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
191 + fp = fopen(zone_tab, "r");
196 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
198 + while (fgets(line, sizeof line, fp)) {
199 + char *p = line, *code, *name, *comment;
201 + double latitude, longitude;
203 + while (isspace(*p))
206 + if (*p == '#' || *p == '\0' || *p == '\n')
209 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
217 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
218 + p = parse_iso6709(p, &latitude);
222 + p = parse_iso6709(p, &longitude);
227 + if (!p || *p != '\t') {
231 + /* name = string */
233 + while (*p != '\t' && *p && *p != '\n')
238 + /* comment = string */
240 + while (*p != '\t' && *p && *p != '\n')
243 + if (*p == '\n' || *p == '\t')
246 + hash = tz_hash(name);
247 + i = malloc(sizeof *i);
248 + memcpy(i->code, code, 2);
249 + strncpy(i->name, name, sizeof i->name);
250 + i->comment = strdup(comment);
251 + i->longitude = longitude;
252 + i->latitude = latitude;
253 + i->next = li[hash];
255 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
263 +/* Return location info from hash table, using given timezone name.
264 + * Returns NULL if the name could not be found. */
265 +const struct location_info *find_zone_info(struct location_info **li,
268 + uint32_t hash = tz_hash(name);
269 + const struct location_info *l;
275 + for (l = li[hash]; l; l = l->next) {
276 + if (strcasecmp(l->name, name) == 0)
283 +/* Filter out some non-tzdata files and the posix/right databases, if
285 +static int index_filter(const struct dirent *ent)
287 + return strcmp(ent->d_name, ".") != 0
288 + && strcmp(ent->d_name, "..") != 0
289 + && strcmp(ent->d_name, "posix") != 0
290 + && strcmp(ent->d_name, "posixrules") != 0
291 + && strcmp(ent->d_name, "right") != 0
292 + && strstr(ent->d_name, ".tab") == NULL;
295 +static int sysdbcmp(const void *first, const void *second)
297 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
299 + return strcasecmp(alpha->id, beta->id);
303 +/* Create the zone identifier index by trawling the filesystem. */
304 +static void create_zone_index(timelib_tzdb *db)
306 + size_t dirstack_size, dirstack_top;
307 + size_t index_size, index_next;
308 + timelib_tzdb_index_entry *db_index;
311 + /* LIFO stack to hold directory entries to scan; each slot is a
312 + * directory name relative to the zoneinfo prefix. */
313 + dirstack_size = 32;
314 + dirstack = malloc(dirstack_size * sizeof *dirstack);
316 + dirstack[0] = strdup("");
320 + db_index = malloc(index_size * sizeof *db_index);
324 + struct dirent **ents;
325 + char name[PATH_MAX], *top;
328 + /* Pop the top stack entry, and iterate through its contents. */
329 + top = dirstack[--dirstack_top];
330 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
332 + count = php_scandir(name, &ents, index_filter, php_alphasort);
334 + while (count > 0) {
336 + const char *leaf = ents[count - 1]->d_name;
338 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
341 + if (strlen(name) && stat(name, &st) == 0) {
342 + /* Name, relative to the zoneinfo prefix. */
343 + const char *root = top;
345 + if (root[0] == '/') root++;
347 + snprintf(name, sizeof name, "%s%s%s", root,
348 + *root ? "/": "", leaf);
350 + if (S_ISDIR(st.st_mode)) {
351 + if (dirstack_top == dirstack_size) {
352 + dirstack_size *= 2;
353 + dirstack = realloc(dirstack,
354 + dirstack_size * sizeof *dirstack);
356 + dirstack[dirstack_top++] = strdup(name);
359 + if (index_next == index_size) {
361 + db_index = realloc(db_index,
362 + index_size * sizeof *db_index);
365 + db_index[index_next++].id = strdup(name);
369 + free(ents[--count]);
372 + if (count != -1) free(ents);
374 + } while (dirstack_top);
376 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
378 + db->index = db_index;
379 + db->index_size = index_next;
384 +#define FAKE_HEADER "1234\0??\1??"
385 +#define FAKE_UTC_POS (7 - 4)
387 +/* Create a fake data segment for database 'sysdb'. */
388 +static void fake_data_segment(timelib_tzdb *sysdb,
389 + struct location_info **info)
394 + data = malloc(3 * sysdb->index_size + 7);
396 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
398 + for (n = 0; n < sysdb->index_size; n++) {
399 + const struct location_info *li;
400 + timelib_tzdb_index_entry *ent;
402 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
404 + /* Lookup the timezone name in the hash table. */
405 + if (strcmp(ent->id, "UTC") == 0) {
406 + ent->pos = FAKE_UTC_POS;
410 + li = find_zone_info(info, ent->id);
412 + /* If found, append the BC byte and the
413 + * country code; set the position for this
414 + * section of timezone data. */
415 + ent->pos = (p - data) - 4;
417 + *p++ = li->code[0];
418 + *p++ = li->code[1];
421 + /* If not found, the timezone data can
422 + * point at the header. */
427 + sysdb->data = (unsigned char *)data;
430 +/* Returns true if the passed-in stat structure describes a
431 + * probably-valid timezone file. */
432 +static int is_valid_tzfile(const struct stat *st)
434 + return S_ISREG(st->st_mode) && st->st_size > 20;
437 +/* To allow timezone names to be used case-insensitively, find the
438 + * canonical name for this timezone, if possible. */
439 +static const char *canonical_tzname(const char *timezone)
441 + if (timezonedb_system) {
442 + timelib_tzdb_index_entry *ent, lookup;
444 + lookup.id = (char *)timezone;
446 + ent = bsearch(&lookup, timezonedb_system->index,
447 + timezonedb_system->index_size, sizeof lookup,
457 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
458 + * length of the mapped data is placed in *length. */
459 +static char *map_tzfile(const char *timezone, size_t *length)
461 + char fname[PATH_MAX];
466 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
470 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
472 + fd = open(fname, O_RDONLY);
475 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
480 + *length = st.st_size;
481 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
484 + return p != MAP_FAILED ? p : NULL;
489 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
491 int left = 0, right = tzdb->index_size - 1;
492 #ifdef HAVE_SETLOCALE
493 @@ -337,21 +767,88 @@ static int seek_to_tz_position(const uns
497 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
498 + char **map, size_t *maplen,
499 + const timelib_tzdb *tzdb)
501 +#ifdef HAVE_SYSTEM_TZDATA
502 + if (tzdb == timezonedb_system) {
505 + orig = map_tzfile(timezone, maplen);
506 + if (orig == NULL) {
510 + (*tzf) = (unsigned char *)orig;
517 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
521 const timelib_tzdb *timelib_builtin_db(void)
523 +#ifdef HAVE_SYSTEM_TZDATA
524 + if (timezonedb_system == NULL) {
525 + timelib_tzdb *tmp = malloc(sizeof *tmp);
527 + tmp->version = "0.system";
529 + create_zone_index(tmp);
530 + system_location_table = create_location_table();
531 + fake_data_segment(tmp, system_location_table);
532 + timezonedb_system = tmp;
535 + return timezonedb_system;
537 return &timezonedb_builtin;
541 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
543 +#ifdef HAVE_SYSTEM_TZDATA
544 + *count = timezonedb_system->index_size;
545 + return timezonedb_system->index;
547 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
548 return timezonedb_idx_builtin;
552 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
554 const unsigned char *tzf;
555 - return (seek_to_tz_position(&tzf, timezone, tzdb));
557 +#ifdef HAVE_SYSTEM_TZDATA
558 + if (tzdb == timezonedb_system) {
559 + char fname[PATH_MAX];
562 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
566 + if (system_location_table) {
567 + if (find_zone_info(system_location_table, timezone) != NULL) {
568 + /* found in cache */
573 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
575 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
579 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
582 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
583 @@ -376,24 +873,54 @@ static void read_64bit_header(const unsi
584 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
586 const unsigned char *tzf;
587 + char *memmap = NULL;
592 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
593 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
594 tmp = timelib_tzinfo_ctor(timezone);
596 version = read_preamble(&tzf, tmp);
597 read_header(&tzf, tmp);
598 read_transistions(&tzf, tmp);
599 read_types(&tzf, tmp);
600 - if (version == 2) {
601 - skip_64bit_preamble(&tzf, tmp);
602 - read_64bit_header(&tzf, tmp);
603 - skip_64bit_transistions(&tzf, tmp);
604 - skip_64bit_types(&tzf, tmp);
605 - skip_posix_string(&tzf, tmp);
607 - read_location(&tzf, tmp);
609 +#ifdef HAVE_SYSTEM_TZDATA
611 + const struct location_info *li;
613 + /* TZif-style - grok the location info from the system database,
616 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
617 + tmp->location.comments = timelib_strdup(li->comment);
618 + strncpy(tmp->location.country_code, li->code, 2);
619 + tmp->location.longitude = li->longitude;
620 + tmp->location.latitude = li->latitude;
624 + strcpy(tmp->location.country_code, "??");
626 + tmp->location.comments = timelib_strdup("");
629 + /* Now done with the mmap segment - discard it. */
630 + munmap(memmap, maplen);
634 + /* PHP-style - use the embedded info. */
635 + if (version == 2) {
636 + skip_64bit_preamble(&tzf, tmp);
637 + read_64bit_header(&tzf, tmp);
638 + skip_64bit_transistions(&tzf, tmp);
639 + skip_64bit_types(&tzf, tmp);
640 + skip_posix_string(&tzf, tmp);
642 + read_location(&tzf, tmp);
647 diff -up php-7.0.0RC1/ext/date/lib/timelib.m4.systzdata php-7.0.0RC1/ext/date/lib/timelib.m4
648 --- php-7.0.0RC1/ext/date/lib/timelib.m4.systzdata 2015-08-18 23:39:24.000000000 +0200
649 +++ php-7.0.0RC1/ext/date/lib/timelib.m4 2015-08-22 07:47:34.854055364 +0200
650 @@ -78,3 +78,17 @@ stdlib.h
652 dnl Check for strtoll, atoll
653 AC_CHECK_FUNCS(strtoll atoll strftime)
655 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
656 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
659 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
660 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
662 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
663 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
664 + [Define for location of system timezone data])