Skip to content

Commit f3cb7ff

Browse files
committed
Fix non-6 digit microsecond date time formats
RFC3339 allows for second fractions to be any length, however PHP's date/time formatting ALWAYS prints them out as 6 digits. This means if a date-time is passed with a < 6 digit second fractional, the FormatConstraint::validateDateTime will fail as the formatted date will contain 6 digits. E.g. '2000-05-01T12:12:12.123Z' is passed as a date time and is valid. However after parsing, format() will produce '2000-05-01T12:12:12:12.123000Z'. Fixes issue #145.
1 parent f791815 commit f3cb7ff

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,19 @@ protected function validateDateTime($datetime, $format)
130130
return false;
131131
}
132132

133-
return $datetime === $dt->format($format);
133+
if ($datetime === $dt->format($format)) {
134+
return true;
135+
}
136+
137+
// handles the case where a non-6 digit microsecond datetime is passed
138+
// which will fail the above string comparison because the passed
139+
// $datetime may be '2000-05-01T12:12:12.123Z' but format() will return
140+
// '2000-05-01T12:12:12.123000Z'
141+
if ((strpos('u', $format) !== -1) && (intval($dt->format('u')) > 0)) {
142+
return true;
143+
}
144+
145+
return false;
134146
}
135147

136148
protected function validateRegex($regex)

tests/JsonSchema/Tests/Constraints/FormatTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp()
1717
{
1818
date_default_timezone_set('UTC');
1919
}
20-
20+
2121
public function testNullThing()
2222
{
2323
$validator = new FormatConstraint();
@@ -80,6 +80,7 @@ public function getValidFormats()
8080
array('2000-05-01T12:12:12+0100', 'date-time'),
8181
array('2000-05-01T12:12:12+01:00', 'date-time'),
8282
array('2000-05-01T12:12:12.123456Z', 'date-time'),
83+
array('2000-05-01T12:12:12.123Z', 'date-time'),
8384

8485
array('0', 'utc-millisec'),
8586

0 commit comments

Comments
 (0)