Serde: impl Serialize and Deserialize for std::time::SystemTime

Created on 2 Sep 2016  路  5Comments  路  Source: serde-rs/serde

If there is a different type that is better suited for timestamps in serialized data that's already supported by serde, I'd be happy to know about it. The serialized format should probably be ISO 8601.

enhancement

Most helpful comment

Would also be nice to support std::time::Instant as well.

All 5 comments

:+1: We're going to have to use this approach mentioned in the documentation:

Although a SystemTime cannot be directly inspected, the UNIX_EPOCH constant is provided in this module as an anchor in time to learn information about a SystemTime. By calculating the duration from this fixed point in time, a SystemTime can be converted to a human-readable time, or perhaps some other string representation.

@jplatte for now can you use chrono::DateTime which already supports Serde serialization to ISO 8601?

@dtolnay Thanks for the pointer, I actually like the explicitness of that API, I'll happily use it, not just temporarily. However, it doesn't seem to be supported in the latest crates.io release of serde (0.8.6), I get

the trait bound `chrono::DateTime<chrono::Local>: serde::Deserialize` is not satisfied

(same if I try UTC)

@jplatte they make you opt into Serde support because not everybody wants it:

https://github.com/lifthrasiir/rust-chrono/blob/6cc5d1869641f75bda2135e58aef60f333df4227/Cargo.toml#L21

[dependencies]
chrono = { version = "0.2", features = ["serde"] }

Ah, thanks again, now it works! :)

Would also be nice to support std::time::Instant as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dtolnay picture dtolnay  路  3Comments

vityafx picture vityafx  路  3Comments

dtolnay picture dtolnay  路  3Comments

dtolnay picture dtolnay  路  3Comments

pitkley picture pitkley  路  3Comments