Skip to content

Encoded DateTime incompatible with Decoder #29

@jahudka

Description

@jahudka

DateTime representation in generated Neon isn't properly parsed as a DateTime-compatible string and is therefore decoded as a string instead of a DateTime instance.

This breaks stuff, as $data !== decode(encode($data)).

PHP 7.0.3 CLI from MacPorts.

$data = [
    'myDate' => new DateTime('2016-06-03T19:00:00+02:00'),
];

$neon = Nette\Neon\Neon::encode($data, Nette\Neon\Neon::BLOCK);
$decoded = Nette\Neon\Neon::decode($neon);

// Fatal error: Call to a member function format() on a string
$decoded['myDate']->format('Y-m-d H:i:s O');

The problem is that the encoder formats a DateTime using the Y-m-d H:i:s O format string; the O format character doesn't add a colon between the timezone offset's hours and minutes (unlike the P format character). The regular expression in the decoder won't match the string without the colon in the timezone offset (the specific part of the regexp in question being Z|[-+]\d\d?(?::\d\d)?).

Fixing this is as simple as putting a single question mark in the decoder regexp after the offending colon, making it Z|[-+]\d\d?(?::?\d\d)?. I'd fix this and send a PR, but I won't get around to doing it today..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions