{"id":16801085,"url":"https://github.com/daxpedda/web-time","last_synced_at":"2025-04-12T11:52:15.312Z","repository":{"id":147940179,"uuid":"619419252","full_name":"daxpedda/web-time","owner":"daxpedda","description":"Drop-in replacement for std::time for Wasm in browsers","archived":false,"fork":false,"pushed_at":"2024-10-13T21:58:49.000Z","size":254,"stargazers_count":48,"open_issues_count":0,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-14T09:35:52.526Z","etag":null,"topics":["browser","instant","rust","systemtime","time","wasm","wasm-bindgen","web"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daxpedda.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["daxpedda"]}},"created_at":"2023-03-27T05:26:13.000Z","updated_at":"2024-10-13T21:58:52.000Z","dependencies_parsed_at":"2023-10-17T04:30:25.539Z","dependency_job_id":"ced3b099-bd35-43da-8292-68a9a0ac4746","html_url":"https://github.com/daxpedda/web-time","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daxpedda%2Fweb-time","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daxpedda%2Fweb-time/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daxpedda%2Fweb-time/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daxpedda%2Fweb-time/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daxpedda","download_url":"https://codeload.github.com/daxpedda/web-time/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565016,"owners_count":21125414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browser","instant","rust","systemtime","time","wasm","wasm-bindgen","web"],"created_at":"2024-10-13T09:35:52.081Z","updated_at":"2025-04-12T11:52:15.290Z","avatar_url":"https://github.com/daxpedda.png","language":"Rust","readme":"# web-time\n\n[![Crates.io Version](https://img.shields.io/crates/v/web-time.svg)](https://crates.io/crates/web-time)\n[![Live Build Status](https://img.shields.io/github/check-runs/daxpedda/web-time/main?label=CI)](https://github.com/daxpedda/web-time/actions?query=branch%3Amain)\n[![Docs.rs Documentation](https://img.shields.io/docsrs/web-time?label=docs.rs)](https://docs.rs/web-time/1.1.0)\n[![Main Documentation](https://img.shields.io/github/actions/workflow/status/daxpedda/web-time/coverage-documentation.yaml?branch=main\u0026label=main%20docs)](https://daxpedda.github.io/web-time/doc/web_time)\n[![Test Coverage](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fdaxpedda.github.io%2Fweb-time%2Fcoverage%2Fcoverage.json\u0026query=%24.coverage\u0026label=Test%20Coverage)](https://daxpedda.github.io/web-time/coverage)\n\n## Description\n\nComplete drop-in replacement for [`std::time`] that works in browsers.\n\nCurrently [`Instant::now()`] and [`SystemTime::now()`] will simply panic when using the\n`wasm32-unknown-unknown` target. This implementation uses [`Performance.now()`] for [`Instant`] and\n[`Date.now()`] for [`SystemTime`] to offer a drop-in replacement that works in browsers.\n\nAt the same time the library will simply re-export [`std::time`] when not using the\n`wasm32-unknown-unknown` or `wasm32v1-none` target and will not pull in any dependencies.\n\nAdditionally, if compiled with `target-feature = \"atomics\"` it will synchronize the timestamps to\naccount for different context's, like web workers. See [`Performance.timeOrigin`] for more\ninformation.\n\n## Target\n\nThis library specifically targets browsers, that support [`Performance.now()`], with the\n`wasm32-unknown-unknown` or `wasm32v1-none` target. Emscripten is not supported. WASI doesn't\nrequire support as it has it's own native API to deal with [`std::time`].\n\nFurthermore it depends on [`wasm-bindgen`], which is required. This library will continue to depend\non it until a viable alternative presents itself, in which case multiple ecosystems could be\nsupported.\n\n## Note\n\n### Ticking during sleep\n\nCurrently a known bug is affecting browsers on operating system other then Windows. This bug\nprevents [`Instant`] from continuing to tick when the context is asleep. While this doesn't conflict\nwith Rusts requirements of [`Instant`], by chance Rust's Std\n[has the same problem](https://github.com/rust-lang/rust/issues/79462).\n\nSee\n[the MDN documentation on this](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#ticking_during_sleep)\nfor more information.\n\n### Context support\n\nThe implementation of [`Instant::now()`] relies on the availability of the [`Performance` object], a\nlack thereof will cause a panic. This can happen if called from a [worklet].\n\n## Usage\n\nYou can simply import the types you need:\n\n```rust\nuse web_time::{Instant, SystemTime};\n\nlet now = Instant::now();\nlet time = SystemTime::now();\n```\n\nUsing `-Ctarget-feature=+nontrapping-fptoint` will improve the performance of [`Instant::now()`] and\n[`SystemTime::now()`], but the vast majority of the time is still spent going through JS.\n\n## Features\n\n### `std` (enabled by default)\n\nEnables the corresponding crate feature in all dependencies and allows for some optimized\ninstruction output.\n\nWithout this crate feature compilation the standard library is not included. Has no effect on\ntargets other then `wasm32-unknown-unknown` or `wasm32v1-none`.\n\n### `msrv` (enabled by default)\n\nAllows `web-time` to make use of features only available in higher MSRVs. This offers compile-time\ndetection and does not break compilation when enabled with the crates MSRV.\n\n- Rust v1.77 + `std`: Enables the use of the [`f64.nearest`] instruction. Which will significantly\n  reduce the instruction count for [`Instant::now()`].\n- Rust Nightly: Enables the use of the [`f64.trunc`] and [`f64.nearest`] instruction. Which will\n  significantly reduce the instruction count for [`Instant::now()`].\n\n### `serde`\n\nImplements [`serde::Deserialize`] and [`serde::Serialize`] for [`SystemTime`].\n\n## Conditional Configurations\n\n### `docsrs`\n\nThis requires Rust nightly and enhances the documentation. It must only be used with `RUSTDOCFLAGS`,\nnot with `RUSTFLAGS`.\n\n## MSRV Policy\n\nThe MSRV is v1.60. Changes to the MSRV will be accompanied by a minor version bump.\n\n## Changelog\n\nSee the [CHANGELOG] file for details.\n\n## Contributing\n\nSee the [CONTRIBUTING] file for details.\n\n## Attribution\n\nInspiration was taken from the [instant](https://github.com/sebcrozet/instant/tree/v0.1.12) project.\n\nAdditional insight was taken from the [time](https://github.com/time-rs/time/tree/v0.3.20) project.\n\n## License\n\nLicensed under either of\n\n- Apache License, Version 2.0 ([LICENSE-APACHE] or \u003chttp://www.apache.org/licenses/LICENSE-2.0\u003e)\n- MIT license ([LICENSE-MIT] or \u003chttp://opensource.org/licenses/MIT\u003e)\n\nat your option.\n\n### Copyright\n\nA majority of the code and documentation was taken from [`std::time`]. For license information see\n[#License](https://github.com/rust-lang/rust/tree/1.68.1#license).\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the\nwork by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n\n[CHANGELOG]: https://github.com/daxpedda/web-time/blob/v1.1.0/CHANGELOG.md\n[CONTRIBUTING]: https://github.com/daxpedda/web-time/blob/v1.1.0/CONTRIBUTING.md\n[LICENSE-MIT]: https://github.com/daxpedda/web-time/blob/v1.1.0/LICENSE-MIT\n[LICENSE-APACHE]: https://github.com/daxpedda/web-time/blob/v1.1.0/LICENSE-APACHE\n[worklet]: https://developer.mozilla.org/en-US/docs/Web/API/Worklet\n[`Date.now()`]:\n\thttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now\n[`Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html\n[`Instant::now()`]: https://doc.rust-lang.org/std/time/struct.Instant.html#method.now\n[`SystemTime`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html\n[`SystemTime::now()`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#method.now\n[`std::time`]: https://doc.rust-lang.org/std/time/\n[`performance.now()`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now\n[`Performance.timeOrigin`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin\n[`Performance` object]: https://developer.mozilla.org/en-US/docs/Web/API/performance_property\n[`serde::Deserialize`]: https://docs.rs/serde/1/serde/trait.Deserialize.html\n[`serde::Serialize`]: https://docs.rs/serde/1/serde/trait.Serialize.html\n[`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen\n[`f64.nearest`]:\n\thttps://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric\n[`f64.trunc`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric\n","funding_links":["https://github.com/sponsors/daxpedda"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaxpedda%2Fweb-time","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaxpedda%2Fweb-time","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaxpedda%2Fweb-time/lists"}