Skip to content

Commit

Permalink
Merge pull request #279 from karpfediem/hotfix/replace_system_time_we…
Browse files Browse the repository at this point in the history
…b_time

Draft: replace std::time:SystemTime with web_time::SystemTime
  • Loading branch information
ChristopherRabotin authored Jan 15, 2024
2 parents da8e406 + bfee0f7 commit 822c0a0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ lexical-core = { version = "0.8.5", default-features = false, features = [
reqwest = { version = "0.11", features = ["blocking", "json"], optional = true }
tabled = { version = "0.15.0", optional = true }
openssl = { version = "0.10", features = ["vendored"], optional = true }
web-time = { version = "1.0.0", optional = true }

[target.wasm32-unknown-unknown.dependencies]
js-sys = { version = "0.3" }
Expand Down Expand Up @@ -67,7 +68,7 @@ iai = "0.1"

[features]
default = ["std"]
std = ["serde", "serde_derive"]
std = ["serde", "serde_derive", "web-time"]
asn1der = ["der"]
python = ["std", "asn1der", "pyo3", "ut1"]
ut1 = ["std", "reqwest", "tabled", "openssl"]
Expand Down
18 changes: 2 additions & 16 deletions src/system_time.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
use crate::{Duration, Errors};

#[cfg(target_arch = "wasm32")]
pub(crate) fn duration_since_unix_epoch() -> Result<Duration, Errors> {
{
use crate::Unit;
use wasm_bindgen_rs::prelude::*;
js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance"))
.map_err(|_| Errors::SystemTimeError)
.map(|performance| {
performance.unchecked_into::<web_sys::Performance>().now() * Unit::Second
})
}
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn duration_since_unix_epoch() -> Result<Duration, Errors> {
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
web_time::SystemTime::now()
.duration_since(web_time::SystemTime::UNIX_EPOCH)
.map_err(|_| Errors::SystemTimeError)
.and_then(|d| d.try_into().map_err(|_| Errors::SystemTimeError))
}
2 changes: 1 addition & 1 deletion tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ fn unix() {
// Continuous check that the system time as reported by this machine is within millisecond accuracy of what we compute
#[cfg(feature = "std")]
{
use std::time::SystemTime;
use web_time::SystemTime;

let std_unix_time_s = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
Expand Down

0 comments on commit 822c0a0

Please sign in to comment.