Deserialize "epoch" in milliseconds #410
-
I'm deserializing a JSON data where one of the field is a unix timestamp in milliseconds. With chrono I'm using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
What code do you have? I presume you're using a custom deserializer, as there's not one in this library with the exact behavior you're looking for (I imagine you already noticed this). |
Beta Was this translation helpful? Give feedback.
-
I ended up with a deserializer looking like this pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<OffsetDateTime, D::Error>
where
D: Deserializer<'de>,
{
let ms = i64::deserialize(deserializer)?;
let sec = ms / 1000;
OffsetDateTime::from_unix_timestamp(sec)
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(sec), &err))
} |
Beta Was this translation helpful? Give feedback.
What code do you have? I presume you're using a custom deserializer, as there's not one in this library with the exact behavior you're looking for (I imagine you already noticed this).