pub struct TimestampSecondsWithFrac<FORMAT = f64, STRICTNESS = Strict>(/* private fields */)
where
FORMAT: Format,
STRICTNESS: Strictness;Expand description
De/Serialize timestamps as seconds since the UNIX epoch
De/serialize timestamps as seconds since the UNIX epoch.
Subsecond precision is only supported for TimestampSecondsWithFrac, but not for TimestampSeconds.
You can configure the serialization format between integers, floats, and stringified numbers with the FORMAT specifier and configure the deserialization with the STRICTNESS specifier.
Serialization of integers will round the timestamp to the nearest value.
The STRICTNESS specifier can either be formats::Strict or formats::Flexible and defaults to formats::Strict.
formats::Strict means that deserialization only supports the type given in FORMAT, e.g., if FORMAT is i64 deserialization from a f64 will error.
formats::Flexible means that deserialization will perform a best effort to extract the correct timestamp and allows deserialization from any type.
For example, deserializing TimestampSeconds<f64, Flexible> will discard any subsecond precision during deserialization from f64 and will parse a String as an integer number.
This type also supports chrono::DateTime and chrono::NaiveDateTime with the chrono-feature flag.
This type also supports [time::OffsetDateTime][::time_0_3::OffsetDateTime] and [time::PrimitiveDateTime][::time_0_3::PrimitiveDateTime] with the time_0_3-feature flag.
This table lists the available FORMATs for the different timestamp types.
The FORMAT specifier defaults to i64 or f64.
| Timestamp Type | Converter | Available FORMATs |
|---|---|---|
std::time::SystemTime | TimestampSeconds | i64, f64, String |
std::time::SystemTime | TimestampSecondsWithFrac | f64, String |
chrono::DateTime<Utc> | TimestampSeconds | i64, f64, String |
chrono::DateTime<Utc> | TimestampSecondsWithFrac | f64, String |
chrono::DateTime<Local> | TimestampSeconds | i64, f64, String |
chrono::DateTime<Local> | TimestampSecondsWithFrac | f64, String |
chrono::NaiveDateTime | TimestampSeconds | i64, f64, String |
chrono::NaiveDateTime | TimestampSecondsWithFrac | f64, String |
time::OffsetDateTime | TimestampSeconds | i64, f64, String |
time::OffsetDateTime | TimestampSecondsWithFrac | f64, String |
time::PrimitiveDateTime | TimestampSeconds | i64, f64, String |
time::PrimitiveDateTime | TimestampSecondsWithFrac | f64, String |
§Examples
use std::time::{Duration, SystemTime};
#[serde_as]
#[derive(Deserialize, Serialize)]
struct Timestamps {
#[serde_as(as = "TimestampSecondsWithFrac<f64>")]
st_f64: SystemTime,
#[serde_as(as = "TimestampSecondsWithFrac<String>")]
st_string: SystemTime,
}
// Serialization
// See how the values get rounded, since subsecond precision is not allowed.
let ts = Timestamps {
st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 500_000_000)).unwrap(),
st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 999_999_000)).unwrap(),
};
// Observe the different data types
let expected = json!({
"st_f64": 12345.5,
"st_string": "12345.999999",
});
assert_eq!(expected, serde_json::to_value(ts).unwrap());
// Deserialization works too
// Subsecond precision in numbers will be rounded away
let json = json!({
"st_f64": 12345.5,
"st_string": "12345.987654",
});
let expected = Timestamps {
st_f64: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 500_000_000)).unwrap(),
st_string: SystemTime::UNIX_EPOCH.checked_add(Duration::new(12345, 987_654_000)).unwrap(),
};
assert_eq!(expected, serde_json::from_value(json).unwrap());chrono::DateTime<Utc> and chrono::DateTime<Local> are also supported when using the chrono_0_4 feature.
Like SystemTime, it is a signed timestamp, thus can be de/serialized as an i64.
use chrono::{DateTime, Local, TimeZone, Utc};
#[serde_as]
#[derive(Deserialize, Serialize)]
struct Timestamps {
#[serde_as(as = "TimestampSecondsWithFrac<f64>")]
dt_f64: DateTime<Utc>,
#[serde_as(as = "TimestampSecondsWithFrac<String>")]
dt_string: DateTime<Local>,
}
// Serialization
let ts = Timestamps {
dt_f64: Utc.timestamp_opt(-12345, 500_000_000).unwrap(),
dt_string: Local.timestamp_opt(12345, 999_999_000).unwrap(),
};
// Observe the different data types
let expected = json!({
"dt_f64": -12344.5,
"dt_string": "12345.999999",
});
assert_eq!(expected, serde_json::to_value(ts).unwrap());
// Deserialization works too
let json = json!({
"dt_f64": -12344.5,
"dt_string": "12345.987",
});
let expected = Timestamps {
dt_f64: Utc.timestamp_opt(-12345, 500_000_000).unwrap(),
dt_string: Local.timestamp_opt(12345, 987_000_000).unwrap(),
};
assert_eq!(expected, serde_json::from_value(json).unwrap());Trait Implementations§
Source§impl<'de> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac
impl<'de> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac
Source§fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'de, FORMAT> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac<FORMAT, Flexible>where
FORMAT: Format,
impl<'de, FORMAT> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac<FORMAT, Flexible>where
FORMAT: Format,
Source§fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'de> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac<String>
impl<'de> DeserializeAs<'de, SystemTime> for TimestampSecondsWithFrac<String>
Source§fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(
deserializer: D,
) -> Result<SystemTime, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<STRICTNESS> SerializeAs<SystemTime> for TimestampSecondsWithFrac<String, STRICTNESS>where
STRICTNESS: Strictness,
impl<STRICTNESS> SerializeAs<SystemTime> for TimestampSecondsWithFrac<String, STRICTNESS>where
STRICTNESS: Strictness,
Source§fn serialize_as<S>(
source: &SystemTime,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize_as<S>(
source: &SystemTime,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl<STRICTNESS> SerializeAs<SystemTime> for TimestampSecondsWithFrac<f64, STRICTNESS>where
STRICTNESS: Strictness,
impl<STRICTNESS> SerializeAs<SystemTime> for TimestampSecondsWithFrac<f64, STRICTNESS>where
STRICTNESS: Strictness,
Source§fn serialize_as<S>(
source: &SystemTime,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize_as<S>(
source: &SystemTime,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Auto Trait Implementations§
impl<FORMAT, STRICTNESS> Freeze for TimestampSecondsWithFrac<FORMAT, STRICTNESS>
impl<FORMAT, STRICTNESS> RefUnwindSafe for TimestampSecondsWithFrac<FORMAT, STRICTNESS>where
FORMAT: RefUnwindSafe,
STRICTNESS: RefUnwindSafe,
impl<FORMAT, STRICTNESS> Send for TimestampSecondsWithFrac<FORMAT, STRICTNESS>
impl<FORMAT, STRICTNESS> Sync for TimestampSecondsWithFrac<FORMAT, STRICTNESS>
impl<FORMAT, STRICTNESS> Unpin for TimestampSecondsWithFrac<FORMAT, STRICTNESS>
impl<FORMAT, STRICTNESS> UnwindSafe for TimestampSecondsWithFrac<FORMAT, STRICTNESS>where
FORMAT: UnwindSafe,
STRICTNESS: UnwindSafe,
Blanket Implementations§
Source§impl<S> AssignWithType for S
impl<S> AssignWithType for S
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
Source§impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.