Skip to content

Commit f3a9ebb

Browse files
committed
fix!: Time::setTimestamp()'s different behavior than DateTime
1 parent 0a15e7b commit f3a9ebb

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

system/I18n/TimeTrait.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,7 @@ public function setTimezone($timezone)
697697
#[ReturnTypeWillChange]
698698
public function setTimestamp($timestamp)
699699
{
700-
$time = date('Y-m-d H:i:s', $timestamp);
701-
702-
return static::parse($time, $this->timezone, $this->locale);
700+
return parent::setTimestamp($timestamp);
703701
}
704702

705703
// --------------------------------------------------------------------

tests/system/I18n/TimeTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Test\CIUnitTestCase;
1919
use Config\App;
2020
use DateTime;
21+
use DateTimeImmutable;
2122
use DateTimeZone;
2223
use IntlDateFormatter;
2324
use Locale;
@@ -719,13 +720,28 @@ public function testSetTimezone(): void
719720

720721
public function testSetTimestamp(): void
721722
{
722-
$time = Time::parse('May 10, 2017', 'America/Chicago');
723-
$stamp = strtotime('April 1, 2017');
724-
$time2 = $time->setTimestamp($stamp);
723+
$time1 = Time::parse('May 10, 2017', 'America/Chicago');
724+
725+
$stamp = strtotime('2017-04-01'); // We use UTC as the default timezone.
726+
$time2 = $time1->setTimestamp($stamp);
725727

726728
$this->assertInstanceOf(Time::class, $time2);
727-
$this->assertNotSame($time, $time2);
728-
$this->assertSame('2017-04-01 00:00:00', $time2->toDateTimeString());
729+
$this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P'));
730+
$this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P'));
731+
}
732+
733+
public function testSetTimestampDateTimeImmutable(): void
734+
{
735+
$time1 = new DateTimeImmutable(
736+
'May 10, 2017',
737+
new DateTimeZone('America/Chicago')
738+
);
739+
740+
$stamp = strtotime('2017-04-01'); // We use UTC as the default timezone.
741+
$time2 = $time1->setTimestamp($stamp);
742+
743+
$this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P'));
744+
$this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P'));
729745
}
730746

731747
public function testToDateString(): void

0 commit comments

Comments
 (0)