than embedding a copy. Discussed upstream but was not desired.
History:
+r24: add internal UTC if tzdata is missing
+r23: fix possible buffer overflow
+r22: retrieve tzdata version from /usr/share/zoneinfo/tzdata.zi
+r21: adapt for timelib 2021.03 (in 8.1.0)
r20: adapt for timelib 2020.03 (in 8.0.10RC1)
r19: adapt for timelib 2020.02 (in 8.0.0beta2)
r18: adapt for autotool change in 7.3.3RC1
r2: add filesystem trawl to set up name alias index
r1: initial revision
+
diff -up ./ext/date/config0.m4.systzdata ./ext/date/config0.m4
---- ./ext/date/config0.m4.systzdata 2021-08-10 11:35:28.000000000 +0200
-+++ ./ext/date/config0.m4 2021-08-10 12:09:41.067003517 +0200
-@@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
- dnl Check for strtoll, atoll
- AC_CHECK_FUNCS(strtoll atoll)
+--- ./ext/date/config0.m4.systzdata 2024-07-03 16:21:20.240786848 +0200
++++ ./ext/date/config0.m4 2024-07-03 16:25:14.838995464 +0200
+@@ -8,6 +8,18 @@
+ [PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -Wno-implicit-fallthrough"],,
+ [-Werror])
+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
-+[ --with-system-tzdata[=DIR] to specify use of system timezone data],
-+no, no)
++ [AS_HELP_STRING([--with-system-tzdata[=DIR]],[to specify use of system timezone data])], no, no)
+
+if test "$PHP_SYSTEM_TZDATA" != "no"; then
+ AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
+ fi
+fi
+
- PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
- timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
+ PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
+ timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c lib/parse_posix.c
lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
diff -up ./ext/date/lib/parse_tz.c.systzdata ./ext/date/lib/parse_tz.c
---- ./ext/date/lib/parse_tz.c.systzdata 2021-08-10 11:35:28.000000000 +0200
-+++ ./ext/date/lib/parse_tz.c 2021-08-10 12:12:13.191605207 +0200
-@@ -26,8 +26,21 @@
+--- ./ext/date/lib/parse_tz.c.systzdata 2024-07-02 15:43:13.000000000 +0200
++++ ./ext/date/lib/parse_tz.c 2024-07-03 16:21:20.240786848 +0200
+@@ -26,9 +26,33 @@
#include "timelib.h"
#include "timelib_private.h"
+
+#include "php_scandir.h"
+
++static const unsigned char internal_utc[] = {
++ 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
++ 0x00, 0x00, 0x55, 0x54, 0x43, 0x00, 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00,
++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
++ 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x54, 0x43, 0x00, 0x0a, 0x55, 0x54, 0x43,
++ 0x30, 0x0a
++};
++
+#else
#define TIMELIB_SUPPORTS_V2DATA
+ #define TIMELIB_SUPPORT_SLIM_FILE
#include "timezonedb.h"
+#endif
+
#if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
# if defined(__LITTLE_ENDIAN__)
-@@ -94,6 +107,11 @@ static int read_php_preamble(const unsig
+@@ -95,6 +119,11 @@ static int read_php_preamble(const unsig
{
uint32_t version;
/* read ID */
version = (*tzf)[3] - '0';
*tzf += 4;
-@@ -435,7 +453,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
+@@ -577,7 +606,475 @@ void timelib_dump_tzinfo(timelib_tzinfo
}
}
+ return timelib_strcasecmp(alpha->id, beta->id);
+}
+
++/* Retrieve tzdata version. */
++static void retrieve_zone_version(timelib_tzdb *db)
++{
++ static char buf[30];
++ char path[PATH_MAX];
++ FILE *fp;
++
++ strncpy(path, ZONEINFO_PREFIX "/tzdata.zi", sizeof(path));
++
++ fp = fopen(path, "r");
++ if (fp) {
++ if (fgets(buf, sizeof(buf), fp)) {
++ if (!memcmp(buf, "# version ", 10) &&
++ isdigit(buf[10]) &&
++ isdigit(buf[11]) &&
++ isdigit(buf[12]) &&
++ isdigit(buf[13]) &&
++ islower(buf[14])) {
++ if (buf[14] >= 't') { /* 2022t = 2022.20 */
++ buf[17] = 0;
++ buf[16] = buf[14] - 't' + '0';
++ buf[15] = '2';
++ } else if (buf[14] >= 'j') { /* 2022j = 2022.10 */
++ buf[17] = 0;
++ buf[16] = buf[14] - 'j' + '0';
++ buf[15] = '1';
++ } else { /* 2022a = 2022.1 */
++ buf[16] = 0;
++ buf[15] = buf[14] - 'a' + '1';
++ }
++ buf[14] = '.';
++ db->version = buf+10;
++ }
++ }
++ fclose(fp);
++ }
++}
++
+
+/* Create the zone identifier index by trawling the filesystem. */
+static void create_zone_index(timelib_tzdb *db)
+
+ qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
+
++ if (!index_next) {
++ db_index[index_next++].id = strdup("UTC");
++ }
+ db->index = db_index;
+ db->index_size = index_next;
+
+ size_t n;
+ char *data, *p;
+
-+ data = malloc(3 * sysdb->index_size + 7);
++ data = malloc(3 * sysdb->index_size + sizeof(FAKE_HEADER) - 1);
+
+ p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
+
+
+ fd = open(fname, O_RDONLY);
+ if (fd == -1) {
-+ return NULL;
++ if (strcmp(timezone, "UTC")) {
++ return NULL;
++ } else {
++ *length = sizeof(internal_utc);
++ return internal_utc;
++ }
+ } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
+ close(fd);
+ return NULL;
{
int left = 0, right = tzdb->index_size - 1;
-@@ -461,9 +901,48 @@ static int seek_to_tz_position(const uns
+@@ -603,9 +1100,49 @@ static int seek_to_tz_position(const uns
return 0;
}
+ if (timezonedb_system == NULL) {
+ timelib_tzdb *tmp = malloc(sizeof *tmp);
+
-+ tmp->version = "0.system";
++ tmp->version = "0";
+ tmp->data = NULL;
+ create_zone_index(tmp);
++ retrieve_zone_version(tmp);
+ system_location_table = create_location_table();
+ fake_data_segment(tmp, system_location_table);
+ timezonedb_system = tmp;
}
const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
-@@ -475,7 +954,30 @@ const timelib_tzdb_index_entry *timelib_
+@@ -617,7 +1154,32 @@ const timelib_tzdb_index_entry *timelib_
int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
{
const unsigned char *tzf;
+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
+ return 0;
+ }
-+
++ if (!strcmp(timezone, "UTC")) {
++ return 1;
++ }
+ if (system_location_table) {
+ if (find_zone_info(system_location_table, timezone) != NULL) {
+ /* found in cache */
}
static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
-@@ -517,6 +1019,8 @@ static timelib_tzinfo* timelib_tzinfo_ct
+@@ -662,6 +1224,8 @@ static timelib_tzinfo* timelib_tzinfo_ct
timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
{
const unsigned char *tzf;
timelib_tzinfo *tmp;
int version;
int transitions_result, types_result;
-@@ -524,7 +1028,7 @@ timelib_tzinfo *timelib_parse_tzfile(con
+@@ -669,7 +1233,7 @@ timelib_tzinfo *timelib_parse_tzfile(con
*error_code = TIMELIB_ERROR_NO_ERROR;
tmp = timelib_tzinfo_ctor(timezone);
version = read_preamble(&tzf, tmp, &type);
-@@ -563,11 +1067,36 @@ timelib_tzinfo *timelib_parse_tzfile(con
+@@ -712,11 +1276,38 @@ timelib_tzinfo *timelib_parse_tzfile(con
+ return NULL;
}
- skip_posix_string(&tzf, tmp);
+#ifdef HAVE_SYSTEM_TZDATA
+ if (memmap) {
+ }
+
+ /* Now done with the mmap segment - discard it. */
-+ munmap(memmap, maplen);
++ if (memmap != internal_utc) {
++ munmap(memmap, maplen);
++ }
+ } else {
+#endif
if (type == TIMELIB_TZINFO_PHP) {
} else {
*error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
tmp = NULL;
+diff -up ./ext/date/php_date.c.systzdata ./ext/date/php_date.c
+--- ./ext/date/php_date.c.systzdata 2024-07-02 15:43:13.000000000 +0200
++++ ./ext/date/php_date.c 2024-07-03 16:21:20.240786848 +0200
+@@ -487,7 +487,11 @@ PHP_MINFO_FUNCTION(date)
+ php_info_print_table_row(2, "date/time support", "enabled");
+ php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
+ php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
++#ifdef HAVE_SYSTEM_TZDATA
++ php_info_print_table_row(2, "Timezone Database", "system");
++#else
+ php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
++#endif
+ php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
+ php_info_print_table_end();
+