Skip to content

Commit 2030114

Browse files
committed
fix!: Time::setTimestamp()'s different behavior than DateTime
1 parent bc61694 commit 2030114

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
@@ -699,9 +699,7 @@ public function setTimezone($timezone)
699699
#[ReturnTypeWillChange]
700700
public function setTimestamp($timestamp)
701701
{
702-
$time = date('Y-m-d H:i:s', $timestamp);
703-
704-
return self::parse($time, $this->timezone, $this->locale);
702+
return parent::setTimestamp($timestamp);
705703
}
706704

707705
// --------------------------------------------------------------------

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;
@@ -708,13 +709,28 @@ public function testSetTimezone(): void
708709

709710
public function testSetTimestamp(): void
710711
{
711-
$time = Time::parse('May 10, 2017', 'America/Chicago');
712-
$stamp = strtotime('April 1, 2017');
713-
$time2 = $time->setTimestamp($stamp);
712+
$time1 = Time::parse('May 10, 2017', 'America/Chicago');
713+
714+
$stamp = strtotime('2017-04-01'); // We use UTC as the default timezone.
715+
$time2 = $time1->setTimestamp($stamp);
714716

715717
$this->assertInstanceOf(Time::class, $time2);
716-
$this->assertNotSame($time, $time2);
717-
$this->assertSame('2017-04-01 00:00:00', $time2->toDateTimeString());
718+
$this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P'));
719+
$this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P'));
720+
}
721+
722+
public function testSetTimestampDateTimeImmutable(): void
723+
{
724+
$time1 = new DateTimeImmutable(
725+
'May 10, 2017',
726+
new DateTimeZone('America/Chicago')
727+
);
728+
729+
$stamp = strtotime('2017-04-01'); // We use UTC as the default timezone.
730+
$time2 = $time1->setTimestamp($stamp);
731+
732+
$this->assertSame('2017-05-10 00:00:00 -05:00', $time1->format('Y-m-d H:i:s P'));
733+
$this->assertSame('2017-03-31 19:00:00 -05:00', $time2->format('Y-m-d H:i:s P'));
718734
}
719735

720736
public function testToDateString(): void

0 commit comments

Comments
 (0)