time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.09k stars 273 forks source link

`Instant::now()` for wasm #559

Closed daxpedda closed 1 year ago

daxpedda commented 1 year ago

Currently calling Instant::now() just panics on wasm in the browser, this is a known issue in std and will probably never get fixed without a new target specifically for browsers.

This could be easily implemented for time though, with the help of performance.now(), even without pulling in web-sys.

With the current API this can't be done because std::time::Instant can't be constructed in any way.

The solution would be to not expose std::time::Instant in time::Instant and instead just provide into() or as_ref() methods that are disabled on wasm. The big problem here is of course the breaking change.

I know this is probably way out there but still wanted to leave this here just in case.

jhpratt commented 1 year ago

I'm on mobile at the moment, but I'm fairly certain this sort of thing has been brought up before in some way or another. time::Instant is intended to be a wrapper around std::time::Instant for the sole reason to provide additional trait impls and methods. Ultimately the issue is upstream, which is where it should be fixed if desired.