Skip to content

Commit a2e469d

Browse files
authored
Merge pull request #2990 from Instrye/develop
fix. Time::setTimezone can't work
2 parents a49f617 + 35b12b4 commit a2e469d

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

system/I18n/Time.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,14 +773,15 @@ protected function setValue(string $name, $value)
773773
/**
774774
* Returns a new instance with the revised timezone.
775775
*
776-
* @param \DateTimeZone $timezone
776+
* @param string|\DateTimeZone $timezone
777777
*
778778
* @return \CodeIgniter\I18n\Time
779779
* @throws \Exception
780780
*/
781781
public function setTimezone($timezone)
782782
{
783-
return Time::parse($this->toDateTimeString(), $timezone, $this->locale);
783+
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
784+
return Time::instance($this->toDateTime()->setTimezone($timezone), $this->locale);
784785
}
785786

786787
/**

tests/system/I18n/TimeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,14 @@ public function testHumanizeNow()
974974
$this->assertEquals('Just now', $time->humanize());
975975
}
976976

977+
public function testSetTimezoneDate()
978+
{
979+
$time = Time::parse('13 May 2020 10:00', 'GMT');
980+
$time2 = $time->setTimezone('GMT+8');
981+
$this->assertEquals('2020-05-13 10:00:00', $time->toDateTimeString());
982+
$this->assertEquals('2020-05-13 18:00:00', $time2->toDateTimeString());
983+
}
984+
977985
//--------------------------------------------------------------------
978986
// Missing tests
979987

user_guide_src/source/libraries/time.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,15 @@ setTimezone()
347347

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

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

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

356+
echo $time->toDateTimeString(); // 2020-05-13 10:00:00
357+
echo $time2->toDateTimeString(); // 2020-05-13 18:00:00
358+
356359
setTimestamp()
357360
--------------
358361

0 commit comments

Comments
 (0)