From e8610aed0d492e290fafcfa5c4f22e6d2d977c5e Mon Sep 17 00:00:00 2001 From: Dominik Voda Date: Wed, 16 Dec 2020 13:54:19 +0100 Subject: [PATCH] RFC3339 DateTime format as a default + support for milliseconds (#15) --- src/DateTimeFormatter.php | 65 +++++++++--- src/DateTimeFormatterTest.php | 65 ++++++++---- src/DateTimeFromString.php | 20 ++-- src/DateTimeFromStringTest.php | 141 ++++++++++++++++++++++----- src/DateTimeFromTimestamp.php | 11 ++- src/DateTimeModifierTest.php | 32 +++--- src/Format/Rfc3339ExtendedFormat.php | 22 +++++ src/StringFromNow.php | 2 +- 8 files changed, 278 insertions(+), 80 deletions(-) create mode 100644 src/Format/Rfc3339ExtendedFormat.php diff --git a/src/DateTimeFormatter.php b/src/DateTimeFormatter.php index cdf5683..1a3bfc4 100644 --- a/src/DateTimeFormatter.php +++ b/src/DateTimeFormatter.php @@ -2,6 +2,7 @@ namespace BrandEmbassy\DateTime; +use BrandEmbassy\DateTime\Format\Rfc3339ExtendedFormat; use DateTime; use DateTimeInterface; use DateTimeZone; @@ -10,68 +11,102 @@ final class DateTimeFormatter { - public static function format(DateTimeInterface $dateTime, string $format): string + public static function formatAs(DateTimeInterface $dateTime, string $format): string { return $dateTime->format($format); } - public static function formatTimestamp(int $timestamp, string $format): string + public static function formatTimestampAs(int $timestamp, string $format): string { $dateTimeImmutable = DateTimeFromTimestamp::create($timestamp); - return self::format($dateTimeImmutable, $format); + return self::formatAs($dateTimeImmutable, $format); } - public static function formatAsAtom(DateTimeInterface $dateTime): string + public static function format(DateTimeInterface $dateTime): string { - return self::format($dateTime, DateTime::ATOM); + return self::formatAs($dateTime, DateTime::RFC3339); } - public static function formatTimestampAsAtom(int $timestamp): string + public static function formatWithMilliseconds(DateTimeInterface $dateTime): string + { + return self::formatAs($dateTime, Rfc3339ExtendedFormat::getOutputFormat()); + } + + + public static function formatTimestamp(int $timestamp): string { $dateTimeImmutable = DateTimeFromTimestamp::create($timestamp); - return self::formatAsAtom($dateTimeImmutable); + return self::format($dateTimeImmutable); } - public static function formatInTimezone( + public static function formatTimestampIncludingMilliseconds(int $timestampIncludingMilliseconds): string + { + $dateTimeImmutable = DateTimeFromTimestamp::createIncludingMilliseconds($timestampIncludingMilliseconds); + + return self::formatWithMilliseconds($dateTimeImmutable); + } + + + public static function formatInTimezoneAs( DateTimeInterface $dateTime, DateTimeZone $dateTimeZone, string $format ): string { assert(method_exists($dateTime, 'setTimezone')); - return self::format($dateTime->setTimezone($dateTimeZone), $format); + return self::formatAs($dateTime->setTimezone($dateTimeZone), $format); } - public static function formatInTimezoneAsAtom(DateTimeInterface $dateTime, DateTimeZone $dateTimeZone): string + public static function formatInTimezone(DateTimeInterface $dateTime, DateTimeZone $dateTimeZone): string { assert(method_exists($dateTime, 'setTimezone')); - return self::formatAsAtom($dateTime->setTimezone($dateTimeZone)); + return self::format($dateTime->setTimezone($dateTimeZone)); } - public static function formatTimestampInTimezone( + public static function formatInTimezoneWithMilliseconds( + DateTimeInterface $dateTime, + DateTimeZone $dateTimeZone + ): string { + assert(method_exists($dateTime, 'setTimezone')); + + return self::formatWithMilliseconds($dateTime->setTimezone($dateTimeZone)); + } + + + public static function formatTimestampInTimezoneAs( int $timestamp, DateTimeZone $dateTimeZone, string $format ): string { $dateTimeImmutable = DateTimeFromTimestamp::create($timestamp); - return self::formatInTimezone($dateTimeImmutable, $dateTimeZone, $format); + return self::formatInTimezoneAs($dateTimeImmutable, $dateTimeZone, $format); } - public static function formatTimestampInTimezoneAsAtom(int $timestamp, DateTimeZone $dateTimeZone): string + public static function formatTimestampInTimezone(int $timestamp, DateTimeZone $dateTimeZone): string { $dateTimeImmutable = DateTimeFromTimestamp::create($timestamp); - return self::formatInTimezoneAsAtom($dateTimeImmutable, $dateTimeZone); + return self::formatInTimezone($dateTimeImmutable, $dateTimeZone); + } + + + public static function formatTimestampWithMillisecondsInTimezone( + int $timestampIncludingMilliseconds, + DateTimeZone $dateTimeZone + ): string { + $dateTimeImmutable = DateTimeFromTimestamp::createIncludingMilliseconds($timestampIncludingMilliseconds); + + return self::formatInTimezoneWithMilliseconds($dateTimeImmutable, $dateTimeZone); } } diff --git a/src/DateTimeFormatterTest.php b/src/DateTimeFormatterTest.php index d397c76..44474ff 100644 --- a/src/DateTimeFormatterTest.php +++ b/src/DateTimeFormatterTest.php @@ -6,33 +6,45 @@ use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; -use PHPStan\Testing\TestCase; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\TestCase; final class DateTimeFormatterTest extends TestCase { - private const DATETIME_IN_ATOM = '2017-05-10T12:13:14+02:00'; + private const DATETIME_WITHOUT_MILLISECONDS = '2017-05-10T12:13:14+02:00'; + private const DATETIME_WITH_MILLISECONDS = '2017-05-10T12:13:14.000+02:00'; /** * @dataProvider getDateTimeToFormat */ - public function testFormatAsAtom(DateTimeInterface $dateTime): void + public function testFormat(DateTimeInterface $dateTime): void { - $formattedDateTime = DateTimeFormatter::formatAsAtom($dateTime); + $formattedDateTime = DateTimeFormatter::format($dateTime); - Assert::assertSame(self::DATETIME_IN_ATOM, $formattedDateTime); + Assert::assertSame(self::DATETIME_WITHOUT_MILLISECONDS, $formattedDateTime); } /** * @dataProvider getDateTimeToFormat */ - public function testFormatInTimezone(DateTimeInterface $dateTime): void + public function testFormatWithMilliseconds(DateTimeInterface $dateTime): void + { + $formattedDateTime = DateTimeFormatter::formatWithMilliseconds($dateTime); + + Assert::assertSame(self::DATETIME_WITH_MILLISECONDS, $formattedDateTime); + } + + + /** + * @dataProvider getDateTimeToFormat + */ + public function testFormatInTimezoneAs(DateTimeInterface $dateTime): void { $expectedUtcFormat = '2017-05-10T10:13:14+00:00'; - $formattedDateTime = DateTimeFormatter::formatInTimezone( + $formattedDateTime = DateTimeFormatter::formatInTimezoneAs( $dateTime, new DateTimeZone('UTC'), DateTime::ATOM @@ -45,11 +57,11 @@ public function testFormatInTimezone(DateTimeInterface $dateTime): void /** * @dataProvider getDateTimeToFormat */ - public function testFormatInTimezoneAsAtom(DateTimeInterface $dateTime): void + public function testFormatInTimezone(DateTimeInterface $dateTime): void { $expectedUtcFormat = '2017-05-10T10:13:14+00:00'; - $formattedDateTime = DateTimeFormatter::formatInTimezoneAsAtom($dateTime, new DateTimeZone('UTC')); + $formattedDateTime = DateTimeFormatter::formatInTimezone($dateTime, new DateTimeZone('UTC')); Assert::assertSame($expectedUtcFormat, $formattedDateTime); } @@ -61,15 +73,15 @@ public function testFormatInTimezoneAsAtom(DateTimeInterface $dateTime): void public function getDateTimeToFormat(): array { return [ - 'immutable' => [new DateTimeImmutable(self::DATETIME_IN_ATOM)], - 'muttable' => [new DateTime(self::DATETIME_IN_ATOM)], + 'immutable' => [new DateTimeImmutable(self::DATETIME_WITHOUT_MILLISECONDS)], + 'muttable' => [new DateTime(self::DATETIME_WITHOUT_MILLISECONDS)], ]; } public function testFormatTimestamp(): void { - $formattedDateTime = DateTimeFormatter::formatTimestamp(1496237560, 'Y-m-d'); + $formattedDateTime = DateTimeFormatter::formatTimestampAs(1496237560, 'Y-m-d'); Assert::assertSame('2017-05-31', $formattedDateTime); } @@ -77,15 +89,23 @@ public function testFormatTimestamp(): void public function testFormatTimestampAsAtom(): void { - $formattedDateTime = DateTimeFormatter::formatTimestampAsAtom(1496237560); + $formattedDateTime = DateTimeFormatter::formatTimestamp(1496237560); Assert::assertSame('2017-05-31T13:32:40+00:00', $formattedDateTime); } - public function testFormatTimestampInTimezone(): void + public function testFormatTimestampWithMillis(): void { - $formattedDateTime = DateTimeFormatter::formatTimestampInTimezone( + $formattedDateTime = DateTimeFormatter::formatTimestampIncludingMilliseconds(1496237560456); + + Assert::assertSame('2017-05-31T13:32:40.456+00:00', $formattedDateTime); + } + + + public function testFormatTimestampInTimezoneAs(): void + { + $formattedDateTime = DateTimeFormatter::formatTimestampInTimezoneAs( 1496237560, new DateTimeZone('Europe/Prague'), DateTime::ATOM @@ -95,13 +115,24 @@ public function testFormatTimestampInTimezone(): void } - public function testFormatTimestampInTimezoneAsAtom(): void + public function testFormatTimestampInTimezone(): void { - $formattedDateTime = DateTimeFormatter::formatTimestampInTimezoneAsAtom( + $formattedDateTime = DateTimeFormatter::formatTimestampInTimezone( 1496237560, new DateTimeZone('Europe/Prague') ); Assert::assertSame('2017-05-31T15:32:40+02:00', $formattedDateTime); } + + + public function testFormatTimestampWithMillisecondsInTimezone(): void + { + $formattedDateTime = DateTimeFormatter::formatTimestampWithMillisecondsInTimezone( + 1496237560456, + new DateTimeZone('Europe/Prague') + ); + + Assert::assertSame('2017-05-31T15:32:40.456+02:00', $formattedDateTime); + } } diff --git a/src/DateTimeFromString.php b/src/DateTimeFromString.php index 5c470ec..9bdb5b7 100644 --- a/src/DateTimeFromString.php +++ b/src/DateTimeFromString.php @@ -2,6 +2,7 @@ namespace BrandEmbassy\DateTime; +use BrandEmbassy\DateTime\Format\Rfc3339ExtendedFormat; use DateTime; use DateTimeImmutable; use DateTimeZone; @@ -13,7 +14,7 @@ final class DateTimeFromString /** * @throws InvalidDateTimeStringException */ - public static function create(string $format, string $dateTimeString): DateTimeImmutable + public static function createFromFormat(string $format, string $dateTimeString): DateTimeImmutable { $dateTime = DateTimeImmutable::createFromFormat($format, $dateTimeString); @@ -24,19 +25,24 @@ public static function create(string $format, string $dateTimeString): DateTimeI } - /** - * @throws InvalidDateTimeStringException - */ - public static function createFromAtom(string $dateTimeAtomString): DateTimeImmutable + public static function create(string $dateTimeStringInRfc3339): DateTimeImmutable + { + return self::createFromFormat(DateTime::RFC3339, $dateTimeStringInRfc3339); + } + + + public static function createWithMilliseconds(string $dateTimeStringInRfc3339ExtendedString): DateTimeImmutable { - return self::create(DateTime::ATOM, $dateTimeAtomString); + $format = Rfc3339ExtendedFormat::getInputFormat(); + + return self::createFromFormat($format, $dateTimeStringInRfc3339ExtendedString); } /** * @throws InvalidDateTimeStringException */ - public static function createWithTimezone( + public static function createWithTimezoneFromFormat( string $format, string $dateTimeString, DateTimeZone $timezone diff --git a/src/DateTimeFromStringTest.php b/src/DateTimeFromStringTest.php index 8c96e4d..025727b 100644 --- a/src/DateTimeFromStringTest.php +++ b/src/DateTimeFromStringTest.php @@ -2,86 +2,183 @@ namespace BrandEmbassy\DateTime; +use BrandEmbassy\DateTime\Format\Rfc3339ExtendedFormat; use DateTime; use DateTimeZone; use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; +use function sprintf; -class DateTimeFromStringTest extends TestCase +final class DateTimeFromStringTest extends TestCase { /** - * @dataProvider dateTimeToCreateProvider + * @dataProvider dateTimeInFormatProvider */ - public function testCreateDateTime( + public function testCreateFromFormat( string $expectedDateTime, string $format, string $dateTimeString ): void { - $dateTime = DateTimeFromString::create($format, $dateTimeString); + $rfcFormat = Rfc3339ExtendedFormat::getOutputFormat(); - Assert::assertSame($expectedDateTime, $dateTime->format(DateTime::ATOM)); + $dateTime = DateTimeFromString::createFromFormat($format, $dateTimeString); + + Assert::assertSame($expectedDateTime, $dateTime->format($rfcFormat)); } /** * @return mixed[] */ - public function dateTimeToCreateProvider(): array + public function dateTimeInFormatProvider(): array { + $rfcFormat = Rfc3339ExtendedFormat::getInputFormat(); + return [ 'Unix timestamp' => [ - 'expectedDateTime' => '2017-05-31T13:32:40+00:00', + 'expectedDateTime' => '2017-05-31T13:32:40.000+00:00', 'format' => 'U', 'dateTimeString' => '1496237560', ], 'Negative Unix timestamp' => [ - 'expectedDateTime' => '1969-12-31T23:59:59+00:00', + 'expectedDateTime' => '1969-12-31T23:59:59.000+00:00', 'format' => 'U', 'dateTimeString' => '-1', ], 'ISO 8601 with TZ designators ±hh:mm' => [ - 'expectedDateTime' => '2017-05-10T12:13:14+05:00', + 'expectedDateTime' => '2017-05-10T12:13:14.000+05:00', 'format' => DateTime::ATOM, 'dateTimeString' => '2017-05-10T12:13:14+05:00', ], 'ISO 8601 with TZ designator Z' => [ - 'expectedDateTime' => '2017-05-10T12:13:14+00:00', + 'expectedDateTime' => '2017-05-10T12:13:14.000+00:00', 'format' => DateTime::ATOM, 'dateTimeString' => '2017-05-10T12:13:14Z', ], 'ISO 8601 with TZ designator ±hhmm' => [ - 'expectedDateTime' => '2017-05-10T12:13:14+08:00', + 'expectedDateTime' => '2017-05-10T12:13:14.000+08:00', 'format' => DateTime::ATOM, 'dateTimeString' => '2017-05-10T12:13:14+0800', ], 'ISO 8601 with TZ designator ±hh' => [ - 'expectedDateTime' => '2017-05-10T12:13:14-03:00', + 'expectedDateTime' => '2017-05-10T12:13:14.000-03:00', 'format' => DateTime::ATOM, 'dateTimeString' => '2017-05-10T12:13:14-03', ], + 'RFC3339 with milliseconds' => [ + 'expectedDateTime' => '2017-05-10T12:13:14.314+00:00', + 'format' => $rfcFormat, + 'dateTimeString' => '2017-05-10T12:13:14.314Z', + ], + ]; + } + + + /** + * @dataProvider validDateTimeProvider + */ + public function testCreateDateTime(string $expectedDateTimeString, string $dateTimeString): void + { + $dateTime = DateTimeFromString::create($dateTimeString); + + Assert::assertSame($expectedDateTimeString, $dateTime->format(DateTime::RFC3339_EXTENDED)); + } + + + /** + * @return array> + */ + public function validDateTimeProvider(): array + { + return [ + 'TZ +2h without millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.000+02:00', + 'dateTimeString' => '2017-05-10T12:13:14+02:00', + ], + 'TZ -2h without millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.000-02:00', + 'dateTimeString' => '2017-05-10T12:13:14-02:00', + ], + 'UTC without millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.000+00:00', + 'dateTimeString' => '2017-05-10T12:13:14Z', + ], + ]; + } + + + /** + * @dataProvider validDateTimeWithMillisecondsProvider + */ + public function testCreateDateTimeWithMilliseconds(string $expectedDateTimeString, string $dateTimeString): void + { + $dateTime = DateTimeFromString::createWithMilliseconds($dateTimeString); + + Assert::assertSame($expectedDateTimeString, $dateTime->format(DateTime::RFC3339_EXTENDED)); + } + + + /** + * @return array> + */ + public function validDateTimeWithMillisecondsProvider(): array + { + return [ + 'TZ +2h with millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.345+02:00', + 'dateTimeString' => '2017-05-10T12:13:14.345+02:00', + ], + 'TZ -2h with millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.345-02:00', + 'dateTimeString' => '2017-05-10T12:13:14.345-02:00', + ], + 'UTC with millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.222+00:00', + 'dateTimeString' => '2017-05-10T12:13:14.222Z', + ], + 'Zero millis' => [ + 'expectedDateTimeString' => '2017-05-10T12:13:14.000+00:00', + 'dateTimeString' => '2017-05-10T12:13:14.000+00:00', + ], ]; } - public function testCreateDateTimeFromAtom(): void + public function testThrowExceptionWhenCannotCreateDateTime(): void + { + $this->expectException(InvalidDateTimeStringException::class); + $this->expectExceptionMessage( + "Can't convert '2020-12-05T02:50:16.123+03:00' to datetime using format Y-m-d\TH:i:sP." + ); + + DateTimeFromString::create('2020-12-05T02:50:16.123+03:00'); + } + + + public function testThrowExceptionWhenCannotCreateDateTimeWithMilliseconds(): void { - $dateTimeInAtom = '2017-05-10T12:13:14+02:00'; + $this->expectException(InvalidDateTimeStringException::class); + $this->expectExceptionMessage( + sprintf( + "Can't convert '2020-12-05T02:50:16+03:00' to datetime using format %s.", + Rfc3339ExtendedFormat::getInputFormat() + ) + ); - $dateTime = DateTimeFromString::createFromAtom($dateTimeInAtom); - Assert::assertSame($dateTimeInAtom, $dateTime->format(DateTime::ATOM)); + DateTimeFromString::createWithMilliseconds('2020-12-05T02:50:16+03:00'); } /** - * @dataProvider dateTimeWithTimeZoneToCreateProvider + * @dataProvider dateTimeWithTimeZoneAndFormatToCreateProvider */ - public function testCreateDateTimeWithTimezone( + public function testCreateDateTimeWithTimezoneFromFormat( string $expectedDateTime, string $format, string $dateTimeString, DateTimeZone $timeZone ): void { - $dateTime = DateTimeFromString::createWithTimezone($format, $dateTimeString, $timeZone); + $dateTime = DateTimeFromString::createWithTimezoneFromFormat($format, $dateTimeString, $timeZone); Assert::assertSame($expectedDateTime, $dateTime->format(DateTime::ATOM)); } @@ -90,7 +187,7 @@ public function testCreateDateTimeWithTimezone( /** * @return mixed[] */ - public function dateTimeWithTimeZoneToCreateProvider(): array + public function dateTimeWithTimeZoneAndFormatToCreateProvider(): array { return [ [ @@ -120,7 +217,7 @@ public function testThrowExceptionWhenCantCreateDateTime( $this->expectException(InvalidDateTimeStringException::class); $this->expectExceptionMessage($expectedExceptionMessage); - DateTimeFromString::create($format, $dateTimeString); + DateTimeFromString::createFromFormat($format, $dateTimeString); } @@ -135,7 +232,7 @@ public function testThrowExceptionWhenCantCreateDateTimeWithTimeZone( $this->expectException(InvalidDateTimeStringException::class); $this->expectExceptionMessage($expectedExceptionMessage); - DateTimeFromString::createWithTimezone($format, $dateTimeString, new DateTimeZone('Europe/Prague')); + DateTimeFromString::createWithTimezoneFromFormat($format, $dateTimeString, new DateTimeZone('Europe/Prague')); } diff --git a/src/DateTimeFromTimestamp.php b/src/DateTimeFromTimestamp.php index a728aef..4c5f085 100644 --- a/src/DateTimeFromTimestamp.php +++ b/src/DateTimeFromTimestamp.php @@ -16,7 +16,7 @@ public static function create(int $timestamp): DateTimeImmutable $dateTime = DateTimeImmutable::createFromFormat('U', (string)$timestamp); if (!$dateTime instanceof DateTimeImmutable) { - throw new InvalidArgumentException(sprintf('Can\'t convert timestamp %s to datetime.', $timestamp)); + throw new InvalidArgumentException(sprintf('Can\'t convert timestamp %d to datetime.', $timestamp)); } return $dateTime; @@ -28,6 +28,13 @@ public static function create(int $timestamp): DateTimeImmutable */ public static function createIncludingMilliseconds(int $milliseconds): DateTimeImmutable { - return self::create((int)($milliseconds / 1000)); + $timestampWithMilliseconds = sprintf('%.3f', $milliseconds / 1000); + $dateTime = DateTimeImmutable::createFromFormat('U.u', $timestampWithMilliseconds); + + if (!$dateTime instanceof DateTimeImmutable) { + throw new InvalidArgumentException(sprintf('Can\'t convert timestamp %d to datetime.', $milliseconds)); + } + + return $dateTime; } } diff --git a/src/DateTimeModifierTest.php b/src/DateTimeModifierTest.php index faa85e6..5fc5fb3 100644 --- a/src/DateTimeModifierTest.php +++ b/src/DateTimeModifierTest.php @@ -13,12 +13,12 @@ final class DateTimeModifierTest extends TestCase */ public function testGetBeginningOfTheDay(string $expectedAtom, string $originAtom): void { - $originDateTime = DateTimeFromString::createFromAtom($originAtom); - $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + $originDateTime = DateTimeFromString::create($originAtom); + $expectedDateTime = DateTimeFromString::create($expectedAtom); Assert::assertSame( - DateTimeFormatter::formatAsAtom($expectedDateTime), - DateTimeFormatter::formatAsAtom(DateTimeModifier::getBeginningOfTheDay($originDateTime)) + DateTimeFormatter::format($expectedDateTime), + DateTimeFormatter::format(DateTimeModifier::getBeginningOfTheDay($originDateTime)) ); } @@ -47,14 +47,14 @@ public function testGetBeginningOfTheDayInTimezone( string $originAtom, string $dateTimeZoneName ): void { - $originDateTime = DateTimeFromString::createFromAtom($originAtom); - $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + $originDateTime = DateTimeFromString::create($originAtom); + $expectedDateTime = DateTimeFromString::create($expectedAtom); $dateTimeZone = new DateTimeZone($dateTimeZoneName); Assert::assertSame( - DateTimeFormatter::formatAsAtom($expectedDateTime), - DateTimeFormatter::formatAsAtom( + DateTimeFormatter::format($expectedDateTime), + DateTimeFormatter::format( DateTimeModifier::getBeginningOfTheDayInTimezone($originDateTime, $dateTimeZone) ) ); @@ -96,12 +96,12 @@ public function getBeginningOfTheDayInTimezoneDataProvider(): array */ public function testGetEndOfTheDay(string $expectedAtom, string $originAtom): void { - $originDateTime = DateTimeFromString::createFromAtom($originAtom); - $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + $originDateTime = DateTimeFromString::create($originAtom); + $expectedDateTime = DateTimeFromString::create($expectedAtom); Assert::assertSame( - DateTimeFormatter::formatAsAtom($expectedDateTime), - DateTimeFormatter::formatAsAtom(DateTimeModifier::getEndOfTheDay($originDateTime)) + DateTimeFormatter::format($expectedDateTime), + DateTimeFormatter::format(DateTimeModifier::getEndOfTheDay($originDateTime)) ); } @@ -130,14 +130,14 @@ public function testGetEndOfTheDayInTimezone( string $originAtom, string $dateTimeZoneName ): void { - $originDateTime = DateTimeFromString::createFromAtom($originAtom); - $expectedDateTime = DateTimeFromString::createFromAtom($expectedAtom); + $originDateTime = DateTimeFromString::create($originAtom); + $expectedDateTime = DateTimeFromString::create($expectedAtom); $dateTimeZone = new DateTimeZone($dateTimeZoneName); Assert::assertSame( - DateTimeFormatter::formatAsAtom($expectedDateTime), - DateTimeFormatter::formatAsAtom( + DateTimeFormatter::format($expectedDateTime), + DateTimeFormatter::format( DateTimeModifier::getEndOfTheDayInTimezone($originDateTime, $dateTimeZone) ) ); diff --git a/src/Format/Rfc3339ExtendedFormat.php b/src/Format/Rfc3339ExtendedFormat.php new file mode 100644 index 0000000..01d7a6c --- /dev/null +++ b/src/Format/Rfc3339ExtendedFormat.php @@ -0,0 +1,22 @@ += 70300 + ? DateTime::RFC3339_EXTENDED + : 'Y-m-d\TH:i:s.uP'; + } +} diff --git a/src/StringFromNow.php b/src/StringFromNow.php index 6b95659..336ab91 100644 --- a/src/StringFromNow.php +++ b/src/StringFromNow.php @@ -22,6 +22,6 @@ public function formatNowInTimezone(DateTimeZone $dateTimeZone, string $format): { $now = $this->dateTimeImmutableFactory->getNow(); - return DateTimeFormatter::formatInTimezone($now, $dateTimeZone, $format); + return DateTimeFormatter::formatInTimezoneAs($now, $dateTimeZone, $format); } }