Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Utc::now() and Local::now() on unsupported platforms #1567

Merged
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
15 changes: 10 additions & 5 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,16 @@ impl DateTime<Utc> {
feature = "std",
all(
feature = "now",
not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))
any(
unix,
windows,
target_os = "solid_asp3",
target_os = "hermit",
target_os = "wasi",
target_os = "xous",
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "teeos",
)
)
))]
pub(crate) fn try_from_system_time(t: std::time::SystemTime) -> Result<DateTime<Utc>, Error> {
Expand Down
21 changes: 16 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,19 @@ 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(
unix,
windows,
target_os = "solid_asp3",
target_os = "hermit",
target_os = "wasi",
target_os = "xous",
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "teeos",
all(target_arch = "wasm32", feature = "wasmbind")
))]
pub fn now() -> crate::DateTime<Local> {
crate::Utc::now().with_timezone(&Local)
}
}

Expand Down
40 changes: 20 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,25 @@ 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"))
)))]
// Covers the platforms with `SystemTime::time()` supported by the Rust Standard Library as of
// Rust 1.78. See:
// https://github.com/rust-lang/rust/blob/5e9bed7b1e1524936001b37b6ecbf406179d8ebe/library/std/src/sys/pal/mod.rs
// Note that some platforms listed in the PAL table do not support `SystemTime::time()` (e.g.,
// `zkvm` and `wasm`). When updating this configuration, also be sure to update `Local::now()`
// and `DateTime::try_from_system_time()`.
#[cfg(any(
unix,
windows,
target_os = "solid_asp3",
target_os = "hermit",
target_os = "wasi",
target_os = "xous",
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "teeos",
))]
#[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 +101,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
Loading