Skip to content

Commit

Permalink
refactor(proto): move FromMillis/ToMillis to separate mod
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Aug 30, 2024
1 parent 03b741f commit 6b7e4f2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 61 deletions.
1 change: 1 addition & 0 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod tenderdash_grpc;
pub use tenderdash_grpc::*;

pub mod serializers;
mod time;

pub use meta::ABCI_VERSION;
use prelude::*;
Expand Down
3 changes: 3 additions & 0 deletions proto/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pub use alloc::{
vec::Vec,
};
pub use core::prelude::v1::*;

#[allow(unused_imports)]
pub use crate::time::{FromMillis, ToMillis};
66 changes: 5 additions & 61 deletions proto/src/serializers/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,67 +29,11 @@ impl From<Rfc3339> for Timestamp {
}
}

pub trait ToMilis {
/// Convert protobuf timestamp into miliseconds since epoch
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Arguments
///
/// * millis - time since epoch, in miliseconds
///
/// # Panics
///
/// Panics when timestamp doesn't fit `u64` type
fn to_milis(&self) -> u64;
}

impl ToMilis for Timestamp {
/// Convert protobuf timestamp into miliseconds since epoch
fn to_milis(&self) -> u64 {
chrono::DateTime::from_timestamp(self.seconds, self.nanos as u32)
.unwrap()
.to_utc()
.timestamp_millis()
.try_into()
.expect("timestamp value out of u64 range")
}
}

pub trait FromMilis {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Arguments
///
/// * millis - time since epoch, in miliseconds; must fit `i64` type
fn from_milis(millis: u64) -> Self;
}

impl FromMilis for Timestamp {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Panics
///
/// Panics when `millis` don't fit `i64` type
fn from_milis(millis: u64) -> Self {
let dt = chrono::DateTime::from_timestamp_millis(
millis
.try_into()
.expect("milliseconds timestamp out of i64 range"),
)
.expect("cannot parse timestamp")
.to_utc();

Self {
nanos: dt.timestamp_subsec_nanos() as i32,
seconds: dt.timestamp(),
}
}
}
// Code moved to crate::time, but kept here for compatibility
#[deprecated = "use crate::prelude::FromMillis instead"]
pub use crate::time::FromMillis as FromMilis;
#[deprecated = "use crate::prelude::ToMillis instead"]
pub use crate::time::ToMillis as ToMilis;

/// Deserialize string into Timestamp
pub fn deserialize<'de, D>(deserializer: D) -> Result<Timestamp, D::Error>
Expand Down
77 changes: 77 additions & 0 deletions proto/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! Time conversion traits and functions
use crate::google::protobuf::Timestamp;
pub trait ToMillis {
/// Convert protobuf timestamp into miliseconds since epoch
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Arguments
///
/// * millis - time since epoch, in miliseconds
///
/// # Panics
///
/// Panics when timestamp doesn't fit `u64` type
fn to_millis(&self) -> u64;

#[deprecated = "use `to_millis` instead"]
fn to_milis(&self) -> u64 {
self.to_millis()
}
}

impl ToMillis for Timestamp {
/// Convert protobuf timestamp into miliseconds since epoch
fn to_millis(&self) -> u64 {
chrono::DateTime::from_timestamp(self.seconds, self.nanos as u32)
.unwrap()
.to_utc()
.timestamp_millis()
.try_into()
.expect("timestamp value out of u64 range")
}
}

pub trait FromMillis {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Arguments
///
/// * millis - time since epoch, in miliseconds; must fit `i64` type
fn from_millis(millis: u64) -> Self;

#[deprecated = "use `from_millis` instead"]
fn from_milis(millis: u64) -> Self
where
Self: Sized,
{
Self::from_millis(millis)
}
}

impl FromMillis for Timestamp {
/// Create protobuf timestamp from miliseconds since epoch
///
/// Note there is a resolution difference, as timestamp uses nanoseconds
///
/// # Panics
///
/// Panics when `millis` don't fit `i64` type
fn from_millis(millis: u64) -> Self {
let dt = chrono::DateTime::from_timestamp_millis(
millis
.try_into()
.expect("milliseconds timestamp out of i64 range"),
)
.expect("cannot parse timestamp")
.to_utc();

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

0 comments on commit 6b7e4f2

Please sign in to comment.