b444af
# License: MIT
b444af
# http://opensource.org/licenses/MIT
b444af
b444af
Add support for use of the system timezone database, rather
b444af
than embedding a copy.  Discussed upstream but was not desired.
b444af
b444af
History:
240c49
r21: retrieve tzdata version from /usr/share/zoneinfo/tzdata.zi
b444af
r20: adapt for timelib 2020.03 (in 8.0.10RC1)
b444af
r19: adapt for timelib 2020.02 (in 8.0.0beta2)
b444af
r18: adapt for autotool change in 7.3.3RC1
b444af
r17: adapt for timelib 2018.01 (in 7.3.2RC1)
b444af
r16: adapt for timelib 2017.06 (in 7.2.3RC1)
b444af
r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
b444af
r14: improve check for valid tz file
b444af
r13: adapt for upstream changes to use PHP allocator
b444af
r12: adapt for upstream changes for new zic
b444af
r11: use canonical names to avoid more case sensitivity issues
b444af
     round lat/long from zone.tab towards zero per builtin db
b444af
r10: make timezone case insensitive
b444af
r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
b444af
r8: fix compile error without --with-system-tzdata configured
b444af
r7: improve check for valid timezone id to exclude directories
b444af
r6: fix fd leak in r5, fix country code/BC flag use in
b444af
    timezone_identifiers_list() using system db,
b444af
    fix use of PECL timezonedb to override system db,
b444af
r5: reverts addition of "System/Localtime" fake tzname.
b444af
    updated for 5.3.0, parses zone.tab to pick up mapping between
b444af
    timezone name, country code and long/lat coords
b444af
r4: added "System/Localtime" tzname which uses /etc/localtime
b444af
r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
b444af
r2: add filesystem trawl to set up name alias index
b444af
r1: initial revision
b444af
240c49
diff --git a/ext/date/config0.m4 b/ext/date/config0.m4
240c49
index 20e4164aaa..a61243646d 100644
240c49
--- a/ext/date/config0.m4
240c49
+++ b/ext/date/config0.m4
b444af
@@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
b444af
 dnl Check for strtoll, atoll
b444af
 AC_CHECK_FUNCS(strtoll atoll)
b444af
 
b444af
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
b444af
+[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
b444af
+no, no)
b444af
+
b444af
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
b444af
+   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
b444af
+
b444af
+   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
b444af
+      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
b444af
+                         [Define for location of system timezone data])
b444af
+   fi
b444af
+fi
b444af
+
b444af
 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
b444af
 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
b444af
                  lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
240c49
diff --git a/ext/date/lib/parse_tz.c b/ext/date/lib/parse_tz.c
240c49
index e9bd0f136d..c04ff01adc 100644
240c49
--- a/ext/date/lib/parse_tz.c
240c49
+++ b/ext/date/lib/parse_tz.c
b444af
@@ -26,8 +26,21 @@
b444af
 #include "timelib.h"
b444af
 #include "timelib_private.h"
b444af
 
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+#include <sys/mman.h>
b444af
+#include <sys/stat.h>
b444af
+#include <limits.h>
b444af
+#include <fcntl.h>
b444af
+#include <unistd.h>
b444af
+
b444af
+#include "php_scandir.h"
b444af
+
b444af
+#else
b444af
 #define TIMELIB_SUPPORTS_V2DATA
b444af
 #include "timezonedb.h"
b444af
+#endif
b444af
+
b444af
+#include <ctype.h>
b444af
 
b444af
 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
b444af
 # if defined(__LITTLE_ENDIAN__)
240c49
@@ -94,6 +107,11 @@ static int read_php_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
b444af
 {
b444af
 	uint32_t version;
b444af
 
b444af
+	if (memcmp(*tzf, "TZif", 4) == 0) {
b444af
+		*tzf += 20;
b444af
+		return 0;
b444af
+	}
b444af
+
b444af
 	/* read ID */
b444af
 	version = (*tzf)[3] - '0';
b444af
 	*tzf += 4;
240c49
@@ -435,7 +453,467 @@ void timelib_dump_tzinfo(timelib_tzinfo *tz)
b444af
 	}
b444af
 }
b444af
 
b444af
-static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+
b444af
+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
b444af
+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
b444af
+#else
b444af
+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
b444af
+#endif
b444af
+
b444af
+/* System timezone database pointer. */
b444af
+static const timelib_tzdb *timezonedb_system;
b444af
+
b444af
+/* Hash table entry for the cache of the zone.tab mapping table. */
b444af
+struct location_info {
b444af
+        char code[2];
b444af
+        double latitude, longitude;
b444af
+        char name[64];
b444af
+        char *comment;
b444af
+        struct location_info *next;
b444af
+};
b444af
+
b444af
+/* Cache of zone.tab. */
b444af
+static struct location_info **system_location_table;
b444af
+
b444af
+/* Size of the zone.tab hash table; a random-ish prime big enough to
b444af
+ * prevent too many collisions. */
b444af
+#define LOCINFO_HASH_SIZE (1021)
b444af
+
b444af
+/* Compute a case insensitive hash of str */
b444af
+static uint32_t tz_hash(const char *str)
b444af
+{
b444af
+    const unsigned char *p = (const unsigned char *)str;
b444af
+    uint32_t hash = 5381;
b444af
+    int c;
b444af
+
b444af
+    while ((c = tolower(*p++)) != '\0') {
b444af
+        hash = (hash << 5) ^ hash ^ c;
b444af
+    }
b444af
+
b444af
+    return hash % LOCINFO_HASH_SIZE;
b444af
+}
b444af
+
b444af
+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
b444af
+ * parsed string on success, or NULL on parse error.  On success,
b444af
+ * writes the parsed number to *result. */
b444af
+static char *parse_iso6709(char *p, double *result)
b444af
+{
b444af
+    double v, sign;
b444af
+    char *pend;
b444af
+    size_t len;
b444af
+
b444af
+    if (*p == '+')
b444af
+        sign = 1.0;
b444af
+    else if (*p == '-')
b444af
+        sign = -1.0;
b444af
+    else
b444af
+        return NULL;
b444af
+
b444af
+    p++;
b444af
+    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
b444af
+        ;;
b444af
+
b444af
+    /* Annoying encoding used by zone.tab has no decimal point, so use
b444af
+     * the length to determine the format:
b444af
+     * 
b444af
+     * 4 = DDMM
b444af
+     * 5 = DDDMM
b444af
+     * 6 = DDMMSS
b444af
+     * 7 = DDDMMSS
b444af
+     */
b444af
+    len = pend - p;
b444af
+    if (len < 4 || len > 7) {
b444af
+        return NULL;
b444af
+    }
b444af
+
b444af
+    /* p => [D]DD */
b444af
+    v = (p[0] - '0') * 10.0 + (p[1] - '0');
b444af
+    p += 2;
b444af
+    if (len == 5 || len == 7)
b444af
+        v = v * 10.0 + (*p++ - '0');
b444af
+    /* p => MM[SS] */
b444af
+    v += (10.0 * (p[0] - '0')
b444af
+          + p[1] - '0') / 60.0;
b444af
+    p += 2;
b444af
+    /* p => [SS] */
b444af
+    if (len > 5) {
b444af
+        v += (10.0 * (p[0] - '0')
b444af
+              + p[1] - '0') / 3600.0;
b444af
+        p += 2;
b444af
+    }
b444af
+
b444af
+    /* Round to five decimal place, not because it's a good idea,
b444af
+     * but, because the builtin data uses rounded data, so, match
b444af
+     * that. */
b444af
+    *result = trunc(v * sign * 100000.0) / 100000.0;
b444af
+
b444af
+    return p;
b444af
+}
b444af
+
b444af
+/* This function parses the zone.tab file to build up the mapping of
b444af
+ * timezone to country code and geographic location, and returns a
b444af
+ * hash table.  The hash table is indexed by the function:
b444af
+ *
b444af
+ *   tz_hash(timezone-name)
b444af
+ */
b444af
+static struct location_info **create_location_table(void)
b444af
+{
b444af
+    struct location_info **li, *i;
b444af
+    char zone_tab[PATH_MAX];
b444af
+    char line[512];
b444af
+    FILE *fp;
b444af
+
b444af
+    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
b444af
+
b444af
+    fp = fopen(zone_tab, "r");
b444af
+    if (!fp) {
b444af
+        return NULL;
b444af
+    }
b444af
+
b444af
+    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
b444af
+
b444af
+    while (fgets(line, sizeof line, fp)) {
b444af
+        char *p = line, *code, *name, *comment;
b444af
+        uint32_t hash;
b444af
+        double latitude, longitude;
b444af
+
b444af
+        while (isspace(*p))
b444af
+            p++;
b444af
+
b444af
+        if (*p == '#' || *p == '\0' || *p == '\n')
b444af
+            continue;
b444af
+        
b444af
+        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
b444af
+            continue;
b444af
+        
b444af
+        /* code => AA */
b444af
+        code = p;
b444af
+        p[2] = 0;
b444af
+        p += 3;
b444af
+
b444af
+        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
b444af
+        p = parse_iso6709(p, &latitude);
b444af
+        if (!p) {
b444af
+            continue;
b444af
+        }
b444af
+        p = parse_iso6709(p, &longitude);
b444af
+        if (!p) {
b444af
+            continue;
b444af
+        }
b444af
+
b444af
+        if (!p || *p != '\t') {
b444af
+            continue;
b444af
+        }
b444af
+
b444af
+        /* name = string */
b444af
+        name = ++p;
b444af
+        while (*p != '\t' && *p && *p != '\n')
b444af
+            p++;
b444af
+
b444af
+        *p++ = '\0';
b444af
+
b444af
+        /* comment = string */
b444af
+        comment = p;
b444af
+        while (*p != '\t' && *p && *p != '\n')
b444af
+            p++;
b444af
+
b444af
+        if (*p == '\n' || *p == '\t')
b444af
+            *p = '\0';
b444af
+        
b444af
+        hash = tz_hash(name);
b444af
+        i = malloc(sizeof *i);
b444af
+        memcpy(i->code, code, 2);
b444af
+        strncpy(i->name, name, sizeof i->name);
b444af
+        i->comment = strdup(comment);
b444af
+        i->longitude = longitude;
b444af
+        i->latitude = latitude;
b444af
+        i->next = li[hash];
b444af
+        li[hash] = i;
b444af
+        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
b444af
+    }
b444af
+
b444af
+    fclose(fp);
b444af
+
b444af
+    return li;
b444af
+}
b444af
+
b444af
+/* Return location info from hash table, using given timezone name.
b444af
+ * Returns NULL if the name could not be found. */
b444af
+const struct location_info *find_zone_info(struct location_info **li, 
b444af
+                                           const char *name)
b444af
+{
b444af
+    uint32_t hash = tz_hash(name);
b444af
+    const struct location_info *l;
b444af
+
b444af
+    if (!li) {
b444af
+        return NULL;
b444af
+    }
b444af
+
b444af
+    for (l = li[hash]; l; l = l->next) {
b444af
+        if (timelib_strcasecmp(l->name, name) == 0)
b444af
+            return l;
b444af
+    }
b444af
+
b444af
+    return NULL;
b444af
+}    
b444af
+
b444af
+/* Filter out some non-tzdata files and the posix/right databases, if
b444af
+ * present. */
b444af
+static int index_filter(const struct dirent *ent)
b444af
+{
b444af
+	return strcmp(ent->d_name, ".") != 0
b444af
+		&& strcmp(ent->d_name, "..") != 0
b444af
+		&& strcmp(ent->d_name, "posix") != 0
b444af
+		&& strcmp(ent->d_name, "posixrules") != 0
b444af
+		&& strcmp(ent->d_name, "right") != 0
b444af
+		&& strstr(ent->d_name, ".list") == NULL
b444af
+		&& strstr(ent->d_name, ".tab") == NULL;
b444af
+}
b444af
+
b444af
+static int sysdbcmp(const void *first, const void *second)
b444af
+{
b444af
+        const timelib_tzdb_index_entry *alpha = first, *beta = second;
b444af
+
b444af
+        return timelib_strcasecmp(alpha->id, beta->id);
b444af
+}
b444af
+
b444af
+
240c49
+/* Retrieve tzdata version. */
240c49
+static void retrieve_zone_version(timelib_tzdb *db)
240c49
+{
240c49
+    static char buf[30];
240c49
+    char path[PATH_MAX];
240c49
+    FILE *fp;
240c49
+
240c49
+    strncpy(path, ZONEINFO_PREFIX "/tzdata.zi", sizeof(path));
240c49
+
240c49
+    fp = fopen(path, "r");
240c49
+    if (fp) {
240c49
+		if (fgets(buf, sizeof(buf), fp)) {
240c49
+			if (!memcmp(buf, "# version ", 10) &&
240c49
+				isdigit(buf[10]) &&
240c49
+				isdigit(buf[11]) &&
240c49
+				isdigit(buf[12]) &&
240c49
+				isdigit(buf[13]) &&
240c49
+				islower(buf[14])) {
240c49
+				if (buf[14] >= 't') {        /* 2022t = 2022.20 */
240c49
+					buf[17] = 0;
240c49
+					buf[16] = buf[14] - 't' + '0';
240c49
+					buf[15] = '2';
240c49
+				} else if (buf[14] >= 'j') { /* 2022j = 2022.10 */
240c49
+					buf[17] = 0;
240c49
+					buf[16] = buf[14] - 'j' + '0';
240c49
+					buf[15] = '1';
240c49
+				} else {                     /* 2022a = 2022.1  */
240c49
+					buf[16] = 0;
240c49
+					buf[15] = buf[14] - 'a' + '1';
240c49
+				}
240c49
+				buf[14] = '.';
240c49
+				db->version = buf+10;
240c49
+			}
240c49
+		}
240c49
+		fclose(fp);
240c49
+    }
240c49
+}
240c49
+
b444af
+/* Create the zone identifier index by trawling the filesystem. */
b444af
+static void create_zone_index(timelib_tzdb *db)
b444af
+{
b444af
+	size_t dirstack_size,  dirstack_top;
b444af
+	size_t index_size, index_next;
b444af
+	timelib_tzdb_index_entry *db_index;
b444af
+	char **dirstack;
b444af
+
b444af
+	/* LIFO stack to hold directory entries to scan; each slot is a
b444af
+	 * directory name relative to the zoneinfo prefix. */
b444af
+	dirstack_size = 32;
b444af
+	dirstack = malloc(dirstack_size * sizeof *dirstack);
b444af
+	dirstack_top = 1;
b444af
+	dirstack[0] = strdup("");
b444af
+	
b444af
+	/* Index array. */
b444af
+	index_size = 64;
b444af
+	db_index = malloc(index_size * sizeof *db_index);
b444af
+	index_next = 0;
b444af
+
b444af
+	do {
b444af
+		struct dirent **ents;
b444af
+		char name[PATH_MAX], *top;
b444af
+		int count;
b444af
+
b444af
+		/* Pop the top stack entry, and iterate through its contents. */
b444af
+		top = dirstack[--dirstack_top];
b444af
+		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
b444af
+
b444af
+		count = php_scandir(name, &ents, index_filter, php_alphasort);
b444af
+
b444af
+		while (count > 0) {
b444af
+			struct stat st;
b444af
+			const char *leaf = ents[count - 1]->d_name;
b444af
+
b444af
+			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
b444af
+				 top, leaf);
b444af
+			
b444af
+			if (strlen(name) && stat(name, &st) == 0) {
b444af
+				/* Name, relative to the zoneinfo prefix. */
b444af
+				const char *root = top;
b444af
+
b444af
+				if (root[0] == '/') root++;
b444af
+
b444af
+				snprintf(name, sizeof name, "%s%s%s", root, 
b444af
+					 *root ? "/": "", leaf);
b444af
+
b444af
+				if (S_ISDIR(st.st_mode)) {
b444af
+					if (dirstack_top == dirstack_size) {
b444af
+						dirstack_size *= 2;
b444af
+						dirstack = realloc(dirstack, 
b444af
+								   dirstack_size * sizeof *dirstack);
b444af
+					}
b444af
+					dirstack[dirstack_top++] = strdup(name);
b444af
+				}
b444af
+				else {
b444af
+					if (index_next == index_size) {
b444af
+						index_size *= 2;
b444af
+						db_index = realloc(db_index,
b444af
+								   index_size * sizeof *db_index);
b444af
+					}
b444af
+
b444af
+					db_index[index_next++].id = strdup(name);
b444af
+				}
b444af
+			}
b444af
+
b444af
+			free(ents[--count]);
b444af
+		}
b444af
+		
b444af
+		if (count != -1) free(ents);
b444af
+		free(top);
b444af
+	} while (dirstack_top);
b444af
+
b444af
+        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
b444af
+
b444af
+	db->index = db_index;
b444af
+	db->index_size = index_next;
b444af
+
b444af
+	free(dirstack);
b444af
+}
b444af
+
b444af
+#define FAKE_HEADER "1234\0??\1??"
b444af
+#define FAKE_UTC_POS (7 - 4)
b444af
+
b444af
+/* Create a fake data segment for database 'sysdb'. */
b444af
+static void fake_data_segment(timelib_tzdb *sysdb,
b444af
+                              struct location_info **info)
b444af
+{
b444af
+        size_t n;
b444af
+        char *data, *p;
b444af
+        
b444af
+        data = malloc(3 * sysdb->index_size + 7);
b444af
+
b444af
+        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
b444af
+
b444af
+        for (n = 0; n < sysdb->index_size; n++) {
b444af
+                const struct location_info *li;
b444af
+                timelib_tzdb_index_entry *ent;
b444af
+
b444af
+                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
b444af
+
b444af
+                /* Lookup the timezone name in the hash table. */
b444af
+                if (strcmp(ent->id, "UTC") == 0) {
b444af
+                        ent->pos = FAKE_UTC_POS;
b444af
+                        continue;
b444af
+                }
b444af
+
b444af
+                li = find_zone_info(info, ent->id);
b444af
+                if (li) {
b444af
+                        /* If found, append the BC byte and the
b444af
+                         * country code; set the position for this
b444af
+                         * section of timezone data.  */
b444af
+                        ent->pos = (p - data) - 4;
b444af
+                        *p++ = '\1';
b444af
+                        *p++ = li->code[0];
b444af
+                        *p++ = li->code[1];
b444af
+                }
b444af
+                else {
b444af
+                        /* If not found, the timezone data can
b444af
+                         * point at the header. */
b444af
+                        ent->pos = 0;
b444af
+                }
b444af
+        }
b444af
+        
b444af
+        sysdb->data = (unsigned char *)data;
b444af
+}
b444af
+
b444af
+/* Returns true if the passed-in stat structure describes a
b444af
+ * probably-valid timezone file. */
b444af
+static int is_valid_tzfile(const struct stat *st, int fd)
b444af
+{
b444af
+	if (fd) {
b444af
+		char buf[20];
b444af
+		if (read(fd, buf, 20)!=20) {
b444af
+			return 0;
b444af
+		}
b444af
+		lseek(fd, SEEK_SET, 0);
b444af
+		if (memcmp(buf, "TZif", 4)) {
b444af
+			return 0;
b444af
+		}
b444af
+	}
b444af
+	return S_ISREG(st->st_mode) && st->st_size > 20;
b444af
+}
b444af
+
b444af
+/* To allow timezone names to be used case-insensitively, find the
b444af
+ * canonical name for this timezone, if possible. */
b444af
+static const char *canonical_tzname(const char *timezone)
b444af
+{
b444af
+    if (timezonedb_system) {
b444af
+        timelib_tzdb_index_entry *ent, lookup;
b444af
+
b444af
+        lookup.id = (char *)timezone;
b444af
+
b444af
+        ent = bsearch(&lookup, timezonedb_system->index,
b444af
+                      timezonedb_system->index_size, sizeof lookup,
b444af
+                      sysdbcmp);
b444af
+        if (ent) {
b444af
+            return ent->id;
b444af
+        }
b444af
+    }
b444af
+
b444af
+    return timezone;
b444af
+}
b444af
+
b444af
+/* Return the mmap()ed tzfile if found, else NULL.  On success, the
b444af
+ * length of the mapped data is placed in *length. */
b444af
+static char *map_tzfile(const char *timezone, size_t *length)
b444af
+{
b444af
+	char fname[PATH_MAX];
b444af
+	struct stat st;
b444af
+	char *p;
b444af
+	int fd;
b444af
+	
b444af
+	if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
b444af
+		return NULL;
b444af
+	}
b444af
+
b444af
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
b444af
+
b444af
+	fd = open(fname, O_RDONLY);
b444af
+	if (fd == -1) {
b444af
+		return NULL;
b444af
+	} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
b444af
+		close(fd);
b444af
+		return NULL;
b444af
+	}
b444af
+
b444af
+	*length = st.st_size;
b444af
+	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
b444af
+	close(fd);
b444af
+	
b444af
+	return p != MAP_FAILED ? p : NULL;
b444af
+}
b444af
+
b444af
+#endif
b444af
+
b444af
+static int inmem_seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
b444af
 {
b444af
 	int left = 0, right = tzdb->index_size - 1;
b444af
 
240c49
@@ -461,9 +939,49 @@ static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
b444af
 	return 0;
b444af
 }
b444af
 
b444af
+static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
b444af
+			       char **map, size_t *maplen,
b444af
+			       const timelib_tzdb *tzdb)
b444af
+{
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+	if (tzdb == timezonedb_system) {
b444af
+		char *orig;
b444af
+
b444af
+		orig = map_tzfile(timezone, maplen);
b444af
+		if (orig == NULL) {
b444af
+			return 0;
b444af
+		}
b444af
+
b444af
+		(*tzf) = (unsigned char *)orig;
b444af
+		*map = orig;
b444af
+        return 1;
b444af
+	}
b444af
+	else
b444af
+#endif
b444af
+	{
b444af
+		return inmem_seek_to_tz_position(tzf, timezone, tzdb);
b444af
+	}
b444af
+}
b444af
+
b444af
 const timelib_tzdb *timelib_builtin_db(void)
b444af
 {
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+	if (timezonedb_system == NULL) {
b444af
+		timelib_tzdb *tmp = malloc(sizeof *tmp);
b444af
+
b444af
+		tmp->version = "0.system";
b444af
+		tmp->data = NULL;
b444af
+		create_zone_index(tmp);
240c49
+		retrieve_zone_version(tmp);
b444af
+		system_location_table = create_location_table();
b444af
+		fake_data_segment(tmp, system_location_table);
b444af
+		timezonedb_system = tmp;
b444af
+	}
b444af
+
b444af
+	return timezonedb_system;
b444af
+#else
b444af
 	return &timezonedb_builtin;
b444af
+#endif
b444af
 }
b444af
 
b444af
 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
240c49
@@ -475,7 +993,30 @@ const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_
b444af
 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
b444af
 {
b444af
 	const unsigned char *tzf;
b444af
-	return (seek_to_tz_position(&tzf, timezone, tzdb));
b444af
+
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+	if (tzdb == timezonedb_system) {
b444af
+		char fname[PATH_MAX];
b444af
+		struct stat st;
b444af
+
b444af
+		if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
b444af
+			return 0;
b444af
+		}
b444af
+
b444af
+		if (system_location_table) {
b444af
+			if (find_zone_info(system_location_table, timezone) != NULL) {
b444af
+				/* found in cache */
b444af
+				return 1;
b444af
+			}
b444af
+		}
b444af
+
b444af
+		snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
b444af
+
b444af
+		return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
b444af
+	}
b444af
+#endif
b444af
+
b444af
+	return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
b444af
 }
b444af
 
b444af
 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
240c49
@@ -517,6 +1058,8 @@ static timelib_tzinfo* timelib_tzinfo_ctor(const char *name)
b444af
 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
b444af
 {
b444af
 	const unsigned char *tzf;
b444af
+	char *memmap = NULL;
b444af
+	size_t maplen;
b444af
 	timelib_tzinfo *tmp;
b444af
 	int version;
b444af
 	int transitions_result, types_result;
240c49
@@ -524,7 +1067,7 @@ timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *t
b444af
 
b444af
 	*error_code = TIMELIB_ERROR_NO_ERROR;
b444af
 
b444af
-	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
b444af
+	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
b444af
 		tmp = timelib_tzinfo_ctor(timezone);
b444af
 
b444af
 		version = read_preamble(&tzf, tmp, &type);
240c49
@@ -563,11 +1106,36 @@ timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *t
b444af
 		}
b444af
 		skip_posix_string(&tzf, tmp);
b444af
 
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+		if (memmap) {
b444af
+			const struct location_info *li;
b444af
+
b444af
+			/* TZif-style - grok the location info from the system database,
b444af
+			 * if possible. */
b444af
+
b444af
+			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
b444af
+				tmp->location.comments = timelib_strdup(li->comment);
b444af
+				strncpy(tmp->location.country_code, li->code, 2);
b444af
+				tmp->location.longitude = li->longitude;
b444af
+				tmp->location.latitude = li->latitude;
b444af
+				tmp->bc = 1;
b444af
+			}
b444af
+			else {
b444af
+				set_default_location_and_comments(&tzf, tmp);
b444af
+			}
b444af
+
b444af
+			/* Now done with the mmap segment - discard it. */
b444af
+			munmap(memmap, maplen);
b444af
+		} else {
b444af
+#endif
b444af
 		if (type == TIMELIB_TZINFO_PHP) {
b444af
 			read_location(&tzf, tmp);
b444af
 		} else {
b444af
 			set_default_location_and_comments(&tzf, tmp);
b444af
 		}
b444af
+#ifdef HAVE_SYSTEM_TZDATA
b444af
+		}
b444af
+#endif
b444af
 	} else {
b444af
 		*error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
b444af
 		tmp = NULL;
240c49
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
240c49
index 2d5cffb963..389f09f313 100644
240c49
--- a/ext/date/php_date.c
240c49
+++ b/ext/date/php_date.c
240c49
@@ -457,7 +457,11 @@ PHP_MINFO_FUNCTION(date)
240c49
 	php_info_print_table_row(2, "date/time support", "enabled");
240c49
 	php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
240c49
 	php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
240c49
+#ifdef HAVE_SYSTEM_TZDATA
240c49
+	php_info_print_table_row(2, "Timezone Database", "system");
240c49
+#else
240c49
 	php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
240c49
+#endif
240c49
 	php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
240c49
 	php_info_print_table_end();
240c49