time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.06k stars 261 forks source link

Cannot Deserialize timestamp with milliseconds #647

Closed ogarcia closed 5 months ago

ogarcia commented 5 months ago

My code is simple:

use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

// Adds milliseconds support to timestamps
time::serde::format_description!(ts_milliseconds, OffsetDateTime, "[unix_timestamp precision:millisecond]");

#[derive(Debug, Deserialize)]
pub struct Profile {
    pub id: String,
    pub name: String,
    #[serde(with = "ts_milliseconds")]
    pub createdAt: OffsetDateTime,
    #[serde(with = "ts_milliseconds")]
    pub updatedAt: OffsetDateTime,
}

When I go to use the structure to deserialize from a JSON I get the following error.

invalid type: integer `1617099399918`, expected a(n) `OffsetDateTime` in the format \"[unix_timestamp precision:millisecond]\"

The truth is that I have run out of ideas, I understand that it should work as it is, but it does not. In the cargo.toml I have the following:

time = { version = "~0.3", features = ["formatting", "macros", "parsing", "serde"] }
jhpratt commented 5 months ago

The value is expected to be a string, not an integer. Formatting and parsing is always done with strings.

ogarcia commented 5 months ago

The value is expected to be a string, not an integer. Formatting and parsing is always done with strings.

I understand then that it is impossible to deserialize a timestamp with milliseconds coming in an integer.

And you have not thought about including this possibility? It is not so rare nowadays to work with timestamps with milliseconds and the most normal thing is that they come as integers and not as strings.

In fact, I think it would be very interesting if it was even a module, something like time::serde::timestamp_millis. What do you think?

jhpratt commented 5 months ago

I have previously rejected that (on multiple occasions) because I assumed it could be replicated using the time::serde::format_description! macro. Given that it cannot and that time::serde::timestamp is serialized/deserialized as an integer, I would be fine with a PR adding it for milliseconds, microseconds, and nanoseconds.