diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index 3257ad06189f..aee5e1b9e00d 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -699,9 +699,7 @@ public function setTimezone($timezone) #[ReturnTypeWillChange] public function setTimestamp($timestamp) { - $time = date('Y-m-d H:i:s', $timestamp); - - return self::parse($time, $this->timezone, $this->locale); + return parent::setTimestamp($timestamp); } // -------------------------------------------------------------------- diff --git a/tests/system/I18n/TimeTest.php b/tests/system/I18n/TimeTest.php index 0e5bc733407c..2799fb805626 100644 --- a/tests/system/I18n/TimeTest.php +++ b/tests/system/I18n/TimeTest.php @@ -18,6 +18,7 @@ use CodeIgniter\Test\CIUnitTestCase; use Config\App; use DateTime; +use DateTimeImmutable; use DateTimeZone; use IntlDateFormatter; use Locale; @@ -708,13 +709,28 @@ public function testSetTimezone(): void public function testSetTimestamp(): void { - $time = Time::parse('May 10, 2017', 'America/Chicago'); - $stamp = strtotime('April 1, 2017'); - $time2 = $time->setTimestamp($stamp); + $time1 = Time::parse('May 10, 2017', 'America/Chicago'); + + $stamp = strtotime('2017-04-01'); // We use UTC as the default timezone. + $time2 = $time1->setTimestamp($stamp); $this->assertInstanceOf(Time::class, $time2); - $this->assertNotSame($time, $time2); - $this->assertSame('2017-04-01 00:00:00', $time2->toDateTimeString()); + $this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P')); + $this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P')); + } + + public function testSetTimestampDateTimeImmutable(): void + { + $time1 = new DateTimeImmutable( + 'May 10, 2017', + new DateTimeZone('America/Chicago') + ); + + $stamp = strtotime('2017-04-01'); // We use UTC as the default timezone. + $time2 = $time1->setTimestamp($stamp); + + $this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P')); + $this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P')); } public function testToDateString(): void