2 # http://opensource.org/licenses/MIT
4 Add support for use of the system timezone database, rather
5 than embedding a copy. Discussed upstream but was not desired.
8 r19: adapt for timelib 2020.02 (in 8.0.0beta2)
9 r18: adapt for autotool change in 7.3.3RC1
10 r17: adapt for timelib 2018.01 (in 7.3.2RC1)
11 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
12 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
13 r14: improve check for valid tz file
14 r13: adapt for upstream changes to use PHP allocator
15 r12: adapt for upstream changes for new zic
16 r11: use canonical names to avoid more case sensitivity issues
17 round lat/long from zone.tab towards zero per builtin db
18 r10: make timezone case insensitive
19 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
20 r8: fix compile error without --with-system-tzdata configured
21 r7: improve check for valid timezone id to exclude directories
22 r6: fix fd leak in r5, fix country code/BC flag use in
23 timezone_identifiers_list() using system db,
24 fix use of PECL timezonedb to override system db,
25 r5: reverts addition of "System/Localtime" fake tzname.
26 updated for 5.3.0, parses zone.tab to pick up mapping between
27 timezone name, country code and long/lat coords
28 r4: added "System/Localtime" tzname which uses /etc/localtime
29 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
30 r2: add filesystem trawl to set up name alias index
33 diff -up php-8.0.0beta3/ext/date/config0.m4.systzdata php-8.0.0beta3/ext/date/config0.m4
34 --- php-8.0.0beta3/ext/date/config0.m4.systzdata 2020-09-01 19:13:26.000000000 +0200
35 +++ php-8.0.0beta3/ext/date/config0.m4 2020-09-02 08:07:51.039979873 +0200
36 @@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
37 dnl Check for strtoll, atoll
38 AC_CHECK_FUNCS(strtoll atoll)
40 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
41 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
44 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
45 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
47 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
48 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
49 + [Define for location of system timezone data])
53 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
54 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
55 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
56 diff -up php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata php-8.0.0beta3/ext/date/lib/parse_tz.c
57 --- php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata 2020-09-01 19:13:26.000000000 +0200
58 +++ php-8.0.0beta3/ext/date/lib/parse_tz.c 2020-09-02 08:07:51.039979873 +0200
61 #include "timelib_private.h"
63 +#ifdef HAVE_SYSTEM_TZDATA
64 +#include <sys/mman.h>
65 +#include <sys/stat.h>
70 +#include "php_scandir.h"
73 #define TIMELIB_SUPPORTS_V2DATA
74 #include "timezonedb.h"
79 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
80 # if defined(__LITTLE_ENDIAN__)
81 @@ -94,6 +107,11 @@ static int read_php_preamble(const unsig
85 + if (memcmp(*tzf, "TZif", 4) == 0) {
91 version = (*tzf)[3] - '0';
93 @@ -418,7 +436,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
97 -static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
98 +#ifdef HAVE_SYSTEM_TZDATA
100 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
101 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
103 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
106 +/* System timezone database pointer. */
107 +static const timelib_tzdb *timezonedb_system;
109 +/* Hash table entry for the cache of the zone.tab mapping table. */
110 +struct location_info {
112 + double latitude, longitude;
115 + struct location_info *next;
118 +/* Cache of zone.tab. */
119 +static struct location_info **system_location_table;
121 +/* Size of the zone.tab hash table; a random-ish prime big enough to
122 + * prevent too many collisions. */
123 +#define LOCINFO_HASH_SIZE (1021)
125 +/* Compute a case insensitive hash of str */
126 +static uint32_t tz_hash(const char *str)
128 + const unsigned char *p = (const unsigned char *)str;
129 + uint32_t hash = 5381;
132 + while ((c = tolower(*p++)) != '\0') {
133 + hash = (hash << 5) ^ hash ^ c;
136 + return hash % LOCINFO_HASH_SIZE;
139 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
140 + * parsed string on success, or NULL on parse error. On success,
141 + * writes the parsed number to *result. */
142 +static char *parse_iso6709(char *p, double *result)
150 + else if (*p == '-')
156 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
159 + /* Annoying encoding used by zone.tab has no decimal point, so use
160 + * the length to determine the format:
168 + if (len < 4 || len > 7) {
173 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
175 + if (len == 5 || len == 7)
176 + v = v * 10.0 + (*p++ - '0');
178 + v += (10.0 * (p[0] - '0')
179 + + p[1] - '0') / 60.0;
183 + v += (10.0 * (p[0] - '0')
184 + + p[1] - '0') / 3600.0;
188 + /* Round to five decimal place, not because it's a good idea,
189 + * but, because the builtin data uses rounded data, so, match
191 + *result = trunc(v * sign * 100000.0) / 100000.0;
196 +/* This function parses the zone.tab file to build up the mapping of
197 + * timezone to country code and geographic location, and returns a
198 + * hash table. The hash table is indexed by the function:
200 + * tz_hash(timezone-name)
202 +static struct location_info **create_location_table(void)
204 + struct location_info **li, *i;
205 + char zone_tab[PATH_MAX];
209 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
211 + fp = fopen(zone_tab, "r");
216 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
218 + while (fgets(line, sizeof line, fp)) {
219 + char *p = line, *code, *name, *comment;
221 + double latitude, longitude;
223 + while (isspace(*p))
226 + if (*p == '#' || *p == '\0' || *p == '\n')
229 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
237 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
238 + p = parse_iso6709(p, &latitude);
242 + p = parse_iso6709(p, &longitude);
247 + if (!p || *p != '\t') {
251 + /* name = string */
253 + while (*p != '\t' && *p && *p != '\n')
258 + /* comment = string */
260 + while (*p != '\t' && *p && *p != '\n')
263 + if (*p == '\n' || *p == '\t')
266 + hash = tz_hash(name);
267 + i = malloc(sizeof *i);
268 + memcpy(i->code, code, 2);
269 + strncpy(i->name, name, sizeof i->name);
270 + i->comment = strdup(comment);
271 + i->longitude = longitude;
272 + i->latitude = latitude;
273 + i->next = li[hash];
275 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
283 +/* Return location info from hash table, using given timezone name.
284 + * Returns NULL if the name could not be found. */
285 +const struct location_info *find_zone_info(struct location_info **li,
288 + uint32_t hash = tz_hash(name);
289 + const struct location_info *l;
295 + for (l = li[hash]; l; l = l->next) {
296 + if (timelib_strcasecmp(l->name, name) == 0)
303 +/* Filter out some non-tzdata files and the posix/right databases, if
305 +static int index_filter(const struct dirent *ent)
307 + return strcmp(ent->d_name, ".") != 0
308 + && strcmp(ent->d_name, "..") != 0
309 + && strcmp(ent->d_name, "posix") != 0
310 + && strcmp(ent->d_name, "posixrules") != 0
311 + && strcmp(ent->d_name, "right") != 0
312 + && strstr(ent->d_name, ".list") == NULL
313 + && strstr(ent->d_name, ".tab") == NULL;
316 +static int sysdbcmp(const void *first, const void *second)
318 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
320 + return timelib_strcasecmp(alpha->id, beta->id);
324 +/* Create the zone identifier index by trawling the filesystem. */
325 +static void create_zone_index(timelib_tzdb *db)
327 + size_t dirstack_size, dirstack_top;
328 + size_t index_size, index_next;
329 + timelib_tzdb_index_entry *db_index;
332 + /* LIFO stack to hold directory entries to scan; each slot is a
333 + * directory name relative to the zoneinfo prefix. */
334 + dirstack_size = 32;
335 + dirstack = malloc(dirstack_size * sizeof *dirstack);
337 + dirstack[0] = strdup("");
341 + db_index = malloc(index_size * sizeof *db_index);
345 + struct dirent **ents;
346 + char name[PATH_MAX], *top;
349 + /* Pop the top stack entry, and iterate through its contents. */
350 + top = dirstack[--dirstack_top];
351 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
353 + count = php_scandir(name, &ents, index_filter, php_alphasort);
355 + while (count > 0) {
357 + const char *leaf = ents[count - 1]->d_name;
359 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
362 + if (strlen(name) && stat(name, &st) == 0) {
363 + /* Name, relative to the zoneinfo prefix. */
364 + const char *root = top;
366 + if (root[0] == '/') root++;
368 + snprintf(name, sizeof name, "%s%s%s", root,
369 + *root ? "/": "", leaf);
371 + if (S_ISDIR(st.st_mode)) {
372 + if (dirstack_top == dirstack_size) {
373 + dirstack_size *= 2;
374 + dirstack = realloc(dirstack,
375 + dirstack_size * sizeof *dirstack);
377 + dirstack[dirstack_top++] = strdup(name);
380 + if (index_next == index_size) {
382 + db_index = realloc(db_index,
383 + index_size * sizeof *db_index);
386 + db_index[index_next++].id = strdup(name);
390 + free(ents[--count]);
393 + if (count != -1) free(ents);
395 + } while (dirstack_top);
397 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
399 + db->index = db_index;
400 + db->index_size = index_next;
405 +#define FAKE_HEADER "1234\0??\1??"
406 +#define FAKE_UTC_POS (7 - 4)
408 +/* Create a fake data segment for database 'sysdb'. */
409 +static void fake_data_segment(timelib_tzdb *sysdb,
410 + struct location_info **info)
415 + data = malloc(3 * sysdb->index_size + 7);
417 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
419 + for (n = 0; n < sysdb->index_size; n++) {
420 + const struct location_info *li;
421 + timelib_tzdb_index_entry *ent;
423 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
425 + /* Lookup the timezone name in the hash table. */
426 + if (strcmp(ent->id, "UTC") == 0) {
427 + ent->pos = FAKE_UTC_POS;
431 + li = find_zone_info(info, ent->id);
433 + /* If found, append the BC byte and the
434 + * country code; set the position for this
435 + * section of timezone data. */
436 + ent->pos = (p - data) - 4;
438 + *p++ = li->code[0];
439 + *p++ = li->code[1];
442 + /* If not found, the timezone data can
443 + * point at the header. */
448 + sysdb->data = (unsigned char *)data;
451 +/* Returns true if the passed-in stat structure describes a
452 + * probably-valid timezone file. */
453 +static int is_valid_tzfile(const struct stat *st, int fd)
457 + if (read(fd, buf, 20)!=20) {
460 + lseek(fd, SEEK_SET, 0);
461 + if (memcmp(buf, "TZif", 4)) {
465 + return S_ISREG(st->st_mode) && st->st_size > 20;
468 +/* To allow timezone names to be used case-insensitively, find the
469 + * canonical name for this timezone, if possible. */
470 +static const char *canonical_tzname(const char *timezone)
472 + if (timezonedb_system) {
473 + timelib_tzdb_index_entry *ent, lookup;
475 + lookup.id = (char *)timezone;
477 + ent = bsearch(&lookup, timezonedb_system->index,
478 + timezonedb_system->index_size, sizeof lookup,
488 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
489 + * length of the mapped data is placed in *length. */
490 +static char *map_tzfile(const char *timezone, size_t *length)
492 + char fname[PATH_MAX];
497 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
501 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
503 + fd = open(fname, O_RDONLY);
506 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
511 + *length = st.st_size;
512 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
515 + return p != MAP_FAILED ? p : NULL;
520 +static int inmem_seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
522 int left = 0, right = tzdb->index_size - 1;
524 @@ -444,9 +884,48 @@ static int seek_to_tz_position(const uns
528 +static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
529 + char **map, size_t *maplen,
530 + const timelib_tzdb *tzdb)
532 +#ifdef HAVE_SYSTEM_TZDATA
533 + if (tzdb == timezonedb_system) {
536 + orig = map_tzfile(timezone, maplen);
537 + if (orig == NULL) {
541 + (*tzf) = (unsigned char *)orig;
548 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
552 const timelib_tzdb *timelib_builtin_db(void)
554 +#ifdef HAVE_SYSTEM_TZDATA
555 + if (timezonedb_system == NULL) {
556 + timelib_tzdb *tmp = malloc(sizeof *tmp);
558 + tmp->version = "0.system";
560 + create_zone_index(tmp);
561 + system_location_table = create_location_table();
562 + fake_data_segment(tmp, system_location_table);
563 + timezonedb_system = tmp;
566 + return timezonedb_system;
568 return &timezonedb_builtin;
572 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
573 @@ -458,7 +937,30 @@ const timelib_tzdb_index_entry *timelib_
574 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
576 const unsigned char *tzf;
577 - return (seek_to_tz_position(&tzf, timezone, tzdb));
579 +#ifdef HAVE_SYSTEM_TZDATA
580 + if (tzdb == timezonedb_system) {
581 + char fname[PATH_MAX];
584 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
588 + if (system_location_table) {
589 + if (find_zone_info(system_location_table, timezone) != NULL) {
590 + /* found in cache */
595 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
597 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
601 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
604 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
605 @@ -500,12 +1002,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
606 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
608 const unsigned char *tzf;
609 + char *memmap = NULL;
613 int transitions_result, types_result;
614 unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
616 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
617 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
618 tmp = timelib_tzinfo_ctor(timezone);
620 version = read_preamble(&tzf, tmp, &type);
621 @@ -540,11 +1044,36 @@ timelib_tzinfo *timelib_parse_tzfile(con
623 skip_posix_string(&tzf, tmp);
625 +#ifdef HAVE_SYSTEM_TZDATA
627 + const struct location_info *li;
629 + /* TZif-style - grok the location info from the system database,
632 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
633 + tmp->location.comments = timelib_strdup(li->comment);
634 + strncpy(tmp->location.country_code, li->code, 2);
635 + tmp->location.longitude = li->longitude;
636 + tmp->location.latitude = li->latitude;
640 + set_default_location_and_comments(&tzf, tmp);
643 + /* Now done with the mmap segment - discard it. */
644 + munmap(memmap, maplen);
647 if (type == TIMELIB_TZINFO_PHP) {
648 read_location(&tzf, tmp);
650 set_default_location_and_comments(&tzf, tmp);
652 +#ifdef HAVE_SYSTEM_TZDATA
656 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;