mirror of
https://github.com/chronotope/chrono.git
synced 2025-09-30 14:32:19 +00:00
Just rely on the standard library for Local::now
This commit is contained in:
parent
bdf8b19515
commit
7fb3e5921c
@ -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<Local> {
|
||||
inner::now()
|
||||
Utc::now().with_timezone(&Local)
|
||||
}
|
||||
|
||||
/// Returns a `DateTime` which corresponds to the current date and time.
|
||||
|
@ -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",
|
||||
@ -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 {
|
||||
|
@ -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| {
|
||||
|
@ -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<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);
|
||||
@ -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)?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user