diff --git a/CHANGELOG.md b/CHANGELOG.md index 096ee785..1f6fc7f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG +0.2.9 (TBD) +=========== +TODO + +Enhancements: + +* [#296](https://github.com/BurntSushi/jiff/issues/296): +Provide a better panic message when `Zoned::now()` fails on WASM. + + 0.2.8 (2025-04-13) ================== This release fixes a bug where the constructors on `SignedDuration` diff --git a/src/now.rs b/src/now.rs index 6478d0ed..6865850f 100644 --- a/src/now.rs +++ b/src/now.rs @@ -22,7 +22,23 @@ pub(crate) use self::sys::*; )))] mod sys { pub(crate) fn system_time() -> std::time::SystemTime { - std::time::SystemTime::now() + // `SystemTime::now()` should continue to panic on this exact target in + // the future as well; Instead of letting `std` panic, we panic first + // with a more informative error message. + if cfg!(all( + not(feature = "js"), + any(target_arch = "wasm32", target_arch = "wasm64"), + target_os = "unknown" + )) { + panic!( + "getting the current time in wasm32-unknown-unknown \ + is not possible with just the standard library, \ + enable Jiff's `js` feature if you are \ + targeting a browser environment", + ); + } else { + std::time::SystemTime::now() + } } #[cfg(any( @@ -31,7 +47,18 @@ mod sys { feature = "tzdb-concatenated" ))] pub(crate) fn monotonic_time() -> Option { - Some(std::time::Instant::now()) + // Same reasoning as above, but we return `None` instead of panicking, + // because Jiff can deal with environments that don't provide + // monotonic time. + if cfg!(all( + not(feature = "js"), + any(target_arch = "wasm32", target_arch = "wasm64"), + target_os = "unknown" + )) { + None + } else { + Some(std::time::Instant::now()) + } } }