Skip to content

fix. Time::setTimezone can't work #2990

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

Merged
merged 3 commits into from
May 15, 2020
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
5 changes: 3 additions & 2 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,14 +773,15 @@ protected function setValue(string $name, $value)
/**
* Returns a new instance with the revised timezone.
*
* @param \DateTimeZone $timezone
* @param string|\DateTimeZone $timezone
*
* @return \CodeIgniter\I18n\Time
* @throws \Exception
*/
public function setTimezone($timezone)
{
return Time::parse($this->toDateTimeString(), $timezone, $this->locale);
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
return Time::instance($this->toDateTime()->setTimezone($timezone), $this->locale);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,14 @@ public function testHumanizeNow()
$this->assertEquals('Just now', $time->humanize());
}

public function testSetTimezoneDate()
{
$time = Time::parse('13 May 2020 10:00', 'GMT');
$time2 = $time->setTimezone('GMT+8');
$this->assertEquals('2020-05-13 10:00:00', $time->toDateTimeString());
$this->assertEquals('2020-05-13 18:00:00', $time2->toDateTimeString());
}

//--------------------------------------------------------------------
// Missing tests

Expand Down
9 changes: 6 additions & 3 deletions user_guide_src/source/libraries/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,15 @@ setTimezone()

Converts the time from it's current timezone into the new one::

$time = Time::parse('May 10, 2017', 'America/Chicago');
$time = Time::parse('13 May 2020 10:00', 'America/Chicago');
$time2 = $time->setTimezone('Europe/London'); // Returns new instance converted to new timezone

echo $time->timezoneName; // American/Chicago
echo $time2->timezoneName; // Europe/London
echo $time->getTimezoneName(); // American/Chicago
echo $time2->getTimezoneName(); // Europe/London

echo $time->toDateTimeString(); // 2020-05-13 10:00:00
echo $time2->toDateTimeString(); // 2020-05-13 18:00:00

setTimestamp()
--------------

Expand Down