From 91a0c9c920e8346f93116791a40c5a1b028055ad Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Mon, 29 Oct 2018 18:31:10 +0100 Subject: [PATCH] Add toDateTimeImmutable() methods --- src/LocalDate.php | 8 ++++++++ src/LocalDateTime.php | 8 ++++++++ src/LocalTime.php | 8 ++++++++ src/ZonedDateTime.php | 8 ++++++++ tests/LocalDateTest.php | 15 +++++++++++++++ tests/LocalDateTimeTest.php | 15 +++++++++++++++ tests/LocalTimeTest.php | 15 +++++++++++++++ tests/ZonedDateTimeTest.php | 15 +++++++++++++++ 8 files changed, 92 insertions(+) diff --git a/src/LocalDate.php b/src/LocalDate.php index a0d4cb7..9dc44b8 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -802,6 +802,14 @@ public function toDateTime() : \DateTime return $this->atTime(LocalTime::midnight())->toDateTime(); } + /** + * @return \DateTimeImmutable + */ + public function toDateTimeImmutable() : \DateTimeImmutable + { + return \DateTimeImmutable::createFromMutable($this->toDateTime()); + } + /** * Returns the ISO 8601 representation of this LocalDate. * diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index e7609f4..bf6ad36 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -911,6 +911,14 @@ public function toDateTime() : \DateTime return $this->atTimeZone(TimeZone::utc())->toDateTime(); } + /** + * @return \DateTimeImmutable + */ + public function toDateTimeImmutable() : \DateTimeImmutable + { + return \DateTimeImmutable::createFromMutable($this->toDateTime()); + } + /** * @return string */ diff --git a/src/LocalTime.php b/src/LocalTime.php index 1ab4315..79bad51 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -698,6 +698,14 @@ public function toDateTime() : \DateTime return $this->atDate(LocalDate::of(0, 1, 1))->toDateTime(); } + /** + * @return \DateTimeImmutable + */ + public function toDateTimeImmutable() : \DateTimeImmutable + { + return \DateTimeImmutable::createFromMutable($this->toDateTime()); + } + /** * Returns this time as a string, such as 10:15. * diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 125b4f2..c512543 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -911,6 +911,14 @@ public function toDateTime() : \DateTime return \DateTime::createFromFormat($format, $dateTime, $dateTimeZone); } + /** + * @return \DateTimeImmutable + */ + public function toDateTimeImmutable() : \DateTimeImmutable + { + return \DateTimeImmutable::createFromMutable($this->toDateTime()); + } + /** * @return string */ diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index f80353d..0fc85d1 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -1298,6 +1298,21 @@ public function testToDateTime(string $dateTime, string $expected) $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); } + /** + * @dataProvider providerToDateTime + * + * @param string $dateTime The date-time string that will be parse()d by LocalDate. + * @param string $expected The expected output from the native DateTime object. + */ + public function testToDateTimeImmutable(string $dateTime, string $expected) + { + $zonedDateTime = LocalDate::parse($dateTime); + $dateTime = $zonedDateTime->toDateTimeImmutable(); + + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); + $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); + } + /** * @return array */ diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index a3e5590..1711a44 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -1272,6 +1272,21 @@ public function testToDateTime(string $dateTime, string $expected) $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); } + /** + * @dataProvider providerToDateTime + * + * @param string $dateTime The date-time string that will be parse()d by LocalDateTime. + * @param string $expected The expected output from the native DateTime object. + */ + public function testToDateTimeImmutable(string $dateTime, string $expected) + { + $zonedDateTime = LocalDateTime::parse($dateTime); + $dateTime = $zonedDateTime->toDateTimeImmutable(); + + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); + $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); + } + /** * @return array */ diff --git a/tests/LocalTimeTest.php b/tests/LocalTimeTest.php index 98ce3a7..3a1632d 100644 --- a/tests/LocalTimeTest.php +++ b/tests/LocalTimeTest.php @@ -1174,6 +1174,21 @@ public function testToDateTime(string $dateTime, string $expected) $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); } + /** + * @dataProvider providerToDateTime + * + * @param string $dateTime The date-time string that will be parse()d by LocalTime. + * @param string $expected The expected output from the native DateTime object. + */ + public function testToDateTimeImmutable(string $dateTime, string $expected) + { + $zonedDateTime = LocalTime::parse($dateTime); + $dateTime = $zonedDateTime->toDateTimeImmutable(); + + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); + $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); + } + /** * @return array */ diff --git a/tests/ZonedDateTimeTest.php b/tests/ZonedDateTimeTest.php index 8a85565..0c6d520 100644 --- a/tests/ZonedDateTimeTest.php +++ b/tests/ZonedDateTimeTest.php @@ -795,6 +795,21 @@ public function testToDateTime(string $dateTime, string $expected) $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); } + /** + * @dataProvider providerToDateTime + * + * @param string $dateTime The date-time string that will be parse()d by ZonedDateTime. + * @param string $expected The expected output from the native DateTime object. + */ + public function testToDateTimeImmutable(string $dateTime, string $expected) + { + $zonedDateTime = ZonedDateTime::parse($dateTime); + $dateTime = $zonedDateTime->toDateTimeImmutable(); + + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); + $this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO')); + } + /** * @return array */