Skip to content

Commit

Permalink
Merge pull request #433 from cakephp/fix-432
Browse files Browse the repository at this point in the history
Restore Chronos::toNative()
  • Loading branch information
dereuromark authored Oct 2, 2023
2 parents 635fa0c + 1dff50d commit 54164f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,18 @@ public function diffForHumans(?DateTimeInterface $other = null, bool $absolute =
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}

/**
* Returns a DateTimeImmutable instance
*
* This method returns a PHP DateTimeImmutable without Chronos extensions.
*
* @return \DateTimeImmutable
*/
public function toNative(): DateTimeImmutable
{
return new DateTimeImmutable($this->format('Y-m-d H:i:s.u'), $this->getTimezone());
}

/**
* Get a part of the object
*
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase/DateTime/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,13 @@ public function testToWeek($date, $expected)
{
$this->assertSame($expected, (new Chronos($date))->toWeek());
}

public function testToNative(): void
{
$c = Chronos::now();
$native = $c->toNative();
$this->assertSame($c->format(DATE_ATOM), $native->format(DATE_ATOM));
$this->assertEquals($c->getTimezone(), $native->getTimezone());
$this->assertEquals($c->format('u'), $native->format('u'));
}
}

0 comments on commit 54164f6

Please sign in to comment.