Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Time::createFromTimestamp() returns Time with UTC #8544

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ public static function createFromFormat($format, $datetime, $timezone = null)
public static function createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null)
{
$time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);
$timezone ??= 'UTC';

$timezone ??= date_default_timezone_get();

return $time->setTimezone($timezone);
}
Expand Down
14 changes: 9 additions & 5 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,21 @@ public function testCreateFromFormatWithInvalidFormat(): void

public function testCreateFromTimestamp(): void
{
// Set the timezone temporarily to UTC to make sure the test timestamp is correct
// Save the current timezone.
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');

$timestamp = strtotime('2017-03-18 midnight');
// Change the timezone other than UTC.
date_default_timezone_set('Asia/Tokyo'); // +09:00

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

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

$this->assertSame(date('2017-03-18 00:00:00'), $time->toDateTimeString());
$this->assertSame('Asia/Tokyo', $time->getTimezone()->getName());
$this->assertSame('2017-03-18 00:00:00', $time->format('Y-m-d H:i:s'));

// Restore timezone.
date_default_timezone_set($tz);
}

public function testCreateFromTimestampWithTimezone(): void
Expand Down
9 changes: 9 additions & 0 deletions user_guide_src/source/changelogs/v4.4.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ Release Date: Unreleased
BREAKING
********

Time::createFromTimestamp()
===========================

A bug that caused :ref:`Time::createFromTimestamp() <time-createfromtimestamp>`
to return a Time instance with a timezone of UTC has been fixed.

Starting with this version, when you do not specify a timezone, a Time instance
with the app's timezone is returned by default.

***************
Message Changes
***************
Expand Down
14 changes: 14 additions & 0 deletions user_guide_src/source/installation/upgrade_446.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ Mandatory File Changes
Breaking Changes
****************

Time::createFromTimestamp() Timezone Change
===========================================

When you do not specify a timezone, now
:ref:`Time::createFromTimestamp() <time-createfromtimestamp>` returns a Time
instance with the app's timezone is returned.

If you want to keep the timezone UTC, you need to call ``setTimezone('UTC')``::

use CodeIgniter\I18n\Time;

$time = Time::createFromTimestamp(1501821586)->setTimezone('UTC');


*********************
Breaking Enhancements
*********************
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/libraries/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ and returns a ``Time`` instance, instead of DateTimeImmutable:

.. literalinclude:: time/011.php

.. _time-createfromtimestamp:

createFromTimestamp()
=====================

This method takes a UNIX timestamp and, optionally, the timezone and locale, to create a new Time instance:

.. literalinclude:: time/012.php

.. note:: Due to a bug, prior to v4.4.6, this method returned a Time instance
in timezone UTC when you do not specify a timezone.

createFromInstance()
====================

Expand Down
Loading