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 r18: adapt for autotool change in 7.3.3RC1
9 r17: adapt for timelib 2018.01 (in 7.3.2RC1)
10 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
11 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
12 r14: improve check for valid tz file
13 r13: adapt for upstream changes to use PHP allocator
14 r12: adapt for upstream changes for new zic
15 r11: use canonical names to avoid more case sensitivity issues
16 round lat/long from zone.tab towards zero per builtin db
17 r10: make timezone case insensitive
18 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
19 r8: fix compile error without --with-system-tzdata configured
20 r7: improve check for valid timezone id to exclude directories
21 r6: fix fd leak in r5, fix country code/BC flag use in
22 timezone_identifiers_list() using system db,
23 fix use of PECL timezonedb to override system db,
24 r5: reverts addition of "System/Localtime" fake tzname.
25 updated for 5.3.0, parses zone.tab to pick up mapping between
26 timezone name, country code and long/lat coords
27 r4: added "System/Localtime" tzname which uses /etc/localtime
28 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
29 r2: add filesystem trawl to set up name alias index
32 diff -up php-7.3.3RC1/ext/date/config0.m4.systzdata php-7.3.3RC1/ext/date/config0.m4
33 --- php-7.3.3RC1/ext/date/config0.m4.systzdata 2019-02-19 14:57:51.314601701 +0100
34 +++ php-7.3.3RC1/ext/date/config0.m4 2019-02-19 14:58:29.050812587 +0100
36 dnl Check for strtoll, atoll
37 AC_CHECK_FUNCS(strtoll atoll)
39 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
40 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
43 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
44 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
46 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
47 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
48 + [Define for location of system timezone data])
52 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
53 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
54 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
55 diff -up php-7.3.3RC1/ext/date/lib/parse_tz.c.systzdata php-7.3.3RC1/ext/date/lib/parse_tz.c
56 --- php-7.3.3RC1/ext/date/lib/parse_tz.c.systzdata 2019-02-19 12:18:27.000000000 +0100
57 +++ php-7.3.3RC1/ext/date/lib/parse_tz.c 2019-02-19 14:57:20.397428931 +0100
60 #include "timelib_private.h"
62 +#ifdef HAVE_SYSTEM_TZDATA
63 +#include <sys/mman.h>
64 +#include <sys/stat.h>
69 +#include "php_scandir.h"
72 #define TIMELIB_SUPPORTS_V2DATA
73 #include "timezonedb.h"
78 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
79 # if defined(__LITTLE_ENDIAN__)
80 @@ -87,6 +100,11 @@ static int read_php_preamble(const unsig
84 + if (memcmp(*tzf, "TZif", 4) == 0) {
90 version = (*tzf)[3] - '0';
92 @@ -411,7 +429,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
96 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
97 +#ifdef HAVE_SYSTEM_TZDATA
99 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
100 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
102 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
105 +/* System timezone database pointer. */
106 +static const timelib_tzdb *timezonedb_system;
108 +/* Hash table entry for the cache of the zone.tab mapping table. */
109 +struct location_info {
111 + double latitude, longitude;
114 + struct location_info *next;
117 +/* Cache of zone.tab. */
118 +static struct location_info **system_location_table;
120 +/* Size of the zone.tab hash table; a random-ish prime big enough to
121 + * prevent too many collisions. */
122 +#define LOCINFO_HASH_SIZE (1021)
124 +/* Compute a case insensitive hash of str */
125 +static uint32_t tz_hash(const char *str)
127 + const unsigned char *p = (const unsigned char *)str;
128 + uint32_t hash = 5381;
131 + while ((c = tolower(*p++)) != '\0') {
132 + hash = (hash << 5) ^ hash ^ c;
135 + return hash % LOCINFO_HASH_SIZE;
138 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
139 + * parsed string on success, or NULL on parse error. On success,
140 + * writes the parsed number to *result. */
141 +static char *parse_iso6709(char *p, double *result)
149 + else if (*p == '-')
155 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
158 + /* Annoying encoding used by zone.tab has no decimal point, so use
159 + * the length to determine the format:
167 + if (len < 4 || len > 7) {
172 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
174 + if (len == 5 || len == 7)
175 + v = v * 10.0 + (*p++ - '0');
177 + v += (10.0 * (p[0] - '0')
178 + + p[1] - '0') / 60.0;
182 + v += (10.0 * (p[0] - '0')
183 + + p[1] - '0') / 3600.0;
187 + /* Round to five decimal place, not because it's a good idea,
188 + * but, because the builtin data uses rounded data, so, match
190 + *result = trunc(v * sign * 100000.0) / 100000.0;
195 +/* This function parses the zone.tab file to build up the mapping of
196 + * timezone to country code and geographic location, and returns a
197 + * hash table. The hash table is indexed by the function:
199 + * tz_hash(timezone-name)
201 +static struct location_info **create_location_table(void)
203 + struct location_info **li, *i;
204 + char zone_tab[PATH_MAX];
208 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
210 + fp = fopen(zone_tab, "r");
215 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
217 + while (fgets(line, sizeof line, fp)) {
218 + char *p = line, *code, *name, *comment;
220 + double latitude, longitude;
222 + while (isspace(*p))
225 + if (*p == '#' || *p == '\0' || *p == '\n')
228 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
236 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
237 + p = parse_iso6709(p, &latitude);
241 + p = parse_iso6709(p, &longitude);
246 + if (!p || *p != '\t') {
250 + /* name = string */
252 + while (*p != '\t' && *p && *p != '\n')
257 + /* comment = string */
259 + while (*p != '\t' && *p && *p != '\n')
262 + if (*p == '\n' || *p == '\t')
265 + hash = tz_hash(name);
266 + i = malloc(sizeof *i);
267 + memcpy(i->code, code, 2);
268 + strncpy(i->name, name, sizeof i->name);
269 + i->comment = strdup(comment);
270 + i->longitude = longitude;
271 + i->latitude = latitude;
272 + i->next = li[hash];
274 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
282 +/* Return location info from hash table, using given timezone name.
283 + * Returns NULL if the name could not be found. */
284 +const struct location_info *find_zone_info(struct location_info **li,
287 + uint32_t hash = tz_hash(name);
288 + const struct location_info *l;
294 + for (l = li[hash]; l; l = l->next) {
295 + if (timelib_strcasecmp(l->name, name) == 0)
302 +/* Filter out some non-tzdata files and the posix/right databases, if
304 +static int index_filter(const struct dirent *ent)
306 + return strcmp(ent->d_name, ".") != 0
307 + && strcmp(ent->d_name, "..") != 0
308 + && strcmp(ent->d_name, "posix") != 0
309 + && strcmp(ent->d_name, "posixrules") != 0
310 + && strcmp(ent->d_name, "right") != 0
311 + && strstr(ent->d_name, ".list") == NULL
312 + && strstr(ent->d_name, ".tab") == NULL;
315 +static int sysdbcmp(const void *first, const void *second)
317 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
319 + return timelib_strcasecmp(alpha->id, beta->id);
323 +/* Create the zone identifier index by trawling the filesystem. */
324 +static void create_zone_index(timelib_tzdb *db)
326 + size_t dirstack_size, dirstack_top;
327 + size_t index_size, index_next;
328 + timelib_tzdb_index_entry *db_index;
331 + /* LIFO stack to hold directory entries to scan; each slot is a
332 + * directory name relative to the zoneinfo prefix. */
333 + dirstack_size = 32;
334 + dirstack = malloc(dirstack_size * sizeof *dirstack);
336 + dirstack[0] = strdup("");
340 + db_index = malloc(index_size * sizeof *db_index);
344 + struct dirent **ents;
345 + char name[PATH_MAX], *top;
348 + /* Pop the top stack entry, and iterate through its contents. */
349 + top = dirstack[--dirstack_top];
350 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
352 + count = php_scandir(name, &ents, index_filter, php_alphasort);
354 + while (count > 0) {
356 + const char *leaf = ents[count - 1]->d_name;
358 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
361 + if (strlen(name) && stat(name, &st) == 0) {
362 + /* Name, relative to the zoneinfo prefix. */
363 + const char *root = top;
365 + if (root[0] == '/') root++;
367 + snprintf(name, sizeof name, "%s%s%s", root,
368 + *root ? "/": "", leaf);
370 + if (S_ISDIR(st.st_mode)) {
371 + if (dirstack_top == dirstack_size) {
372 + dirstack_size *= 2;
373 + dirstack = realloc(dirstack,
374 + dirstack_size * sizeof *dirstack);
376 + dirstack[dirstack_top++] = strdup(name);
379 + if (index_next == index_size) {
381 + db_index = realloc(db_index,
382 + index_size * sizeof *db_index);
385 + db_index[index_next++].id = strdup(name);
389 + free(ents[--count]);
392 + if (count != -1) free(ents);
394 + } while (dirstack_top);
396 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
398 + db->index = db_index;
399 + db->index_size = index_next;
404 +#define FAKE_HEADER "1234\0??\1??"
405 +#define FAKE_UTC_POS (7 - 4)
407 +/* Create a fake data segment for database 'sysdb'. */
408 +static void fake_data_segment(timelib_tzdb *sysdb,
409 + struct location_info **info)
414 + data = malloc(3 * sysdb->index_size + 7);
416 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
418 + for (n = 0; n < sysdb->index_size; n++) {
419 + const struct location_info *li;
420 + timelib_tzdb_index_entry *ent;
422 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
424 + /* Lookup the timezone name in the hash table. */
425 + if (strcmp(ent->id, "UTC") == 0) {
426 + ent->pos = FAKE_UTC_POS;
430 + li = find_zone_info(info, ent->id);
432 + /* If found, append the BC byte and the
433 + * country code; set the position for this
434 + * section of timezone data. */
435 + ent->pos = (p - data) - 4;
437 + *p++ = li->code[0];
438 + *p++ = li->code[1];
441 + /* If not found, the timezone data can
442 + * point at the header. */
447 + sysdb->data = (unsigned char *)data;
450 +/* Returns true if the passed-in stat structure describes a
451 + * probably-valid timezone file. */
452 +static int is_valid_tzfile(const struct stat *st, int fd)
456 + if (read(fd, buf, 20)!=20) {
459 + lseek(fd, SEEK_SET, 0);
460 + if (memcmp(buf, "TZif", 4)) {
464 + return S_ISREG(st->st_mode) && st->st_size > 20;
467 +/* To allow timezone names to be used case-insensitively, find the
468 + * canonical name for this timezone, if possible. */
469 +static const char *canonical_tzname(const char *timezone)
471 + if (timezonedb_system) {
472 + timelib_tzdb_index_entry *ent, lookup;
474 + lookup.id = (char *)timezone;
476 + ent = bsearch(&lookup, timezonedb_system->index,
477 + timezonedb_system->index_size, sizeof lookup,
487 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
488 + * length of the mapped data is placed in *length. */
489 +static char *map_tzfile(const char *timezone, size_t *length)
491 + char fname[PATH_MAX];
496 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
500 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
502 + fd = open(fname, O_RDONLY);
505 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
510 + *length = st.st_size;
511 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
514 + return p != MAP_FAILED ? p : NULL;
519 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
521 int left = 0, right = tzdb->index_size - 1;
523 @@ -437,9 +877,48 @@ static int seek_to_tz_position(const uns
527 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
528 + char **map, size_t *maplen,
529 + const timelib_tzdb *tzdb)
531 +#ifdef HAVE_SYSTEM_TZDATA
532 + if (tzdb == timezonedb_system) {
535 + orig = map_tzfile(timezone, maplen);
536 + if (orig == NULL) {
540 + (*tzf) = (unsigned char *)orig;
547 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
551 const timelib_tzdb *timelib_builtin_db(void)
553 +#ifdef HAVE_SYSTEM_TZDATA
554 + if (timezonedb_system == NULL) {
555 + timelib_tzdb *tmp = malloc(sizeof *tmp);
557 + tmp->version = "0.system";
559 + create_zone_index(tmp);
560 + system_location_table = create_location_table();
561 + fake_data_segment(tmp, system_location_table);
562 + timezonedb_system = tmp;
565 + return timezonedb_system;
567 return &timezonedb_builtin;
571 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
572 @@ -451,7 +930,30 @@ const timelib_tzdb_index_entry *timelib_
573 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
575 const unsigned char *tzf;
576 - return (seek_to_tz_position(&tzf, timezone, tzdb));
578 +#ifdef HAVE_SYSTEM_TZDATA
579 + if (tzdb == timezonedb_system) {
580 + char fname[PATH_MAX];
583 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
587 + if (system_location_table) {
588 + if (find_zone_info(system_location_table, timezone) != NULL) {
589 + /* found in cache */
594 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
596 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
600 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
603 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
604 @@ -493,12 +995,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
605 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code)
607 const unsigned char *tzf;
608 + char *memmap = NULL;
612 int transitions_result, types_result;
613 unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
615 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
616 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
617 tmp = timelib_tzinfo_ctor(timezone);
619 version = read_preamble(&tzf, tmp, &type);
620 @@ -537,11 +1041,36 @@ timelib_tzinfo *timelib_parse_tzfile(cha
622 skip_posix_string(&tzf, 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 (type == TIMELIB_TZINFO_PHP) {
647 read_location(&tzf, tmp);
649 set_default_location_and_comments(&tzf, tmp);
651 +#ifdef HAVE_SYSTEM_TZDATA
655 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;