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 diff -up php-5.6.9RC1/ext/date/lib/parse_tz.c.systzdata php-5.6.9RC1/ext/date/lib/parse_tz.c
24 --- php-5.6.9RC1/ext/date/lib/parse_tz.c.systzdata 2015-04-30 00:00:18.000000000 +0200
25 +++ php-5.6.9RC1/ext/date/lib/parse_tz.c 2015-04-30 06:36:47.019617321 +0200
30 +#ifdef HAVE_SYSTEM_TZDATA
31 +#include <sys/mman.h>
32 +#include <sys/stat.h>
37 +#include "php_scandir.h"
48 +#ifndef HAVE_SYSTEM_TZDATA
49 #include "timezonedb.h"
54 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
55 # if defined(__LITTLE_ENDIAN__)
56 @@ -53,6 +68,10 @@ static int read_preamble(const unsigned
60 + if (memcmp(*tzf, "TZif", 4) == 0) {
65 version = (*tzf)[3] - '0';
67 @@ -296,7 +315,418 @@ void timelib_dump_tzinfo(timelib_tzinfo
71 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
72 +#ifdef HAVE_SYSTEM_TZDATA
74 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
75 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
77 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
80 +/* System timezone database pointer. */
81 +static const timelib_tzdb *timezonedb_system;
83 +/* Hash table entry for the cache of the zone.tab mapping table. */
84 +struct location_info {
86 + double latitude, longitude;
89 + struct location_info *next;
92 +/* Cache of zone.tab. */
93 +static struct location_info **system_location_table;
95 +/* Size of the zone.tab hash table; a random-ish prime big enough to
96 + * prevent too many collisions. */
97 +#define LOCINFO_HASH_SIZE (1021)
99 +/* Compute a case insensitive hash of str */
100 +static uint32_t tz_hash(const char *str)
102 + const unsigned char *p = (const unsigned char *)str;
103 + uint32_t hash = 5381;
106 + while ((c = tolower(*p++)) != '\0') {
107 + hash = (hash << 5) ^ hash ^ c;
110 + return hash % LOCINFO_HASH_SIZE;
113 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
114 + * parsed string on success, or NULL on parse error. On success,
115 + * writes the parsed number to *result. */
116 +static char *parse_iso6709(char *p, double *result)
124 + else if (*p == '-')
130 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
133 + /* Annoying encoding used by zone.tab has no decimal point, so use
134 + * the length to determine the format:
142 + if (len < 4 || len > 7) {
147 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
149 + if (len == 5 || len == 7)
150 + v = v * 10.0 + (*p++ - '0');
152 + v += (10.0 * (p[0] - '0')
153 + + p[1] - '0') / 60.0;
157 + v += (10.0 * (p[0] - '0')
158 + + p[1] - '0') / 3600.0;
162 + /* Round to five decimal place, not because it's a good idea,
163 + * but, because the builtin data uses rounded data, so, match
165 + *result = trunc(v * sign * 100000.0) / 100000.0;
170 +/* This function parses the zone.tab file to build up the mapping of
171 + * timezone to country code and geographic location, and returns a
172 + * hash table. The hash table is indexed by the function:
174 + * tz_hash(timezone-name)
176 +static struct location_info **create_location_table(void)
178 + struct location_info **li, *i;
179 + char zone_tab[PATH_MAX];
183 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
185 + fp = fopen(zone_tab, "r");
190 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
192 + while (fgets(line, sizeof line, fp)) {
193 + char *p = line, *code, *name, *comment;
195 + double latitude, longitude;
197 + while (isspace(*p))
200 + if (*p == '#' || *p == '\0' || *p == '\n')
203 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
211 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
212 + p = parse_iso6709(p, &latitude);
216 + p = parse_iso6709(p, &longitude);
221 + if (!p || *p != '\t') {
225 + /* name = string */
227 + while (*p != '\t' && *p && *p != '\n')
232 + /* comment = string */
234 + while (*p != '\t' && *p && *p != '\n')
237 + if (*p == '\n' || *p == '\t')
240 + hash = tz_hash(name);
241 + i = malloc(sizeof *i);
242 + memcpy(i->code, code, 2);
243 + strncpy(i->name, name, sizeof i->name);
244 + i->comment = strdup(comment);
245 + i->longitude = longitude;
246 + i->latitude = latitude;
247 + i->next = li[hash];
249 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
257 +/* Return location info from hash table, using given timezone name.
258 + * Returns NULL if the name could not be found. */
259 +const struct location_info *find_zone_info(struct location_info **li,
262 + uint32_t hash = tz_hash(name);
263 + const struct location_info *l;
269 + for (l = li[hash]; l; l = l->next) {
270 + if (strcasecmp(l->name, name) == 0)
277 +/* Filter out some non-tzdata files and the posix/right databases, if
279 +static int index_filter(const struct dirent *ent)
281 + return strcmp(ent->d_name, ".") != 0
282 + && strcmp(ent->d_name, "..") != 0
283 + && strcmp(ent->d_name, "posix") != 0
284 + && strcmp(ent->d_name, "posixrules") != 0
285 + && strcmp(ent->d_name, "right") != 0
286 + && strstr(ent->d_name, ".tab") == NULL;
289 +static int sysdbcmp(const void *first, const void *second)
291 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
293 + return strcasecmp(alpha->id, beta->id);
297 +/* Create the zone identifier index by trawling the filesystem. */
298 +static void create_zone_index(timelib_tzdb *db)
300 + size_t dirstack_size, dirstack_top;
301 + size_t index_size, index_next;
302 + timelib_tzdb_index_entry *db_index;
305 + /* LIFO stack to hold directory entries to scan; each slot is a
306 + * directory name relative to the zoneinfo prefix. */
307 + dirstack_size = 32;
308 + dirstack = malloc(dirstack_size * sizeof *dirstack);
310 + dirstack[0] = strdup("");
314 + db_index = malloc(index_size * sizeof *db_index);
318 + struct dirent **ents;
319 + char name[PATH_MAX], *top;
322 + /* Pop the top stack entry, and iterate through its contents. */
323 + top = dirstack[--dirstack_top];
324 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
326 + count = php_scandir(name, &ents, index_filter, php_alphasort);
328 + while (count > 0) {
330 + const char *leaf = ents[count - 1]->d_name;
332 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
335 + if (strlen(name) && stat(name, &st) == 0) {
336 + /* Name, relative to the zoneinfo prefix. */
337 + const char *root = top;
339 + if (root[0] == '/') root++;
341 + snprintf(name, sizeof name, "%s%s%s", root,
342 + *root ? "/": "", leaf);
344 + if (S_ISDIR(st.st_mode)) {
345 + if (dirstack_top == dirstack_size) {
346 + dirstack_size *= 2;
347 + dirstack = realloc(dirstack,
348 + dirstack_size * sizeof *dirstack);
350 + dirstack[dirstack_top++] = strdup(name);
353 + if (index_next == index_size) {
355 + db_index = realloc(db_index,
356 + index_size * sizeof *db_index);
359 + db_index[index_next++].id = strdup(name);
363 + free(ents[--count]);
366 + if (count != -1) free(ents);
368 + } while (dirstack_top);
370 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
372 + db->index = db_index;
373 + db->index_size = index_next;
378 +#define FAKE_HEADER "1234\0??\1??"
379 +#define FAKE_UTC_POS (7 - 4)
381 +/* Create a fake data segment for database 'sysdb'. */
382 +static void fake_data_segment(timelib_tzdb *sysdb,
383 + struct location_info **info)
388 + data = malloc(3 * sysdb->index_size + 7);
390 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
392 + for (n = 0; n < sysdb->index_size; n++) {
393 + const struct location_info *li;
394 + timelib_tzdb_index_entry *ent;
396 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
398 + /* Lookup the timezone name in the hash table. */
399 + if (strcmp(ent->id, "UTC") == 0) {
400 + ent->pos = FAKE_UTC_POS;
404 + li = find_zone_info(info, ent->id);
406 + /* If found, append the BC byte and the
407 + * country code; set the position for this
408 + * section of timezone data. */
409 + ent->pos = (p - data) - 4;
411 + *p++ = li->code[0];
412 + *p++ = li->code[1];
415 + /* If not found, the timezone data can
416 + * point at the header. */
421 + sysdb->data = (unsigned char *)data;
424 +/* Returns true if the passed-in stat structure describes a
425 + * probably-valid timezone file. */
426 +static int is_valid_tzfile(const struct stat *st)
428 + return S_ISREG(st->st_mode) && st->st_size > 20;
431 +/* To allow timezone names to be used case-insensitively, find the
432 + * canonical name for this timezone, if possible. */
433 +static const char *canonical_tzname(const char *timezone)
435 + if (timezonedb_system) {
436 + timelib_tzdb_index_entry *ent, lookup;
438 + lookup.id = (char *)timezone;
440 + ent = bsearch(&lookup, timezonedb_system->index,
441 + timezonedb_system->index_size, sizeof lookup,
451 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
452 + * length of the mapped data is placed in *length. */
453 +static char *map_tzfile(const char *timezone, size_t *length)
455 + char fname[PATH_MAX];
460 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
464 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
466 + fd = open(fname, O_RDONLY);
469 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
474 + *length = st.st_size;
475 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
478 + return p != MAP_FAILED ? p : NULL;
483 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
485 int left = 0, right = tzdb->index_size - 1;
486 #ifdef HAVE_SETLOCALE
487 @@ -335,21 +765,87 @@ static int seek_to_tz_position(const uns
491 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
492 + char **map, size_t *maplen,
493 + const timelib_tzdb *tzdb)
495 +#ifdef HAVE_SYSTEM_TZDATA
496 + if (tzdb == timezonedb_system) {
499 + orig = map_tzfile(timezone, maplen);
500 + if (orig == NULL) {
504 + (*tzf) = (unsigned char *)orig ;
511 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
515 const timelib_tzdb *timelib_builtin_db(void)
517 +#ifdef HAVE_SYSTEM_TZDATA
518 + if (timezonedb_system == NULL) {
519 + timelib_tzdb *tmp = malloc(sizeof *tmp);
521 + tmp->version = "0.system";
523 + create_zone_index(tmp);
524 + system_location_table = create_location_table();
525 + fake_data_segment(tmp, system_location_table);
526 + timezonedb_system = tmp;
529 + return timezonedb_system;
531 return &timezonedb_builtin;
535 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
537 +#ifdef HAVE_SYSTEM_TZDATA
538 + *count = timezonedb_system->index_size;
539 + return timezonedb_system->index;
541 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
542 return timezonedb_idx_builtin;
546 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
548 const unsigned char *tzf;
549 - return (seek_to_tz_position(&tzf, timezone, tzdb));
551 +#ifdef HAVE_SYSTEM_TZDATA
552 + if (tzdb == timezonedb_system) {
553 + char fname[PATH_MAX];
556 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
560 + if (system_location_table) {
561 + if (find_zone_info(system_location_table, timezone) != NULL) {
562 + /* found in cache */
567 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
569 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
572 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
575 static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
576 @@ -374,24 +870,54 @@ static void read_64bit_header(const unsi
577 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
579 const unsigned char *tzf;
580 + char *memmap = NULL;
585 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
586 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
587 tmp = timelib_tzinfo_ctor(timezone);
589 version = read_preamble(&tzf, tmp);
590 read_header(&tzf, tmp);
591 read_transistions(&tzf, tmp);
592 read_types(&tzf, tmp);
593 - if (version == 2) {
594 - skip_64bit_preamble(&tzf, tmp);
595 - read_64bit_header(&tzf, tmp);
596 - skip_64bit_transistions(&tzf, tmp);
597 - skip_64bit_types(&tzf, tmp);
598 - skip_posix_string(&tzf, tmp);
600 - read_location(&tzf, tmp);
602 +#ifdef HAVE_SYSTEM_TZDATA
604 + const struct location_info *li;
606 + /* TZif-style - grok the location info from the system database,
609 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
610 + tmp->location.comments = strdup(li->comment);
611 + strncpy(tmp->location.country_code, li->code, 2);
612 + tmp->location.longitude = li->longitude;
613 + tmp->location.latitude = li->latitude;
617 + strcpy(tmp->location.country_code, "??");
619 + tmp->location.comments = strdup("");
622 + /* Now done with the mmap segment - discard it. */
623 + munmap(memmap, maplen);
627 + if (version == 2) {
628 + skip_64bit_preamble(&tzf, tmp);
629 + read_64bit_header(&tzf, tmp);
630 + skip_64bit_transistions(&tzf, tmp);
631 + skip_64bit_types(&tzf, tmp);
632 + skip_posix_string(&tzf, tmp);
634 + /* PHP-style - use the embedded info. */
635 + read_location(&tzf, tmp);
640 diff -up php-5.6.9RC1/ext/date/lib/timelib.m4.systzdata php-5.6.9RC1/ext/date/lib/timelib.m4
641 --- php-5.6.9RC1/ext/date/lib/timelib.m4.systzdata 2015-04-30 00:00:18.000000000 +0200
642 +++ php-5.6.9RC1/ext/date/lib/timelib.m4 2015-04-30 06:32:08.549500385 +0200
643 @@ -78,3 +78,17 @@ stdlib.h
645 dnl Check for strtoll, atoll
646 AC_CHECK_FUNCS(strtoll atoll strftime)
648 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
649 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
652 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
653 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
655 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
656 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
657 + [Define for location of system timezone data])