Skip to content

Commit

Permalink
better support rfc3339
Browse files Browse the repository at this point in the history
Additional support for microsecond values greater than 6 digits.

Signed-off-by: John Laswell <[email protected]>
  • Loading branch information
jlaswell committed Apr 18, 2023
1 parent f2e22c6 commit bb40e7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.phpunit.cache
.phpunit.result.cache

composer.lock
phpcs.xml
Expand Down
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" executionOrder="depends,defects" failOnRisky="true" failOnWarning="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" executionOrder="depends,defects" failOnRisky="true" failOnWarning="true">
<testsuites>
<testsuite name="Unit Test Suite">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
</testsuites>
<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
31 changes: 29 additions & 2 deletions src/Utilities/TimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public static function decode(?string $time): ?DateTimeImmutable
return null;
}

if (false !== strpos($time, '.')) {
$time = self::cleanMicroseconds($time);
}

/** @psalm-suppress UndefinedFunction */
$decoded = DateTimeImmutable::createFromFormat(
\str_contains($time, '.') ? self::RFC3339_EXTENDED_FORMAT : self::RFC3339_FORMAT,
\strtoupper($time),
false !== strpos($time, '.') ? self::RFC3339_EXTENDED_FORMAT : self::RFC3339_FORMAT,
strtoupper($time),
new DateTimeZone(self::TIME_ZONE)
);

Expand All @@ -49,4 +53,27 @@ public static function decode(?string $time): ?DateTimeImmutable

return $decoded;
}

private static function cleanMicroseconds(string $time): string
{
/** @psalm-suppress PossiblyUndefinedArrayOffset */
list($seconds, $microseconds) = explode('.', $time, 2);

if (substr($microseconds, -1) === 'Z' && strlen($microseconds) > 7) {
$microseconds = substr($microseconds, 0, 6) . 'Z';
return \sprintf('%s.%s', $seconds, $microseconds);
}

if (false !== strpos($microseconds, '+') && strlen($microseconds) > 11) {
$microseconds = substr($microseconds, 0, -5);
return \sprintf('%s.%s', $seconds, $microseconds);
}

if (strlen($microseconds) > 6) {
$microseconds = \substr($microseconds, 0, 6);
return \sprintf('%s.%s', $seconds, $microseconds);
}

return $time;
}
}
8 changes: 7 additions & 1 deletion tests/Unit/Utilities/TimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testEncode(): void
);
}

public function providesDecodeCases(): array
public static function providesDecodeCases(): array
{
return [
// UTC
Expand All @@ -36,6 +36,9 @@ public 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.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', '1985-04-12T23:20:50.123456Z'],

// +01:00
['2018-04-05T16:31:00Z', '2018-04-05T17:31:00+01:00'],
Expand All @@ -51,6 +54,9 @@ public 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.1234567Z', '1985-04-12T23:20:50.123456+01:00'],
// ['1985-04-12T22:20:50.12345678Z', '1985-04-12T23:20:50.123456+01:00'],
// ['1985-04-12T22:20:50.123456789Z', '1985-04-12T23:20:50.123456+01:00'],
];
}

Expand Down

0 comments on commit bb40e7d

Please sign in to comment.