-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support to serialize and deserialize timestamps with different r…
…esolutions (#648)
- Loading branch information
Showing
5 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with microseconds for | ||
//! the purposes of serde. | ||
//! | ||
//! Use this module in combination with serde's [`#[with]`][with] attribute. | ||
//! | ||
//! When deserializing, the offset is assumed to be UTC. | ||
//! | ||
//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
//! [with]: https://serde.rs/field-attrs.html#with | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::OffsetDateTime; | ||
|
||
/// Serialize an `OffsetDateTime` as its Unix timestamp with microseconds | ||
pub fn serialize<S: Serializer>( | ||
datetime: &OffsetDateTime, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let timestamp = datetime.unix_timestamp_nanos() / 1_000; | ||
timestamp.serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `OffsetDateTime` from its Unix timestamp with microseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { | ||
let value: i128 = <_>::deserialize(deserializer)?; | ||
OffsetDateTime::from_unix_timestamp_nanos(value * 1_000) | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
|
||
/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with microseconds | ||
/// for the purposes of serde. | ||
/// | ||
/// Use this module in combination with serde's [`#[with]`][with] attribute. | ||
/// | ||
/// When deserializing, the offset is assumed to be UTC. | ||
/// | ||
/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
/// [with]: https://serde.rs/field-attrs.html#with | ||
pub mod option { | ||
#[allow(clippy::wildcard_imports)] | ||
use super::*; | ||
|
||
/// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with microseconds | ||
pub fn serialize<S: Serializer>( | ||
option: &Option<OffsetDateTime>, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
option | ||
.map(|timestamp| timestamp.unix_timestamp_nanos() / 1_000) | ||
.serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with microseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>( | ||
deserializer: D, | ||
) -> Result<Option<OffsetDateTime>, D::Error> { | ||
Option::deserialize(deserializer)? | ||
.map(|value: i128| OffsetDateTime::from_unix_timestamp_nanos(value * 1_000)) | ||
.transpose() | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with milliseconds for | ||
//! the purposes of serde. | ||
//! | ||
//! Use this module in combination with serde's [`#[with]`][with] attribute. | ||
//! | ||
//! When deserializing, the offset is assumed to be UTC. | ||
//! | ||
//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
//! [with]: https://serde.rs/field-attrs.html#with | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::OffsetDateTime; | ||
|
||
/// Serialize an `OffsetDateTime` as its Unix timestamp with milliseconds | ||
pub fn serialize<S: Serializer>( | ||
datetime: &OffsetDateTime, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let timestamp = datetime.unix_timestamp_nanos() / 1_000_000; | ||
timestamp.serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `OffsetDateTime` from its Unix timestamp with milliseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { | ||
let value: i128 = <_>::deserialize(deserializer)?; | ||
OffsetDateTime::from_unix_timestamp_nanos(value * 1_000_000) | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
|
||
/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with milliseconds | ||
/// for the purposes of serde. | ||
/// | ||
/// Use this module in combination with serde's [`#[with]`][with] attribute. | ||
/// | ||
/// When deserializing, the offset is assumed to be UTC. | ||
/// | ||
/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
/// [with]: https://serde.rs/field-attrs.html#with | ||
pub mod option { | ||
#[allow(clippy::wildcard_imports)] | ||
use super::*; | ||
|
||
/// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with milliseconds | ||
pub fn serialize<S: Serializer>( | ||
option: &Option<OffsetDateTime>, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
option | ||
.map(|timestamp| timestamp.unix_timestamp_nanos() / 1_000_000) | ||
.serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with milliseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>( | ||
deserializer: D, | ||
) -> Result<Option<OffsetDateTime>, D::Error> { | ||
Option::deserialize(deserializer)? | ||
.map(|value: i128| OffsetDateTime::from_unix_timestamp_nanos(value * 1_000_000)) | ||
.transpose() | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! Treat an [`OffsetDateTime`] as a [Unix timestamp] with nanoseconds for | ||
//! the purposes of serde. | ||
//! | ||
//! Use this module in combination with serde's [`#[with]`][with] attribute. | ||
//! | ||
//! When deserializing, the offset is assumed to be UTC. | ||
//! | ||
//! [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
//! [with]: https://serde.rs/field-attrs.html#with | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::OffsetDateTime; | ||
|
||
/// Serialize an `OffsetDateTime` as its Unix timestamp with nanoseconds | ||
pub fn serialize<S: Serializer>( | ||
datetime: &OffsetDateTime, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
datetime.unix_timestamp_nanos().serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `OffsetDateTime` from its Unix timestamp with nanoseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<OffsetDateTime, D::Error> { | ||
OffsetDateTime::from_unix_timestamp_nanos(<_>::deserialize(deserializer)?) | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
|
||
/// Treat an `Option<OffsetDateTime>` as a [Unix timestamp] with nanoseconds | ||
/// for the purposes of serde. | ||
/// | ||
/// Use this module in combination with serde's [`#[with]`][with] attribute. | ||
/// | ||
/// When deserializing, the offset is assumed to be UTC. | ||
/// | ||
/// [Unix timestamp]: https://en.wikipedia.org/wiki/Unix_time | ||
/// [with]: https://serde.rs/field-attrs.html#with | ||
pub mod option { | ||
#[allow(clippy::wildcard_imports)] | ||
use super::*; | ||
|
||
/// Serialize an `Option<OffsetDateTime>` as its Unix timestamp with nanoseconds | ||
pub fn serialize<S: Serializer>( | ||
option: &Option<OffsetDateTime>, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
option | ||
.map(OffsetDateTime::unix_timestamp_nanos) | ||
.serialize(serializer) | ||
} | ||
|
||
/// Deserialize an `Option<OffsetDateTime>` from its Unix timestamp with nanoseconds | ||
pub fn deserialize<'a, D: Deserializer<'a>>( | ||
deserializer: D, | ||
) -> Result<Option<OffsetDateTime>, D::Error> { | ||
Option::deserialize(deserializer)? | ||
.map(OffsetDateTime::from_unix_timestamp_nanos) | ||
.transpose() | ||
.map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) | ||
} | ||
} |