Skip to content

Commit

Permalink
feat(proto): Timestamp::from_milis()
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Jun 26, 2023
1 parent 29b5056 commit 169f487
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions proto/src/serializers/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ impl ToMilis for Timestamp {
}
}

pub trait FromMilis {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
fn from_milis(millis: i64) -> Self;
}

impl FromMilis for Timestamp {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
fn from_milis(millis: i64) -> Self {
let dt =
chrono::NaiveDateTime::from_timestamp_millis(millis).expect("cannot parse timestamp");

Self {
nanos: dt.timestamp_subsec_nanos() as i32,
seconds: dt.timestamp(),
}
}
}

/// Deserialize string into Timestamp
pub fn deserialize<'de, D>(deserializer: D) -> Result<Timestamp, D::Error>
where
Expand Down

0 comments on commit 169f487

Please sign in to comment.