diff --git a/src/Chronos.php b/src/Chronos.php index 3cc8bad..bbb3377 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -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))) { @@ -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); diff --git a/tests/TestCase/DateTime/ConstructTest.php b/tests/TestCase/DateTime/ConstructTest.php index 8e85f9c..f8d1730 100644 --- a/tests/TestCase/DateTime/ConstructTest.php +++ b/tests/TestCase/DateTime/ConstructTest.php @@ -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; @@ -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();