From a109b3cdd51742bb119d078c57706e50760c28c4 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 22 Apr 2023 20:14:04 +0100 Subject: [PATCH] Revert "Better support RFC3339 (#47)" This reverts commit 298474a46cf97bc91b121ec8506e93a0366e73a2. --- .github/workflows/static.yml | 4 +- .gitignore | 1 - phpunit.xml.dist | 7 ++-- src/Utilities/TimeFormatter.php | 45 ++++++---------------- tests/Unit/Utilities/TimeFormatterTest.php | 9 +---- 5 files changed, 17 insertions(+), 49 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 135bab5..cb5e2d0 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -17,7 +17,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.2' + php-version: '7.4' tools: composer:v2 coverage: none env: @@ -44,7 +44,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.2' + php-version: '7.4' tools: composer:v2 coverage: none env: diff --git a/.gitignore b/.gitignore index 4b21b4a..7ea05c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .phpunit.cache -.phpunit.result.cache composer.lock phpcs.xml diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0f08581..39df3df 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,13 @@ - + tests/Unit - - + src - + diff --git a/src/Utilities/TimeFormatter.php b/src/Utilities/TimeFormatter.php index 5884226..b166608 100644 --- a/src/Utilities/TimeFormatter.php +++ b/src/Utilities/TimeFormatter.php @@ -16,6 +16,9 @@ final class TimeFormatter private const TIME_FORMAT = 'Y-m-d\TH:i:s\Z'; private const TIME_ZONE = 'UTC'; + private const RFC3339_FORMAT = 'Y-m-d\TH:i:sP'; + private const RFC3339_EXTENDED_FORMAT = 'Y-m-d\TH:i:s.uP'; + public static function encode(?DateTimeImmutable $time): ?string { if ($time === null) { @@ -31,45 +34,19 @@ public static function decode(?string $time): ?DateTimeImmutable return null; } - $time = self::trimMicroseconds($time); + /** @psalm-suppress UndefinedFunction */ + $decoded = DateTimeImmutable::createFromFormat( + \str_contains($time, '.') ? self::RFC3339_EXTENDED_FORMAT : self::RFC3339_FORMAT, + \strtoupper($time), + new DateTimeZone(self::TIME_ZONE) + ); - try { - $decoded = new DateTimeImmutable($time); - } catch (\Throwable $th) { + if ($decoded === false) { throw new ValueError( \sprintf('%s(): Argument #1 ($time) is not a valid RFC3339 timestamp', __METHOD__) ); } - return self::shiftWithTimezone($time, $decoded); - } - - private static function trimMicroseconds(string $time): string - { - $microseconds = explode('.', $time, 2); - if (isset($microseconds[1])) { - $microsecondsAndTimezone = explode('+', $microseconds[1], 2); - if (count($microsecondsAndTimezone) === 1) { - $microsecondsAndTimezone = explode('-', $microseconds[1], 2); - } - $timezone = isset($microsecondsAndTimezone[1]) ? sprintf('+%s', $microsecondsAndTimezone[1]) : ''; - $time = sprintf( - "%s.%s%s", - $microseconds[0], - substr($microsecondsAndTimezone[0], 0, 6), - $timezone - ); - } - - return $time; - } - - private static function shiftWithTimezone(string $time, DateTimeImmutable $datetime): DateTimeImmutable - { - if (\strpos($time, '+') === false && \strpos($time, '-') === false && \strtoupper(\substr($time, -1)) !== 'Z') { - return $datetime->setTimezone(new \DateTimeZone('UTC')); - } - - return $datetime; + return $decoded; } } diff --git a/tests/Unit/Utilities/TimeFormatterTest.php b/tests/Unit/Utilities/TimeFormatterTest.php index 4152fd3..be3514e 100644 --- a/tests/Unit/Utilities/TimeFormatterTest.php +++ b/tests/Unit/Utilities/TimeFormatterTest.php @@ -19,7 +19,7 @@ public function testEncode(): void ); } - public static function providesDecodeCases(): array + public function providesDecodeCases(): array { return [ // UTC @@ -36,9 +36,6 @@ public static function providesDecodeCases(): array ['1985-04-12T23:20:50.123450Z', '1985-04-12T23:20:50.12345Z'], ['1985-04-12T23:20:50.123450Z', '1985-04-12T23:20:50.123450Z'], ['1985-04-12T23:20:50.123456Z', '1985-04-12T23:20:50.123456Z'], - ['1985-04-12T23:20:50.123456Z', '1985-04-12T23:20:50.1234567Z'], - ['1985-04-12T23:20:50.123456Z', '1985-04-12T23:20:50.12345678Z'], - ['1985-04-12T23:20:50.123456Z', '1985-04-12T23:20:50.123456789Z'], // +01:00 ['2018-04-05T16:31:00Z', '2018-04-05T17:31:00+01:00'], @@ -54,10 +51,6 @@ public static function providesDecodeCases(): array ['1985-04-12T22:20:50.123450Z', '1985-04-12T23:20:50.12345+01:00'], ['1985-04-12T22:20:50.123450Z', '1985-04-12T23:20:50.123450+01:00'], ['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.123456+01:00'], - ['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.1234567+01:00'], - ['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.12345678+01:00'], - ['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.123456789+01:00'], - ['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.1234567890+01:00'], ]; }