Skip to content

Commit

Permalink
Enforce UTC timestamps, fixes codeigniter4#3951
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed May 11, 2021
1 parent 48365c5 commit 1c70ca6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public static function createFromFormat($format, $datetime, $timeZone = null)
*/
public static function createFromTimestamp(int $timestamp, $timezone = null, string $locale = null)
{
return new Time(date('Y-m-d H:i:s', $timestamp), $timezone, $locale);
return new Time(gmdate('Y-m-d H:i:s', $timestamp), $timezone ?? 'UTC', $locale);
}

//--------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,15 @@ public function testCreateFromFormatWithInvalidFormat()

public function testCreateFromTimestamp()
{
$time = Time::createFromTimestamp(strtotime('2017-03-18 midnight'));
// Se the timezone temporarily to UTC to make sure the test timestamp is correct
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');

$timestamp = strtotime('2017-03-18 midnight');

date_default_timezone_set($tz);

$time = Time::createFromTimestamp($timestamp);

$this->assertEquals(date('2017-03-18 00:00:00'), $time->toDateTimeString());
}
Expand Down

0 comments on commit 1c70ca6

Please sign in to comment.