Add test about utc_to_timestamp
This commit is contained in:
parent
69e047cf5a
commit
29ddd61eec
4 changed files with 32 additions and 12 deletions
|
@ -12,12 +12,7 @@ service CachedPeerService {
|
|||
rpc List(CachedPeerListRequest) returns (CachedPeerListResponse);
|
||||
}
|
||||
|
||||
message CachedPeerListRequest {
|
||||
uint32 start = 1;
|
||||
uint32 count = 2;
|
||||
PeerListOrderBy order_by = 3;
|
||||
|
||||
}
|
||||
message CachedPeerListRequest {}
|
||||
|
||||
message CachedPeerMessage {
|
||||
uint32 number = 1;
|
||||
|
|
|
@ -8,8 +8,8 @@ impl From<&CachedAddressModel> for CachedAddressMessage {
|
|||
fn from(a: &CachedAddressModel) -> Self {
|
||||
Self {
|
||||
number: a.id,
|
||||
created_at: Some(utc_to_timestamp(a.created_at)),
|
||||
updated_at: Some(utc_to_timestamp(a.updated_at)),
|
||||
created_at: Some(utc_to_timestamp(&a.created_at)),
|
||||
updated_at: Some(utc_to_timestamp(&a.updated_at)),
|
||||
multiaddress: Multiaddr::from(a.multiaddress.clone()).to_string(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ impl From<(&CachedPeerModel, &Vec<CachedAddressModel>)> for CachedPeerMessage {
|
|||
Self {
|
||||
number: peer.id,
|
||||
peer_id: peer.peer_id.to_string(),
|
||||
created_at: Some(utc_to_timestamp(peer.created_at)),
|
||||
created_at: Some(utc_to_timestamp(&peer.created_at)),
|
||||
addresses: addresses.iter().map(|x| CachedAddressMessage::from(x)).collect(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,38 @@
|
|||
use prost_types::Timestamp;
|
||||
use chrono::{DateTime, Timelike, Utc};
|
||||
use chrono::{DateTime, TimeZone, Timelike, Utc};
|
||||
pub mod async_convert;
|
||||
pub mod emptiable;
|
||||
pub mod mergeable;
|
||||
pub mod runnable;
|
||||
|
||||
pub fn utc_to_timestamp(utc: DateTime<Utc>) -> Timestamp {
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use chrono::Utc;
|
||||
/// use std::time::SystemTime;
|
||||
/// use prost_types::Timestamp;
|
||||
/// use caretta_core::utils::utc_to_timestamp;
|
||||
///
|
||||
/// let now_utc = Utc::now();
|
||||
/// let now_timestamp = utc_to_timestamp(&now_utc);
|
||||
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
|
||||
/// ```
|
||||
pub fn utc_to_timestamp(utc: &DateTime<Utc>) -> Timestamp {
|
||||
Timestamp{
|
||||
seconds: utc.timestamp(),
|
||||
nanos: i32::try_from(utc.nanosecond()).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use std::time::SystemTime;
|
||||
/// use prost_types::Timestamp;
|
||||
/// use caretta_core::utils::timestamp_to_utc;
|
||||
///
|
||||
/// let now_timestamp = Timestamp::from(SystemTime::now());
|
||||
/// let now_utc = timestamp_to_utc(&now_timestamp);
|
||||
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
|
||||
/// ```
|
||||
pub fn timestamp_to_utc(t: &Timestamp) -> DateTime<Utc> {
|
||||
Utc.timestamp_opt(t.seconds, u32::try_from(t.nanos).unwrap()).unwrap()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue