Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add new_timestamp to Session #1175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 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 @@ -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
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there was discussion about it, created issue #1179

/// 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