14
14
use CodeIgniter \I18n \Exceptions \I18nException ;
15
15
use DateInterval ;
16
16
use DateTime ;
17
+ use DateTimeImmutable ;
18
+ use DateTimeInterface ;
17
19
use DateTimeZone ;
18
20
use Exception ;
19
21
use IntlCalendar ;
@@ -57,7 +59,7 @@ class Time extends DateTime
57
59
protected static $ relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i ' ;
58
60
59
61
/**
60
- * @var \CodeIgniter\I18n\Time|DateTime |null
62
+ * @var static|DateTimeInterface |null
61
63
*/
62
64
protected static $ testNow ;
63
65
@@ -301,15 +303,15 @@ public static function createFromTimestamp(int $timestamp, $timezone = null, str
301
303
//--------------------------------------------------------------------
302
304
303
305
/**
304
- * Takes an instance of DateTime and returns an instance of Time with it's same values.
306
+ * Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
305
307
*
306
- * @param DateTime $dateTime
307
- * @param string|null $locale
308
+ * @param DateTimeInterface $dateTime
309
+ * @param string|null $locale
308
310
*
309
311
* @return Time
310
312
* @throws Exception
311
313
*/
312
- public static function instance ( DateTime $ dateTime , string $ locale = null )
314
+ public static function createFromInstance ( DateTimeInterface $ dateTime , string $ locale = null )
313
315
{
314
316
$ date = $ dateTime ->format ('Y-m-d H:i:s ' );
315
317
$ timezone = $ dateTime ->getTimezone ();
@@ -319,6 +321,25 @@ public static function instance(DateTime $dateTime, string $locale = null)
319
321
320
322
//--------------------------------------------------------------------
321
323
324
+ /**
325
+ * Takes an instance of DateTime and returns an instance of Time with it's same values.
326
+ *
327
+ * @param DateTime $dateTime
328
+ * @param string|null $locale
329
+ *
330
+ * @return Time
331
+ * @throws Exception
332
+ *
333
+ * @deprecated Use createFromInstance() instead
334
+ * @codeCoverageIgnore
335
+ */
336
+ public static function instance (DateTime $ dateTime , string $ locale = null )
337
+ {
338
+ return self ::createFromInstance ($ dateTime , $ locale );
339
+ }
340
+
341
+ //--------------------------------------------------------------------
342
+
322
343
/**
323
344
* Converts the current instance to a mutable DateTime object.
324
345
*
@@ -341,9 +362,9 @@ public function toDateTime()
341
362
* Creates an instance of Time that will be returned during testing
342
363
* when calling 'Time::now' instead of the current time.
343
364
*
344
- * @param Time|DateTime |string|null $datetime
345
- * @param DateTimeZone|string|null $timezone
346
- * @param string|null $locale
365
+ * @param Time|DateTimeInterface |string|null $datetime
366
+ * @param DateTimeZone|string|null $timezone
367
+ * @param string|null $locale
347
368
*
348
369
* @throws Exception
349
370
*/
@@ -361,7 +382,7 @@ public static function setTestNow($datetime = null, $timezone = null, string $lo
361
382
{
362
383
$ datetime = new Time ($ datetime , $ timezone , $ locale );
363
384
}
364
- elseif ($ datetime instanceof DateTime && ! $ datetime instanceof Time)
385
+ elseif ($ datetime instanceof DateTimeInterface && ! $ datetime instanceof Time)
365
386
{
366
387
$ datetime = new Time ($ datetime ->format ('Y-m-d H:i:s ' ), $ timezone );
367
388
}
@@ -1038,8 +1059,8 @@ public function toLocalizedString(string $format = null)
1038
1059
* and are not required to be in the same timezone, as both times are
1039
1060
* converted to UTC and compared that way.
1040
1061
*
1041
- * @param Time|DateTime |string $testTime
1042
- * @param string|null $timezone
1062
+ * @param Time|DateTimeInterface |string $testTime
1063
+ * @param string|null $timezone
1043
1064
*
1044
1065
* @return boolean
1045
1066
* @throws Exception
@@ -1060,15 +1081,15 @@ public function equals($testTime, string $timezone = null): bool
1060
1081
/**
1061
1082
* Ensures that the times are identical, taking timezone into account.
1062
1083
*
1063
- * @param Time|DateTime |string $testTime
1064
- * @param string|null $timezone
1084
+ * @param Time|DateTimeInterface |string $testTime
1085
+ * @param string|null $timezone
1065
1086
*
1066
1087
* @return boolean
1067
1088
* @throws Exception
1068
1089
*/
1069
1090
public function sameAs ($ testTime , string $ timezone = null ): bool
1070
1091
{
1071
- if ($ testTime instanceof DateTime )
1092
+ if ($ testTime instanceof DateTimeInterface )
1072
1093
{
1073
1094
$ testTime = $ testTime ->format ('Y-m-d H:i:s ' );
1074
1095
}
@@ -1232,19 +1253,18 @@ public function getUTCObject($time, string $timezone = null)
1232
1253
{
1233
1254
if ($ time instanceof Time)
1234
1255
{
1235
- $ time = $ time ->toDateTime ()
1236
- ->setTimezone (new DateTimeZone ('UTC ' ));
1237
- }
1238
- elseif ($ time instanceof DateTime)
1239
- {
1240
- $ time = $ time ->setTimezone (new DateTimeZone ('UTC ' ));
1256
+ $ time = $ time ->toDateTime ();
1241
1257
}
1242
1258
elseif (is_string ($ time ))
1243
1259
{
1244
1260
$ timezone = $ timezone ?: $ this ->timezone ;
1245
1261
$ timezone = $ timezone instanceof DateTimeZone ? $ timezone : new DateTimeZone ($ timezone );
1246
1262
$ time = new DateTime ($ time , $ timezone );
1247
- $ time = $ time ->setTimezone (new DateTimeZone ('UTC ' ));
1263
+ }
1264
+
1265
+ if ($ time instanceof DateTime || $ time instanceof DateTimeImmutable)
1266
+ {
1267
+ $ time = $ time ->setTimezone (new DateTimeZone ('UTC ' ));
1248
1268
}
1249
1269
1250
1270
return $ time ;
0 commit comments