Skip to content

Commit

Permalink
Issue-414: Allow The Option of T or space for Date time.
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterEl committed Apr 12, 2017
1 parent 9225c96 commit 09b7205
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/JsonSchema/Rfc3339.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,9 @@ public static function createFromString($string)
$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') === 10 ? '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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Rfc3339Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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'))
)
);
}

Expand All @@ -54,6 +62,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')
);
}
}

0 comments on commit 09b7205

Please sign in to comment.