serde_firestore_value/typ/
timestamp.rs#[derive(
Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
)]
#[serde(rename = "$__serde-firestore-value_private_timestamp")]
pub struct Timestamp {
pub seconds: i64,
pub nanos: i32,
}
impl Timestamp {
pub(crate) const NAME: &'static str = "$__serde-firestore-value_private_timestamp";
}
impl From<Timestamp> for prost_types::Timestamp {
fn from(Timestamp { seconds, nanos }: Timestamp) -> Self {
Self { seconds, nanos }
}
}
impl From<prost_types::Timestamp> for Timestamp {
fn from(prost_types::Timestamp { seconds, nanos }: prost_types::Timestamp) -> Self {
Self { seconds, nanos }
}
}
#[cfg(feature = "chrono")]
impl std::convert::TryFrom<Timestamp> for chrono::DateTime<chrono::Utc> {
type Error = crate::Error;
fn try_from(Timestamp { seconds, nanos }: Timestamp) -> Result<Self, Self::Error> {
let nanos = u32::try_from(nanos).map_err(|_| {
crate::Error::from(crate::error::ErrorCode::Custom(format!(
"chrono::DateTime::<chrono::Utc>::try_from(Timestamp) / u32::try_from({})",
nanos
)))
})?;
Self::from_timestamp(seconds, nanos).ok_or_else(|| {
crate::Error::from(crate::error::ErrorCode::Custom(format!(
"chrono::DateTime::<chrono::Utc>::try_from(Timestamp) / chrono::DateTime::<chrono::Utc>::from_timestamp({}, {})",
seconds, nanos
)))
})
}
}
#[cfg(feature = "time")]
impl std::convert::TryFrom<Timestamp> for time::OffsetDateTime {
type Error = crate::Error;
fn try_from(Timestamp { seconds, nanos }: Timestamp) -> Result<Self, Self::Error> {
let timestamp_nanos = i128::from(seconds) * 1_000_000_000_i128 + i128::from(nanos);
Self::from_unix_timestamp_nanos(timestamp_nanos).map_err(|e| {
crate::Error::from(crate::error::ErrorCode::Custom(format!(
"time::OffsetDateTime::try_from(Tiemstamp) / time::OffsetDateTime::from_unix_timestamp_nanos({}) : {}",
timestamp_nanos,
e
)))
})
}
}
#[cfg(feature = "chrono")]
impl std::convert::TryFrom<chrono::DateTime<chrono::Utc>> for Timestamp {
type Error = crate::Error;
fn try_from(date_time: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
let seconds = date_time.timestamp();
let nanos = date_time.timestamp_subsec_nanos();
let nanos = i32::try_from(nanos).map_err(|_| {
crate::Error::from(crate::error::ErrorCode::Custom(format!(
"Timestamp::try_from(chrono::DateTime::<chrono::Utc>) / i32::try_from({})",
nanos
)))
})?;
Ok(Self { seconds, nanos })
}
}
#[cfg(feature = "time")]
impl std::convert::TryFrom<time::OffsetDateTime> for Timestamp {
type Error = crate::Error;
fn try_from(offset_date_time: time::OffsetDateTime) -> Result<Self, Self::Error> {
let seconds = offset_date_time.unix_timestamp();
let nanos = offset_date_time.unix_timestamp_nanos() % 1_000_000_000_i128;
let nanos = i32::try_from(nanos).map_err(|_| {
crate::Error::from(crate::error::ErrorCode::Custom(format!(
"Timestamp::try_from(chrono::DateTime::<chrono::Utc>) / i32::try_from({})",
nanos
)))
})?;
Ok(Self { seconds, nanos })
}
}