From e81a60a2864eb0929e5f3d1de0c8550d2f43cbe4 Mon Sep 17 00:00:00 2001 From: Artem Ukrainskiy Date: Wed, 25 Nov 2026 10:11:54 +0000 Subject: [PATCH] ext/date: Look up the time zone offset in date_format() only when used For every local time, date_format() allocated a timelib_time_offset and an abbreviation string before reading the format, and for a time zone identifier it also searched the transitions. Only I O P p T Z c r use them, and e for a zone given as an offset or abbreviation. The offset is now built once, when the format first reaches one of these characters, and not at all for a format without them. --- UPGRADING | 4 + ext/date/php_date.c | 96 ++++++++++++------- .../date_format_escaped_timezone_chars.phpt | 43 +++++++++ 3 files changed, 111 insertions(+), 32 deletions(-) create mode 100644 ext/date/tests/date_format_escaped_timezone_chars.phpt diff --git a/UPGRADING b/UPGRADING index 607f9427ed6b..dbb697b23ef0 100644 --- a/UPGRADING +++ b/UPGRADING @@ -220,6 +220,10 @@ PHP 8.7 UPGRADE NOTES . Improved performance of integer to string conversion (string casts, echo, implode(), json_encode(), serialize(), var_export() and others). +- Date: + . Improved performance of date() and DateTimeInterface::format() when the + format prints no time zone offset, abbreviation or DST flag. + - DOM: . Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for small inputs. diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 98c0240400c1..5857570d232a 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -670,6 +670,45 @@ static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib } /* }}} */ +/* The time zone offset of t, as the format characters I O P p T e Z c r print it. */ +static timelib_time_offset *date_format_offset(const timelib_time *t) +{ + timelib_time_offset *offset; + + if (t->zone_type == TIMELIB_ZONETYPE_ABBR) { + offset = timelib_time_offset_ctor(); + offset->offset = (t->z + (t->dst * 3600)); + offset->leap_secs = 0; + offset->is_dst = t->dst; + offset->abbr = timelib_strdup(t->tz_abbr); + } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) { + offset = timelib_time_offset_ctor(); + offset->offset = (t->z); + offset->leap_secs = 0; + offset->is_dst = 0; + offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */ + snprintf(offset->abbr, 9, "GMT%c%02d%02d", + (offset->offset < 0) ? '-' : '+', + abs(offset->offset / 3600), + abs((offset->offset % 3600) / 60)); + } else if (t->zone_type == TIMELIB_ZONETYPE_ID) { + offset = timelib_get_time_zone_info(t->sse, t->tz_info); + } else { + /* Shouldn't happen, but code defensively */ + offset = timelib_time_offset_ctor(); + } + + return offset; +} + +/* Sets *offset to the time zone offset of t the first time a local time needs it. */ +static zend_always_inline void date_format_need_offset(timelib_time_offset **offset, const timelib_time *t, bool localtime) +{ + if (localtime && !*offset) { + *offset = date_format_offset(t); + } +} + /* {{{ date_format - (gm)date helper */ static zend_string *date_format(const char *format, size_t format_len, const timelib_time *t, bool localtime) { @@ -686,31 +725,6 @@ static zend_string *date_format(const char *format, size_t format_len, const tim return ZSTR_EMPTY_ALLOC(); } - if (localtime) { - if (t->zone_type == TIMELIB_ZONETYPE_ABBR) { - offset = timelib_time_offset_ctor(); - offset->offset = (t->z + (t->dst * 3600)); - offset->leap_secs = 0; - offset->is_dst = t->dst; - offset->abbr = timelib_strdup(t->tz_abbr); - } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) { - offset = timelib_time_offset_ctor(); - offset->offset = (t->z); - offset->leap_secs = 0; - offset->is_dst = 0; - offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */ - snprintf(offset->abbr, 9, "GMT%c%02d%02d", - (offset->offset < 0) ? '-' : '+', - abs(offset->offset / 3600), - abs((offset->offset % 3600) / 60)); - } else if (t->zone_type == TIMELIB_ZONETYPE_ID) { - offset = timelib_get_time_zone_info(t->sse, t->tz_info); - } else { - /* Shouldn't happen, but code defensively */ - offset = timelib_time_offset_ctor(); - } - } - for (i = 0; i < format_len; i++) { rfc_colon = false; switch (format[i]) { @@ -769,22 +783,31 @@ static zend_string *date_format(const char *format, size_t format_len, const tim case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break; /* timezone */ - case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break; + case 'I': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); + break; case 'p': + date_format_need_offset(&offset, t, localtime); if (!localtime || strcmp(offset->abbr, "UTC") == 0 || strcmp(offset->abbr, "Z") == 0 || strcmp(offset->abbr, "GMT+0000") == 0) { length = slprintf(buffer, sizeof(buffer), "%s", "Z"); break; } ZEND_FALLTHROUGH; case 'P': rfc_colon = true; ZEND_FALLTHROUGH; - case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d", + case 'O': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d", localtime ? ((offset->offset < 0) ? '-' : '+') : '+', localtime ? abs(offset->offset / 3600) : 0, rfc_colon ? ":" : "", localtime ? abs((offset->offset % 3600) / 60) : 0 ); break; - case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break; + case 'T': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); + break; case 'e': if (!localtime) { length = slprintf(buffer, sizeof(buffer), "%s", "UTC"); } else { @@ -793,9 +816,11 @@ static zend_string *date_format(const char *format, size_t format_len, const tim length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name); break; case TIMELIB_ZONETYPE_ABBR: + date_format_need_offset(&offset, t, localtime); length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr); break; case TIMELIB_ZONETYPE_OFFSET: { + date_format_need_offset(&offset, t, localtime); int seconds = offset->offset % 60; if (seconds == 0) { length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d", @@ -816,10 +841,15 @@ static zend_string *date_format(const char *format, size_t format_len, const tim } } break; - case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break; + case 'Z': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); + break; /* full date/time */ - case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", + case 'c': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", (zend_long) t->y, (int) t->m, (int) t->d, (int) t->h, (int) t->i, (int) t->s, localtime ? ((offset->offset < 0) ? '-' : '+') : '+', @@ -827,7 +857,9 @@ static zend_string *date_format(const char *format, size_t format_len, const tim localtime ? abs((offset->offset % 3600) / 60) : 0 ); break; - case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d", + case 'r': + date_format_need_offset(&offset, t, localtime); + length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d", php_date_short_day_name(t->y, t->m, t->d), (int) t->d, mon_short_names[t->m - 1], (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s, @@ -847,7 +879,7 @@ static zend_string *date_format(const char *format, size_t format_len, const tim smart_str_0(&string); - if (localtime) { + if (offset) { timelib_time_offset_dtor(offset); } diff --git a/ext/date/tests/date_format_escaped_timezone_chars.phpt b/ext/date/tests/date_format_escaped_timezone_chars.phpt new file mode 100644 index 000000000000..82977575cde0 --- /dev/null +++ b/ext/date/tests/date_format_escaped_timezone_chars.phpt @@ -0,0 +1,43 @@ +--TEST-- +date_format(): time zone characters, escaped and not, on every kind of zone +--INI-- +date.timezone=UTC +--FILE-- +format('\I\O\P\p\T\e\Z\c\r Y-m-d\TH:i:s')); + var_dump($d->format('\\\\T \TT')); + var_dump($d->format('e p T P O Z I')); + var_dump($d->format('Y-m-d H:i:s T (P) e')); + var_dump($d->format('c | r')); +} +var_dump(gmdate('e p T P O Z I c', 1763469296)); +?> +--EXPECT-- +string(29) "IOPpTeZcr 2026-07-01T12:00:00" +string(9) "\UTC TUTC" +string(26) "UTC Z UTC +00:00 +0000 0 0" +string(36) "2026-07-01 12:00:00 UTC (+00:00) UTC" +string(59) "2026-07-01T12:00:00+00:00 | Wed, 01 Jul 2026 12:00:00 +0000" +string(29) "IOPpTeZcr 2026-07-01T12:00:00" +string(11) "\CEST TCEST" +string(45) "Europe/Madrid +02:00 CEST +02:00 +0200 7200 1" +string(47) "2026-07-01 12:00:00 CEST (+02:00) Europe/Madrid" +string(59) "2026-07-01T12:00:00+02:00 | Wed, 01 Jul 2026 12:00:00 +0200" +string(29) "IOPpTeZcr 2026-11-18T12:34:56" +string(19) "\GMT-0930 TGMT-0930" +string(44) "-09:30 -09:30 GMT-0930 -09:30 -0930 -34200 0" +string(44) "2026-11-18 12:34:56 GMT-0930 (-09:30) -09:30" +string(59) "2026-11-18T12:34:56-09:30 | Wed, 18 Nov 2026 12:34:56 -0930" +string(29) "IOPpTeZcr 2026-11-18T12:34:56" +string(9) "\EST TEST" +string(36) "EST -05:00 EST -05:00 -0500 -18000 0" +string(36) "2026-11-18 12:34:56 EST (-05:00) EST" +string(59) "2026-11-18T12:34:56-05:00 | Wed, 18 Nov 2026 12:34:56 -0500" +string(52) "UTC Z GMT +00:00 +0000 0 0 2025-11-18T12:34:56+00:00"