Skip to content

Commit

Permalink
Decoder: fixed datetime regexp [Closes #29]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 17, 2016
1 parent 209868a commit 4b00342
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Neon/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Decoder
'?:[\t\ ]+', // whitespace
);

const PATTERN_DATETIME = '#\d\d\d\d-\d\d?-\d\d?(?:(?:[Tt]| +)\d\d?:\d\d:\d\d(?:\.\d*)? *(?:Z|[-+]\d\d?(?::\d\d)?)?)?\z#A';
const PATTERN_DATETIME = '#\d\d\d\d-\d\d?-\d\d?(?:(?:[Tt]| +)\d\d?:\d\d:\d\d(?:\.\d*)? *(?:Z|[-+]\d\d?(?::?\d\d)?)?)?\z#A';

const PATTERN_HEX = '#0x[0-9a-fA-F]+\z#A';

Expand Down
2 changes: 1 addition & 1 deletion src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Encoder
*/
public function encode($var, $options = NULL)
{
if ($var instanceof \DateTime) {
if ($var instanceof \DateTime || $var instanceof \DateTimeImmutable) {
return $var->format('Y-m-d H:i:s O');

} elseif ($var instanceof Entity) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Neon/Decoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,13 @@ foreach (array_merge($dataSet['invalid syntax'], $dataSet['invalid encoding']) a
Neon::decode($set[0]);
}, 'Nette\Neon\Exception');
}

// datetime
Assert::equal(
new DateTime('2016-06-03T19:00:00+02:00'),
Neon::decode('2016-06-03 19:00:00 +0200')
);
Assert::equal(
new DateTime('2016-06-03T19:00:00+02:00'),
Neon::decode('2016-06-03 19:00:00 +02:00')
);
12 changes: 12 additions & 0 deletions tests/Neon/Encoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ Assert::same(
'foo(1, 2)::bar(3)',
Neon::encode(Neon::decode('foo(1,2)::bar(3)'))
);

Assert::same(
'2016-06-03 19:00:00 +0200',
Neon::encode(new DateTime('2016-06-03T19:00:00+02:00'))
);

if (PHP_VERSION_ID >= 50500) {
Assert::same(
'2016-06-03 19:00:00 +0200',
Neon::encode(new DateTimeImmutable('2016-06-03T19:00:00+02:00'))
);
}

0 comments on commit 4b00342

Please sign in to comment.