Skip to content

Commit

Permalink
Remove Utc::now() and dependents without wasmbind
Browse files Browse the repository at this point in the history
`std::time::SystemTime::now()` panics in WASM environments other than
Emscripten (i.e., wasm32-unknown-emscripten) or WASI (e.g.,
wasm32-wasi). Since compilation errors are preferable to unexpected
runtime panics, this PR removes the `Utc::now()` function from this
crate's public interface altogether in unsupported WASM environments
unless the `wasmbind` feature is enabled. This catches the case in
which a user of the crate forgets to enable the `wasmbind` feature
(see ramosbugs/openidconnect-rs#127 and ramosbugs/oauth2-rs#230) in
build targets that require it.

Fixes chronotope#1301.
  • Loading branch information
ramosbugs committed Apr 9, 2024
1 parent 332be72 commit b0b2df8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
6 changes: 1 addition & 5 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,7 @@ impl DateTime<Utc> {
feature = "std",
all(
feature = "now",
not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))
any(not(target_arch = "wasm32"), target_os = "emscripten", target_os = "wasi")
)
))]
pub(crate) fn try_from_system_time(t: std::time::SystemTime) -> Result<DateTime<Utc>, Error> {
Expand Down
16 changes: 11 additions & 5 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Serialize};

use super::fixed::FixedOffset;
use super::{MappedLocalTime, TimeZone};
use crate::{DateTime, NaiveDateTime, Utc};
use crate::NaiveDateTime;

#[cfg(unix)]
#[path = "unix.rs"]
Expand Down Expand Up @@ -120,8 +120,8 @@ impl Local {
/// Returns a `DateTime<Local>` which corresponds to the current date, time and offset from
/// UTC.
///
/// See also the similar [`Utc::now()`] which returns `DateTime<Utc>`, i.e. without the local
/// offset.
/// See also the similar [`Utc::now()`](crate::Utc::now) which returns `DateTime<Utc>`, i.e.
/// without the local offset.
///
/// # Example
///
Expand All @@ -144,8 +144,14 @@ impl Local {
/// let offset = FixedOffset::east(5 * 60 * 60).unwrap();
/// let now_with_offset = Local::now().with_timezone(&offset);
/// ```
pub fn now() -> DateTime<Local> {
Utc::now().with_timezone(&Local)
#[cfg(any(
not(target_arch = "wasm32"),
feature = "wasmbind",
target_os = "emscripten",
target_os = "wasi",
))]
pub fn now() -> crate::DateTime<Local> {
crate::Utc::now().with_timezone(&Local)
}
}

Expand Down
25 changes: 5 additions & 20 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,12 @@
//! The UTC (Coordinated Universal Time) time zone.

use core::fmt;
#[cfg(all(
feature = "now",
not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))
))]
use std::time::SystemTime;

#[cfg(any(feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

use super::{FixedOffset, MappedLocalTime, Offset, TimeZone};
use crate::naive::NaiveDateTime;
#[cfg(feature = "now")]
use crate::DateTime;
#[cfg(all(feature = "now", doc))]
use crate::OutOfRange;

Expand Down Expand Up @@ -82,14 +71,10 @@ impl Utc {
/// Panics if the system clock is set to a time in the extremely distant past or future, such
/// that it is out of the range representable by `DateTime<Utc>`. It is assumed that this
/// crate will no longer be in use by that time.
#[cfg(not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten", target_os = "wasi"))]
#[must_use]
pub fn now() -> DateTime<Utc> {
DateTime::try_from_system_time(SystemTime::now()).expect(
pub fn now() -> crate::DateTime<Utc> {
crate::DateTime::try_from_system_time(std::time::SystemTime::now()).expect(
"system clock is set to a time extremely far into the past or future; cannot convert",
)
}
Expand All @@ -101,9 +86,9 @@ impl Utc {
not(any(target_os = "emscripten", target_os = "wasi"))
))]
#[must_use]
pub fn now() -> DateTime<Utc> {
pub fn now() -> crate::DateTime<Utc> {
let now = js_sys::Date::new_0();
DateTime::<Utc>::from(now)
crate::DateTime::<Utc>::from(now)
}
}

Expand Down

0 comments on commit b0b2df8

Please sign in to comment.