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 r20: adapt for timelib 2020.03 (in 8.0.10RC1)
9 r19: adapt for timelib 2020.02 (in 8.0.0beta2)
10 r18: adapt for autotool change in 7.3.3RC1
11 r17: adapt for timelib 2018.01 (in 7.3.2RC1)
12 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
13 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
14 r14: improve check for valid tz file
15 r13: adapt for upstream changes to use PHP allocator
16 r12: adapt for upstream changes for new zic
17 r11: use canonical names to avoid more case sensitivity issues
18 round lat/long from zone.tab towards zero per builtin db
19 r10: make timezone case insensitive
20 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
21 r8: fix compile error without --with-system-tzdata configured
22 r7: improve check for valid timezone id to exclude directories
23 r6: fix fd leak in r5, fix country code/BC flag use in
24 timezone_identifiers_list() using system db,
25 fix use of PECL timezonedb to override system db,
26 r5: reverts addition of "System/Localtime" fake tzname.
27 updated for 5.3.0, parses zone.tab to pick up mapping between
28 timezone name, country code and long/lat coords
29 r4: added "System/Localtime" tzname which uses /etc/localtime
30 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
31 r2: add filesystem trawl to set up name alias index
34 diff -up ./ext/date/config0.m4.systzdata ./ext/date/config0.m4
35 --- ./ext/date/config0.m4.systzdata 2021-08-10 11:35:28.000000000 +0200
36 +++ ./ext/date/config0.m4 2021-08-10 12:09:41.067003517 +0200
37 @@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
38 dnl Check for strtoll, atoll
39 AC_CHECK_FUNCS(strtoll atoll)
41 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
42 +[ --with-system-tzdata[=DIR] to specify use of system timezone data],
45 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
46 + AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
48 + if test "$PHP_SYSTEM_TZDATA" != "yes"; then
49 + AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
50 + [Define for location of system timezone data])
54 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
55 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
56 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
57 diff -up ./ext/date/lib/parse_tz.c.systzdata ./ext/date/lib/parse_tz.c
58 --- ./ext/date/lib/parse_tz.c.systzdata 2021-08-10 11:35:28.000000000 +0200
59 +++ ./ext/date/lib/parse_tz.c 2021-08-10 12:12:13.191605207 +0200
62 #include "timelib_private.h"
64 +#ifdef HAVE_SYSTEM_TZDATA
65 +#include <sys/mman.h>
66 +#include <sys/stat.h>
71 +#include "php_scandir.h"
74 #define TIMELIB_SUPPORTS_V2DATA
75 #include "timezonedb.h"
80 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
81 # if defined(__LITTLE_ENDIAN__)
82 @@ -94,6 +107,11 @@ static int read_php_preamble(const unsig
86 + if (memcmp(*tzf, "TZif", 4) == 0) {
92 version = (*tzf)[3] - '0';
94 @@ -435,7 +453,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
98 -static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
99 +#ifdef HAVE_SYSTEM_TZDATA
101 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
102 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
104 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
107 +/* System timezone database pointer. */
108 +static const timelib_tzdb *timezonedb_system;
110 +/* Hash table entry for the cache of the zone.tab mapping table. */
111 +struct location_info {
113 + double latitude, longitude;
116 + struct location_info *next;
119 +/* Cache of zone.tab. */
120 +static struct location_info **system_location_table;
122 +/* Size of the zone.tab hash table; a random-ish prime big enough to
123 + * prevent too many collisions. */
124 +#define LOCINFO_HASH_SIZE (1021)
126 +/* Compute a case insensitive hash of str */
127 +static uint32_t tz_hash(const char *str)
129 + const unsigned char *p = (const unsigned char *)str;
130 + uint32_t hash = 5381;
133 + while ((c = tolower(*p++)) != '\0') {
134 + hash = (hash << 5) ^ hash ^ c;
137 + return hash % LOCINFO_HASH_SIZE;
140 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
141 + * parsed string on success, or NULL on parse error. On success,
142 + * writes the parsed number to *result. */
143 +static char *parse_iso6709(char *p, double *result)
151 + else if (*p == '-')
157 + for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
160 + /* Annoying encoding used by zone.tab has no decimal point, so use
161 + * the length to determine the format:
169 + if (len < 4 || len > 7) {
174 + v = (p[0] - '0') * 10.0 + (p[1] - '0');
176 + if (len == 5 || len == 7)
177 + v = v * 10.0 + (*p++ - '0');
179 + v += (10.0 * (p[0] - '0')
180 + + p[1] - '0') / 60.0;
184 + v += (10.0 * (p[0] - '0')
185 + + p[1] - '0') / 3600.0;
189 + /* Round to five decimal place, not because it's a good idea,
190 + * but, because the builtin data uses rounded data, so, match
192 + *result = trunc(v * sign * 100000.0) / 100000.0;
197 +/* This function parses the zone.tab file to build up the mapping of
198 + * timezone to country code and geographic location, and returns a
199 + * hash table. The hash table is indexed by the function:
201 + * tz_hash(timezone-name)
203 +static struct location_info **create_location_table(void)
205 + struct location_info **li, *i;
206 + char zone_tab[PATH_MAX];
210 + strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
212 + fp = fopen(zone_tab, "r");
217 + li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
219 + while (fgets(line, sizeof line, fp)) {
220 + char *p = line, *code, *name, *comment;
222 + double latitude, longitude;
224 + while (isspace(*p))
227 + if (*p == '#' || *p == '\0' || *p == '\n')
230 + if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
238 + /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
239 + p = parse_iso6709(p, &latitude);
243 + p = parse_iso6709(p, &longitude);
248 + if (!p || *p != '\t') {
252 + /* name = string */
254 + while (*p != '\t' && *p && *p != '\n')
259 + /* comment = string */
261 + while (*p != '\t' && *p && *p != '\n')
264 + if (*p == '\n' || *p == '\t')
267 + hash = tz_hash(name);
268 + i = malloc(sizeof *i);
269 + memcpy(i->code, code, 2);
270 + strncpy(i->name, name, sizeof i->name);
271 + i->comment = strdup(comment);
272 + i->longitude = longitude;
273 + i->latitude = latitude;
274 + i->next = li[hash];
276 + /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
284 +/* Return location info from hash table, using given timezone name.
285 + * Returns NULL if the name could not be found. */
286 +const struct location_info *find_zone_info(struct location_info **li,
289 + uint32_t hash = tz_hash(name);
290 + const struct location_info *l;
296 + for (l = li[hash]; l; l = l->next) {
297 + if (timelib_strcasecmp(l->name, name) == 0)
304 +/* Filter out some non-tzdata files and the posix/right databases, if
306 +static int index_filter(const struct dirent *ent)
308 + return strcmp(ent->d_name, ".") != 0
309 + && strcmp(ent->d_name, "..") != 0
310 + && strcmp(ent->d_name, "posix") != 0
311 + && strcmp(ent->d_name, "posixrules") != 0
312 + && strcmp(ent->d_name, "right") != 0
313 + && strstr(ent->d_name, ".list") == NULL
314 + && strstr(ent->d_name, ".tab") == NULL;
317 +static int sysdbcmp(const void *first, const void *second)
319 + const timelib_tzdb_index_entry *alpha = first, *beta = second;
321 + return timelib_strcasecmp(alpha->id, beta->id);
325 +/* Create the zone identifier index by trawling the filesystem. */
326 +static void create_zone_index(timelib_tzdb *db)
328 + size_t dirstack_size, dirstack_top;
329 + size_t index_size, index_next;
330 + timelib_tzdb_index_entry *db_index;
333 + /* LIFO stack to hold directory entries to scan; each slot is a
334 + * directory name relative to the zoneinfo prefix. */
335 + dirstack_size = 32;
336 + dirstack = malloc(dirstack_size * sizeof *dirstack);
338 + dirstack[0] = strdup("");
342 + db_index = malloc(index_size * sizeof *db_index);
346 + struct dirent **ents;
347 + char name[PATH_MAX], *top;
350 + /* Pop the top stack entry, and iterate through its contents. */
351 + top = dirstack[--dirstack_top];
352 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
354 + count = php_scandir(name, &ents, index_filter, php_alphasort);
356 + while (count > 0) {
358 + const char *leaf = ents[count - 1]->d_name;
360 + snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
363 + if (strlen(name) && stat(name, &st) == 0) {
364 + /* Name, relative to the zoneinfo prefix. */
365 + const char *root = top;
367 + if (root[0] == '/') root++;
369 + snprintf(name, sizeof name, "%s%s%s", root,
370 + *root ? "/": "", leaf);
372 + if (S_ISDIR(st.st_mode)) {
373 + if (dirstack_top == dirstack_size) {
374 + dirstack_size *= 2;
375 + dirstack = realloc(dirstack,
376 + dirstack_size * sizeof *dirstack);
378 + dirstack[dirstack_top++] = strdup(name);
381 + if (index_next == index_size) {
383 + db_index = realloc(db_index,
384 + index_size * sizeof *db_index);
387 + db_index[index_next++].id = strdup(name);
391 + free(ents[--count]);
394 + if (count != -1) free(ents);
396 + } while (dirstack_top);
398 + qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
400 + db->index = db_index;
401 + db->index_size = index_next;
406 +#define FAKE_HEADER "1234\0??\1??"
407 +#define FAKE_UTC_POS (7 - 4)
409 +/* Create a fake data segment for database 'sysdb'. */
410 +static void fake_data_segment(timelib_tzdb *sysdb,
411 + struct location_info **info)
416 + data = malloc(3 * sysdb->index_size + 7);
418 + p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
420 + for (n = 0; n < sysdb->index_size; n++) {
421 + const struct location_info *li;
422 + timelib_tzdb_index_entry *ent;
424 + ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
426 + /* Lookup the timezone name in the hash table. */
427 + if (strcmp(ent->id, "UTC") == 0) {
428 + ent->pos = FAKE_UTC_POS;
432 + li = find_zone_info(info, ent->id);
434 + /* If found, append the BC byte and the
435 + * country code; set the position for this
436 + * section of timezone data. */
437 + ent->pos = (p - data) - 4;
439 + *p++ = li->code[0];
440 + *p++ = li->code[1];
443 + /* If not found, the timezone data can
444 + * point at the header. */
449 + sysdb->data = (unsigned char *)data;
452 +/* Returns true if the passed-in stat structure describes a
453 + * probably-valid timezone file. */
454 +static int is_valid_tzfile(const struct stat *st, int fd)
458 + if (read(fd, buf, 20)!=20) {
461 + lseek(fd, SEEK_SET, 0);
462 + if (memcmp(buf, "TZif", 4)) {
466 + return S_ISREG(st->st_mode) && st->st_size > 20;
469 +/* To allow timezone names to be used case-insensitively, find the
470 + * canonical name for this timezone, if possible. */
471 +static const char *canonical_tzname(const char *timezone)
473 + if (timezonedb_system) {
474 + timelib_tzdb_index_entry *ent, lookup;
476 + lookup.id = (char *)timezone;
478 + ent = bsearch(&lookup, timezonedb_system->index,
479 + timezonedb_system->index_size, sizeof lookup,
489 +/* Return the mmap()ed tzfile if found, else NULL. On success, the
490 + * length of the mapped data is placed in *length. */
491 +static char *map_tzfile(const char *timezone, size_t *length)
493 + char fname[PATH_MAX];
498 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
502 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
504 + fd = open(fname, O_RDONLY);
507 + } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
512 + *length = st.st_size;
513 + p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
516 + return p != MAP_FAILED ? p : NULL;
521 +static int inmem_seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
523 int left = 0, right = tzdb->index_size - 1;
525 @@ -461,9 +901,48 @@ static int seek_to_tz_position(const uns
529 +static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
530 + char **map, size_t *maplen,
531 + const timelib_tzdb *tzdb)
533 +#ifdef HAVE_SYSTEM_TZDATA
534 + if (tzdb == timezonedb_system) {
537 + orig = map_tzfile(timezone, maplen);
538 + if (orig == NULL) {
542 + (*tzf) = (unsigned char *)orig;
549 + return inmem_seek_to_tz_position(tzf, timezone, tzdb);
553 const timelib_tzdb *timelib_builtin_db(void)
555 +#ifdef HAVE_SYSTEM_TZDATA
556 + if (timezonedb_system == NULL) {
557 + timelib_tzdb *tmp = malloc(sizeof *tmp);
559 + tmp->version = "0.system";
561 + create_zone_index(tmp);
562 + system_location_table = create_location_table();
563 + fake_data_segment(tmp, system_location_table);
564 + timezonedb_system = tmp;
567 + return timezonedb_system;
569 return &timezonedb_builtin;
573 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
574 @@ -475,7 +954,30 @@ const timelib_tzdb_index_entry *timelib_
575 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
577 const unsigned char *tzf;
578 - return (seek_to_tz_position(&tzf, timezone, tzdb));
580 +#ifdef HAVE_SYSTEM_TZDATA
581 + if (tzdb == timezonedb_system) {
582 + char fname[PATH_MAX];
585 + if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
589 + if (system_location_table) {
590 + if (find_zone_info(system_location_table, timezone) != NULL) {
591 + /* found in cache */
596 + snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
598 + return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
602 + return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
605 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
606 @@ -517,6 +1019,8 @@ static timelib_tzinfo* timelib_tzinfo_ct
607 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
609 const unsigned char *tzf;
610 + char *memmap = NULL;
614 int transitions_result, types_result;
615 @@ -524,7 +1028,7 @@ timelib_tzinfo *timelib_parse_tzfile(con
617 *error_code = TIMELIB_ERROR_NO_ERROR;
619 - if (seek_to_tz_position(&tzf, timezone, tzdb)) {
620 + if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
621 tmp = timelib_tzinfo_ctor(timezone);
623 version = read_preamble(&tzf, tmp, &type);
624 @@ -563,11 +1067,36 @@ timelib_tzinfo *timelib_parse_tzfile(con
626 skip_posix_string(&tzf, tmp);
628 +#ifdef HAVE_SYSTEM_TZDATA
630 + const struct location_info *li;
632 + /* TZif-style - grok the location info from the system database,
635 + if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
636 + tmp->location.comments = timelib_strdup(li->comment);
637 + strncpy(tmp->location.country_code, li->code, 2);
638 + tmp->location.longitude = li->longitude;
639 + tmp->location.latitude = li->latitude;
643 + set_default_location_and_comments(&tzf, tmp);
646 + /* Now done with the mmap segment - discard it. */
647 + munmap(memmap, maplen);
650 if (type == TIMELIB_TZINFO_PHP) {
651 read_location(&tzf, tmp);
653 set_default_location_and_comments(&tzf, tmp);
655 +#ifdef HAVE_SYSTEM_TZDATA
659 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;