mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 23:36:17 +00:00
Fold top-level sys module into offset::local
This commit is contained in:
parent
5f59b2c24f
commit
b36663b03b
@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
//! The local (system) time zone.
|
//! The local (system) time zone.
|
||||||
|
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
#[cfg(feature = "rkyv")]
|
#[cfg(feature = "rkyv")]
|
||||||
use rkyv::{Archive, Deserialize, Serialize};
|
use rkyv::{Archive, Deserialize, Serialize};
|
||||||
|
|
||||||
use super::fixed::FixedOffset;
|
use super::fixed::FixedOffset;
|
||||||
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
|
||||||
use super::sys::{self, Timespec};
|
|
||||||
use super::{LocalResult, TimeZone};
|
use super::{LocalResult, TimeZone};
|
||||||
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
||||||
use crate::naive::NaiveTime;
|
use crate::naive::NaiveTime;
|
||||||
@ -17,23 +17,37 @@ use crate::{Date, DateTime};
|
|||||||
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
||||||
use crate::{Datelike, Timelike};
|
use crate::{Datelike, Timelike};
|
||||||
|
|
||||||
|
#[cfg(all(not(unix), not(windows)))]
|
||||||
|
#[path = "sys/stub.rs"]
|
||||||
|
mod inner;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[path = "sys/unix.rs"]
|
||||||
|
mod inner;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
#[path = "sys/windows.rs"]
|
||||||
|
mod inner;
|
||||||
|
|
||||||
|
use inner::{local_tm_to_time, time_to_local_tm, utc_tm_to_time};
|
||||||
|
|
||||||
/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
|
/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
|
||||||
/// This assumes that `time` is working correctly, i.e. any error is fatal.
|
/// This assumes that `time` is working correctly, i.e. any error is fatal.
|
||||||
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
||||||
fn tm_to_datetime(mut tm: sys::Tm) -> DateTime<Local> {
|
fn tm_to_datetime(mut tm: Tm) -> DateTime<Local> {
|
||||||
if tm.tm_sec >= 60 {
|
if tm.tm_sec >= 60 {
|
||||||
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
|
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
|
||||||
tm.tm_sec = 59;
|
tm.tm_sec = 59;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
fn tm_to_naive_date(tm: &sys::Tm) -> NaiveDate {
|
fn tm_to_naive_date(tm: &Tm) -> NaiveDate {
|
||||||
// from_yo is more efficient than from_ymd (since it's the internal representation).
|
// from_yo is more efficient than from_ymd (since it's the internal representation).
|
||||||
NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1)
|
NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn tm_to_naive_date(tm: &sys::Tm) -> NaiveDate {
|
fn tm_to_naive_date(tm: &Tm) -> NaiveDate {
|
||||||
// ...but tm_yday is broken in Windows (issue #85)
|
// ...but tm_yday is broken in Windows (issue #85)
|
||||||
NaiveDate::from_ymd(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32)
|
NaiveDate::from_ymd(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32)
|
||||||
}
|
}
|
||||||
@ -51,13 +65,13 @@ fn tm_to_datetime(mut tm: sys::Tm) -> DateTime<Local> {
|
|||||||
|
|
||||||
/// Converts a local `NaiveDateTime` to the `time::Timespec`.
|
/// Converts a local `NaiveDateTime` to the `time::Timespec`.
|
||||||
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
|
||||||
fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> sys::Timespec {
|
fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> Timespec {
|
||||||
// well, this exploits an undocumented `Tm::to_timespec` behavior
|
// well, this exploits an undocumented `Tm::to_timespec` behavior
|
||||||
// to get the exact function we want (either `timegm` or `mktime`).
|
// to get the exact function we want (either `timegm` or `mktime`).
|
||||||
// the number 1 is arbitrary but should be non-zero to trigger `mktime`.
|
// the number 1 is arbitrary but should be non-zero to trigger `mktime`.
|
||||||
let tm_utcoff = if local { 1 } else { 0 };
|
let tm_utcoff = if local { 1 } else { 0 };
|
||||||
|
|
||||||
let tm = sys::Tm {
|
let tm = Tm {
|
||||||
tm_sec: d.second() as i32,
|
tm_sec: d.second() as i32,
|
||||||
tm_min: d.minute() as i32,
|
tm_min: d.minute() as i32,
|
||||||
tm_hour: d.hour() as i32,
|
tm_hour: d.hour() as i32,
|
||||||
@ -196,6 +210,103 @@ impl TimeZone for Local {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A record specifying a time value in seconds and nanoseconds, where
|
||||||
|
/// nanoseconds represent the offset from the given second.
|
||||||
|
///
|
||||||
|
/// For example a timespec of 1.2 seconds after the beginning of the epoch would
|
||||||
|
/// be represented as {sec: 1, nsec: 200000000}.
|
||||||
|
pub(super) struct Timespec {
|
||||||
|
sec: i64,
|
||||||
|
nsec: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Timespec {
|
||||||
|
/// Constructs a timespec representing the current time in UTC.
|
||||||
|
pub(super) 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.
|
||||||
|
pub(super) fn local(self) -> Tm {
|
||||||
|
let mut tm = Tm {
|
||||||
|
tm_sec: 0,
|
||||||
|
tm_min: 0,
|
||||||
|
tm_hour: 0,
|
||||||
|
tm_mday: 0,
|
||||||
|
tm_mon: 0,
|
||||||
|
tm_year: 0,
|
||||||
|
tm_wday: 0,
|
||||||
|
tm_yday: 0,
|
||||||
|
tm_isdst: 0,
|
||||||
|
tm_utcoff: 0,
|
||||||
|
tm_nsec: 0,
|
||||||
|
};
|
||||||
|
time_to_local_tm(self.sec, &mut tm);
|
||||||
|
tm.tm_nsec = self.nsec;
|
||||||
|
tm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Holds a calendar date and time broken down into its components (year, month,
|
||||||
|
/// day, and so on), also called a broken-down time value.
|
||||||
|
// FIXME: use c_int instead of i32?
|
||||||
|
#[cfg(feature = "clock")]
|
||||||
|
#[repr(C)]
|
||||||
|
pub(crate) struct Tm {
|
||||||
|
/// Seconds after the minute - [0, 60]
|
||||||
|
pub(crate) tm_sec: i32,
|
||||||
|
|
||||||
|
/// Minutes after the hour - [0, 59]
|
||||||
|
pub(crate) tm_min: i32,
|
||||||
|
|
||||||
|
/// Hours after midnight - [0, 23]
|
||||||
|
pub(crate) tm_hour: i32,
|
||||||
|
|
||||||
|
/// Day of the month - [1, 31]
|
||||||
|
pub(crate) tm_mday: i32,
|
||||||
|
|
||||||
|
/// Months since January - [0, 11]
|
||||||
|
pub(crate) tm_mon: i32,
|
||||||
|
|
||||||
|
/// Years since 1900
|
||||||
|
pub(crate) tm_year: i32,
|
||||||
|
|
||||||
|
/// Days since Sunday - [0, 6]. 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
|
||||||
|
pub(crate) tm_wday: i32,
|
||||||
|
|
||||||
|
/// Days since January 1 - [0, 365]
|
||||||
|
pub(crate) tm_yday: i32,
|
||||||
|
|
||||||
|
/// Daylight Saving Time flag.
|
||||||
|
///
|
||||||
|
/// This value is positive if Daylight Saving Time is in effect, zero if
|
||||||
|
/// Daylight Saving Time is not in effect, and negative if this information
|
||||||
|
/// is not available.
|
||||||
|
pub(crate) tm_isdst: i32,
|
||||||
|
|
||||||
|
/// Identifies the time zone that was used to compute this broken-down time
|
||||||
|
/// value, including any adjustment for Daylight Saving Time. This is the
|
||||||
|
/// number of seconds east of UTC. For example, for U.S. Pacific Daylight
|
||||||
|
/// Time, the value is `-7*60*60 = -25200`.
|
||||||
|
pub(crate) tm_utcoff: i32,
|
||||||
|
|
||||||
|
/// Nanoseconds after the second - [0, 10<sup>9</sup> - 1]
|
||||||
|
pub(crate) tm_nsec: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tm {
|
||||||
|
/// Convert time to the seconds from January 1, 1970
|
||||||
|
pub(super) fn to_timespec(&self) -> Timespec {
|
||||||
|
let sec = match self.tm_utcoff {
|
||||||
|
0 => utc_tm_to_time(self),
|
||||||
|
_ => local_tm_to_time(self),
|
||||||
|
};
|
||||||
|
Timespec { sec, nsec: self.tm_nsec }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Local;
|
use super::Local;
|
||||||
|
@ -25,11 +25,16 @@ use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
|
|||||||
use crate::Weekday;
|
use crate::Weekday;
|
||||||
use crate::{Date, DateTime};
|
use crate::{Date, DateTime};
|
||||||
|
|
||||||
#[cfg(all(
|
mod fixed;
|
||||||
feature = "clock",
|
pub use self::fixed::FixedOffset;
|
||||||
not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))
|
|
||||||
))]
|
#[cfg(feature = "clock")]
|
||||||
mod sys;
|
mod local;
|
||||||
|
#[cfg(feature = "clock")]
|
||||||
|
pub use self::local::Local;
|
||||||
|
|
||||||
|
mod utc;
|
||||||
|
pub use self::utc::Utc;
|
||||||
|
|
||||||
/// The conversion result from the local time to the timezone-aware datetime types.
|
/// The conversion result from the local time to the timezone-aware datetime types.
|
||||||
#[derive(Clone, PartialEq, Debug, Copy, Eq, Hash)]
|
#[derive(Clone, PartialEq, Debug, Copy, Eq, Hash)]
|
||||||
@ -472,16 +477,6 @@ pub trait TimeZone: Sized + Clone {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod fixed;
|
|
||||||
#[cfg(feature = "clock")]
|
|
||||||
mod local;
|
|
||||||
mod utc;
|
|
||||||
|
|
||||||
pub use self::fixed::FixedOffset;
|
|
||||||
#[cfg(feature = "clock")]
|
|
||||||
pub use self::local::Local;
|
|
||||||
pub use self::utc::Utc;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -1,128 +0,0 @@
|
|||||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
//! Platform wrappers for converting UTC times to and from the local time zone.
|
|
||||||
//!
|
|
||||||
//! This code was rescued from v0.1 of the time crate, which is no longer
|
|
||||||
//! maintained. It has been substantially stripped down to the bare minimum
|
|
||||||
//! required by chrono.
|
|
||||||
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
#[cfg(all(not(unix), not(windows)))]
|
|
||||||
#[path = "stub.rs"]
|
|
||||||
mod inner;
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[path = "unix.rs"]
|
|
||||||
mod inner;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[path = "windows.rs"]
|
|
||||||
mod inner;
|
|
||||||
|
|
||||||
use inner::{local_tm_to_time, time_to_local_tm, utc_tm_to_time};
|
|
||||||
|
|
||||||
/// A record specifying a time value in seconds and nanoseconds, where
|
|
||||||
/// nanoseconds represent the offset from the given second.
|
|
||||||
///
|
|
||||||
/// For example a timespec of 1.2 seconds after the beginning of the epoch would
|
|
||||||
/// be represented as {sec: 1, nsec: 200000000}.
|
|
||||||
pub(super) struct Timespec {
|
|
||||||
sec: i64,
|
|
||||||
nsec: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Timespec {
|
|
||||||
/// Constructs a timespec representing the current time in UTC.
|
|
||||||
pub(super) 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.
|
|
||||||
pub(super) fn local(self) -> Tm {
|
|
||||||
let mut tm = Tm {
|
|
||||||
tm_sec: 0,
|
|
||||||
tm_min: 0,
|
|
||||||
tm_hour: 0,
|
|
||||||
tm_mday: 0,
|
|
||||||
tm_mon: 0,
|
|
||||||
tm_year: 0,
|
|
||||||
tm_wday: 0,
|
|
||||||
tm_yday: 0,
|
|
||||||
tm_isdst: 0,
|
|
||||||
tm_utcoff: 0,
|
|
||||||
tm_nsec: 0,
|
|
||||||
};
|
|
||||||
time_to_local_tm(self.sec, &mut tm);
|
|
||||||
tm.tm_nsec = self.nsec;
|
|
||||||
tm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Holds a calendar date and time broken down into its components (year, month,
|
|
||||||
/// day, and so on), also called a broken-down time value.
|
|
||||||
// FIXME: use c_int instead of i32?
|
|
||||||
#[cfg(feature = "clock")]
|
|
||||||
#[repr(C)]
|
|
||||||
pub(crate) struct Tm {
|
|
||||||
/// Seconds after the minute - [0, 60]
|
|
||||||
pub(crate) tm_sec: i32,
|
|
||||||
|
|
||||||
/// Minutes after the hour - [0, 59]
|
|
||||||
pub(crate) tm_min: i32,
|
|
||||||
|
|
||||||
/// Hours after midnight - [0, 23]
|
|
||||||
pub(crate) tm_hour: i32,
|
|
||||||
|
|
||||||
/// Day of the month - [1, 31]
|
|
||||||
pub(crate) tm_mday: i32,
|
|
||||||
|
|
||||||
/// Months since January - [0, 11]
|
|
||||||
pub(crate) tm_mon: i32,
|
|
||||||
|
|
||||||
/// Years since 1900
|
|
||||||
pub(crate) tm_year: i32,
|
|
||||||
|
|
||||||
/// Days since Sunday - [0, 6]. 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
|
|
||||||
pub(crate) tm_wday: i32,
|
|
||||||
|
|
||||||
/// Days since January 1 - [0, 365]
|
|
||||||
pub(crate) tm_yday: i32,
|
|
||||||
|
|
||||||
/// Daylight Saving Time flag.
|
|
||||||
///
|
|
||||||
/// This value is positive if Daylight Saving Time is in effect, zero if
|
|
||||||
/// Daylight Saving Time is not in effect, and negative if this information
|
|
||||||
/// is not available.
|
|
||||||
pub(crate) tm_isdst: i32,
|
|
||||||
|
|
||||||
/// Identifies the time zone that was used to compute this broken-down time
|
|
||||||
/// value, including any adjustment for Daylight Saving Time. This is the
|
|
||||||
/// number of seconds east of UTC. For example, for U.S. Pacific Daylight
|
|
||||||
/// Time, the value is `-7*60*60 = -25200`.
|
|
||||||
pub(crate) tm_utcoff: i32,
|
|
||||||
|
|
||||||
/// Nanoseconds after the second - [0, 10<sup>9</sup> - 1]
|
|
||||||
pub(crate) tm_nsec: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tm {
|
|
||||||
/// Convert time to the seconds from January 1, 1970
|
|
||||||
pub(super) fn to_timespec(&self) -> Timespec {
|
|
||||||
let sec = match self.tm_utcoff {
|
|
||||||
0 => utc_tm_to_time(self),
|
|
||||||
_ => local_tm_to_time(self),
|
|
||||||
};
|
|
||||||
Timespec { sec, nsec: self.tm_nsec }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user