Skip to content

Commit b9ddba5

Browse files
author
Benoît Burnichon
committed
Add Rfc339 utility class to create instances of DateTime
1 parent 5a49ebd commit b9ddba5

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/JsonSchema/Rfc3339.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace JsonSchema;
4+
5+
class Rfc3339
6+
{
7+
const REGEX = '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))/';
8+
9+
/**
10+
* Try creating a DateTime instance
11+
*
12+
* @param string $string
13+
* @return \DateTime|null
14+
*/
15+
public static function createFromString($string)
16+
{
17+
if (!preg_match(self::REGEX, strtoupper($string), $matches)) {
18+
return null;
19+
}
20+
21+
$dateAndTime = $matches[1];
22+
$microseconds = $matches[2] ?: '.000000';
23+
$timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';
24+
25+
$dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));
26+
27+
return $dateTime ?: null;
28+
}
29+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace JsonSchema\Tests;
4+
5+
use JsonSchema\Rfc3339;
6+
7+
class Rfc3339Test extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @param string $string
11+
* @param \DateTime|null $expected
12+
* @dataProvider provideValidFormats
13+
*/
14+
public function testCreateFromValidString($string, \DateTime $expected)
15+
{
16+
$actual = Rfc3339::createFromString($string);
17+
18+
$this->assertInstanceOf('DateTime', $actual);
19+
$this->assertEquals($expected->format('U.u'), $actual->format('U.u'));
20+
}
21+
22+
/**
23+
* @param string $string
24+
* @dataProvider provideInvalidFormats
25+
*/
26+
public function testCreateFromInvalidString($string)
27+
{
28+
$this->assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string));
29+
}
30+
31+
public function provideValidFormats()
32+
{
33+
return array(
34+
array(
35+
'2000-05-01T12:12:12Z',
36+
\DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC'))
37+
),
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')),
40+
array(
41+
'2000-05-01T12:12:12.123456Z',
42+
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC'))
43+
),
44+
array(
45+
'2000-05-01T12:12:12.123Z',
46+
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC'))
47+
),
48+
);
49+
}
50+
51+
public function provideInvalidFormats()
52+
{
53+
return array(
54+
array('1999-1-11T00:00:00Z'),
55+
array('1999-01-11T00:00:00+100'),
56+
array('1999-01-11T00:00:00+1:00'),
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)