1 Add support for use of the system timezone database, rather
2 than embedding a copy. Discussed upstream but was not desired.
5 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
6 r8: fix compile error without --with-system-tzdata configured
7 r7: improve check for valid timezone id to exclude directories
8 r6: fix fd leak in r5, fix country code/BC flag use in
9 timezone_identifiers_list() using system db,
10 fix use of PECL timezonedb to override system db,
11 r5: reverts addition of "System/Localtime" fake tzname.
12 updated for 5.3.0, parses zone.tab to pick up mapping between
13 timezone name, country code and long/lat coords
14 r4: added "System/Localtime" tzname which uses /etc/localtime
15 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
16 r2: add filesystem trawl to set up name alias index
19 --- a/ext/date/lib/parse_tz.c
20 +++ b/ext/date/lib/parse_tz.c
25 +#ifdef HAVE_SYSTEM_TZDATA
26 +#include <sys/mman.h>
27 +#include <sys/stat.h>
32 +#include "php_scandir.h"
43 +#ifndef HAVE_SYSTEM_TZDATA
44 #include "timezonedb.h"
49 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
50 # if defined(__LITTLE_ENDIAN__)
53 static void read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
58 + if (memcmp(tzf, "TZif", 4) == 0) {
67 tz->bc = (**tzf == '\1');
69 @@ -260,7 +280,397 @@ void timelib_dump_tzinfo(timelib_tzinfo
73 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
74 +#ifdef HAVE_SYSTEM_TZDATA
76 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
77 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
79 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
82 +/* System timezone database pointer. */
83 +static const timelib_tzdb *timezonedb_system = NULL;
85 +/* Hash table entry for the cache of the zone.tab mapping table. */
86 +struct location_info {
88 + double latitude, longitude;
91 + struct location_info *next;
94 +/* Cache of zone.tab. */
95 +static struct location_info **system_location_table;
97 +/* Size of the zone.tab hash table; a random-ish prime big enough to
98 + * prevent too many collisions. */
99 +#define LOCINFO_HASH_SIZE (1021)
101 +static uint32_t tz_hash(const char *str)
103 + const unsigned char *p = (const unsigned char *)str;
104 + uint32_t hash = 5381;
107 + while ((c = *p++) != '\0') {
108 + hash = (hash << 5) ^ hash ^ c;
111 + return hash % LOCINFO_HASH_SIZE;
114 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
115 + * parsed string on success, or NULL on parse error. On success,
116 + * writes the parsed number to *result. */
117 +static char *parse_iso6709(char *p, double *result)
125 + else if (*p == '-')
131 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
134 + /* Annoying encoding used by zone.tab has no decimal point, so use
135 + * the length to determine the format:
143 + if (len < 4 || len > 7) {
148 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
150 + if (len == 5 || len == 7)
151 + v = v * 10.0 + (*p++ - '0');
153 + v += (10.0 * (p[0] - '0')
154 + + p[1] - '0') / 60.0;
158 + v += (10.0 * (p[0] - '0')
159 + + p[1] - '0') / 3600.0;
163 + /* Round to five decimal place, not because it's a good idea,
164 + * but, because the builtin data uses rounded data, so, match
166 + *result = round(v * sign * 100000.0) / 100000.0;
171 +/* This function parses the zone.tab file to build up the mapping of
172 + * timezone to country code and geographic location, and returns a
173 + * hash table. The hash table is indexed by the function:
175 + * tz_hash(timezone-name)
177 +static struct location_info **create_location_table(void)
179 + struct location_info **li, *i;
180 + char zone_tab[PATH_MAX];
184 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
186 + fp = fopen(zone_tab, "r");
191 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
193 + while (fgets(line, sizeof line, fp)) {
194 + char *p = line, *code, *name, *comment;
196 + double latitude, longitude;
198 + while (isspace(*p))
201 + if (*p == '#' || *p == '\0' || *p == '\n')
204 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
212 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
213 + p = parse_iso6709(p, &latitude);
217 + p = parse_iso6709(p, &longitude);
222 + if (!p || *p != '\t') {
226 + /* name = string */
228 + while (*p != '\t' && *p && *p != '\n')
233 + /* comment = string */
235 + while (*p != '\t' && *p && *p != '\n')
238 + if (*p == '\n' || *p == '\t')
241 + hash = tz_hash(name);
242 + i = malloc(sizeof *i);
243 + memcpy(i->code, code, 2);
244 + strncpy(i->name, name, sizeof i->name);
245 + i->comment = strdup(comment);
246 + i->longitude = longitude;
247 + i->latitude = latitude;
248 + i->next = li[hash];
250 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
258 +/* Return location info from hash table, using given timezone name.
259 + * Returns NULL if the name could not be found. */
260 +const struct location_info *find_zone_info(struct location_info **li,
263 + uint32_t hash = tz_hash(name);
264 + const struct location_info *l;
270 + for (l = li[hash]; l; l = l->next) {
271 + if (strcasecmp(l->name, name) == 0)
278 +/* Filter out some non-tzdata files and the posix/right databases, if
280 +static int index_filter(const struct dirent *ent)
282 + return strcmp(ent->d_name, ".") != 0
283 + && strcmp(ent->d_name, "..") != 0
284 + && strcmp(ent->d_name, "posix") != 0
285 + && strcmp(ent->d_name, "posixrules") != 0
286 + && strcmp(ent->d_name, "right") != 0
287 + && strstr(ent->d_name, ".tab") == NULL;
290 +static int sysdbcmp(const void *first, const void *second)
292 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
294 + return strcmp(alpha->id, beta->id);
298 +/* Create the zone identifier index by trawling the filesystem. */
299 +static void create_zone_index(timelib_tzdb *db)
301 + size_t dirstack_size, dirstack_top;
302 + size_t index_size, index_next;
303 + timelib_tzdb_index_entry *db_index;
306 + /* LIFO stack to hold directory entries to scan; each slot is a
307 + * directory name relative to the zoneinfo prefix. */
308 + dirstack_size = 32;
309 + dirstack = malloc(dirstack_size * sizeof *dirstack);
311 + dirstack[0] = strdup("");
315 + db_index = malloc(index_size * sizeof *db_index);
319 + struct dirent **ents;
320 + char name[PATH_MAX], *top;
323 + /* Pop the top stack entry, and iterate through its contents. */
324 + top = dirstack[--dirstack_top];
325 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
327 + count = php_scandir(name, &ents, index_filter, php_alphasort);
329 + while (count > 0) {
331 + const char *leaf = ents[count - 1]->d_name;
333 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
336 + if (strlen(name) && stat(name, &st) == 0) {
337 + /* Name, relative to the zoneinfo prefix. */
338 + const char *root = top;
340 + if (root[0] == '/') root++;
342 + snprintf(name, sizeof name, "%s%s%s", root,
343 + *root ? "/": "", leaf);
345 + if (S_ISDIR(st.st_mode)) {
346 + if (dirstack_top == dirstack_size) {
347 + dirstack_size *= 2;
348 + dirstack = realloc(dirstack,
349 + dirstack_size * sizeof *dirstack);
351 + dirstack[dirstack_top++] = strdup(name);
354 + if (index_next == index_size) {
356 + db_index = realloc(db_index,
357 + index_size * sizeof *db_index);
360 + db_index[index_next++].id = strdup(name);
364 + free(ents[--count]);
367 + if (count != -1) free(ents);
369 + } while (dirstack_top);
371 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
373 + db->index = db_index;
374 + db->index_size = index_next;
379 +#define FAKE_HEADER "1234\0??\1??"
380 +#define FAKE_UTC_POS (7 - 4)
382 +/* Create a fake data segment for database 'sysdb'. */
383 +static void fake_data_segment(timelib_tzdb *sysdb,
384 + struct location_info **info)
389 + data = malloc(3 * sysdb->index_size + 7);
391 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
393 + for (n = 0; n < sysdb->index_size; n++) {
394 + const struct location_info *li;
395 + timelib_tzdb_index_entry *ent;
397 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
399 + /* Lookup the timezone name in the hash table. */
400 + if (strcmp(ent->id, "UTC") == 0) {
401 + ent->pos = FAKE_UTC_POS;
405 + li = find_zone_info(info, ent->id);
407 + /* If found, append the BC byte and the
408 + * country code; set the position for this
409 + * section of timezone data. */
410 + ent->pos = (p - data) - 4;
412 + *p++ = li->code[0];
413 + *p++ = li->code[1];
416 + /* If not found, the timezone data can
417 + * point at the header. */
422 + sysdb->data = (unsigned char *)data;
425 +/* Returns true if the passed-in stat structure describes a
426 + * probably-valid timezone file. */
427 +static int is_valid_tzfile(const struct stat *st)
429 + return S_ISREG(st->st_mode) && st->st_size > 20;
432 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
433 + * length of the mapped data is placed in *length. */
434 +static char *map_tzfile(const char *timezone, size_t *length)
436 + char fname[PATH_MAX];
441 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
445 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
447 + fd = open(fname, O_RDONLY);
450 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
455 + *length = st.st_size;
456 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
459 + return p != MAP_FAILED ? p : NULL;
464 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
466 int left = 0, right = tzdb->index_size - 1;
467 #ifdef HAVE_SETLOCALE
468 @@ -299,36 +709,128 @@ static int seek_to_tz_position(const uns
472 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
473 + char **map, size_t *maplen,
474 + const timelib_tzdb *tzdb)
476 +#ifdef HAVE_SYSTEM_TZDATA
477 + if (tzdb == timezonedb_system) {
480 + orig = map_tzfile(timezone, maplen);
481 + if (orig == NULL) {
485 + (*tzf) = (unsigned char *)orig ;
493 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
497 const timelib_tzdb *timelib_builtin_db(void)
499 +#ifdef HAVE_SYSTEM_TZDATA
500 + if (timezonedb_system == NULL) {
501 + timelib_tzdb *tmp = malloc(sizeof *tmp);
503 + tmp->version = "0.system";
505 + create_zone_index(tmp);
506 + system_location_table = create_location_table();
507 + fake_data_segment(tmp, system_location_table);
508 + timezonedb_system = tmp;
512 + return timezonedb_system;
514 return &timezonedb_builtin;
518 const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
520 +#ifdef HAVE_SYSTEM_TZDATA
521 + *count = timezonedb_system->index_size;
522 + return timezonedb_system->index;
524 *count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
525 return timezonedb_idx_builtin;
529 int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
531 const unsigned char *tzf;
532 - return (seek_to_tz_position(&tzf, timezone, tzdb));
534 +#ifdef HAVE_SYSTEM_TZDATA
535 + if (tzdb == timezonedb_system) {
536 + char fname[PATH_MAX];
539 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
543 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
545 + return stat(fname, &st) == 0 && is_valid_tzfile(&st);
549 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
552 timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
554 const unsigned char *tzf;
555 + char *memmap = NULL;
559 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
560 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
561 tmp = timelib_tzinfo_ctor(timezone);
563 read_preamble(&tzf, tmp);
564 read_header(&tzf, tmp);
565 read_transistions(&tzf, tmp);
566 read_types(&tzf, tmp);
567 - read_location(&tzf, tmp);
569 +#ifdef HAVE_SYSTEM_TZDATA
571 + const struct location_info *li;
573 + /* TZif-style - grok the location info from the system database,
576 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
577 + tmp->location.comments = strdup(li->comment);
578 + strncpy(tmp->location.country_code, li->code, 2);
579 + tmp->location.longitude = li->longitude;
580 + tmp->location.latitude = li->latitude;
584 + strcpy(tmp->location.country_code, "??");
586 + tmp->location.comments = strdup("");
589 + /* Now done with the mmap segment - discard it. */
590 + munmap(memmap, maplen);
594 + /* PHP-style - use the embedded info. */
595 + read_location(&tzf, tmp);
600 --- a/ext/date/lib/timelib.m4
601 +++ b/ext/date/lib/timelib.m4
602 @@ -78,3 +78,17 @@ stdlib.h
604 dnl Check for strtoll, atoll
605 AC_CHECK_FUNCS(strtoll atoll strftime)
607 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
608 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
611 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
612 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
614 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
615 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
616 + [Define for location of system timezone data])