Skip to content

Commit

Permalink
Migrate to web_time::SystemTime for wasm32-unknown-unknown targets (
Browse files Browse the repository at this point in the history
#118)

See title.

This should allow `SystemTime` to work on any wasm targets.
  • Loading branch information
nekevss authored Dec 4, 2024
1 parent 454d1a8 commit 1fc7c16
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ tzif = { version = "0.2.3", optional = true }
jiff-tzdb = { version = "0.1.1", optional = true }
combine = { version = "4.6.7", optional = true }

# System time feature
web-time = { version = "1.1.0", optional = true }

[features]
log = ["dep:log"]
experimental = ["tzdb"]
Expand Down
34 changes: 32 additions & 2 deletions src/components/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Now {

pub fn plain_date_time_with_provider(
tz: Option<TimeZone>,
provider: &mut impl TzProvider,
provider: &impl TzProvider,
) -> TemporalResult<PlainDateTime> {
let iso = system_date_time(tz, provider)?;
Ok(PlainDateTime::new_unchecked(iso, Calendar::default()))
Expand All @@ -45,7 +45,7 @@ impl Now {
#[cfg(feature = "std")]
fn system_date_time(
tz: Option<TimeZone>,
provider: &mut impl TzProvider,
provider: &impl TzProvider,
) -> TemporalResult<IsoDateTime> {
// 1. If temporalTimeZoneLike is undefined, then
// a. Let timeZone be SystemTimeZoneIdentifier().
Expand All @@ -69,3 +69,33 @@ fn system_instant() -> TemporalResult<Instant> {
let nanos = sys::get_system_nanoseconds()?;
Instant::try_new(i128::from_u128(nanos).temporal_unwrap()?)
}

#[cfg(test)]
mod tests {
use std::thread;
use std::time::Duration as StdDuration;

use crate::{partial::PartialDuration, tzdb::FsTzdbProvider, Duration, Now};

#[cfg(feature = "tzdb")]
#[test]
fn now_datetime_test() {
let provider = &FsTzdbProvider::default();

let now = Now::plain_date_time_with_provider(None, provider).unwrap();
thread::sleep(StdDuration::from_secs(2));
let then = Now::plain_date_time_with_provider(None, provider).unwrap();

let two_seconds = Duration::from_partial_duration(PartialDuration {
seconds: Some(2.into()),
..Default::default()
})
.unwrap();

let now_plus_two = now.add(&two_seconds, None).unwrap();

assert_eq!(now_plus_two.second(), then.second());
assert_eq!(now_plus_two.minute(), then.minute());
assert_eq!(now_plus_two.hour(), then.hour());
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
extern crate alloc;
extern crate core;

// TODO: Support SystemTime directly / pull in OS code from std::time?
#[cfg(feature = "std")]
extern crate std;

Expand Down
4 changes: 2 additions & 2 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use alloc::string::{String, ToString};

use crate::{TemporalError, TemporalResult};

use std::time::{SystemTime, UNIX_EPOCH};
use web_time::{SystemTime, UNIX_EPOCH};

// TODO: Need to implement system handling for non_std.
// TODO: Need to implement SystemTime handling for non_std.

/// Returns the system time in nanoseconds.
pub(crate) fn get_system_nanoseconds() -> TemporalResult<u128> {
Expand Down
4 changes: 3 additions & 1 deletion src/tzdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ impl Tzif {

#[inline]
fn get_local_record(db: &DataBlock, idx: usize) -> LocalTimeTypeRecord {
db.local_time_type_records[db.transition_types[idx]]
// NOTE: Transition type can be empty. If no transition_type exists,
// then use 0 as the default index of local_time_type_records.
db.local_time_type_records[db.transition_types.get(idx).copied().unwrap_or(0)]
}

#[inline]
Expand Down

0 comments on commit 1fc7c16

Please sign in to comment.