-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,76 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use std::time::SystemTime; | ||
use std::{ | ||
hash::{Hash, Hasher}, | ||
time::{Duration, SystemTime}, | ||
}; | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::{prelude::*, types::PyType}; | ||
|
||
use crate::macros::wrapper; | ||
use crate::{macros::wrapper, utils::IntoPyResult}; | ||
|
||
wrapper!(zenoh::time::Timestamp: Clone, PartialEq, PartialOrd); | ||
wrapper!(zenoh::time::TimestampId: Copy, Clone, PartialEq, PartialOrd); | ||
|
||
#[pymethods] | ||
impl TimestampId { | ||
fn __richcmp__(&self, other: &Self, op: pyo3::pyclass::CompareOp) -> bool { | ||
match op { | ||
pyo3::pyclass::CompareOp::Lt => self < other, | ||
pyo3::pyclass::CompareOp::Le => self <= other, | ||
pyo3::pyclass::CompareOp::Eq => self == other, | ||
pyo3::pyclass::CompareOp::Ne => self != other, | ||
pyo3::pyclass::CompareOp::Gt => self > other, | ||
pyo3::pyclass::CompareOp::Ge => self >= other, | ||
} | ||
} | ||
|
||
fn __bytes__(&self) -> [u8; zenoh::time::TimestampId::MAX_SIZE] { | ||
self.0.to_le_bytes() | ||
} | ||
|
||
fn __hash__(&self, py: Python) -> PyResult<isize> { | ||
self.__bytes__().into_py(py).bind(py).hash() | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!("{:?}", self.0) | ||
} | ||
|
||
fn __str__(&self) -> String { | ||
format!("{}", self.0) | ||
} | ||
} | ||
|
||
wrapper!(zenoh::time::Timestamp: Clone, PartialEq, PartialOrd, Hash); | ||
|
||
#[pymethods] | ||
impl Timestamp { | ||
fn get_time(&self) -> SystemTime { | ||
self.0.get_time().to_system_time() | ||
} | ||
|
||
fn get_id(&self) -> TimestampId { | ||
(*self.0.get_id()).into() | ||
} | ||
|
||
fn get_diff_duration(&self, other: Timestamp) -> Duration { | ||
self.0.get_diff_duration(&other.0) | ||
} | ||
|
||
fn to_string_rfc3339_lossy(&self) -> String { | ||
self.0.to_string_rfc3339_lossy() | ||
} | ||
|
||
#[classmethod] | ||
fn parse_rfc3339(_cls: &Bound<PyType>, s: &str) -> PyResult<Self> { | ||
Ok(Self( | ||
zenoh::time::Timestamp::parse_rfc3339(s) | ||
.map_err(|err| err.cause) | ||
.into_pyres()?, | ||
)) | ||
} | ||
|
||
fn __richcmp__(&self, other: &Self, op: pyo3::pyclass::CompareOp) -> bool { | ||
match op { | ||
pyo3::pyclass::CompareOp::Lt => self < other, | ||
|
@@ -36,6 +92,12 @@ impl Timestamp { | |
} | ||
} | ||
|
||
fn __hash__(&self) -> u64 { | ||
let mut hasher = std::collections::hash_map::DefaultHasher::new(); | ||
self.0.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!("{:?}", self.0) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters