Skip to content

Commit bb40e7d

Browse files
committed
better support rfc3339
Additional support for microsecond values greater than 6 digits. Signed-off-by: John Laswell <[email protected]>
1 parent f2e22c6 commit bb40e7d

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.phpunit.cache
2+
.phpunit.result.cache
23

34
composer.lock
45
phpcs.xml

phpunit.xml.dist

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<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">
33
<testsuites>
44
<testsuite name="Unit Test Suite">
55
<directory suffix="Test.php">tests/Unit</directory>
66
</testsuite>
77
</testsuites>
8-
<coverage>
8+
<coverage/>
9+
<source>
910
<include>
1011
<directory suffix=".php">src</directory>
1112
</include>
12-
</coverage>
13+
</source>
1314
</phpunit>

src/Utilities/TimeFormatter.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public static function decode(?string $time): ?DateTimeImmutable
3434
return null;
3535
}
3636

37+
if (false !== strpos($time, '.')) {
38+
$time = self::cleanMicroseconds($time);
39+
}
40+
3741
/** @psalm-suppress UndefinedFunction */
3842
$decoded = DateTimeImmutable::createFromFormat(
39-
\str_contains($time, '.') ? self::RFC3339_EXTENDED_FORMAT : self::RFC3339_FORMAT,
40-
\strtoupper($time),
43+
false !== strpos($time, '.') ? self::RFC3339_EXTENDED_FORMAT : self::RFC3339_FORMAT,
44+
strtoupper($time),
4145
new DateTimeZone(self::TIME_ZONE)
4246
);
4347

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

5054
return $decoded;
5155
}
56+
57+
private static function cleanMicroseconds(string $time): string
58+
{
59+
/** @psalm-suppress PossiblyUndefinedArrayOffset */
60+
list($seconds, $microseconds) = explode('.', $time, 2);
61+
62+
if (substr($microseconds, -1) === 'Z' && strlen($microseconds) > 7) {
63+
$microseconds = substr($microseconds, 0, 6) . 'Z';
64+
return \sprintf('%s.%s', $seconds, $microseconds);
65+
}
66+
67+
if (false !== strpos($microseconds, '+') && strlen($microseconds) > 11) {
68+
$microseconds = substr($microseconds, 0, -5);
69+
return \sprintf('%s.%s', $seconds, $microseconds);
70+
}
71+
72+
if (strlen($microseconds) > 6) {
73+
$microseconds = \substr($microseconds, 0, 6);
74+
return \sprintf('%s.%s', $seconds, $microseconds);
75+
}
76+
77+
return $time;
78+
}
5279
}

tests/Unit/Utilities/TimeFormatterTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testEncode(): void
1919
);
2020
}
2121

22-
public function providesDecodeCases(): array
22+
public static function providesDecodeCases(): array
2323
{
2424
return [
2525
// UTC
@@ -36,6 +36,9 @@ public function providesDecodeCases(): array
3636
['1985-04-12T23:20:50.123450Z', '1985-04-12T23:20:50.12345Z'],
3737
['1985-04-12T23:20:50.123450Z', '1985-04-12T23:20:50.123450Z'],
3838
['1985-04-12T23:20:50.123456Z', '1985-04-12T23:20:50.123456Z'],
39+
// ['1985-04-12T23:20:50.1234567Z', '1985-04-12T23:20:50.123456Z'],
40+
// ['1985-04-12T23:20:50.12345678Z', '1985-04-12T23:20:50.123456Z'],
41+
// ['1985-04-12T23:20:50.123456789Z', '1985-04-12T23:20:50.123456Z'],
3942

4043
// +01:00
4144
['2018-04-05T16:31:00Z', '2018-04-05T17:31:00+01:00'],
@@ -51,6 +54,9 @@ public function providesDecodeCases(): array
5154
['1985-04-12T22:20:50.123450Z', '1985-04-12T23:20:50.12345+01:00'],
5255
['1985-04-12T22:20:50.123450Z', '1985-04-12T23:20:50.123450+01:00'],
5356
['1985-04-12T22:20:50.123456Z', '1985-04-12T23:20:50.123456+01:00'],
57+
// ['1985-04-12T22:20:50.1234567Z', '1985-04-12T23:20:50.123456+01:00'],
58+
// ['1985-04-12T22:20:50.12345678Z', '1985-04-12T23:20:50.123456+01:00'],
59+
// ['1985-04-12T22:20:50.123456789Z', '1985-04-12T23:20:50.123456+01:00'],
5460
];
5561
}
5662

0 commit comments

Comments
 (0)