Skip to content

Commit f9e27c3

Browse files
committed
Merge pull request #207 from johnnoel/master
Fix non-6 digit microsecond date time formats
2 parents b2e6b9a + f3cb7ff commit f9e27c3

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)