From 992e1c32361812db8d7541ad008b107a489763fc Mon Sep 17 00:00:00 2001 From: Hunter Elbourn Date: Wed, 12 Apr 2017 15:14:17 -0700 Subject: [PATCH] Issue-414: Allow The Option of T or space for Date time. (#415) --- src/JsonSchema/Rfc3339.php | 6 +++--- tests/Rfc3339Test.php | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/JsonSchema/Rfc3339.php b/src/JsonSchema/Rfc3339.php index fb2eb7d5..adca581a 100644 --- a/src/JsonSchema/Rfc3339.php +++ b/src/JsonSchema/Rfc3339.php @@ -4,7 +4,7 @@ class Rfc3339 { - const REGEX = '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/'; + const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/'; /** * Try creating a DateTime instance @@ -22,8 +22,8 @@ public static function createFromString($string) $dateAndTime = $matches[1]; $microseconds = $matches[2] ?: '.000000'; $timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00'; - - $dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC')); + $dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP'; + $dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC')); return $dateTime ?: null; } diff --git a/tests/Rfc3339Test.php b/tests/Rfc3339Test.php index d01da520..13294d0a 100644 --- a/tests/Rfc3339Test.php +++ b/tests/Rfc3339Test.php @@ -35,8 +35,14 @@ public function provideValidFormats() '2000-05-01T12:12:12Z', \DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC')) ), - array('2000-05-01T12:12:12+0100', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')), - array('2000-05-01T12:12:12+01:00', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')), + array( + '2000-05-01T12:12:12+0100', + \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') + ), + array( + '2000-05-01T12:12:12+01:00', + \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') + ), array( '2000-05-01T12:12:12.123456Z', \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC')) @@ -45,6 +51,14 @@ public function provideValidFormats() '2000-05-01T12:12:12.123Z', \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC')) ), + array( + '2000-05-01 12:12:12.123Z', + \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC')) + ), + array( + '2000-05-01 12:12:12.123456Z', + \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC')) + ) ); } @@ -54,6 +68,8 @@ public function provideInvalidFormats() array('1999-1-11T00:00:00Z'), array('1999-01-11T00:00:00+100'), array('1999-01-11T00:00:00+1:00'), + array('1999-01-01 00:00:00Z'), + array('1999-1-11 00:00:00Z') ); } }