File tree Expand file tree Collapse file tree 4 files changed +40
-4
lines changed Expand file tree Collapse file tree 4 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 17
17
use Cake \Chronos \Chronos ;
18
18
use Cake \Chronos \ChronosInterface ;
19
19
use DateTime ;
20
+ use DateTimeInterface ;
20
21
use DateTimeZone ;
21
22
use IntlDateFormatter ;
22
23
use JsonSerializable ;
@@ -103,7 +104,7 @@ class FrozenTime extends Chronos implements JsonSerializable
103
104
*/
104
105
public function __construct ($ time = null , $ tz = null )
105
106
{
106
- if ($ time instanceof DateTime ) {
107
+ if ($ time instanceof DateTimeInterface ) {
107
108
$ tz = $ time ->getTimeZone ();
108
109
$ time = $ time ->format ('Y-m-d H:i:s ' );
109
110
}
Original file line number Diff line number Diff line change 17
17
use Cake \Chronos \ChronosInterface ;
18
18
use Cake \Chronos \MutableDateTime ;
19
19
use DateTime ;
20
- use DateTimeImmutable ;
20
+ use DateTimeInterface ;
21
21
use DateTimeZone ;
22
22
use IntlDateFormatter ;
23
23
use JsonSerializable ;
@@ -103,15 +103,14 @@ class Time extends MutableDateTime implements JsonSerializable
103
103
*/
104
104
public function __construct ($ time = null , $ tz = null )
105
105
{
106
- if ($ time instanceof DateTime || $ time instanceof DateTimeImmutable ) {
106
+ if ($ time instanceof DateTimeInterface ) {
107
107
$ tz = $ time ->getTimeZone ();
108
108
$ time = $ time ->format ('Y-m-d H:i:s ' );
109
109
}
110
110
111
111
if (is_numeric ($ time )) {
112
112
$ time = '@ ' . $ time ;
113
113
}
114
-
115
114
parent ::__construct ($ time , $ tz );
116
115
}
117
116
Original file line number Diff line number Diff line change @@ -64,6 +64,24 @@ public static function classNameProvider()
64
64
return ['mutable ' => ['Cake\I18n\Date ' ], 'immutable ' => ['Cake\I18n\FrozenDate ' ]];
65
65
}
66
66
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
+
67
85
/**
68
86
* test formatting dates taking in account preferred i18n locale file
69
87
*
Original file line number Diff line number Diff line change @@ -77,6 +77,24 @@ public static function classNameProvider()
77
77
return ['mutable ' => ['Cake\I18n\Time ' ], 'immutable ' => ['Cake\I18n\FrozenTime ' ]];
78
78
}
79
79
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
+
80
98
/**
81
99
* provider for timeAgoInWords() tests
82
100
*
You can’t perform that action at this time.
0 commit comments