From 2f8c591e72dce880f31b2e5e6f8018dcf15bf0a9 Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Tue, 14 Mar 2023 03:35:37 -0500 Subject: [PATCH] Add toNative() helpers --- src/Chronos.php | 10 ++++++++++ src/ChronosDate.php | 10 ++++++++++ tests/TestCase/Date/StringsTest.php | 11 +++++++++++ tests/TestCase/DateTime/StringsTest.php | 6 ++++++ 4 files changed, 37 insertions(+) diff --git a/src/Chronos.php b/src/Chronos.php index 96fc8699..66083657 100644 --- a/src/Chronos.php +++ b/src/Chronos.php @@ -2471,6 +2471,16 @@ public function diffForHumans(?Chronos $other = null, bool $absolute = false): s return static::diffFormatter()->diffForHumans($this, $other, $absolute); } + /** + * Returns the time as a DateTimeImmutable instance. + * + * @return \DateTimeImmutable + */ + public function toNative(): DateTimeImmutable + { + return $this->native; + } + /** * Get a part of the object * diff --git a/src/ChronosDate.php b/src/ChronosDate.php index 8fe20fea..2a5c1ddd 100644 --- a/src/ChronosDate.php +++ b/src/ChronosDate.php @@ -1486,6 +1486,16 @@ public function diffForHumans(?ChronosDate $other = null, bool $absolute = false return static::diffFormatter()->diffForHumans($this, $other, $absolute); } + /** + * Returns the date as a DateTimeImmutable instance. + * + * @return \DateTimeImmutable + */ + public function toNative(): DateTimeImmutable + { + return $this->native; + } + /** * Get a part of the object * diff --git a/tests/TestCase/Date/StringsTest.php b/tests/TestCase/Date/StringsTest.php index 2d34946f..7f004bb1 100644 --- a/tests/TestCase/Date/StringsTest.php +++ b/tests/TestCase/Date/StringsTest.php @@ -20,6 +20,11 @@ class StringsTest extends TestCase { + /** + * @var string + */ + protected $tz; + /** * Setup * @@ -167,4 +172,10 @@ public function testToW3cString() $d = ChronosDate::create(1975, 12, 25, 14, 15, 16); $this->assertSame('1975-12-25T00:00:00+00:00', $d->toW3cString()); } + + public function testToNative(): void + { + $d = ChronosDate::now(); + $this->assertSame($d->format('Y-m-d'), $d->toNative()->format('Y-m-d')); + } } diff --git a/tests/TestCase/DateTime/StringsTest.php b/tests/TestCase/DateTime/StringsTest.php index 510b930f..e56f220c 100644 --- a/tests/TestCase/DateTime/StringsTest.php +++ b/tests/TestCase/DateTime/StringsTest.php @@ -213,4 +213,10 @@ public function testToWeek($date, $expected) { $this->assertSame($expected, (new Chronos($date))->toWeek()); } + + public function testToNative(): void + { + $c = Chronos::now(); + $this->assertSame($c->format(DATE_ATOM), $c->toNative()->format(DATE_ATOM)); + } }