2 # http://opensource.org/licenses/MIT
4 Add support for use of the system timezone database, rather
5 than embedding a copy. Discussed upstream but was not desired.
8 r17: adapt for autotool change in 7.2.16RC1
9 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
10 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
11 r14: improve check for valid tz file
12 r13: adapt for upstream changes to use PHP allocator
13 r12: adapt for upstream changes for new zic
14 r11: use canonical names to avoid more case sensitivity issues
15 round lat/long from zone.tab towards zero per builtin db
16 r10: make timezone case insensitive
17 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
18 r8: fix compile error without --with-system-tzdata configured
19 r7: improve check for valid timezone id to exclude directories
20 r6: fix fd leak in r5, fix country code/BC flag use in
21 timezone_identifiers_list() using system db,
22 fix use of PECL timezonedb to override system db,
23 r5: reverts addition of "System/Localtime" fake tzname.
24 updated for 5.3.0, parses zone.tab to pick up mapping between
25 timezone name, country code and long/lat coords
26 r4: added "System/Localtime" tzname which uses /etc/localtime
27 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
28 r2: add filesystem trawl to set up name alias index
31 diff -up php-7.2.16RC1/ext/date/config0.m4.systzdata php-7.2.16RC1/ext/date/config0.m4
32 --- php-7.2.16RC1/ext/date/config0.m4.systzdata 2019-02-19 11:22:22.223741585 +0100
33 +++ php-7.2.16RC1/ext/date/config0.m4 2019-02-19 11:23:05.089111556 +0100
34 @@ -10,6 +10,19 @@ io.h
35 dnl Check for strtoll, atoll
36 AC_CHECK_FUNCS(strtoll atoll)
38 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
39 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
42 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
43 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
45 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
46 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
47 + [Define for location of system timezone data])
51 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
52 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
53 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
54 diff -up php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata php-7.2.16RC1/ext/date/lib/parse_tz.c
55 --- php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata 2019-02-19 11:13:22.000000000 +0100
56 +++ php-7.2.16RC1/ext/date/lib/parse_tz.c 2019-02-19 11:19:40.245313535 +0100
59 #include "timelib_private.h"
61 +#ifdef HAVE_SYSTEM_TZDATA
62 +#include <sys/mman.h>
63 +#include <sys/stat.h>
68 +#include "php_scandir.h"
71 #define TIMELIB_SUPPORTS_V2DATA
72 #include "timezonedb.h"
77 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
78 # if defined(__LITTLE_ENDIAN__)
79 @@ -67,6 +80,11 @@ static int read_php_preamble(const unsig
83 + if (memcmp(*tzf, "TZif", 4) == 0) {
89 version = (*tzf)[3] - '0';
91 @@ -374,7 +392,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
95 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
96 +#ifdef HAVE_SYSTEM_TZDATA
98 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
99 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
101 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
104 +/* System timezone database pointer. */
105 +static const timelib_tzdb *timezonedb_system;
107 +/* Hash table entry for the cache of the zone.tab mapping table. */
108 +struct location_info {
110 + double latitude, longitude;
113 + struct location_info *next;
116 +/* Cache of zone.tab. */
117 +static struct location_info **system_location_table;
119 +/* Size of the zone.tab hash table; a random-ish prime big enough to
120 + * prevent too many collisions. */
121 +#define LOCINFO_HASH_SIZE (1021)
123 +/* Compute a case insensitive hash of str */
124 +static uint32_t tz_hash(const char *str)
126 + const unsigned char *p = (const unsigned char *)str;
127 + uint32_t hash = 5381;
130 + while ((c = tolower(*p++)) != '\0') {
131 + hash = (hash << 5) ^ hash ^ c;
134 + return hash % LOCINFO_HASH_SIZE;
137 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
138 + * parsed string on success, or NULL on parse error. On success,
139 + * writes the parsed number to *result. */
140 +static char *parse_iso6709(char *p, double *result)
148 + else if (*p == '-')
154 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
157 + /* Annoying encoding used by zone.tab has no decimal point, so use
158 + * the length to determine the format:
166 + if (len < 4 || len > 7) {
171 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
173 + if (len == 5 || len == 7)
174 + v = v * 10.0 + (*p++ - '0');
176 + v += (10.0 * (p[0] - '0')
177 + + p[1] - '0') / 60.0;
181 + v += (10.0 * (p[0] - '0')
182 + + p[1] - '0') / 3600.0;
186 + /* Round to five decimal place, not because it's a good idea,
187 + * but, because the builtin data uses rounded data, so, match
189 + *result = trunc(v * sign * 100000.0) / 100000.0;
194 +/* This function parses the zone.tab file to build up the mapping of
195 + * timezone to country code and geographic location, and returns a
196 + * hash table. The hash table is indexed by the function:
198 + * tz_hash(timezone-name)
200 +static struct location_info **create_location_table(void)
202 + struct location_info **li, *i;
203 + char zone_tab[PATH_MAX];
207 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
209 + fp = fopen(zone_tab, "r");
214 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
216 + while (fgets(line, sizeof line, fp)) {
217 + char *p = line, *code, *name, *comment;
219 + double latitude, longitude;
221 + while (isspace(*p))
224 + if (*p == '#' || *p == '\0' || *p == '\n')
227 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
235 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
236 + p = parse_iso6709(p, &latitude);
240 + p = parse_iso6709(p, &longitude);
245 + if (!p || *p != '\t') {
249 + /* name = string */
251 + while (*p != '\t' && *p && *p != '\n')
256 + /* comment = string */
258 + while (*p != '\t' && *p && *p != '\n')
261 + if (*p == '\n' || *p == '\t')
264 + hash = tz_hash(name);
265 + i = malloc(sizeof *i);
266 + memcpy(i->code, code, 2);
267 + strncpy(i->name, name, sizeof i->name);
268 + i->comment = strdup(comment);
269 + i->longitude = longitude;
270 + i->latitude = latitude;
271 + i->next = li[hash];
273 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
281 +/* Return location info from hash table, using given timezone name.
282 + * Returns NULL if the name could not be found. */
283 +const struct location_info *find_zone_info(struct location_info **li,
286 + uint32_t hash = tz_hash(name);
287 + const struct location_info *l;
293 + for (l = li[hash]; l; l = l->next) {
294 + if (timelib_strcasecmp(l->name, name) == 0)
301 +/* Filter out some non-tzdata files and the posix/right databases, if
303 +static int index_filter(const struct dirent *ent)
305 + return strcmp(ent->d_name, ".") != 0
306 + && strcmp(ent->d_name, "..") != 0
307 + && strcmp(ent->d_name, "posix") != 0
308 + && strcmp(ent->d_name, "posixrules") != 0
309 + && strcmp(ent->d_name, "right") != 0
310 + && strstr(ent->d_name, ".list") == NULL
311 + && strstr(ent->d_name, ".tab") == NULL;
314 +static int sysdbcmp(const void *first, const void *second)
316 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
318 + return timelib_strcasecmp(alpha->id, beta->id);
322 +/* Create the zone identifier index by trawling the filesystem. */
323 +static void create_zone_index(timelib_tzdb *db)
325 + size_t dirstack_size, dirstack_top;
326 + size_t index_size, index_next;
327 + timelib_tzdb_index_entry *db_index;
330 + /* LIFO stack to hold directory entries to scan; each slot is a
331 + * directory name relative to the zoneinfo prefix. */
332 + dirstack_size = 32;
333 + dirstack = malloc(dirstack_size * sizeof *dirstack);
335 + dirstack[0] = strdup("");
339 + db_index = malloc(index_size * sizeof *db_index);
343 + struct dirent **ents;
344 + char name[PATH_MAX], *top;
347 + /* Pop the top stack entry, and iterate through its contents. */
348 + top = dirstack[--dirstack_top];
349 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
351 + count = php_scandir(name, &ents, index_filter, php_alphasort);
353 + while (count > 0) {
355 + const char *leaf = ents[count - 1]->d_name;
357 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
360 + if (strlen(name) && stat(name, &st) == 0) {
361 + /* Name, relative to the zoneinfo prefix. */
362 + const char *root = top;
364 + if (root[0] == '/') root++;
366 + snprintf(name, sizeof name, "%s%s%s", root,
367 + *root ? "/": "", leaf);
369 + if (S_ISDIR(st.st_mode)) {
370 + if (dirstack_top == dirstack_size) {
371 + dirstack_size *= 2;
372 + dirstack = realloc(dirstack,
373 + dirstack_size * sizeof *dirstack);
375 + dirstack[dirstack_top++] = strdup(name);
378 + if (index_next == index_size) {
380 + db_index = realloc(db_index,
381 + index_size * sizeof *db_index);
384 + db_index[index_next++].id = strdup(name);
388 + free(ents[--count]);
391 + if (count != -1) free(ents);
393 + } while (dirstack_top);
395 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
397 + db->index = db_index;
398 + db->index_size = index_next;
403 +#define FAKE_HEADER "1234\0??\1??"
404 +#define FAKE_UTC_POS (7 - 4)
406 +/* Create a fake data segment for database 'sysdb'. */
407 +static void fake_data_segment(timelib_tzdb *sysdb,
408 + struct location_info **info)
413 + data = malloc(3 * sysdb->index_size + 7);
415 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
417 + for (n = 0; n < sysdb->index_size; n++) {
418 + const struct location_info *li;
419 + timelib_tzdb_index_entry *ent;
421 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
423 + /* Lookup the timezone name in the hash table. */
424 + if (strcmp(ent->id, "UTC") == 0) {
425 + ent->pos = FAKE_UTC_POS;
429 + li = find_zone_info(info, ent->id);
431 + /* If found, append the BC byte and the
432 + * country code; set the position for this
433 + * section of timezone data. */
434 + ent->pos = (p - data) - 4;
436 + *p++ = li->code[0];
437 + *p++ = li->code[1];
440 + /* If not found, the timezone data can
441 + * point at the header. */
446 + sysdb->data = (unsigned char *)data;
449 +/* Returns true if the passed-in stat structure describes a
450 + * probably-valid timezone file. */
451 +static int is_valid_tzfile(const struct stat *st, int fd)
455 + if (read(fd, buf, 20)!=20) {
458 + lseek(fd, SEEK_SET, 0);
459 + if (memcmp(buf, "TZif", 4)) {
463 + return S_ISREG(st->st_mode) && st->st_size > 20;
466 +/* To allow timezone names to be used case-insensitively, find the
467 + * canonical name for this timezone, if possible. */
468 +static const char *canonical_tzname(const char *timezone)
470 + if (timezonedb_system) {
471 + timelib_tzdb_index_entry *ent, lookup;
473 + lookup.id = (char *)timezone;
475 + ent = bsearch(&lookup, timezonedb_system->index,
476 + timezonedb_system->index_size, sizeof lookup,
486 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
487 + * length of the mapped data is placed in *length. */
488 +static char *map_tzfile(const char *timezone, size_t *length)
490 + char fname[PATH_MAX];
495 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
499 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
501 + fd = open(fname, O_RDONLY);
504 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
509 + *length = st.st_size;
510 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
513 + return p != MAP_FAILED ? p : NULL;
518 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
520 int left = 0, right = tzdb->index_size - 1;
522 @@ -400,9 +840,48 @@ static int seek_to_tz_position(const uns
526 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
527 + char **map, size_t *maplen,
528 + const timelib_tzdb *tzdb)
530 +#ifdef HAVE_SYSTEM_TZDATA
531 + if (tzdb == timezonedb_system) {
534 + orig = map_tzfile(timezone, maplen);
535 + if (orig == NULL) {
539 + (*tzf) = (unsigned char *)orig;
546 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
550 const timelib_tzdb *timelib_builtin_db(void)
552 +#ifdef HAVE_SYSTEM_TZDATA
553 + if (timezonedb_system == NULL) {
554 + timelib_tzdb *tmp = malloc(sizeof *tmp);
556 + tmp->version = "0.system";
558 + create_zone_index(tmp);
559 + system_location_table = create_location_table();
560 + fake_data_segment(tmp, system_location_table);
561 + timezonedb_system = tmp;
564 + return timezonedb_system;
566 return &timezonedb_builtin;
570 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
571 @@ -414,7 +893,30 @@ const timelib_tzdb_index_entry *timelib_
572 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
574 const unsigned char *tzf;
575 - return (seek_to_tz_position(&tzf, timezone, tzdb));
577 +#ifdef HAVE_SYSTEM_TZDATA
578 + if (tzdb == timezonedb_system) {
579 + char fname[PATH_MAX];
582 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
586 + if (system_location_table) {
587 + if (find_zone_info(system_location_table, timezone) != NULL) {
588 + /* found in cache */
593 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
595 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
599 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
602 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
603 @@ -456,12 +958,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
604 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code)
606 const unsigned char *tzf;
607 + char *memmap = NULL;
611 int transitions_result, types_result;
612 unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
614 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
615 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
616 tmp = timelib_tzinfo_ctor(timezone);
618 version = read_preamble(&tzf, tmp, &type);
619 @@ -484,6 +988,29 @@ timelib_tzinfo *timelib_parse_tzfile(cha
620 timelib_tzinfo_dtor(tmp);
624 +#ifdef HAVE_SYSTEM_TZDATA
626 + const struct location_info *li;
628 + /* TZif-style - grok the location info from the system database,
631 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
632 + tmp->location.comments = timelib_strdup(li->comment);
633 + strncpy(tmp->location.country_code, li->code, 2);
634 + tmp->location.longitude = li->longitude;
635 + tmp->location.latitude = li->latitude;
639 + set_default_location_and_comments(&tzf, tmp);
642 + /* Now done with the mmap segment - discard it. */
643 + munmap(memmap, maplen);
646 if (version == 2 || version == 3) {
647 if (!skip_64bit_preamble(&tzf, tmp)) {
648 /* 64 bit preamble is not in place */
649 @@ -501,6 +1028,9 @@ timelib_tzinfo *timelib_parse_tzfile(cha
651 set_default_location_and_comments(&tzf, tmp);
653 +#ifdef HAVE_SYSTEM_TZDATA
657 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;