Skip to content

Commit

Permalink
fix!: Time::setTimestamp()'s different behavior than DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 6, 2024
1 parent bc61694 commit 2030114
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 1 addition & 3 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// --------------------------------------------------------------------
Expand Down
26 changes: 21 additions & 5 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use IntlDateFormatter;
use Locale;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2030114

Please sign in to comment.