From 29ddd61eec5249840dc8e9b148a22043ddfa09d8 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Thu, 14 Aug 2025 06:38:15 +0900 Subject: [PATCH] Add test about utc_to_timestamp --- core/proto/caretta.proto | 7 +------ core/src/proto/cached_address.rs | 4 ++-- core/src/proto/cached_peer.rs | 2 +- core/src/utils/mod.rs | 31 ++++++++++++++++++++++++++++--- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/core/proto/caretta.proto b/core/proto/caretta.proto index 4f83053..8058d4d 100644 --- a/core/proto/caretta.proto +++ b/core/proto/caretta.proto @@ -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; diff --git a/core/src/proto/cached_address.rs b/core/src/proto/cached_address.rs index b538253..ef26c98 100644 --- a/core/src/proto/cached_address.rs +++ b/core/src/proto/cached_address.rs @@ -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(), } } diff --git a/core/src/proto/cached_peer.rs b/core/src/proto/cached_peer.rs index 6e83697..d6393c9 100644 --- a/core/src/proto/cached_peer.rs +++ b/core/src/proto/cached_peer.rs @@ -7,7 +7,7 @@ impl From<(&CachedPeerModel, &Vec)> 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(), } } diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index a9263ef..0254b57 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -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) -> 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) -> Timestamp { Timestamp{ seconds: utc.timestamp(), nanos: i32::try_from(utc.nanosecond()).unwrap(), } -} \ No newline at end of file +} + +/// ## 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.timestamp_opt(t.seconds, u32::try_from(t.nanos).unwrap()).unwrap() +}