Skip to content

Commit 9f6fd40

Browse files
committed
Typehint on the interface instead of concrete classes.
This lets us cover any userland datetime types as well. Also fix #8146 for FrozenTime objects made from other FrozenTime objects.
1 parent 36ce0d5 commit 9f6fd40

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/I18n/FrozenTime.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Cake\Chronos\Chronos;
1818
use Cake\Chronos\ChronosInterface;
1919
use DateTime;
20+
use DateTimeInterface;
2021
use DateTimeZone;
2122
use IntlDateFormatter;
2223
use JsonSerializable;
@@ -103,7 +104,7 @@ class FrozenTime extends Chronos implements JsonSerializable
103104
*/
104105
public function __construct($time = null, $tz = null)
105106
{
106-
if ($time instanceof DateTime) {
107+
if ($time instanceof DateTimeInterface) {
107108
$tz = $time->getTimeZone();
108109
$time = $time->format('Y-m-d H:i:s');
109110
}

src/I18n/Time.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Cake\Chronos\ChronosInterface;
1818
use Cake\Chronos\MutableDateTime;
1919
use DateTime;
20-
use DateTimeImmutable;
20+
use DateTimeInterface;
2121
use DateTimeZone;
2222
use IntlDateFormatter;
2323
use JsonSerializable;
@@ -103,15 +103,14 @@ class Time extends MutableDateTime implements JsonSerializable
103103
*/
104104
public function __construct($time = null, $tz = null)
105105
{
106-
if ($time instanceof DateTime || $time instanceof DateTimeImmutable) {
106+
if ($time instanceof DateTimeInterface) {
107107
$tz = $time->getTimeZone();
108108
$time = $time->format('Y-m-d H:i:s');
109109
}
110110

111111
if (is_numeric($time)) {
112112
$time = '@' . $time;
113113
}
114-
115114
parent::__construct($time, $tz);
116115
}
117116

tests/TestCase/I18n/DateTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ public static function classNameProvider()
6464
return ['mutable' => ['Cake\I18n\Date'], 'immutable' => ['Cake\I18n\FrozenDate']];
6565
}
6666

67+
/**
68+
* Ensure that instances can be built from other objects.
69+
*
70+
* @dataProvider classNameProvider
71+
* @return void
72+
*/
73+
public function testConstructFromAnotherInstance($class)
74+
{
75+
$time = '2015-01-22';
76+
$frozen = new FrozenDate($time, 'America/Chicago');
77+
$subject = new $class($frozen);
78+
$this->assertEquals($time, $subject->format('Y-m-d'), 'frozen date construction');
79+
80+
$mut = new Date($time, 'America/Chicago');
81+
$subject = new $class($mut);
82+
$this->assertEquals($time, $subject->format('Y-m-d'), 'mutable date construction');
83+
}
84+
6785
/**
6886
* test formatting dates taking in account preferred i18n locale file
6987
*

tests/TestCase/I18n/TimeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ public static function classNameProvider()
7777
return ['mutable' => ['Cake\I18n\Time'], 'immutable' => ['Cake\I18n\FrozenTime']];
7878
}
7979

80+
/**
81+
* Ensure that instances can be built from other objects.
82+
*
83+
* @dataProvider classNameProvider
84+
* @return void
85+
*/
86+
public function testConstructFromAnotherInstance($class)
87+
{
88+
$time = '2015-01-22 10:33:44';
89+
$frozen = new FrozenTime($time, 'America/Chicago');
90+
$subject = new $class($frozen);
91+
$this->assertEquals($time, $subject->format('Y-m-d H:i:s'), 'frozen time construction');
92+
93+
$mut = new Time($time, 'America/Chicago');
94+
$subject = new $class($mut);
95+
$this->assertEquals($time, $subject->format('Y-m-d H:i:s'), 'mutable time construction');
96+
}
97+
8098
/**
8199
* provider for timeAgoInWords() tests
82100
*

0 commit comments

Comments
 (0)