Skip to content
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

Support creating Chronos from ChronosTime #425

Merged
merged 1 commit into from
Sep 29, 2023
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
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 @@
* 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 @@
* 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 Expand Up @@ -2379,7 +2379,7 @@
$other = new static($other->format('Y-m-d H:i:s.u'), $utcTz);

return $source->diffInMonths($other, $absolute);
}

Check failure on line 2382 in src/Chronos.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

ImpureMethodCall

src/Chronos.php:2382:37: ImpureMethodCall: Cannot call a possibly-mutating method DateTimeInterface::format from a mutation-free context (see https://psalm.dev/203)

/**
* Get the difference in weeks
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
Loading