Crell / Serde

Robust Serde (serialization/deserialization) library for PHP 8.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider parsing ints for deserializing Dates

withinboredom opened this issue · comments

Oftentimes, we programmers need to deserialize unix timestamps. However, Serde doesn't know what to do with an int when passing it to create a DateTimeImmutable. These are a little funky, requiring an "@" prepended before the number:

$time = 1706309512;

echo (new DateTimeImmutable("@$time"))->format('U');
// outputs 1706309512

Detailed description

When a property is annotated with DateField and the type is an int, cast to a string with "@" prepended on deserialization. Additionally, we can check that format is "U".

Context

Parsing timestamps from external services without having a post-load callback and duplicating properties.

Possible implementation

This should be pretty straightforward to implement.

I ran into this too and in my case one extra complication was that I'm getting a timestamp in milliseconds rather than seconds.
I resorted to just creating a separate exporter for UNIX timestamps along with a UnixTimestamp field.