From 3ec70369dfbfdb307931dcf437ced70e082c3a49 Mon Sep 17 00:00:00 2001 From: Torben Dannhauer Date: Tue, 23 Jun 2026 23:07:59 +0200 Subject: [PATCH 1/2] fix(date): accept DateTimeInterface in legacy Horde_Date constructor _initializeFromObject() only handled DateTime, so DateTimeImmutable and other DateTimeInterface values produced invalid years and broke saves that flow through Nag and other legacy callers. Add regression tests for DateTimeImmutable and parsed ICU dates. --- lib/Horde/Date.php | 26 ++++++++++++++++---------- test/Unnamespaced/DateTest.php | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/Horde/Date.php b/lib/Horde/Date.php index f4c62be..3b14efb 100644 --- a/lib/Horde/Date.php +++ b/lib/Horde/Date.php @@ -1543,7 +1543,7 @@ protected function _initializeFromArray($date) protected function _initializeFromObject($date) { - if ($date instanceof DateTime) { + if ($date instanceof \DateTimeInterface) { $this->_year = (int) $date->format('Y'); $this->_month = (int) $date->format('m'); $this->_mday = (int) $date->format('d'); @@ -1551,19 +1551,25 @@ protected function _initializeFromObject($date) $this->_min = (int) $date->format('i'); $this->_sec = (int) $date->format('s'); $this->_initializeTimezone($date->getTimezone()->getName()); - } else { - $is_horde_date = $date instanceof Horde_Date; + + return; + } + + if ($date instanceof Horde_Date) { foreach (['year', 'month', 'mday', 'hour', 'min', 'sec'] as $key) { - if ($is_horde_date || isset($date->$key)) { - $this->{'_' . $key} = (int) $date->$key; - } + $this->{'_' . $key} = (int) $date->$key; } - if (!$is_horde_date) { - $this->_correct(); - } else { - $this->_initializeTimezone($date->timezone); + $this->_initializeTimezone($date->timezone); + + return; + } + + foreach (['year', 'month', 'mday', 'hour', 'min', 'sec'] as $key) { + if (isset($date->$key)) { + $this->{'_' . $key} = (int) $date->$key; } } + $this->_correct(); } protected function _initializeTimezone($timezone) diff --git a/test/Unnamespaced/DateTest.php b/test/Unnamespaced/DateTest.php index a27ff56..2620018 100644 --- a/test/Unnamespaced/DateTest.php +++ b/test/Unnamespaced/DateTest.php @@ -12,13 +12,14 @@ use date_default_timezone_get; use date_default_timezone_set; use DateTime; +use DateTimeImmutable; use DateTimeZone; +use Horde\Date\Format; use Horde_Date; use Horde_Date_Span; use PHPUnit\Framework\TestCase; use stdClass; use Horde_Date_Exception; -use Horde\Date\Format; /** * @category Horde @@ -76,6 +77,24 @@ public function testConstructor() $date = new Horde_Date($dt); $this->assertEquals('2011-12-10 03:05:06', (string) $date); + $dti = new DateTimeImmutable('2026-06-23 12:00:00', new DateTimeZone('UTC')); + $date = new Horde_Date($dti); + $this->assertEquals(2026, $date->year); + $this->assertEquals(6, $date->month); + $this->assertEquals(23, $date->mday); + + $parsed = Format::parseDateTime( + '23.06.2026 12:00', + '%d.%m.%Y', + 'HH:mm', + 'de_DE' + ); + $date = new Horde_Date($parsed->toDateTimeImmutable()); + $this->assertEquals(2026, $date->year); + $this->assertEquals(6, $date->month); + $this->assertEquals(23, $date->mday); + $this->assertGreaterThan(0, $date->timestamp()); + // Test creating Horde_Date from a string that will use DateTime // internally to parse the date. $date = new Horde_Date('2014-03-20 5:00PM'); From 1800de11fa2b25180bba7e2a993303449d1a6b91 Mon Sep 17 00:00:00 2001 From: Ralf Lang Date: Wed, 24 Jun 2026 06:09:21 +0200 Subject: [PATCH 2/2] test: More complete assertions2 --- lib/Horde/Date.php | 2 +- test/Unnamespaced/DateTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/Horde/Date.php b/lib/Horde/Date.php index 3b14efb..3e061d8 100644 --- a/lib/Horde/Date.php +++ b/lib/Horde/Date.php @@ -1543,7 +1543,7 @@ protected function _initializeFromArray($date) protected function _initializeFromObject($date) { - if ($date instanceof \DateTimeInterface) { + if ($date instanceof DateTimeInterface) { $this->_year = (int) $date->format('Y'); $this->_month = (int) $date->format('m'); $this->_mday = (int) $date->format('d'); diff --git a/test/Unnamespaced/DateTest.php b/test/Unnamespaced/DateTest.php index 2620018..6534ba6 100644 --- a/test/Unnamespaced/DateTest.php +++ b/test/Unnamespaced/DateTest.php @@ -82,6 +82,16 @@ public function testConstructor() $this->assertEquals(2026, $date->year); $this->assertEquals(6, $date->month); $this->assertEquals(23, $date->mday); + $this->assertEquals(12, $date->hour); + $this->assertEquals(0, $date->min); + $this->assertEquals(0, $date->sec); + $this->assertEquals('UTC', $date->timezone); + + /* DateTimeImmutable with a non-UTC timezone must preserve the zone. */ + $dti = new DateTimeImmutable('2026-06-23 12:00:00', new DateTimeZone('America/New_York')); + $date = new Horde_Date($dti); + $this->assertEquals('2026-06-23 12:00:00', (string) $date); + $this->assertEquals('America/New_York', $date->timezone); $parsed = Format::parseDateTime( '23.06.2026 12:00',