af9dc8
Add support for use of the system timezone database, rather
af9dc8
than embedding a copy.  Discussed upstream but was not desired.
af9dc8
af9dc8
History:
af9dc8
r10 : make timezone case insensitive
af9dc8
r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
af9dc8
r8: fix compile error without --with-system-tzdata configured
af9dc8
r7: improve check for valid timezone id to exclude directories
af9dc8
r6: fix fd leak in r5, fix country code/BC flag use in 
af9dc8
    timezone_identifiers_list() using system db,
af9dc8
    fix use of PECL timezonedb to override system db,
af9dc8
r5: reverts addition of "System/Localtime" fake tzname.
af9dc8
    updated for 5.3.0, parses zone.tab to pick up mapping between
af9dc8
    timezone name, country code and long/lat coords
af9dc8
r4: added "System/Localtime" tzname which uses /etc/localtime
af9dc8
r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
af9dc8
r2: add filesystem trawl to set up name alias index
af9dc8
r1: initial revision
af9dc8
af9dc8
--- a/ext/date/lib/parse_tz.c
af9dc8
+++ b/ext/date/lib/parse_tz.c
af9dc8
@@ -20,6 +20,16 @@
af9dc8
 
af9dc8
 #include "timelib.h"
af9dc8
 
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+#include <sys/mman.h>
af9dc8
+#include <sys/stat.h>
af9dc8
+#include <limits.h>
af9dc8
+#include <fcntl.h>
af9dc8
+#include <unistd.h>
af9dc8
+
af9dc8
+#include "php_scandir.h"
af9dc8
+#endif
af9dc8
+
af9dc8
 #include <stdio.h>
af9dc8
 
af9dc8
 #ifdef HAVE_LOCALE_H
af9dc8
@@ -31,7 +41,12 @@
af9dc8
 #else
af9dc8
 #include <strings.h>
af9dc8
 #endif
af9dc8
+
af9dc8
+#ifndef HAVE_SYSTEM_TZDATA
af9dc8
 #include "timezonedb.h"
af9dc8
+#endif
af9dc8
+
af9dc8
+#include <ctype.h>
af9dc8
 
af9dc8
 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
af9dc8
 # if defined(__LITTLE_ENDIAN__)
af9dc8
@@ -51,9 +66,14 @@
af9dc8
 
af9dc8
 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
af9dc8
 {
af9dc8
-	/* skip ID */
af9dc8
-	*tzf += 4;
af9dc8
-	
af9dc8
+        if (memcmp(tzf, "TZif", 4) == 0) {
af9dc8
+                *tzf += 20;
af9dc8
+                return;
af9dc8
+        }
af9dc8
+        
af9dc8
+        /* skip ID */
af9dc8
+        *tzf += 4;
af9dc8
+                
af9dc8
 	/* read BC flag */
af9dc8
 	tz->bc = (**tzf == '\1');
af9dc8
 	*tzf += 1;
af9dc8
@@ -256,7 +276,405 @@
af9dc8
 	}
af9dc8
 }
af9dc8
 
af9dc8
-static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
af9dc8
+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
af9dc8
+#else
af9dc8
+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
af9dc8
+#endif
af9dc8
+
af9dc8
+/* System timezone database pointer. */
af9dc8
+static const timelib_tzdb *timezonedb_system;
af9dc8
+
af9dc8
+/* Hash table entry for the cache of the zone.tab mapping table. */
af9dc8
+struct location_info {
af9dc8
+        char code[2];
af9dc8
+        double latitude, longitude;
af9dc8
+        char name[64];
af9dc8
+        char *comment;
af9dc8
+        struct location_info *next;
af9dc8
+};
af9dc8
+
af9dc8
+/* Cache of zone.tab. */
af9dc8
+static struct location_info **system_location_table;
af9dc8
+
af9dc8
+/* Size of the zone.tab hash table; a random-ish prime big enough to
af9dc8
+ * prevent too many collisions. */
af9dc8
+#define LOCINFO_HASH_SIZE (1021)
af9dc8
+
af9dc8
+/* Compute a case insensitive hash of str */
af9dc8
+static uint32_t tz_hash(const char *str)
af9dc8
+{
af9dc8
+    const unsigned char *p = (const unsigned char *)str;
af9dc8
+    uint32_t hash = 5381;
af9dc8
+    int c;
af9dc8
+    
af9dc8
+    while ((c = tolower(*p++)) != '\0') {
af9dc8
+        hash = (hash << 5) ^ hash ^ c;
af9dc8
+    }
af9dc8
+    
af9dc8
+    return hash % LOCINFO_HASH_SIZE;
af9dc8
+}
af9dc8
+
af9dc8
+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
af9dc8
+ * parsed string on success, or NULL on parse error.  On success,
af9dc8
+ * writes the parsed number to *result. */
af9dc8
+static char *parse_iso6709(char *p, double *result)
af9dc8
+{
af9dc8
+    double v, sign;
af9dc8
+    char *pend;
af9dc8
+    size_t len;
af9dc8
+
af9dc8
+    if (*p == '+')
af9dc8
+        sign = 1.0;
af9dc8
+    else if (*p == '-')
af9dc8
+        sign = -1.0;
af9dc8
+    else
af9dc8
+        return NULL;
af9dc8
+
af9dc8
+    p++;
af9dc8
+    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
af9dc8
+        ;;
af9dc8
+
af9dc8
+    /* Annoying encoding used by zone.tab has no decimal point, so use
af9dc8
+     * the length to determine the format:
af9dc8
+     * 
af9dc8
+     * 4 = DDMM
af9dc8
+     * 5 = DDDMM
af9dc8
+     * 6 = DDMMSS
af9dc8
+     * 7 = DDDMMSS
af9dc8
+     */
af9dc8
+    len = pend - p;
af9dc8
+    if (len < 4 || len > 7) {
af9dc8
+        return NULL;
af9dc8
+    }
af9dc8
+
af9dc8
+    /* p => [D]DD */
af9dc8
+    v = (p[0] - '0') * 10.0 + (p[1] - '0');
af9dc8
+    p += 2;
af9dc8
+    if (len == 5 || len == 7)
af9dc8
+        v = v * 10.0 + (*p++ - '0');
af9dc8
+    /* p => MM[SS] */
af9dc8
+    v += (10.0 * (p[0] - '0')
af9dc8
+          + p[1] - '0') / 60.0;
af9dc8
+    p += 2;
af9dc8
+    /* p => [SS] */
af9dc8
+    if (len > 5) {
af9dc8
+        v += (10.0 * (p[0] - '0')
af9dc8
+              + p[1] - '0') / 3600.0;
af9dc8
+        p += 2;
af9dc8
+    }
af9dc8
+
af9dc8
+    /* Round to five decimal place, not because it's a good idea,
af9dc8
+     * but, because the builtin data uses rounded data, so, match
af9dc8
+     * that. */
af9dc8
+    *result = round(v * sign * 100000.0) / 100000.0;
af9dc8
+
af9dc8
+    return p;
af9dc8
+}
af9dc8
+
af9dc8
+/* This function parses the zone.tab file to build up the mapping of
af9dc8
+ * timezone to country code and geographic location, and returns a
af9dc8
+ * hash table.  The hash table is indexed by the function:
af9dc8
+ *
af9dc8
+ *   tz_hash(timezone-name)
af9dc8
+ */
af9dc8
+static struct location_info **create_location_table(void)
af9dc8
+{
af9dc8
+    struct location_info **li, *i;
af9dc8
+    char zone_tab[PATH_MAX];
af9dc8
+    char line[512];
af9dc8
+    FILE *fp;
af9dc8
+
af9dc8
+    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
af9dc8
+
af9dc8
+    fp = fopen(zone_tab, "r");
af9dc8
+    if (!fp) {
af9dc8
+        return NULL;
af9dc8
+    }
af9dc8
+
af9dc8
+    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
af9dc8
+
af9dc8
+    while (fgets(line, sizeof line, fp)) {
af9dc8
+        char *p = line, *code, *name, *comment;
af9dc8
+        uint32_t hash;
af9dc8
+        double latitude, longitude;
af9dc8
+
af9dc8
+        while (isspace(*p))
af9dc8
+            p++;
af9dc8
+
af9dc8
+        if (*p == '#' || *p == '\0' || *p == '\n')
af9dc8
+            continue;
af9dc8
+        
af9dc8
+        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
af9dc8
+            continue;
af9dc8
+        
af9dc8
+        /* code => AA */
af9dc8
+        code = p;
af9dc8
+        p[2] = 0;
af9dc8
+        p += 3;
af9dc8
+
af9dc8
+        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
af9dc8
+        p = parse_iso6709(p, &latitude);
af9dc8
+        if (!p) {
af9dc8
+            continue;
af9dc8
+        }
af9dc8
+        p = parse_iso6709(p, &longitude);
af9dc8
+        if (!p) {
af9dc8
+            continue;
af9dc8
+        }
af9dc8
+
af9dc8
+        if (!p || *p != '\t') {
af9dc8
+            continue;
af9dc8
+        }
af9dc8
+
af9dc8
+        /* name = string */
af9dc8
+        name = ++p;
af9dc8
+        while (*p != '\t' && *p && *p != '\n')
af9dc8
+            p++;
af9dc8
+
af9dc8
+        *p++ = '\0';
af9dc8
+
af9dc8
+        /* comment = string */
af9dc8
+        comment = p;
af9dc8
+        while (*p != '\t' && *p && *p != '\n')
af9dc8
+            p++;
af9dc8
+
af9dc8
+        if (*p == '\n' || *p == '\t')
af9dc8
+            *p = '\0';
af9dc8
+        
af9dc8
+        hash = tz_hash(name);
af9dc8
+        i = malloc(sizeof *i);
af9dc8
+        memcpy(i->code, code, 2);
af9dc8
+        strncpy(i->name, name, sizeof i->name);
af9dc8
+        i->comment = strdup(comment);
af9dc8
+        i->longitude = longitude;
af9dc8
+        i->latitude = latitude;
af9dc8
+        i->next = li[hash];
af9dc8
+        li[hash] = i;
af9dc8
+        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
af9dc8
+    }
af9dc8
+
af9dc8
+    fclose(fp);
af9dc8
+
af9dc8
+    return li;
af9dc8
+}
af9dc8
+
af9dc8
+/* Return location info from hash table, using given timezone name.
af9dc8
+ * Returns NULL if the name could not be found. */
af9dc8
+const struct location_info *find_zone_info(struct location_info **li, 
af9dc8
+                                           const char *name)
af9dc8
+{
af9dc8
+    uint32_t hash = tz_hash(name);
af9dc8
+    const struct location_info *l;
af9dc8
+
af9dc8
+    if (!li) {
af9dc8
+        return NULL;
af9dc8
+    }
af9dc8
+
af9dc8
+    for (l = li[hash]; l; l = l->next) {
af9dc8
+        if (strcasecmp(l->name, name) == 0)
af9dc8
+            return l;
af9dc8
+    }
af9dc8
+
af9dc8
+    return NULL;
af9dc8
+}    
af9dc8
+
af9dc8
+/* Filter out some non-tzdata files and the posix/right databases, if
af9dc8
+ * present. */
af9dc8
+static int index_filter(const struct dirent *ent)
af9dc8
+{
af9dc8
+	return strcmp(ent->d_name, ".") != 0
af9dc8
+		&& strcmp(ent->d_name, "..") != 0
af9dc8
+		&& strcmp(ent->d_name, "posix") != 0
af9dc8
+		&& strcmp(ent->d_name, "posixrules") != 0
af9dc8
+		&& strcmp(ent->d_name, "right") != 0
af9dc8
+		&& strstr(ent->d_name, ".tab") == NULL;
af9dc8
+}
af9dc8
+
af9dc8
+static int sysdbcmp(const void *first, const void *second)
af9dc8
+{
af9dc8
+        const timelib_tzdb_index_entry *alpha = first, *beta = second;
af9dc8
+
af9dc8
+        return strcmp(alpha->id, beta->id);
af9dc8
+}
af9dc8
+
af9dc8
+
af9dc8
+/* Create the zone identifier index by trawling the filesystem. */
af9dc8
+static void create_zone_index(timelib_tzdb *db)
af9dc8
+{
af9dc8
+	size_t dirstack_size,  dirstack_top;
af9dc8
+	size_t index_size, index_next;
af9dc8
+	timelib_tzdb_index_entry *db_index;
af9dc8
+	char **dirstack;
af9dc8
+
af9dc8
+	/* LIFO stack to hold directory entries to scan; each slot is a
af9dc8
+	 * directory name relative to the zoneinfo prefix. */
af9dc8
+	dirstack_size = 32;
af9dc8
+	dirstack = malloc(dirstack_size * sizeof *dirstack);
af9dc8
+	dirstack_top = 1;
af9dc8
+	dirstack[0] = strdup("");
af9dc8
+	
af9dc8
+	/* Index array. */
af9dc8
+	index_size = 64;
af9dc8
+	db_index = malloc(index_size * sizeof *db_index);
af9dc8
+	index_next = 0;
af9dc8
+
af9dc8
+	do {
af9dc8
+		struct dirent **ents;
af9dc8
+		char name[PATH_MAX], *top;
af9dc8
+		int count;
af9dc8
+
af9dc8
+		/* Pop the top stack entry, and iterate through its contents. */
af9dc8
+		top = dirstack[--dirstack_top];
af9dc8
+		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
af9dc8
+
af9dc8
+		count = php_scandir(name, &ents, index_filter, php_alphasort);
af9dc8
+
af9dc8
+		while (count > 0) {
af9dc8
+			struct stat st;
af9dc8
+			const char *leaf = ents[count - 1]->d_name;
af9dc8
+
af9dc8
+			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
af9dc8
+				 top, leaf);
af9dc8
+			
af9dc8
+			if (strlen(name) && stat(name, &st) == 0) {
af9dc8
+				/* Name, relative to the zoneinfo prefix. */
af9dc8
+				const char *root = top;
af9dc8
+
af9dc8
+				if (root[0] == '/') root++;
af9dc8
+
af9dc8
+				snprintf(name, sizeof name, "%s%s%s", root, 
af9dc8
+					 *root ? "/": "", leaf);
af9dc8
+
af9dc8
+				if (S_ISDIR(st.st_mode)) {
af9dc8
+					if (dirstack_top == dirstack_size) {
af9dc8
+						dirstack_size *= 2;
af9dc8
+						dirstack = realloc(dirstack, 
af9dc8
+								   dirstack_size * sizeof *dirstack);
af9dc8
+					}
af9dc8
+					dirstack[dirstack_top++] = strdup(name);
af9dc8
+				}
af9dc8
+				else {
af9dc8
+					if (index_next == index_size) {
af9dc8
+						index_size *= 2;
af9dc8
+						db_index = realloc(db_index,
af9dc8
+								   index_size * sizeof *db_index);
af9dc8
+					}
af9dc8
+
af9dc8
+					db_index[index_next++].id = strdup(name);
af9dc8
+				}
af9dc8
+			}
af9dc8
+
af9dc8
+			free(ents[--count]);
af9dc8
+		}
af9dc8
+		
af9dc8
+		if (count != -1) free(ents);
af9dc8
+		free(top);
af9dc8
+	} while (dirstack_top);
af9dc8
+
af9dc8
+        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
af9dc8
+
af9dc8
+	db->index = db_index;
af9dc8
+	db->index_size = index_next;
af9dc8
+
af9dc8
+	free(dirstack);
af9dc8
+}
af9dc8
+
af9dc8
+#define FAKE_HEADER "1234\0??\1??"
af9dc8
+#define FAKE_UTC_POS (7 - 4)
af9dc8
+
af9dc8
+/* Create a fake data segment for database 'sysdb'. */
af9dc8
+static void fake_data_segment(timelib_tzdb *sysdb,
af9dc8
+                              struct location_info **info)
af9dc8
+{
af9dc8
+        size_t n;
af9dc8
+        char *data, *p;
af9dc8
+        
af9dc8
+        data = malloc(3 * sysdb->index_size + 7);
af9dc8
+
af9dc8
+        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
af9dc8
+
af9dc8
+        for (n = 0; n < sysdb->index_size; n++) {
af9dc8
+                const struct location_info *li;
af9dc8
+                timelib_tzdb_index_entry *ent;
af9dc8
+
af9dc8
+                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
af9dc8
+
af9dc8
+                /* Lookup the timezone name in the hash table. */
af9dc8
+                if (strcmp(ent->id, "UTC") == 0) {
af9dc8
+                        ent->pos = FAKE_UTC_POS;
af9dc8
+                        continue;
af9dc8
+                }
af9dc8
+
af9dc8
+                li = find_zone_info(info, ent->id);
af9dc8
+                if (li) {
af9dc8
+                        /* If found, append the BC byte and the
af9dc8
+                         * country code; set the position for this
af9dc8
+                         * section of timezone data.  */
af9dc8
+                        ent->pos = (p - data) - 4;
af9dc8
+                        *p++ = '\1';
af9dc8
+                        *p++ = li->code[0];
af9dc8
+                        *p++ = li->code[1];
af9dc8
+                }
af9dc8
+                else {
af9dc8
+                        /* If not found, the timezone data can
af9dc8
+                         * point at the header. */
af9dc8
+                        ent->pos = 0;
af9dc8
+                }
af9dc8
+        }
af9dc8
+        
af9dc8
+        sysdb->data = (unsigned char *)data;
af9dc8
+}
af9dc8
+
af9dc8
+/* Returns true if the passed-in stat structure describes a
af9dc8
+ * probably-valid timezone file. */
af9dc8
+static int is_valid_tzfile(const struct stat *st)
af9dc8
+{
af9dc8
+	return S_ISREG(st->st_mode) && st->st_size > 20;
af9dc8
+}
af9dc8
+
af9dc8
+/* Return the mmap()ed tzfile if found, else NULL.  On success, the
af9dc8
+ * length of the mapped data is placed in *length. */
af9dc8
+static char *map_tzfile(const char *timezone, size_t *length)
af9dc8
+{
af9dc8
+	char fname[PATH_MAX];
af9dc8
+	struct stat st;
af9dc8
+	char *p;
af9dc8
+	int fd;
af9dc8
+	
af9dc8
+	if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
af9dc8
+		return NULL;
af9dc8
+	}
af9dc8
+
af9dc8
+    if (system_location_table) {
af9dc8
+        const struct location_info *li;
af9dc8
+        if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
af9dc8
+            /* Use the stored name to avoid case issue */
af9dc8
+            timezone = li->name;
af9dc8
+        }
af9dc8
+    }
af9dc8
+	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
af9dc8
+	
af9dc8
+	fd = open(fname, O_RDONLY);
af9dc8
+	if (fd == -1) {
af9dc8
+		return NULL;
af9dc8
+	} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
af9dc8
+		close(fd);
af9dc8
+		return NULL;
af9dc8
+	}
af9dc8
+
af9dc8
+	*length = st.st_size;
af9dc8
+	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
af9dc8
+	close(fd);
af9dc8
+	
af9dc8
+	return p != MAP_FAILED ? p : NULL;
af9dc8
+}
af9dc8
+
af9dc8
+#endif
af9dc8
+
af9dc8
+static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
af9dc8
 {
af9dc8
 	int left = 0, right = tzdb->index_size - 1;
af9dc8
 #ifdef HAVE_SETLOCALE
af9dc8
@@ -295,36 +713,135 @@
af9dc8
 	return 0;
af9dc8
 }
af9dc8
 
af9dc8
+static int seek_to_tz_position(const unsigned char **tzf, char *timezone, 
af9dc8
+			       char **map, size_t *maplen,
af9dc8
+			       const timelib_tzdb *tzdb)
af9dc8
+{
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+	if (tzdb == timezonedb_system) {
af9dc8
+		char *orig;
af9dc8
+
af9dc8
+		orig = map_tzfile(timezone, maplen);
af9dc8
+		if (orig == NULL) {
af9dc8
+			return 0;
af9dc8
+		}
af9dc8
+		
af9dc8
+		(*tzf) = (unsigned char *)orig ;
af9dc8
+		*map = orig;
af9dc8
+                
af9dc8
+                return 1;
af9dc8
+	}
af9dc8
+       else
af9dc8
+#endif
af9dc8
+       {
af9dc8
+		return inmem_seek_to_tz_position(tzf, timezone, tzdb);
af9dc8
+	}
af9dc8
+}
af9dc8
+
af9dc8
 const timelib_tzdb *timelib_builtin_db(void)
af9dc8
 {
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+	if (timezonedb_system == NULL) {
af9dc8
+		timelib_tzdb *tmp = malloc(sizeof *tmp);
af9dc8
+
af9dc8
+		tmp->version = "0.system";
af9dc8
+		tmp->data = NULL;
af9dc8
+		create_zone_index(tmp);
af9dc8
+		system_location_table = create_location_table();
af9dc8
+                fake_data_segment(tmp, system_location_table);
af9dc8
+		timezonedb_system = tmp;
af9dc8
+	}
af9dc8
+
af9dc8
+			
af9dc8
+	return timezonedb_system;
af9dc8
+#else
af9dc8
 	return &timezonedb_builtin;
af9dc8
+#endif
af9dc8
 }
af9dc8
 
af9dc8
 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
af9dc8
 {
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+	*count = timezonedb_system->index_size;
af9dc8
+	return timezonedb_system->index;
af9dc8
+#else
af9dc8
 	*count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
af9dc8
 	return timezonedb_idx_builtin;
af9dc8
+#endif
af9dc8
 }
af9dc8
 
af9dc8
 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
af9dc8
 {
af9dc8
 	const unsigned char *tzf;
af9dc8
-	return (seek_to_tz_position(&tzf, timezone, tzdb));
af9dc8
+
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+        if (tzdb == timezonedb_system) {
af9dc8
+            char fname[PATH_MAX];
af9dc8
+            struct stat st;
af9dc8
+
af9dc8
+            if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
af9dc8
+		        return 0;
af9dc8
+            }
af9dc8
+
af9dc8
+            if (system_location_table) {
af9dc8
+                if (find_zone_info(system_location_table, timezone) != NULL) {
af9dc8
+                    /* found in cache */
af9dc8
+                    return 1;
af9dc8
+                }
af9dc8
+            }
af9dc8
+            
af9dc8
+            snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
af9dc8
+            
af9dc8
+            return stat(fname, &st) == 0 && is_valid_tzfile(&st);
af9dc8
+        }
af9dc8
+#endif
af9dc8
+
af9dc8
+	return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
af9dc8
 }
af9dc8
 
af9dc8
 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
af9dc8
 {
af9dc8
 	const unsigned char *tzf;
af9dc8
+	char *memmap = NULL;
af9dc8
+	size_t maplen;
af9dc8
 	timelib_tzinfo *tmp;
af9dc8
 
af9dc8
-	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
af9dc8
+	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
af9dc8
 		tmp = timelib_tzinfo_ctor(timezone);
af9dc8
 
af9dc8
 		read_preamble(&tzf, tmp);
af9dc8
 		read_header(&tzf, tmp);
af9dc8
 		read_transistions(&tzf, tmp);
af9dc8
 		read_types(&tzf, tmp);
af9dc8
-		read_location(&tzf, tmp);
af9dc8
+
af9dc8
+#ifdef HAVE_SYSTEM_TZDATA
af9dc8
+		if (memmap) {
af9dc8
+			const struct location_info *li;
af9dc8
+
af9dc8
+			/* TZif-style - grok the location info from the system database,
af9dc8
+			 * if possible. */
af9dc8
+
af9dc8
+			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
af9dc8
+				tmp->location.comments = strdup(li->comment);
af9dc8
+                                strncpy(tmp->location.country_code, li->code, 2);
af9dc8
+				tmp->location.longitude = li->longitude;
af9dc8
+				tmp->location.latitude = li->latitude;
af9dc8
+				tmp->bc = 1;
af9dc8
+			}
af9dc8
+			else {
af9dc8
+				strcpy(tmp->location.country_code, "??");
af9dc8
+				tmp->bc = 0;
af9dc8
+				tmp->location.comments = strdup("");
af9dc8
+			}
af9dc8
+
af9dc8
+			/* Now done with the mmap segment - discard it. */
af9dc8
+			munmap(memmap, maplen);
af9dc8
+		} else
af9dc8
+#endif
af9dc8
+		{
af9dc8
+			/* PHP-style - use the embedded info. */
af9dc8
+			read_location(&tzf, tmp);
af9dc8
+		}
af9dc8
 	} else {
af9dc8
 		tmp = NULL;
af9dc8
 	}
af9dc8
--- a/ext/date/lib/timelib.m4
af9dc8
+++ b/ext/date/lib/timelib.m4
af9dc8
@@ -78,3 +78,17 @@ stdlib.h
af9dc8
 
af9dc8
 dnl Check for strtoll, atoll
af9dc8
 AC_CHECK_FUNCS(strtoll atoll strftime)
af9dc8
+
af9dc8
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
af9dc8
+[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
af9dc8
+no, no)
af9dc8
+
af9dc8
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
af9dc8
+   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
af9dc8
+
af9dc8
+   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
af9dc8
+      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
af9dc8
+                         [Define for location of system timezone data])
af9dc8
+   fi
af9dc8
+fi
af9dc8
+