From 09e304ea1b6c473efb8837ae9b369c82ded4ba10 Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Mon, 23 Nov 2026 14:51:59 +0200 Subject: [PATCH] date: Skip timelib_strtotime call in date() for "now" --- NEWS | 4 +++ UPGRADING | 4 +++ ext/date/php_date.c | 14 +++++---- ext/date/tests/DateTime_construct_basic1.phpt | 12 +++++++ .../tests/date_get_last_errors_basic.phpt | 31 +++++++++++++++++++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index eaeb9894e2b2..8332bc644a54 100644 --- a/NEWS +++ b/NEWS @@ -23,6 +23,10 @@ PHP NEWS passing an array of CURLFile or CURLStringFile objects to CURLOPT_POSTFIELDS. (Sjoerd Langkemper) +- Date: + . Improved performance of new DateTime(), new DateTimeImmutable() and + date_create() without a time string or with "now". (Alexander Danilov) + - FPM: . Use recommended TYPE, HELP order for OpenMetrics metadata. (Marcel Hernandez) diff --git a/UPGRADING b/UPGRADING index 9dbcd594a29a..3188231cbfee 100644 --- a/UPGRADING +++ b/UPGRADING @@ -215,6 +215,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 new DateTime(), new DateTimeImmutable() and + date_create() without a time string or with "now". + - 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..6f8a5a4be6a2 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2403,9 +2403,11 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz time_t sec; suseconds_t usec; int options = 0; + bool is_now = false; if (dateobj->time) { timelib_time_dtor(dateobj->time); + dateobj->time = NULL; } if (format) { if (time_str_len == 0) { @@ -2417,7 +2419,10 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz time_str = "now"; time_str_len = sizeof("now") - 1; } - dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); + is_now = time_str_len == sizeof("now") - 1 && memcmp(time_str, ZEND_STRL("now")) == 0; + if (!is_now) { + dateobj->time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); + } } /* update last errors and warnings */ @@ -2456,7 +2461,7 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz return 0; } type = tzobj->type; - } else if (dateobj->time->tz_info) { + } else if (dateobj->time && dateobj->time->tz_info) { tzi = dateobj->time->tz_info; } else { tzi = get_timezone_info(); @@ -2484,10 +2489,7 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz timelib_unixtime2local(now, (timelib_sll) sec); php_date_set_time_fraction(now, usec); - if (!format - && time_str_len == sizeof("now") - 1 - && memcmp(time_str, "now", sizeof("now") - 1) == 0) { - timelib_time_dtor(dateobj->time); + if (is_now) { dateobj->time = now; return 1; } diff --git a/ext/date/tests/DateTime_construct_basic1.phpt b/ext/date/tests/DateTime_construct_basic1.phpt index dd2fcdd15ba1..06ff077a11e0 100644 --- a/ext/date/tests/DateTime_construct_basic1.phpt +++ b/ext/date/tests/DateTime_construct_basic1.phpt @@ -13,6 +13,14 @@ var_dump( new DateTime("GMT") ); var_dump( new DateTime("2005-07-14 22:30:41") ); var_dump( new DateTime("2005-07-14 22:30:41 GMT") ); +echo "\n-- Calling the constructor again resets the time zone --\n"; +$d = new DateTime("2005-07-14 22:30:41 Asia/Tokyo"); +$d->__construct(); +var_dump($d->getTimezone()->getName()); +$d = new DateTime("2005-07-14 22:30:41 Asia/Tokyo"); +$d->__construct("now"); +var_dump($d->getTimezone()->getName()); + ?> --EXPECTF-- *** Testing new DateTime() : basic functionality *** @@ -48,3 +56,7 @@ object(DateTime)#%d (3) { ["timezone"]=> string(3) "GMT" } + +-- Calling the constructor again resets the time zone -- +string(13) "Europe/London" +string(13) "Europe/London" diff --git a/ext/date/tests/date_get_last_errors_basic.phpt b/ext/date/tests/date_get_last_errors_basic.phpt index 26e470a7d8a2..724e017b2ca8 100644 --- a/ext/date/tests/date_get_last_errors_basic.phpt +++ b/ext/date/tests/date_get_last_errors_basic.phpt @@ -8,6 +8,37 @@ date.timezone=UTC --FILE-- fn() => new DateTime(), + 'new DateTime("now")' => fn() => new DateTime('now'), + 'new DateTimeImmutable("now")' => fn() => new DateTimeImmutable('now'), + 'date_create()' => fn() => date_create(), + 'date_create("now")' => fn() => date_create('now'), +] as $name => $create) { + date_create('garbage'); + var_dump(date_get_last_errors()['error_count'] > 0); + $create(); + echo $name, ': '; + var_dump(date_get_last_errors()); +} + +// A failed parse after a successful "now" must still record errors. +new DateTime(); +date_create('garbage'); +var_dump(date_get_last_errors()['error_count'] > 0); ?> --EXPECT-- bool(false) +bool(true) +new DateTime(): bool(false) +bool(true) +new DateTime("now"): bool(false) +bool(true) +new DateTimeImmutable("now"): bool(false) +bool(true) +date_create(): bool(false) +bool(true) +date_create("now"): bool(false) +bool(true)