From 1dff50dd7cfb9f3e40cf0f3ed1235c917f222d01 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 2 Oct 2023 09:46:49 -0400 Subject: [PATCH] Restore Chronos::toNative() This was accidently removed in the shuffle of #417 and #427 Fixes #432 --- src/Chronos.php | 12 ++++++++++++ tests/TestCase/DateTime/StringsTest.php | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Chronos.php b/src/Chronos.php index 4183d39..80d50b7 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -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 * diff --git a/tests/TestCase/DateTime/StringsTest.php b/tests/TestCase/DateTime/StringsTest.php index 37eb170..775a387 100644 --- a/tests/TestCase/DateTime/StringsTest.php +++ b/tests/TestCase/DateTime/StringsTest.php @@ -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')); + } }