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

Adds support to serialize and deserialize timestamps with different resolutions #648

Closed ogarcia closed 5 months ago

ogarcia commented 5 months ago

As discussed in #647 this merge request adds the ability to serialize and deserialize timestamps with milliseconds, microseconds and nanoseconds.

For now I have only implemented the milliseconds because I want to see if it looks right to you @jhpratt if so then I do the rest.

The idea is that you can do the following:

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

#[derive(Serialize, Deserialize, Debug)]
struct TimestampSeconds {
    #[serde(with = "timestamp")]
    dt: OffsetDateTime,
}

#[derive(Serialize, Deserialize, Debug)]
struct TimestampMilliseconds {
    #[serde(with = "timestamp::milliseconds")]
    dt: OffsetDateTime,
}
jhpratt commented 5 months ago

At quick glance this looks correct. My nits are minor at this point: I would split the tests for seconds/milliseconds/etc. into different functions, rather than just keeping it as one. I'd also indicate in the module documentation that it's the Unix timestamp in milliseconds (as appropriate).

ogarcia commented 5 months ago

Done. I have added your suggestions. In the documentation of each module I have detailed if it is milliseconds/etc. and I have separated the tests into different functions. :smile:

jhpratt commented 5 months ago

LGTM. Thanks and good work!