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.
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.
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. While this doesn't conflict
with Rusts requirements of Instant
, by chance Rust's Std
has the same problem.
See the MDN documentation on this for more information.
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.
You can simply import the types you need:
use web_time::{Instant, SystemTime};
let now = Instant::now();
let time = SystemTime::now();
Implements serde::Deserialize
and serde::Serialize
for SystemTime
.
This requires Rust nightly and enhances the documentation. It must only be used with RUSTDOCFLAGS
,
not with RUSTFLAGS
.
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.
instant
is a
popular alternative! However the API it implements doesn't match std::time
exactly.
See the CHANGELOG file for details.
See the CONTRIBUTING file for details.
Inspiration was taken from the instant project.
Additional insight was taken from the time project.
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
A majority of the code and documentation was taken from std::time
. For license information see
#License.
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.