Skip to content

Commit f375f0b

Browse files
HunterElerayd
authored andcommitted
Issue-414: Allow The Option of T or space for Date time. (#415)
1 parent f32aaf1 commit f375f0b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/JsonSchema/Rfc3339.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Rfc3339
66
{
7-
const REGEX = '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/';
7+
const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/';
88

99
/**
1010
* Try creating a DateTime instance
@@ -22,8 +22,8 @@ public static function createFromString($string)
2222
$dateAndTime = $matches[1];
2323
$microseconds = $matches[2] ?: '.000000';
2424
$timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';
25-
26-
$dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));
25+
$dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP';
26+
$dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));
2727

2828
return $dateTime ?: null;
2929
}

tests/Rfc3339Test.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ public function provideValidFormats()
3535
'2000-05-01T12:12:12Z',
3636
\DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC'))
3737
),
38-
array('2000-05-01T12:12:12+0100', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
39-
array('2000-05-01T12:12:12+01:00', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
38+
array(
39+
'2000-05-01T12:12:12+0100',
40+
\DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
41+
),
42+
array(
43+
'2000-05-01T12:12:12+01:00',
44+
\DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
45+
),
4046
array(
4147
'2000-05-01T12:12:12.123456Z',
4248
\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()
4551
'2000-05-01T12:12:12.123Z',
4652
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC'))
4753
),
54+
array(
55+
'2000-05-01 12:12:12.123Z',
56+
\DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC'))
57+
),
58+
array(
59+
'2000-05-01 12:12:12.123456Z',
60+
\DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC'))
61+
)
4862
);
4963
}
5064

@@ -54,6 +68,8 @@ public function provideInvalidFormats()
5468
array('1999-1-11T00:00:00Z'),
5569
array('1999-01-11T00:00:00+100'),
5670
array('1999-01-11T00:00:00+1:00'),
71+
array('1999-01-01 00:00:00Z'),
72+
array('1999-1-11 00:00:00Z')
5773
);
5874
}
5975
}

0 commit comments

Comments
 (0)