Skip to content

Commit d08fcb3

Browse files
authored
wasm: improve panic message when js feature isn't enabled
This should hopefully make the opt-in `js` feature more discoverable when users hit panics. Closes #296
1 parent df373bd commit d08fcb3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# CHANGELOG
22

3+
0.2.9 (TBD)
4+
===========
5+
TODO
6+
7+
Enhancements:
8+
9+
* [#296](https://github.com/BurntSushi/jiff/issues/296):
10+
Provide a better panic message when `Zoned::now()` fails on WASM.
11+
12+
313
0.2.8 (2025-04-13)
414
==================
515
This release fixes a bug where the constructors on `SignedDuration`

src/now.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@ pub(crate) use self::sys::*;
2222
)))]
2323
mod sys {
2424
pub(crate) fn system_time() -> std::time::SystemTime {
25-
std::time::SystemTime::now()
25+
// `SystemTime::now()` should continue to panic on this exact target in
26+
// the future as well; Instead of letting `std` panic, we panic first
27+
// with a more informative error message.
28+
if cfg!(all(
29+
not(feature = "js"),
30+
any(target_arch = "wasm32", target_arch = "wasm64"),
31+
target_os = "unknown"
32+
)) {
33+
panic!(
34+
"getting the current time in wasm32-unknown-unknown \
35+
is not possible with just the standard library, \
36+
enable Jiff's `js` feature if you are \
37+
targeting a browser environment",
38+
);
39+
} else {
40+
std::time::SystemTime::now()
41+
}
2642
}
2743

2844
#[cfg(any(
@@ -31,7 +47,18 @@ mod sys {
3147
feature = "tzdb-concatenated"
3248
))]
3349
pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
34-
Some(std::time::Instant::now())
50+
// Same reasoning as above, but we return `None` instead of panicking,
51+
// because Jiff can deal with environments that don't provide
52+
// monotonic time.
53+
if cfg!(all(
54+
not(feature = "js"),
55+
any(target_arch = "wasm32", target_arch = "wasm64"),
56+
target_os = "unknown"
57+
)) {
58+
None
59+
} else {
60+
Some(std::time::Instant::now())
61+
}
3562
}
3663
}
3764

0 commit comments

Comments
 (0)