Skip to content

Commit

Permalink
Use instant for Time::now() (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed May 11, 2023
1 parent 31025c6 commit 50e0026
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/re_log_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ document-features = "0.2"
fixed = { version = "1.17", default-features = false, features = ["serde"] }
half = { workspace = true, features = ["bytemuck"] }
itertools = { workspace = true }
instant = { version = "0.1" }
lazy_static.workspace = true
ndarray.workspace = true
nohash-hasher = "0.2"
Expand Down
15 changes: 13 additions & 2 deletions crates/re_log_types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use time::OffsetDateTime;
pub struct Time(i64);

impl Time {
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn now() -> Self {
let nanos_since_epoch = std::time::SystemTime::UNIX_EPOCH
let nanos_since_epoch = instant::SystemTime::UNIX_EPOCH
.elapsed()
.expect("Expected system clock to be set to after 1970")
.as_nanos() as _;
Expand Down Expand Up @@ -156,6 +155,18 @@ impl TryFrom<std::time::SystemTime> for Time {
}
}

// On non-wasm32 builds, `instant::SystemTime` is a re-export of `std::time::SystemTime`,
// so it's covered by the above `TryFrom`.
#[cfg(target_arch = "wasm32")]
impl TryFrom<instant::SystemTime> for Time {
type Error = ();

fn try_from(time: instant::SystemTime) -> Result<Time, Self::Error> {
time.duration_since(instant::SystemTime::UNIX_EPOCH)
.map(|duration_since_epoch| Time(duration_since_epoch.as_nanos() as _))
}
}

impl TryFrom<time::OffsetDateTime> for Time {
type Error = core::num::TryFromIntError;

Expand Down

0 comments on commit 50e0026

Please sign in to comment.