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