Skip to content

Commit

Permalink
Add Timestamp.get_time and improve Timestamp.seconds_since_unix_epoch (
Browse files Browse the repository at this point in the history
  • Loading branch information
JEnoch authored Sep 18, 2023
1 parent b8f16f1 commit 75ac29b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,12 @@ impl _Timestamp {
}
}
#[getter]
pub fn seconds_since_unix_epoch(&self) -> PyResult<f64> {
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()),
}
pub fn get_time(&self) -> u64 {
self.0.get_time().0
}
#[getter]
pub fn seconds_since_unix_epoch(&self) -> f64 {
self.0.get_time().as_secs_f64()
}
}

Expand Down
9 changes: 8 additions & 1 deletion zenoh/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,18 @@ 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.
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

Expand Down

0 comments on commit 75ac29b

Please sign in to comment.