Skip to content

Expand Time for interface #4633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use CodeIgniter\I18n\Exceptions\I18nException;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use IntlCalendar;
Expand Down Expand Up @@ -57,7 +59,7 @@ class Time extends DateTime
protected static $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i';

/**
* @var \CodeIgniter\I18n\Time|DateTime|null
* @var static|DateTimeInterface|null
*/
protected static $testNow;

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

/**
* Takes an instance of DateTime and returns an instance of Time with it's same values.
* Takes an instance of DateTimeInterface and returns an instance of Time with it's same values.
*
* @param DateTime $dateTime
* @param string|null $locale
* @param DateTimeInterface $dateTime
* @param string|null $locale
*
* @return Time
* @throws Exception
*/
public static function instance(DateTime $dateTime, string $locale = null)
public static function createFromInstance(DateTimeInterface $dateTime, string $locale = null)
{
$date = $dateTime->format('Y-m-d H:i:s');
$timezone = $dateTime->getTimezone();
Expand All @@ -319,6 +321,25 @@ public static function instance(DateTime $dateTime, string $locale = null)

//--------------------------------------------------------------------

/**
* Takes an instance of DateTime and returns an instance of Time with it's same values.
*
* @param DateTime $dateTime
* @param string|null $locale
*
* @return Time
* @throws Exception
*
* @deprecated Use createFromInstance() instead
* @codeCoverageIgnore
*/
public static function instance(DateTime $dateTime, string $locale = null)
{
return self::createFromInstance($dateTime, $locale);
}

//--------------------------------------------------------------------

/**
* Converts the current instance to a mutable DateTime object.
*
Expand All @@ -341,9 +362,9 @@ public function toDateTime()
* Creates an instance of Time that will be returned during testing
* when calling 'Time::now' instead of the current time.
*
* @param Time|DateTime|string|null $datetime
* @param DateTimeZone|string|null $timezone
* @param string|null $locale
* @param Time|DateTimeInterface|string|null $datetime
* @param DateTimeZone|string|null $timezone
* @param string|null $locale
*
* @throws Exception
*/
Expand All @@ -361,7 +382,7 @@ public static function setTestNow($datetime = null, $timezone = null, string $lo
{
$datetime = new Time($datetime, $timezone, $locale);
}
elseif ($datetime instanceof DateTime && ! $datetime instanceof Time)
elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof Time)
{
$datetime = new Time($datetime->format('Y-m-d H:i:s'), $timezone);
}
Expand Down Expand Up @@ -1038,8 +1059,8 @@ public function toLocalizedString(string $format = null)
* and are not required to be in the same timezone, as both times are
* converted to UTC and compared that way.
*
* @param Time|DateTime|string $testTime
* @param string|null $timezone
* @param Time|DateTimeInterface|string $testTime
* @param string|null $timezone
*
* @return boolean
* @throws Exception
Expand All @@ -1060,15 +1081,15 @@ public function equals($testTime, string $timezone = null): bool
/**
* Ensures that the times are identical, taking timezone into account.
*
* @param Time|DateTime|string $testTime
* @param string|null $timezone
* @param Time|DateTimeInterface|string $testTime
* @param string|null $timezone
*
* @return boolean
* @throws Exception
*/
public function sameAs($testTime, string $timezone = null): bool
{
if ($testTime instanceof DateTime)
if ($testTime instanceof DateTimeInterface)
{
$testTime = $testTime->format('Y-m-d H:i:s');
}
Expand Down Expand Up @@ -1232,19 +1253,18 @@ public function getUTCObject($time, string $timezone = null)
{
if ($time instanceof Time)
{
$time = $time->toDateTime()
->setTimezone(new DateTimeZone('UTC'));
}
elseif ($time instanceof DateTime)
{
$time = $time->setTimezone(new DateTimeZone('UTC'));
$time = $time->toDateTime();
}
elseif (is_string($time))
{
$timezone = $timezone ?: $this->timezone;
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
$time = new DateTime($time, $timezone);
$time = $time->setTimezone(new DateTimeZone('UTC'));
}

if ($time instanceof DateTime || $time instanceof DateTimeImmutable)
{
$time = $time->setTimezone(new DateTimeZone('UTC'));
}

return $time;
Expand Down
5 changes: 2 additions & 3 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,12 +1004,11 @@ public function testSetTimezoneDate()
}

//--------------------------------------------------------------------
// Missing tests

public function testInstance()
public function testCreateFromInstance()
{
$datetime = new DateTime();
$time = Time::instance($datetime);
$time = Time::createFromInstance($datetime);
$this->assertTrue($time instanceof Time);
$this->assertTrue($time->sameAs($datetime));
}
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Deprecations:
- Deprecated ``FeatureTestCase`` to use the ``FeatureTestTrait`` instead.
- Deprecated ``ControllerTester`` to use the ``ControllerTestTrait`` instead.
- Consolidated and deprecated ``ControllerResponse`` and ``FeatureResponse`` in favor of ``TestResponse``.
- Deprecated ``Time::instance()``, use ``Time::createFromInstance()`` instead (now accepts ``DateTimeInterface``).

Bugs Fixed:

Expand Down