Skip to content

Commit ffc856d

Browse files
committed
Fix: allow explicit 0 secfracs in datetime format
1 parent 6b2a33e commit ffc856d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,19 @@ protected function validateDateTime($datetime, $format)
127127
return false;
128128
}
129129

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

133145
protected function validateRegex($regex)

tests/Constraints/FormatTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getValidFormats()
8181
array('2000-05-01T12:12:12+01:00', 'date-time'),
8282
array('2000-05-01T12:12:12.123456Z', 'date-time'),
8383
array('2000-05-01T12:12:12.123Z', 'date-time'),
84+
array('2000-05-01T12:12:12.123000Z', 'date-time'),
85+
array('2000-05-01T12:12:12.0Z', 'date-time'),
86+
array('2000-05-01T12:12:12.000Z', 'date-time'),
87+
array('2000-05-01T12:12:12.000000Z', 'date-time'),
8488

8589
array('0', 'utc-millisec'),
8690

@@ -140,6 +144,7 @@ public function getInvalidFormats()
140144
array('1999-1-11T00:00:00Z', 'date-time'),
141145
array('1999-01-11T00:00:00+100', 'date-time'),
142146
array('1999-01-11T00:00:00+1:00', 'date-time'),
147+
array('1999.000Z-01-11T00:00:00+1:00', 'date-time'),
143148

144149
array('-1', 'utc-millisec'),
145150
array(PHP_INT_MAX, 'utc-millisec'),

0 commit comments

Comments
 (0)