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 r21: adapt for timelib 2021.03 (in 8.1.0)
9 r20: adapt for timelib 2020.03 (in 8.0.10RC1)
10 r19: adapt for timelib 2020.02 (in 8.0.0beta2)
11 r18: adapt for autotool change in 7.3.3RC1
12 r17: adapt for timelib 2018.01 (in 7.3.2RC1)
13 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
14 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
15 r14: improve check for valid tz file
16 r13: adapt for upstream changes to use PHP allocator
17 r12: adapt for upstream changes for new zic
18 r11: use canonical names to avoid more case sensitivity issues
19 round lat/long from zone.tab towards zero per builtin db
20 r10: make timezone case insensitive
21 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
22 r8: fix compile error without --with-system-tzdata configured
23 r7: improve check for valid timezone id to exclude directories
24 r6: fix fd leak in r5, fix country code/BC flag use in
25 timezone_identifiers_list() using system db,
26 fix use of PECL timezonedb to override system db,
27 r5: reverts addition of "System/Localtime" fake tzname.
28 updated for 5.3.0, parses zone.tab to pick up mapping between
29 timezone name, country code and long/lat coords
30 r4: added "System/Localtime" tzname which uses /etc/localtime
31 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
32 r2: add filesystem trawl to set up name alias index
35 diff -up php-8.0.0beta3/ext/date/config0.m4.systzdata php-8.0.0beta3/ext/date/config0.m4
36 --- php-8.0.0beta3/ext/date/config0.m4.systzdata 2020-09-01 19:13:26.000000000 +0200
37 +++ php-8.0.0beta3/ext/date/config0.m4 2020-09-02 08:07:51.039979873 +0200
38 @@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
39 dnl Check for strtoll, atoll
40 AC_CHECK_FUNCS(strtoll atoll)
42 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
43 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
46 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
47 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
49 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
50 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
51 + [Define for location of system timezone data])
55 PHP_DATE_CFLAGS="-Wno-implicit-fallthrough -I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
56 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c lib/parse_posix.c
57 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
58 diff -up php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata php-8.0.0beta3/ext/date/lib/parse_tz.c
59 --- php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata 2020-09-01 19:13:26.000000000 +0200
60 +++ php-8.0.0beta3/ext/date/lib/parse_tz.c 2020-09-02 08:07:51.039979873 +0200
63 #include "timelib_private.h"
65 +#ifdef HAVE_SYSTEM_TZDATA
66 +#include <sys/mman.h>
67 +#include <sys/stat.h>
72 +#include "php_scandir.h"
75 #define TIMELIB_SUPPORTS_V2DATA
76 #define TIMELIB_SUPPORT_SLIM_FILE
77 #include "timezonedb.h"
82 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
83 # if defined(__LITTLE_ENDIAN__)
84 @@ -95,6 +108,11 @@ static int read_php_preamble(const unsig
88 + if (memcmp(*tzf, "TZif", 4) == 0) {
94 version = (*tzf)[3] - '0';
96 @@ -577,7 +595,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
100 -static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
101 +#ifdef HAVE_SYSTEM_TZDATA
103 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
104 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
106 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
109 +/* System timezone database pointer. */
110 +static const timelib_tzdb *timezonedb_system;
112 +/* Hash table entry for the cache of the zone.tab mapping table. */
113 +struct location_info {
115 + double latitude, longitude;
118 + struct location_info *next;
121 +/* Cache of zone.tab. */
122 +static struct location_info **system_location_table;
124 +/* Size of the zone.tab hash table; a random-ish prime big enough to
125 + * prevent too many collisions. */
126 +#define LOCINFO_HASH_SIZE (1021)
128 +/* Compute a case insensitive hash of str */
129 +static uint32_t tz_hash(const char *str)
131 + const unsigned char *p = (const unsigned char *)str;
132 + uint32_t hash = 5381;
135 + while ((c = tolower(*p++)) != '\0') {
136 + hash = (hash << 5) ^ hash ^ c;
139 + return hash % LOCINFO_HASH_SIZE;
142 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
143 + * parsed string on success, or NULL on parse error. On success,
144 + * writes the parsed number to *result. */
145 +static char *parse_iso6709(char *p, double *result)
153 + else if (*p == '-')
159 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
162 + /* Annoying encoding used by zone.tab has no decimal point, so use
163 + * the length to determine the format:
171 + if (len < 4 || len > 7) {
176 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
178 + if (len == 5 || len == 7)
179 + v = v * 10.0 + (*p++ - '0');
181 + v += (10.0 * (p[0] - '0')
182 + + p[1] - '0') / 60.0;
186 + v += (10.0 * (p[0] - '0')
187 + + p[1] - '0') / 3600.0;
191 + /* Round to five decimal place, not because it's a good idea,
192 + * but, because the builtin data uses rounded data, so, match
194 + *result = trunc(v * sign * 100000.0) / 100000.0;
199 +/* This function parses the zone.tab file to build up the mapping of
200 + * timezone to country code and geographic location, and returns a
201 + * hash table. The hash table is indexed by the function:
203 + * tz_hash(timezone-name)
205 +static struct location_info **create_location_table(void)
207 + struct location_info **li, *i;
208 + char zone_tab[PATH_MAX];
212 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
214 + fp = fopen(zone_tab, "r");
219 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
221 + while (fgets(line, sizeof line, fp)) {
222 + char *p = line, *code, *name, *comment;
224 + double latitude, longitude;
226 + while (isspace(*p))
229 + if (*p == '#' || *p == '\0' || *p == '\n')
232 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
240 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
241 + p = parse_iso6709(p, &latitude);
245 + p = parse_iso6709(p, &longitude);
250 + if (!p || *p != '\t') {
254 + /* name = string */
256 + while (*p != '\t' && *p && *p != '\n')
261 + /* comment = string */
263 + while (*p != '\t' && *p && *p != '\n')
266 + if (*p == '\n' || *p == '\t')
269 + hash = tz_hash(name);
270 + i = malloc(sizeof *i);
271 + memcpy(i->code, code, 2);
272 + strncpy(i->name, name, sizeof i->name);
273 + i->comment = strdup(comment);
274 + i->longitude = longitude;
275 + i->latitude = latitude;
276 + i->next = li[hash];
278 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
286 +/* Return location info from hash table, using given timezone name.
287 + * Returns NULL if the name could not be found. */
288 +const struct location_info *find_zone_info(struct location_info **li,
291 + uint32_t hash = tz_hash(name);
292 + const struct location_info *l;
298 + for (l = li[hash]; l; l = l->next) {
299 + if (timelib_strcasecmp(l->name, name) == 0)
306 +/* Filter out some non-tzdata files and the posix/right databases, if
308 +static int index_filter(const struct dirent *ent)
310 + return strcmp(ent->d_name, ".") != 0
311 + && strcmp(ent->d_name, "..") != 0
312 + && strcmp(ent->d_name, "posix") != 0
313 + && strcmp(ent->d_name, "posixrules") != 0
314 + && strcmp(ent->d_name, "right") != 0
315 + && strstr(ent->d_name, ".list") == NULL
316 + && strstr(ent->d_name, ".tab") == NULL;
319 +static int sysdbcmp(const void *first, const void *second)
321 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
323 + return timelib_strcasecmp(alpha->id, beta->id);
327 +/* Create the zone identifier index by trawling the filesystem. */
328 +static void create_zone_index(timelib_tzdb *db)
330 + size_t dirstack_size, dirstack_top;
331 + size_t index_size, index_next;
332 + timelib_tzdb_index_entry *db_index;
335 + /* LIFO stack to hold directory entries to scan; each slot is a
336 + * directory name relative to the zoneinfo prefix. */
337 + dirstack_size = 32;
338 + dirstack = malloc(dirstack_size * sizeof *dirstack);
340 + dirstack[0] = strdup("");
344 + db_index = malloc(index_size * sizeof *db_index);
348 + struct dirent **ents;
349 + char name[PATH_MAX], *top;
352 + /* Pop the top stack entry, and iterate through its contents. */
353 + top = dirstack[--dirstack_top];
354 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
356 + count = php_scandir(name, &ents, index_filter, php_alphasort);
358 + while (count > 0) {
360 + const char *leaf = ents[count - 1]->d_name;
362 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
365 + if (strlen(name) && stat(name, &st) == 0) {
366 + /* Name, relative to the zoneinfo prefix. */
367 + const char *root = top;
369 + if (root[0] == '/') root++;
371 + snprintf(name, sizeof name, "%s%s%s", root,
372 + *root ? "/": "", leaf);
374 + if (S_ISDIR(st.st_mode)) {
375 + if (dirstack_top == dirstack_size) {
376 + dirstack_size *= 2;
377 + dirstack = realloc(dirstack,
378 + dirstack_size * sizeof *dirstack);
380 + dirstack[dirstack_top++] = strdup(name);
383 + if (index_next == index_size) {
385 + db_index = realloc(db_index,
386 + index_size * sizeof *db_index);
389 + db_index[index_next++].id = strdup(name);
393 + free(ents[--count]);
396 + if (count != -1) free(ents);
398 + } while (dirstack_top);
400 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
402 + db->index = db_index;
403 + db->index_size = index_next;
408 +#define FAKE_HEADER "1234\0??\1??"
409 +#define FAKE_UTC_POS (7 - 4)
411 +/* Create a fake data segment for database 'sysdb'. */
412 +static void fake_data_segment(timelib_tzdb *sysdb,
413 + struct location_info **info)
418 + data = malloc(3 * sysdb->index_size + 7);
420 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
422 + for (n = 0; n < sysdb->index_size; n++) {
423 + const struct location_info *li;
424 + timelib_tzdb_index_entry *ent;
426 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
428 + /* Lookup the timezone name in the hash table. */
429 + if (strcmp(ent->id, "UTC") == 0) {
430 + ent->pos = FAKE_UTC_POS;
434 + li = find_zone_info(info, ent->id);
436 + /* If found, append the BC byte and the
437 + * country code; set the position for this
438 + * section of timezone data. */
439 + ent->pos = (p - data) - 4;
441 + *p++ = li->code[0];
442 + *p++ = li->code[1];
445 + /* If not found, the timezone data can
446 + * point at the header. */
451 + sysdb->data = (unsigned char *)data;
454 +/* Returns true if the passed-in stat structure describes a
455 + * probably-valid timezone file. */
456 +static int is_valid_tzfile(const struct stat *st, int fd)
460 + if (read(fd, buf, 20)!=20) {
463 + lseek(fd, SEEK_SET, 0);
464 + if (memcmp(buf, "TZif", 4)) {
468 + return S_ISREG(st->st_mode) && st->st_size > 20;
471 +/* To allow timezone names to be used case-insensitively, find the
472 + * canonical name for this timezone, if possible. */
473 +static const char *canonical_tzname(const char *timezone)
475 + if (timezonedb_system) {
476 + timelib_tzdb_index_entry *ent, lookup;
478 + lookup.id = (char *)timezone;
480 + ent = bsearch(&lookup, timezonedb_system->index,
481 + timezonedb_system->index_size, sizeof lookup,
491 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
492 + * length of the mapped data is placed in *length. */
493 +static char *map_tzfile(const char *timezone, size_t *length)
495 + char fname[PATH_MAX];
500 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
504 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
506 + fd = open(fname, O_RDONLY);
509 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
514 + *length = st.st_size;
515 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
518 + return p != MAP_FAILED ? p : NULL;
523 +static int inmem_seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
525 int left = 0, right = tzdb->index_size - 1;
527 @@ -603,9 +1043,48 @@ static int seek_to_tz_position(const uns
531 +static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
532 + char **map, size_t *maplen,
533 + const timelib_tzdb *tzdb)
535 +#ifdef HAVE_SYSTEM_TZDATA
536 + if (tzdb == timezonedb_system) {
539 + orig = map_tzfile(timezone, maplen);
540 + if (orig == NULL) {
544 + (*tzf) = (unsigned char *)orig;
551 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
555 const timelib_tzdb *timelib_builtin_db(void)
557 +#ifdef HAVE_SYSTEM_TZDATA
558 + if (timezonedb_system == NULL) {
559 + timelib_tzdb *tmp = malloc(sizeof *tmp);
561 + tmp->version = "0.system";
563 + create_zone_index(tmp);
564 + system_location_table = create_location_table();
565 + fake_data_segment(tmp, system_location_table);
566 + timezonedb_system = tmp;
569 + return timezonedb_system;
571 return &timezonedb_builtin;
575 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
576 @@ -617,7 +1096,30 @@ const timelib_tzdb_index_entry *timelib_
577 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
579 const unsigned char *tzf;
580 - return (seek_to_tz_position(&tzf, timezone, tzdb));
582 +#ifdef HAVE_SYSTEM_TZDATA
583 + if (tzdb == timezonedb_system) {
584 + char fname[PATH_MAX];
587 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
591 + if (system_location_table) {
592 + if (find_zone_info(system_location_table, timezone) != NULL) {
593 + /* found in cache */
598 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
600 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
604 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
607 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
608 @@ -662,6 +1164,8 @@ static timelib_tzinfo* timelib_tzinfo_ct
609 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
611 const unsigned char *tzf;
612 + char *memmap = NULL;
616 int transitions_result, types_result;
617 @@ -669,7 +1173,7 @@ timelib_tzinfo *timelib_parse_tzfile(con
619 *error_code = TIMELIB_ERROR_NO_ERROR;
621 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
622 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
623 tmp = timelib_tzinfo_ctor(timezone);
625 version = read_preamble(&tzf, tmp, &type);
626 @@ -712,11 +1216,36 @@ timelib_tzinfo *timelib_parse_tzfile(con
630 +#ifdef HAVE_SYSTEM_TZDATA
632 + const struct location_info *li;
634 + /* TZif-style - grok the location info from the system database,
637 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
638 + tmp->location.comments = timelib_strdup(li->comment);
639 + strncpy(tmp->location.country_code, li->code, 2);
640 + tmp->location.longitude = li->longitude;
641 + tmp->location.latitude = li->latitude;
645 + set_default_location_and_comments(&tzf, tmp);
648 + /* Now done with the mmap segment - discard it. */
649 + munmap(memmap, maplen);
652 if (type == TIMELIB_TZINFO_PHP) {
653 read_location(&tzf, tmp);
655 set_default_location_and_comments(&tzf, tmp);
657 +#ifdef HAVE_SYSTEM_TZDATA
661 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;