Debian Codegraph @

third_party/rust/time/src/serde/timestamp/microseconds.rs

raw
1 //! Treat an [`OffsetDateTime`] as a [Unix timestamp] with microseconds for
2 //! the purposes of serde.
3 //!
4 //! Use this module in combination with serde's [`#[with]`][with] attribute.
5 //!
6 //! When deserializing, the offset is assumed to be UTC.
7 //!
8 //! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time
9 //! [with]: https://serde.rs/field-attrs.html#with
10
11 use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
12
13 use crate::OffsetDateTime;
14
15 /// Serialize an `OffsetDateTime` as its Unix timestamp with microseconds
16 pub fn serialize<S: Serializer>(
17 datetime: &OffsetDateTime,
18 serializer: S,
19 ) -> Result<S::Ok, S::Error> {
20 let timestamp = datetime.unix_timestamp_nanos() / 1_000;
21 timestamp.serialize(serializer)
22 }
23
24 /// Deserialize an `OffsetDateTime` from its Unix timestamp with microseconds
25 pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> {
26 let value: i128 = <_>::deserialize(deserializer)?;
27 OffsetDateTime::from_unix_timestamp_nanos(value * 1_000)
28 .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err))
29 }
30
31 /// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with microseconds
32 /// for the purposes of serde.
33 ///
34 /// Use this module in combination with serde's [`#[with]`][with] attribute.
35 ///
36 /// When deserializing, the offset is assumed to be UTC.
37 ///
38 /// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time
39 /// [with]: https://serde.rs/field-attrs.html#with
40 pub mod option {
41 #[allow(clippy::wildcard_imports)]
42 use super::*;
43
44 /// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with microseconds
45 pub fn serialize<S: Serializer>(
46 option: &Option<OffsetDateTime>,
47 serializer: S,
48 ) -> Result<S::Ok, S::Error> {
49 option
50 .map(|timestamp| timestamp.unix_timestamp_nanos() / 1_000)
51 .serialize(serializer)
52 }
53
54 /// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with microseconds
55 pub fn deserialize<'a, D: Deserializer<'a>>(
56 deserializer: D,
57 ) -> Result<Option<OffsetDateTime>, D::Error> {
58 Option::deserialize(deserializer)?
59 .map(|value: i128| OffsetDateTime::from_unix_timestamp_nanos(value * 1_000))
60 .transpose()
61 .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err))
62 }
63 }