Skip to content

Commit

Permalink
Merge pull request #1175 from ZettaScaleLabs/zsl/dev/1.0.0/new_timestamp
Browse files Browse the repository at this point in the history
Draft: Add `new_timestamp` to Session
  • Loading branch information
milyin authored Jun 21, 2024
2 parents 65e5df7 + 3799dc1 commit a6d117b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 23 additions & 2 deletions zenoh/src/api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -657,6 +657,27 @@ impl Session {
pub fn config(&self) -> &Notifier<Config> {
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();
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())
}
}

impl<'a> SessionDeclarations<'a, 'a> for Session {
Expand Down
1 change: 1 addition & 0 deletions zenoh/src/api/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Into<TimestampId>>(id: T) -> Timestamp {
Expand Down

0 comments on commit a6d117b

Please sign in to comment.