Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
31 changes: 29 additions & 2 deletions src/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -31,7 +47,18 @@ mod sys {
feature = "tzdb-concatenated"
))]
pub(crate) fn monotonic_time() -> Option<std::time::Instant> {
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())
}
}
}

Expand Down
Loading