Skip to content

Commit

Permalink
Merge pull request #207 from johnnoel/master
Browse files Browse the repository at this point in the history
Fix non-6 digit microsecond date time formats
  • Loading branch information
bighappyface committed Jan 6, 2016
2 parents b2e6b9a + f3cb7ff commit f9e27c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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) && (intval($dt->format('u')) > 0)) {
return true;
}

return false;
}

protected function validateRegex($regex)
Expand Down
3 changes: 2 additions & 1 deletion tests/JsonSchema/Tests/Constraints/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp()
{
date_default_timezone_set('UTC');
}

public function testNullThing()
{
$validator = new FormatConstraint();
Expand Down Expand Up @@ -80,6 +80,7 @@ public function getValidFormats()
array('2000-05-01T12:12:12+0100', 'date-time'),
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('0', 'utc-millisec'),

Expand Down

0 comments on commit f9e27c3

Please sign in to comment.