Skip to content

Commit

Permalink
Just rely on the standard library for Local::now
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker authored and djc committed May 26, 2023
1 parent bdf8b19 commit 7fb3e59
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,7 +75,7 @@ impl Local {
)))]
#[must_use]
pub fn now() -> DateTime<Local> {
inner::now()
Utc::now().with_timezone(&Local)
}

/// Returns a `DateTime` which corresponds to the current date and time.
Expand Down
11 changes: 0 additions & 11 deletions src/offset/local/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Local> {
tm_to_datetime(Timespec::now().local())
}

/// Converts a local `NaiveDateTime` to the `time::Timespec`.
#[cfg(not(all(
target_arch = "wasm32",
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions src/offset/local/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Local> {
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<DateTime<Local>> {
TZ_INFO.with(|maybe_cache| {
Expand Down
15 changes: 0 additions & 15 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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<Local> {
LocalSysTime::local().datetime()
}

/// Converts a local `NaiveDateTime` to the `time::Timespec`.
pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<DateTime<Local>> {
let naive_sys_time = system_time_from_naive_date_time(d);
Expand All @@ -65,16 +60,6 @@ struct LocalSysTime {
}

impl LocalSysTime {
fn local() -> Self {
let mut now = MaybeUninit::<SYSTEMTIME>::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<Self, Error> {
let local_time = utc_to_local_time(&utc_time)?;
let utc_secs = system_time_as_unix_seconds(&utc_time)?;
Expand Down

0 comments on commit 7fb3e59

Please sign in to comment.