+# License: MIT
+# http://opensource.org/licenses/MIT
+
Add support for use of the system timezone database, rather
than embedding a copy. Discussed upstream but was not desired.
History:
+r17: adapt for autotool change in 7.2.16RC1
+r16: adapt for timelib 2017.06 (in 7.2.3RC1)
+r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
+r14: improve check for valid tz file
+r13: adapt for upstream changes to use PHP allocator
+r12: adapt for upstream changes for new zic
+r11: use canonical names to avoid more case sensitivity issues
+ round lat/long from zone.tab towards zero per builtin db
+r10: make timezone case insensitive
r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
r8: fix compile error without --with-system-tzdata configured
r7: improve check for valid timezone id to exclude directories
-r6: fix fd leak in r5, fix country code/BC flag use in
+r6: fix fd leak in r5, fix country code/BC flag use in
timezone_identifiers_list() using system db,
fix use of PECL timezonedb to override system db,
r5: reverts addition of "System/Localtime" fake tzname.
r2: add filesystem trawl to set up name alias index
r1: initial revision
---- a/ext/date/lib/parse_tz.c
-+++ b/ext/date/lib/parse_tz.c
-@@ -24,6 +24,16 @@
+diff -up php-7.2.16RC1/ext/date/config0.m4.systzdata php-7.2.16RC1/ext/date/config0.m4
+--- php-7.2.16RC1/ext/date/config0.m4.systzdata 2019-02-19 11:22:22.223741585 +0100
++++ php-7.2.16RC1/ext/date/config0.m4 2019-02-19 11:23:05.089111556 +0100
+@@ -10,6 +10,19 @@ io.h
+ dnl Check for strtoll, atoll
+ AC_CHECK_FUNCS(strtoll atoll)
++PHP_ARG_WITH(system-tzdata, for use of system timezone data,
++[ --with-system-tzdata[=DIR] to specify use of system timezone data],
++no, no)
++
++if test "$PHP_SYSTEM_TZDATA" != "no"; then
++ AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
++
++ if test "$PHP_SYSTEM_TZDATA" != "yes"; then
++ AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
++ [Define for location of system timezone data])
++ fi
++fi
++
+ PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
+ timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
+ lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
+diff -up php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata php-7.2.16RC1/ext/date/lib/parse_tz.c
+--- php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata 2019-02-19 11:13:22.000000000 +0100
++++ php-7.2.16RC1/ext/date/lib/parse_tz.c 2019-02-19 11:19:40.245313535 +0100
+@@ -25,8 +25,21 @@
#include "timelib.h"
+ #include "timelib_private.h"
+#ifdef HAVE_SYSTEM_TZDATA
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "php_scandir.h"
-+#endif
+
- #include <stdio.h>
-
- #ifdef HAVE_LOCALE_H
-@@ -35,7 +45,12 @@
- #else
- #include <strings.h>
- #endif
-+
-+#ifndef HAVE_SYSTEM_TZDATA
++#else
+ #define TIMELIB_SUPPORTS_V2DATA
#include "timezonedb.h"
+#endif
+
#if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
# if defined(__LITTLE_ENDIAN__)
-@@ -55,9 +70,14 @@
-
- static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
+@@ -67,6 +80,11 @@ static int read_php_preamble(const unsig
{
-- /* skip ID */
-- *tzf += 4;
--
-+ if (memcmp(tzf, "TZif", 4) == 0) {
-+ *tzf += 20;
-+ return;
-+ }
-+
-+ /* skip ID */
-+ *tzf += 4;
-+
- /* read BC flag */
- tz->bc = (**tzf == '\1');
- *tzf += 1;
-@@ -260,7 +280,397 @@ void timelib_dump_tzinfo(timelib_tzinfo
+ uint32_t version;
+
++ if (memcmp(*tzf, "TZif", 4) == 0) {
++ *tzf += 20;
++ return 0;
++ }
++
+ /* read ID */
+ version = (*tzf)[3] - '0';
+ *tzf += 4;
+@@ -374,7 +392,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
}
}
+#endif
+
+/* System timezone database pointer. */
-+static const timelib_tzdb *timezonedb_system = NULL;
++static const timelib_tzdb *timezonedb_system;
+
+/* Hash table entry for the cache of the zone.tab mapping table. */
+struct location_info {
+ * prevent too many collisions. */
+#define LOCINFO_HASH_SIZE (1021)
+
++/* Compute a case insensitive hash of str */
+static uint32_t tz_hash(const char *str)
+{
+ const unsigned char *p = (const unsigned char *)str;
+ uint32_t hash = 5381;
+ int c;
-+
-+ while ((c = *p++) != '\0') {
++
++ while ((c = tolower(*p++)) != '\0') {
+ hash = (hash << 5) ^ hash ^ c;
+ }
-+
++
+ return hash % LOCINFO_HASH_SIZE;
+}
+
+ /* Round to five decimal place, not because it's a good idea,
+ * but, because the builtin data uses rounded data, so, match
+ * that. */
-+ *result = round(v * sign * 100000.0) / 100000.0;
++ *result = trunc(v * sign * 100000.0) / 100000.0;
+
+ return p;
+}
+ }
+
+ for (l = li[hash]; l; l = l->next) {
-+ if (strcasecmp(l->name, name) == 0)
++ if (timelib_strcasecmp(l->name, name) == 0)
+ return l;
+ }
+
+ && strcmp(ent->d_name, "posix") != 0
+ && strcmp(ent->d_name, "posixrules") != 0
+ && strcmp(ent->d_name, "right") != 0
++ && strstr(ent->d_name, ".list") == NULL
+ && strstr(ent->d_name, ".tab") == NULL;
+}
+
+{
+ const timelib_tzdb_index_entry *alpha = first, *beta = second;
+
-+ return strcmp(alpha->id, beta->id);
++ return timelib_strcasecmp(alpha->id, beta->id);
+}
+
+
+
+/* Returns true if the passed-in stat structure describes a
+ * probably-valid timezone file. */
-+static int is_valid_tzfile(const struct stat *st)
++static int is_valid_tzfile(const struct stat *st, int fd)
+{
++ if (fd) {
++ char buf[20];
++ if (read(fd, buf, 20)!=20) {
++ return 0;
++ }
++ lseek(fd, SEEK_SET, 0);
++ if (memcmp(buf, "TZif", 4)) {
++ return 0;
++ }
++ }
+ return S_ISREG(st->st_mode) && st->st_size > 20;
+}
+
++/* To allow timezone names to be used case-insensitively, find the
++ * canonical name for this timezone, if possible. */
++static const char *canonical_tzname(const char *timezone)
++{
++ if (timezonedb_system) {
++ timelib_tzdb_index_entry *ent, lookup;
++
++ lookup.id = (char *)timezone;
++
++ ent = bsearch(&lookup, timezonedb_system->index,
++ timezonedb_system->index_size, sizeof lookup,
++ sysdbcmp);
++ if (ent) {
++ return ent->id;
++ }
++ }
++
++ return timezone;
++}
++
+/* Return the mmap()ed tzfile if found, else NULL. On success, the
+ * length of the mapped data is placed in *length. */
+static char *map_tzfile(const char *timezone, size_t *length)
+ return NULL;
+ }
+
-+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
-+
++ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
++
+ fd = open(fname, O_RDONLY);
+ if (fd == -1) {
+ return NULL;
-+ } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
++ } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
+ close(fd);
+ return NULL;
+ }
+static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
{
int left = 0, right = tzdb->index_size - 1;
- #ifdef HAVE_SETLOCALE
-@@ -299,36 +709,128 @@ static int seek_to_tz_position(const uns
+
+@@ -400,9 +840,48 @@ static int seek_to_tz_position(const uns
return 0;
}
-+static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
++static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
+ char **map, size_t *maplen,
+ const timelib_tzdb *tzdb)
+{
+ if (orig == NULL) {
+ return 0;
+ }
-+
-+ (*tzf) = (unsigned char *)orig ;
++
++ (*tzf) = (unsigned char *)orig;
+ *map = orig;
-+
-+ return 1;
++ return 1;
+ }
-+ else
++ else
+#endif
-+ {
++ {
+ return inmem_seek_to_tz_position(tzf, timezone, tzdb);
+ }
+}
+ tmp->data = NULL;
+ create_zone_index(tmp);
+ system_location_table = create_location_table();
-+ fake_data_segment(tmp, system_location_table);
++ fake_data_segment(tmp, system_location_table);
+ timezonedb_system = tmp;
+ }
+
-+
+ return timezonedb_system;
+#else
return &timezonedb_builtin;
+#endif
}
- const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
- {
-+#ifdef HAVE_SYSTEM_TZDATA
-+ *count = timezonedb_system->index_size;
-+ return timezonedb_system->index;
-+#else
- *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
- return timezonedb_idx_builtin;
-+#endif
- }
-
+ const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
+@@ -414,7 +893,30 @@ const timelib_tzdb_index_entry *timelib_
int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
{
const unsigned char *tzf;
- return (seek_to_tz_position(&tzf, timezone, tzdb));
+
+#ifdef HAVE_SYSTEM_TZDATA
-+ if (tzdb == timezonedb_system) {
-+ char fname[PATH_MAX];
-+ struct stat st;
++ if (tzdb == timezonedb_system) {
++ char fname[PATH_MAX];
++ struct stat st;
+
-+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
-+ return 0;
-+ }
-+
-+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
-+
-+ return stat(fname, &st) == 0 && is_valid_tzfile(&st);
-+ }
++ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
++ return 0;
++ }
++
++ if (system_location_table) {
++ if (find_zone_info(system_location_table, timezone) != NULL) {
++ /* found in cache */
++ return 1;
++ }
++ }
++
++ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
++
++ return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
++ }
+#endif
+
+ return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
}
- timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
+ static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
+@@ -456,12 +958,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
+ timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code)
{
const unsigned char *tzf;
+ char *memmap = NULL;
+ size_t maplen;
timelib_tzinfo *tmp;
+ int version;
+ int transitions_result, types_result;
+ unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
- if (seek_to_tz_position(&tzf, timezone, tzdb)) {
+ if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
tmp = timelib_tzinfo_ctor(timezone);
- read_preamble(&tzf, tmp);
- read_header(&tzf, tmp);
- read_transistions(&tzf, tmp);
- read_types(&tzf, tmp);
-- read_location(&tzf, tmp);
+ version = read_preamble(&tzf, tmp, &type);
+@@ -484,6 +988,29 @@ timelib_tzinfo *timelib_parse_tzfile(cha
+ timelib_tzinfo_dtor(tmp);
+ return NULL;
+ }
+
+#ifdef HAVE_SYSTEM_TZDATA
+ if (memmap) {
+ * if possible. */
+
+ if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
-+ tmp->location.comments = strdup(li->comment);
-+ strncpy(tmp->location.country_code, li->code, 2);
++ tmp->location.comments = timelib_strdup(li->comment);
++ strncpy(tmp->location.country_code, li->code, 2);
+ tmp->location.longitude = li->longitude;
+ tmp->location.latitude = li->latitude;
+ tmp->bc = 1;
+ }
+ else {
-+ strcpy(tmp->location.country_code, "??");
-+ tmp->bc = 0;
-+ tmp->location.comments = strdup("");
++ set_default_location_and_comments(&tzf, tmp);
+ }
+
+ /* Now done with the mmap segment - discard it. */
+ munmap(memmap, maplen);
-+ } else
++ } else {
+#endif
-+ {
-+ /* PHP-style - use the embedded info. */
-+ read_location(&tzf, tmp);
+ if (version == 2 || version == 3) {
+ if (!skip_64bit_preamble(&tzf, tmp)) {
+ /* 64 bit preamble is not in place */
+@@ -501,6 +1028,9 @@ timelib_tzinfo *timelib_parse_tzfile(cha
+ } else {
+ set_default_location_and_comments(&tzf, tmp);
+ }
++#ifdef HAVE_SYSTEM_TZDATA
+ }
++#endif
} else {
+ *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
tmp = NULL;
- }
---- a/ext/date/lib/timelib.m4
-+++ b/ext/date/lib/timelib.m4
-@@ -78,3 +78,17 @@ stdlib.h
-
- dnl Check for strtoll, atoll
- AC_CHECK_FUNCS(strtoll atoll strftime)
-+
-+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
-+[ --with-system-tzdata[=DIR] to specify use of system timezone data],
-+no, no)
-+
-+if test "$PHP_SYSTEM_TZDATA" != "no"; then
-+ AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
-+
-+ if test "$PHP_SYSTEM_TZDATA" != "yes"; then
-+ AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
-+ [Define for location of system timezone data])
-+ fi
-+fi
-+