From 72867fa7d8321430f24c534cd86efcdfc1406ddd Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Fri, 15 Sep 2023 17:05:54 +0200 Subject: [PATCH 1/3] Improve Timestamp.seconds_since_unix_epoch() --- Cargo.lock | 5 ++--- src/value.rs | 10 +--------- zenoh/value.py | 3 ++- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25cc7254..7c778d22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2251,11 +2251,10 @@ checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" [[package]] name = "uhlc" -version = "0.6.0" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af1438496174a601a35fb41bf9bc408e47384b1df52ddc1252e75ef0ab811322" +checksum = "d1eadef1fa26cbbae1276c46781e8f4d888bdda434779c18ae6c2a0e69991885" dependencies = [ - "hex", "humantime", "lazy_static", "log", diff --git a/src/value.rs b/src/value.rs index 0432412f..26ae8721 100644 --- a/src/value.rs +++ b/src/value.rs @@ -195,15 +195,7 @@ impl _Timestamp { } #[getter] pub fn seconds_since_unix_epoch(&self) -> PyResult { - match self - .0 - .get_time() - .to_system_time() - .duration_since(std::time::UNIX_EPOCH) - { - Ok(o) => Ok(o.as_secs_f64()), - Err(e) => Err(e.to_pyerr()), - } + Ok(self.0.get_time().as_secs_f64()) } } diff --git a/zenoh/value.py b/zenoh/value.py index 5c33685f..d1bcd723 100644 --- a/zenoh/value.py +++ b/zenoh/value.py @@ -118,7 +118,8 @@ def seconds_since_unix_epoch(self) -> float: """ Returns the number of seconds since the Unix Epoch. - You shouldn't use this for comparison though, and rely on comparison operators between members of this class. + Considering the large number of seconds since the Unix Epoch, the precision of the resulting f64 is in the order of microseconds. + Therefore, it should not be used for comparison. Directly comparing Timestamp objects is preferable. """ return super().seconds_since_unix_epoch From 36256f3d8f0a11fe0917841ffa7557f2f03e48c4 Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Mon, 18 Sep 2023 11:10:51 +0200 Subject: [PATCH 2/3] Add Timestamp.get_time --- src/value.rs | 4 ++++ zenoh/value.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/value.rs b/src/value.rs index 26ae8721..24fba452 100644 --- a/src/value.rs +++ b/src/value.rs @@ -194,6 +194,10 @@ impl _Timestamp { } } #[getter] + pub fn get_time(&self) -> PyResult { + Ok(self.0.get_time().0) + } + #[getter] pub fn seconds_since_unix_epoch(&self) -> PyResult { Ok(self.0.get_time().as_secs_f64()) } diff --git a/zenoh/value.py b/zenoh/value.py index d1bcd723..6446f52a 100644 --- a/zenoh/value.py +++ b/zenoh/value.py @@ -114,6 +114,12 @@ class Timestamp(_Timestamp): def _upgrade_(this: _Timestamp) -> 'Timestamp': return _Timestamp.__new__(Timestamp, this) @property + def get_time(self) -> int: + """ + Returns the time part, as generated by the Zenoh HLC in NTP64 format (See https://datatracker.ietf.org/doc/html/rfc5905#section-6). + """ + return super().time + @property def seconds_since_unix_epoch(self) -> float: """ Returns the number of seconds since the Unix Epoch. From ab13b43296b26236872fc0e483aa9e86e9bf9f55 Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Mon, 18 Sep 2023 11:52:55 +0200 Subject: [PATCH 3/3] Remove unecessary Result --- src/value.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/value.rs b/src/value.rs index 24fba452..ceb8d727 100644 --- a/src/value.rs +++ b/src/value.rs @@ -194,12 +194,12 @@ impl _Timestamp { } } #[getter] - pub fn get_time(&self) -> PyResult { - Ok(self.0.get_time().0) + pub fn get_time(&self) -> u64 { + self.0.get_time().0 } #[getter] - pub fn seconds_since_unix_epoch(&self) -> PyResult { - Ok(self.0.get_time().as_secs_f64()) + pub fn seconds_since_unix_epoch(&self) -> f64 { + self.0.get_time().as_secs_f64() } }