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

This commit is contained in:
David Ramos 2024-04-11 03:35:11 -07:00 committed by GitHub
parent 332be72d1d
commit 16c45fa8ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 30 deletions

View File

@ -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> {

View File

@ -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"]
@ -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
///
@ -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)
}
}

View File

@ -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;
@ -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",
)
}
@ -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)
}
}