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) and 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 4566a4d commit 23f7c98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
25 changes: 17 additions & 8 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use rkyv::{Archive, Deserialize, Serialize};
use super::fixed::FixedOffset;
use super::{MappedLocalTime, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
#[allow(deprecated)]
use crate::Date;
use crate::{DateTime, Utc};

#[cfg(unix)]
#[path = "unix.rs"]
Expand Down Expand Up @@ -123,18 +120,24 @@ pub struct Local;

impl Local {
/// Returns a `Date` which corresponds to the current date.
#[cfg(any(
not(target_arch = "wasm32"),
feature = "wasmbind",
target_os = "emscripten",
target_os = "wasi",
))]
#[deprecated(since = "0.4.23", note = "use `Local::now()` instead")]
#[allow(deprecated)]
#[must_use]
pub fn today() -> Date<Local> {
pub fn today() -> crate::Date<Local> {
Local::now().date()
}

/// 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) which returns `DateTime<Utc>`, i.e. without
/// the local offset.
///
/// # Example
///
Expand All @@ -157,8 +160,14 @@ impl Local {
/// let offset = FixedOffset::east_opt(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
39 changes: 15 additions & 24 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +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, UNIX_EPOCH};

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

use super::{FixedOffset, MappedLocalTime, Offset, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
#[cfg(feature = "now")]
#[allow(deprecated)]
use crate::{Date, DateTime};

/// The UTC time zone. This is the most efficient time zone when you don't need the local time.
/// It is also used as an offset (which is also a dummy type).
Expand Down Expand Up @@ -54,13 +42,19 @@ pub struct Utc;
#[cfg(feature = "now")]
impl Utc {
/// Returns a `Date` which corresponds to the current date.
#[cfg(any(
not(target_arch = "wasm32"),
feature = "wasmbind",
target_os = "emscripten",
target_os = "wasi",
))]
#[deprecated(
since = "0.4.23",
note = "use `Utc::now()` instead, potentially with `.date_naive()`"
)]
#[allow(deprecated)]
#[must_use]
pub fn today() -> Date<Utc> {
pub fn today() -> crate::Date<Utc> {
Utc::now().date()
}

Expand All @@ -86,16 +80,13 @@ impl Utc {
/// let offset = FixedOffset::east_opt(5 * 60 * 60).unwrap();
/// let now_with_offset = Utc::now().with_timezone(&offset);
/// ```
#[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> {
let now =
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos()).unwrap()
pub fn now() -> crate::DateTime<Utc> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system time before Unix epoch");
crate::DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos()).unwrap()
}

/// Returns a `DateTime` which corresponds to the current date and time.
Expand All @@ -105,9 +96,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 23f7c98

Please sign in to comment.