From 5a893440b139b1413037b02d045b33a72211ba19 Mon Sep 17 00:00:00 2001 From: Charles Schleich Date: Thu, 20 Jun 2024 00:04:08 +0200 Subject: [PATCH 1/2] Add new_timestamp --- zenoh/src/api/session.rs | 28 +++++++++++++++++++++++++--- zenoh/src/api/time.rs | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index e874cd2393..81444ccbaa 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -21,11 +21,11 @@ use std::{ atomic::{AtomicU16, Ordering}, Arc, RwLock, }, - time::Duration, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use tracing::{error, trace, warn}; -use uhlc::HLC; +use uhlc::{Timestamp, HLC}; use zenoh_buffers::ZBuf; use zenoh_collections::SingleOrVec; use zenoh_config::{unwrap_or_default, wrappers::ZenohId, Config, Notifier}; @@ -630,7 +630,7 @@ impl Session { /// The returned configuration [`Notifier`](Notifier) can be used to read the current /// zenoh configuration through the `get` function or /// modify the zenoh configuration through the `insert`, - /// or `insert_json5` funtion. + /// or `insert_json5` function. /// /// # Examples /// ### Read current zenoh configuration @@ -657,6 +657,28 @@ impl Session { pub fn config(&self) -> &Notifier { self.runtime.config() } + + /// Get a new Timestamp from a Zenoh session [`Session`](Session). + /// + /// The returned timestamp has the current time, with the Session's runtime ZenohID + /// + /// # Examples + /// ### Read current zenoh configuration + /// ``` + /// # #[tokio::main] + /// # async fn main() { + /// use zenoh::prelude::*; + /// + /// let session = zenoh::open(zenoh::config::peer()).await.unwrap(); + /// let timestamp = session.new_timestamp(); + /// # } + /// ``` + pub fn new_timestamp(&self) -> Timestamp { + let id = self.runtime.zid(); + // TODO: Should we make this an Result return type ? + let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().into(); + Timestamp::new(now, id.into()) + } } impl<'a> SessionDeclarations<'a, 'a> for Session { diff --git a/zenoh/src/api/time.rs b/zenoh/src/api/time.rs index a617c2004c..1879143389 100644 --- a/zenoh/src/api/time.rs +++ b/zenoh/src/api/time.rs @@ -15,6 +15,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use zenoh_protocol::core::{Timestamp, TimestampId}; +// TODO: Shall we remove this new_timestamp in favoir of the src/api/session::Session::new_timestamp(); /// Generates a [`Timestamp`] with [`TimestampId`] and current system time /// The [`TimestampId`] can be taken from session id returned by [`SessionInfo::zid()`](crate::api::info::SessionInfo::zid). pub fn new_timestamp>(id: T) -> Timestamp { From 3799dc11d4798c8a746ddbeae046eff30fa77ed6 Mon Sep 17 00:00:00 2001 From: Charles Schleich Date: Thu, 20 Jun 2024 16:18:38 +0200 Subject: [PATCH 2/2] Comment on unwrap being permissable --- zenoh/src/api/session.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index 81444ccbaa..ff6d2daef0 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -675,8 +675,7 @@ impl Session { /// ``` pub fn new_timestamp(&self) -> Timestamp { let id = self.runtime.zid(); - // TODO: Should we make this an Result return type ? - let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().into(); + let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().into(); // UNIX_EPOCH is Returns a Timespec::zero(), Unwrap Should be permissable here Timestamp::new(now, id.into()) } }