Skip to content

Commit 7742812

Browse files
authored
Merge pull request #4633 from MGatner/time-instance
Expand Time for interface
2 parents 8f2effb + 438f420 commit 7742812

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

system/I18n/Time.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use CodeIgniter\I18n\Exceptions\I18nException;
1515
use DateInterval;
1616
use DateTime;
17+
use DateTimeImmutable;
18+
use DateTimeInterface;
1719
use DateTimeZone;
1820
use Exception;
1921
use IntlCalendar;
@@ -57,7 +59,7 @@ class Time extends DateTime
5759
protected static $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i';
5860

5961
/**
60-
* @var \CodeIgniter\I18n\Time|DateTime|null
62+
* @var static|DateTimeInterface|null
6163
*/
6264
protected static $testNow;
6365

@@ -301,15 +303,15 @@ public static function createFromTimestamp(int $timestamp, $timezone = null, str
301303
//--------------------------------------------------------------------
302304

303305
/**
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.
305307
*
306-
* @param DateTime $dateTime
307-
* @param string|null $locale
308+
* @param DateTimeInterface $dateTime
309+
* @param string|null $locale
308310
*
309311
* @return Time
310312
* @throws Exception
311313
*/
312-
public static function instance(DateTime $dateTime, string $locale = null)
314+
public static function createFromInstance(DateTimeInterface $dateTime, string $locale = null)
313315
{
314316
$date = $dateTime->format('Y-m-d H:i:s');
315317
$timezone = $dateTime->getTimezone();
@@ -319,6 +321,25 @@ public static function instance(DateTime $dateTime, string $locale = null)
319321

320322
//--------------------------------------------------------------------
321323

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+
322343
/**
323344
* Converts the current instance to a mutable DateTime object.
324345
*
@@ -341,9 +362,9 @@ public function toDateTime()
341362
* Creates an instance of Time that will be returned during testing
342363
* when calling 'Time::now' instead of the current time.
343364
*
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
347368
*
348369
* @throws Exception
349370
*/
@@ -361,7 +382,7 @@ public static function setTestNow($datetime = null, $timezone = null, string $lo
361382
{
362383
$datetime = new Time($datetime, $timezone, $locale);
363384
}
364-
elseif ($datetime instanceof DateTime && ! $datetime instanceof Time)
385+
elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof Time)
365386
{
366387
$datetime = new Time($datetime->format('Y-m-d H:i:s'), $timezone);
367388
}
@@ -1038,8 +1059,8 @@ public function toLocalizedString(string $format = null)
10381059
* and are not required to be in the same timezone, as both times are
10391060
* converted to UTC and compared that way.
10401061
*
1041-
* @param Time|DateTime|string $testTime
1042-
* @param string|null $timezone
1062+
* @param Time|DateTimeInterface|string $testTime
1063+
* @param string|null $timezone
10431064
*
10441065
* @return boolean
10451066
* @throws Exception
@@ -1060,15 +1081,15 @@ public function equals($testTime, string $timezone = null): bool
10601081
/**
10611082
* Ensures that the times are identical, taking timezone into account.
10621083
*
1063-
* @param Time|DateTime|string $testTime
1064-
* @param string|null $timezone
1084+
* @param Time|DateTimeInterface|string $testTime
1085+
* @param string|null $timezone
10651086
*
10661087
* @return boolean
10671088
* @throws Exception
10681089
*/
10691090
public function sameAs($testTime, string $timezone = null): bool
10701091
{
1071-
if ($testTime instanceof DateTime)
1092+
if ($testTime instanceof DateTimeInterface)
10721093
{
10731094
$testTime = $testTime->format('Y-m-d H:i:s');
10741095
}
@@ -1232,19 +1253,18 @@ public function getUTCObject($time, string $timezone = null)
12321253
{
12331254
if ($time instanceof Time)
12341255
{
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();
12411257
}
12421258
elseif (is_string($time))
12431259
{
12441260
$timezone = $timezone ?: $this->timezone;
12451261
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
12461262
$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'));
12481268
}
12491269

12501270
return $time;

tests/system/I18n/TimeTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,11 @@ public function testSetTimezoneDate()
10041004
}
10051005

10061006
//--------------------------------------------------------------------
1007-
// Missing tests
10081007

1009-
public function testInstance()
1008+
public function testCreateFromInstance()
10101009
{
10111010
$datetime = new DateTime();
1012-
$time = Time::instance($datetime);
1011+
$time = Time::createFromInstance($datetime);
10131012
$this->assertTrue($time instanceof Time);
10141013
$this->assertTrue($time->sameAs($datetime));
10151014
}

user_guide_src/source/changelogs/v4.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Deprecations:
3939
- Deprecated ``FeatureTestCase`` to use the ``FeatureTestTrait`` instead.
4040
- Deprecated ``ControllerTester`` to use the ``ControllerTestTrait`` instead.
4141
- Consolidated and deprecated ``ControllerResponse`` and ``FeatureResponse`` in favor of ``TestResponse``.
42+
- Deprecated ``Time::instance()``, use ``Time::createFromInstance()`` instead (now accepts ``DateTimeInterface``).
4243

4344
Bugs Fixed:
4445

0 commit comments

Comments
 (0)