MCPcopy Index your code
hub / github.com/daxpedda/web-time

github.com/daxpedda/web-time @v1.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.1.0 ↗ · + Follow
64 symbols 89 edges 21 files 10 documented · 16%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

web-time

Crates.io Version Live Build Status Docs.rs Documentation Main Documentation

Description

Complete drop-in replacement for [std::time] that works in browsers.

Currently [Instant::now()] and [SystemTime::now()] will simply panic when using the wasm32-unknown-unknown target. This implementation uses [Performance.now()] for [Instant] and [Date.now()] for [SystemTime] to offer a drop-in replacement that works in browsers.

At the same time the library will simply re-export [std::time] when not using the wasm32-unknown-unknown target and will not pull in any dependencies.

Additionally, if compiled with target-feature = "atomics" it will synchronize the timestamps to account for different context's, like web workers. See [Performance.timeOrigin] for more information.

Using -Ctarget-feature=+nontrapping-fptoint will improve the performance of [Instant::now()] and [SystemTime::now()], but the vast majority of the time is still spent going through JS.

Target

This library specifically targets browsers, that support [Performance.now()], with the wasm32-unknown-unknown target. Emscripten is not supported. WASI doesn't require support as it has it's own native API to deal with [std::time].

Furthermore it depends on [wasm-bindgen], which is required. This library will continue to depend on it until a viable alternative presents itself, in which case multiple ecosystems could be supported.

Note

Ticking during sleep

Currently a known bug is affecting browsers on operating system other then Windows. This bug prevents [Instant] from continuing to tick when the context is asleep. This doesn't necessarily conflict with Rusts requirements of [Instant], but might still be unexpected.

See the MDN documentation on this for more information.

Context support

The implementation of [Instant::now()] relies on the availability of the [Performance object], a lack thereof will cause a panic. This can happen if called from a worklet.

Usage

You can simply import the types you need:

use web_time::{Instant, SystemTime};

let now = Instant::now();
let time = SystemTime::now();

Features

serde

Implements [serde::Deserialize] and [serde::Serialize] for [SystemTime].

MSRV

As this library heavily relies on [wasm-bindgen] the MSRV depends on it. At the point of time this was written the MSRV is 1.60.

Alternatives

instant Crates.io is a popular alternative! However the API it implements doesn't match [std::time] exactly.

Changelog

See the CHANGELOG file for details.

Contributing

See the CONTRIBUTING file for details.

Attribution

Inspiration was taken from the instant project.

Additional insight was taken from the time project.

License

Licensed under either of

at your option.

Copyright

A majority of the code and documentation was taken from [std::time]. For license information see #License.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Extension points exported contracts — how you extend this code

SystemTimeExt (Interface)
Web-specific extension to [`web_time::SystemTime`](crate::SystemTime). [1 implementers]
src/web.rs

Core symbols most depended-on inside this repo

assert
called by 25
src/time/instant.rs
sleep
called by 4
tests/util/web.rs
checked_sub
called by 3
src/time/instant.rs
time_stamp_to_duration
called by 3
src/time/instant.rs
duration_since
called by 2
src/time/instant.rs
checked_duration_since
called by 2
src/time/instant.rs
checked_add
called by 2
src/time/instant.rs
signal
called by 2
tests/util/web.rs

Shape

Method 37
Function 15
Class 9
Enum 2
Interface 1

Languages

Rust100%

Modules by API surface

src/time/instant.rs19 symbols
src/time/system_time.rs13 symbols
src/time/serde.rs11 symbols
tests/util/web.rs8 symbols
tests/serde.rs3 symbols
tests/atomic.rs3 symbols
src/web.rs3 symbols
benches/benchmark.rs2 symbols
tests/util/std.rs1 symbols
tests/traits.rs1 symbols

For agents

$ claude mcp add web-time \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact