From 7fb3e5921c882041bf7df04c66b1809b7ed5c933 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 23 Apr 2023 22:45:59 +0200 Subject: [PATCH] Just rely on the standard library for Local::now --- src/offset/local/mod.rs | 5 +++-- src/offset/local/stub.rs | 11 ----------- src/offset/local/unix.rs | 7 +------ src/offset/local/windows.rs | 15 --------------- 4 files changed, 4 insertions(+), 34 deletions(-) diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 1adddb7d60..74aa902b57 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -10,7 +10,8 @@ use super::fixed::FixedOffset; use super::{LocalResult, TimeZone}; use crate::naive::{NaiveDate, NaiveDateTime}; #[allow(deprecated)] -use crate::{Date, DateTime}; +use crate::Date; +use crate::{DateTime, Utc}; // we don't want `stub.rs` when the target_os is not wasi or emscripten // as we use js-sys to get the date instead @@ -74,7 +75,7 @@ impl Local { )))] #[must_use] pub fn now() -> DateTime { - inner::now() + Utc::now().with_timezone(&Local) } /// Returns a `DateTime` which corresponds to the current date and time. diff --git a/src/offset/local/stub.rs b/src/offset/local/stub.rs index 9ececd3c22..3b42717d82 100644 --- a/src/offset/local/stub.rs +++ b/src/offset/local/stub.rs @@ -13,10 +13,6 @@ use std::time::{SystemTime, UNIX_EPOCH}; use super::{FixedOffset, Local}; use crate::{DateTime, Datelike, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; -pub(super) fn now() -> DateTime { - tm_to_datetime(Timespec::now().local()) -} - /// Converts a local `NaiveDateTime` to the `time::Timespec`. #[cfg(not(all( target_arch = "wasm32", @@ -92,13 +88,6 @@ struct Timespec { } impl Timespec { - /// Constructs a timespec representing the current time in UTC. - fn now() -> Timespec { - let st = - SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch"); - Timespec { sec: st.as_secs() as i64, nsec: st.subsec_nanos() as i32 } - } - /// Converts this timespec into the system's local time. fn local(self) -> Tm { let mut tm = Tm { diff --git a/src/offset/local/unix.rs b/src/offset/local/unix.rs index 68b206cd90..d933cfb1d6 100644 --- a/src/offset/local/unix.rs +++ b/src/offset/local/unix.rs @@ -12,12 +12,7 @@ use std::{cell::RefCell, collections::hash_map, env, fs, hash::Hasher, time::Sys use super::tz_info::TimeZone; use super::{DateTime, FixedOffset, Local, NaiveDateTime}; -use crate::{Datelike, LocalResult, Utc}; - -pub(super) fn now() -> DateTime { - let now = Utc::now().naive_utc(); - naive_to_local(&now, false).unwrap() -} +use crate::{Datelike, LocalResult}; pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult> { TZ_INFO.with(|maybe_cache| { diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index 6475929fe4..455a707839 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -15,7 +15,6 @@ use std::result::Result; use winapi::shared::minwindef::FILETIME; use winapi::um::minwinbase::SYSTEMTIME; -use winapi::um::sysinfoapi::GetLocalTime; use winapi::um::timezoneapi::{ SystemTimeToFileTime, SystemTimeToTzSpecificLocalTime, TzSpecificLocalTimeToSystemTime, }; @@ -40,10 +39,6 @@ macro_rules! windows_sys_call { const HECTONANOSECS_IN_SEC: i64 = 10_000_000; const HECTONANOSEC_TO_UNIX_EPOCH: i64 = 11_644_473_600 * HECTONANOSECS_IN_SEC; -pub(super) fn now() -> DateTime { - LocalSysTime::local().datetime() -} - /// Converts a local `NaiveDateTime` to the `time::Timespec`. pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult> { let naive_sys_time = system_time_from_naive_date_time(d); @@ -65,16 +60,6 @@ struct LocalSysTime { } impl LocalSysTime { - fn local() -> Self { - let mut now = MaybeUninit::::uninit(); - unsafe { GetLocalTime(now.as_mut_ptr()) } - // SAFETY: GetLocalTime cannot fail according to spec, so we can assume the value - // is initialized. - let st = unsafe { now.assume_init() }; - - Self::from_local_time(st).expect("Current local time must exist") - } - fn from_utc_time(utc_time: SYSTEMTIME) -> Result { let local_time = utc_to_local_time(&utc_time)?; let utc_secs = system_time_as_unix_seconds(&utc_time)?;