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