Skip to content

Commit

Permalink
Support creating Chronos from ChronosTime
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
othercorey committed Sep 29, 2023
1 parent af68da5 commit a93a7ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ class Chronos extends DateTimeImmutable
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param \Cake\Chronos\ChronosDate|\DateTimeInterface|string|int|null $time Fixed or relative time
* @param \Cake\Chronos\ChronosDate|\Cake\Chronos\ChronosTime|\DateTimeInterface|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $timezone The timezone for the instance
*/
public function __construct(
ChronosDate|DateTimeInterface|string|int|null $time = 'now',
ChronosDate|ChronosTime|DateTimeInterface|string|int|null $time = 'now',
DateTimeZone|string|null $timezone = null
) {
if (is_int($time) || (is_string($time) && ctype_digit($time))) {
Expand Down Expand Up @@ -461,12 +461,12 @@ public static function instance(DateTimeInterface $other): static
* Chronos::parse('Monday next week')->fn() rather than
* (new Chronos('Monday next week'))->fn()
*
* @param \Cake\Chronos\ChronosDate|\DateTimeInterface|string|int|null $time The strtotime compatible string to parse
* @param \Cake\Chronos\ChronosDate|\Cake\Chronos\ChronosTime|\DateTimeInterface|string|int|null $time The strtotime compatible string to parse
* @param \DateTimeZone|string|null $timezone The DateTimeZone object or timezone name.
* @return static
*/
public static function parse(
ChronosDate|DateTimeInterface|string|int|null $time = 'now',
ChronosDate|ChronosTime|DateTimeInterface|string|int|null $time = 'now',
DateTimeZone|string|null $timezone = null
): static {
return new static($time, $timezone);
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/DateTime/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Cake\Chronos\Chronos;
use Cake\Chronos\ChronosDate;
use Cake\Chronos\ChronosTime;
use Cake\Chronos\Test\TestCase\TestCase;
use DateTime;
use DateTimeImmutable;
Expand Down Expand Up @@ -151,6 +152,24 @@ public function testCreateFromChronosDate()
$this->assertSame('2021-01-01 00:00:00', $chronos->format('Y-m-d H:i:s'));
}

public function testCreateFromChronosTime()
{
$time = new ChronosTime('20:14:12.123456');
$chronos = new Chronos($time);
$this->assertSame((string)Chronos::parse('20:14:12.123456'), (string)$chronos);

$chronos = new Chronos($time, 'Asia/Tokyo');
$this->assertSame((string)Chronos::parse('20:14:12.123456'), (string)$chronos);
$this->assertSame('Asia/Tokyo', $chronos->tzName);

$chronos = Chronos::parse($time);
$this->assertSame((string)Chronos::parse('20:14:12.123456'), (string)$chronos);

$chronos = Chronos::parse($time, 'Asia/Tokyo');
$this->assertSame((string)Chronos::parse('20:14:12.123456'), (string)$chronos);
$this->assertSame('Asia/Tokyo', $chronos->tzName);
}

public function testCreateFromDateTimeInterface()
{
$existingClass = new DateTimeImmutable();
Expand Down

0 comments on commit a93a7ba

Please sign in to comment.