diff --git a/src/DateTime.php b/src/DateTime.php index 5cdc35b..c57aa32 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -6,6 +6,7 @@ use DateInterval; use DateTime as DateTimePhp; +use DateTimeImmutable; use DateTimeZone; use Tuscanicz\DateTimeBundle\Date\Date; use Tuscanicz\DateTimeBundle\Time\Time; @@ -53,6 +54,11 @@ public function toDateTime(): DateTimePhp return new DateTimePhp($this->toFormat('r')); } + public function toDateTimeImmutable(): DateTimeImmutable + { + return DateTimeImmutable::createFromMutable($this->toDateTime()); + } + public function isBetween(DateTime $start, DateTime $end): bool { $thisDateTime = $this->toDateTime(); diff --git a/src/DateTimeFactory.php b/src/DateTimeFactory.php index af0243c..05113fc 100644 --- a/src/DateTimeFactory.php +++ b/src/DateTimeFactory.php @@ -5,6 +5,7 @@ namespace Tuscanicz\DateTimeBundle; use DateTime as DateTimePhp; +use DateTimeInterface; use DateTimeZone; use Tuscanicz\DateTimeBundle\Date\Date; use Tuscanicz\DateTimeBundle\Time\Time; @@ -20,7 +21,7 @@ public function now(string $timezone = self::TIMEZONE_GMT): DateTime $timezonePhp = new DateTimeZone($timezone); $datetimePhp = new DateTimePhp('now', $timezonePhp); - return $this->fromDateTimePhp($datetimePhp, $timezone); + return $this->fromDateTimeInterface($datetimePhp, $timezone); } public function fromTimestamp(int $timestamp, string $timezone = self::TIMEZONE_GMT): DateTime @@ -29,10 +30,10 @@ public function fromTimestamp(int $timestamp, string $timezone = self::TIMEZONE_ $datetimePhp = new DateTimePhp('now', $timezonePhp); $datetimePhp->setTimestamp($timestamp); - return $this->fromDateTimePhp($datetimePhp, $timezone); + return $this->fromDateTimeInterface($datetimePhp, $timezone); } - private function fromDateTimePhp(DateTimePhp $dateTimePhp, string $timezone = DateTimeFactory::TIMEZONE_GMT): DateTime + public function fromDateTimeInterface(DateTimeInterface $dateTimePhp, string $timezone = DateTimeFactory::TIMEZONE_GMT): DateTime { return new DateTime( new Date( diff --git a/tests/DateTimeFactoryTest.php b/tests/DateTimeFactoryTest.php index 3e702c0..541dde5 100644 --- a/tests/DateTimeFactoryTest.php +++ b/tests/DateTimeFactoryTest.php @@ -4,6 +4,8 @@ namespace Tuscanicz\DateTimeBundle; +use DateTime as DateTimePhp; +use DateTimeImmutable; use PHPUnit\Framework\TestCase; use Tuscanicz\DateTimeBundle\Date\Date; use Tuscanicz\DateTimeBundle\Time\Time; @@ -36,4 +38,26 @@ public function testFromTimestamp(): void self::assertEquals($expectedDateTime, $dateTime); } + + public function testFromDateTimeInterfaceWithDateTime(): void + { + $expectedDateTime = new DateTime(new Date(2009, 2, 13), new Time(23, 31, 30)); + $dateTime = $this->dateTimeFactory->fromDateTimeInterface( + new DateTimePhp('2009-02-13 23:31:30'), + self::TIMEZONE_GMT + ); + + self::assertEquals($expectedDateTime, $dateTime); + } + + public function testFromDateTimeInterfaceWithDateTimeImmutable(): void + { + $expectedDateTime = new DateTime(new Date(2009, 2, 13), new Time(23, 31, 30)); + $dateTime = $this->dateTimeFactory->fromDateTimeInterface( + new DateTimeImmutable('2009-02-13 23:31:30'), + self::TIMEZONE_GMT + ); + + self::assertEquals($expectedDateTime, $dateTime); + } } diff --git a/tests/DateTimeTest.php b/tests/DateTimeTest.php index de87415..dcbfd55 100644 --- a/tests/DateTimeTest.php +++ b/tests/DateTimeTest.php @@ -4,6 +4,8 @@ namespace Tuscanicz\DateTimeBundle; +use DateTime as DateTimePhp; +use DateTimeImmutable; use PHPUnit\Framework\TestCase; use Tuscanicz\DateTimeBundle\Date\Date; use Tuscanicz\DateTimeBundle\Time\Time; @@ -85,7 +87,7 @@ public function testToTimestamp(): void * @param bool $expectedResult * @dataProvider isBetweenProvider */ - public function testIsBetween(DateTime $now, DateTime $start, DateTime $end, bool $expectedResult) + public function testIsBetween(DateTime $now, DateTime $start, DateTime $end, bool $expectedResult): void { self::assertSame($expectedResult, $now->isBetween($start, $end)); } @@ -104,7 +106,7 @@ public function isBetweenProvider(): array * @param bool $expectedResult * @dataProvider isSameAsDataProvider */ - public function testIsSameAs(DateTime $oneDateTime, DateTime $secondDateTime, bool $expectedResult) + public function testIsSameAs(DateTime $oneDateTime, DateTime $secondDateTime, bool $expectedResult): void { self::assertSame($expectedResult, $oneDateTime->isSameAs($secondDateTime)); } @@ -283,4 +285,30 @@ public function testAddMinutes(): void self::assertEquals($expectedNewDateTime, $newDateTime); } + + public function testToDateTime(): void + { + $dateTime = new DateTime( + new Date(1987, 7, 31), + new Time(11, 19, 0) + ); + + $actualPhpDateTime = $dateTime->toDateTime(); + $expectedNewDateTime = new DateTimePhp('1987-07-31 11:19:00'); + + self::assertEquals($expectedNewDateTime, $actualPhpDateTime); + } + + public function testToDateTimeImmutable(): void + { + $dateTime = new DateTime( + new Date(1987, 7, 3), + new Time(11, 19, 3) + ); + + $actualPhpDateTime = $dateTime->toDateTimeImmutable(); + $expectedNewDateTime = new DateTimeImmutable('1987-07-03 11:19:03'); + + self::assertEquals($expectedNewDateTime, $actualPhpDateTime); + } }