// This is a part of Chrono. // See README.md and LICENSE.txt for details.
//! The time zone which has a fixed offset from UTC.
use core::fmt; use core::ops::{Add, Sub}; use oldtime::Duration as OldDuration;
usesuper::{LocalResult, Offset, TimeZone}; use div::div_mod_floor; use naive::{NaiveDate, NaiveDateTime, NaiveTime}; use DateTime; use Timelike;
/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// /// Using the [`TimeZone`](./trait.TimeZone.html) methods /// on a `FixedOffset` struct is the preferred way to construct /// `DateTime<FixedOffset>` instances. See the [`east`](#method.east) and /// [`west`](#method.west) methods for examples. #[derive(PartialEq, Eq, Hash, Copy, Clone)] pubstruct FixedOffset {
local_minus_utc: i32,
}
impl FixedOffset { /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference. /// The negative `secs` means the Western Hemisphere. /// /// Panics on the out-of-bound `secs`. /// /// # Example /// /// ~~~~ /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; /// let datetime = FixedOffset::east(5 * hour).ymd(2016, 11, 08) /// .and_hms(0, 0, 0); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ~~~~ pubfn east(secs: i32) -> FixedOffset {
FixedOffset::east_opt(secs).expect("FixedOffset::east out of bounds")
}
/// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference. /// The negative `secs` means the Western Hemisphere. /// /// Returns `None` on the out-of-bound `secs`. pubfn east_opt(secs: i32) -> Option<FixedOffset> { if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: secs })
} else {
None
}
}
/// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference. /// The negative `secs` means the Eastern Hemisphere. /// /// Panics on the out-of-bound `secs`. /// /// # Example /// /// ~~~~ /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; /// let datetime = FixedOffset::west(5 * hour).ymd(2016, 11, 08) /// .and_hms(0, 0, 0); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ~~~~ pubfn west(secs: i32) -> FixedOffset {
FixedOffset::west_opt(secs).expect("FixedOffset::west out of bounds")
}
/// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference. /// The negative `secs` means the Eastern Hemisphere. /// /// Returns `None` on the out-of-bound `secs`. pubfn west_opt(secs: i32) -> Option<FixedOffset> { if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: -secs })
} else {
None
}
}
/// Returns the number of seconds to add to convert from UTC to the local time. #[inline] pubfn local_minus_utc(&self) -> i32 { self.local_minus_utc
}
/// Returns the number of seconds to add to convert from the local time to UTC. #[inline] pubfn utc_minus_local(&self) -> i32 {
-self.local_minus_utc
}
}
impl TimeZone for FixedOffset { type Offset = FixedOffset;
// addition or subtraction of FixedOffset to/from Timelike values is the same as // adding or subtracting the offset's local_minus_utc value // but keep keeps the leap second information. // this should be implemented more efficiently, but for the time being, this is generic right now.
fn add_with_leapsecond<T>(lhs: &T, rhs: i32) -> T where
T: Timelike + Add<OldDuration, Output = T>,
{ // extract and temporarily remove the fractional part and later recover it let nanos = lhs.nanosecond(); let lhs = lhs.with_nanosecond(0).unwrap();
(lhs + OldDuration::seconds(i64::from(rhs))).with_nanosecond(nanos).unwrap()
}
impl Add<FixedOffset> for NaiveTime { type Output = NaiveTime;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.