Skip to content

Commit

Permalink
Merge pull request #234 from ehimen/fix-zero-secfrac
Browse files Browse the repository at this point in the history
Fix: allow explicit 0 secfracs in datetime format
  • Loading branch information
bighappyface authored Jun 28, 2016
2 parents d414acd + ffc856d commit 8490e82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,19 @@ protected function validateDateTime($datetime, $format)
return false;
}

return $datetime === $dt->format($format);
if ($datetime === $dt->format($format)) {
return true;
}

// handles the case where a non-6 digit microsecond datetime is passed
// which will fail the above string comparison because the passed
// $datetime may be '2000-05-01T12:12:12.123Z' but format() will return
// '2000-05-01T12:12:12.123000Z'
if ((strpos('u', $format) !== -1) && (preg_match('/\.\d+Z$/', $datetime))) {
return true;
}

return false;
}

protected function validateRegex($regex)
Expand Down
5 changes: 5 additions & 0 deletions tests/Constraints/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function getValidFormats()
array('2000-05-01T12:12:12+01:00', 'date-time'),
array('2000-05-01T12:12:12.123456Z', 'date-time'),
array('2000-05-01T12:12:12.123Z', 'date-time'),
array('2000-05-01T12:12:12.123000Z', 'date-time'),
array('2000-05-01T12:12:12.0Z', 'date-time'),
array('2000-05-01T12:12:12.000Z', 'date-time'),
array('2000-05-01T12:12:12.000000Z', 'date-time'),

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

Expand Down Expand Up @@ -140,6 +144,7 @@ public function getInvalidFormats()
array('1999-1-11T00:00:00Z', 'date-time'),
array('1999-01-11T00:00:00+100', 'date-time'),
array('1999-01-11T00:00:00+1:00', 'date-time'),
array('1999.000Z-01-11T00:00:00+1:00', 'date-time'),

array('-1', 'utc-millisec'),
array(PHP_INT_MAX, 'utc-millisec'),
Expand Down

0 comments on commit 8490e82

Please sign in to comment.