chrono/src/offset/mod.rs

378 lines
16 KiB
Rust
Raw Normal View History

// This is a part of Chrono.
2014-07-29 06:41:07 +00:00
// See README.md and LICENSE.txt for details.
Flattened intermediate implementation modules. There used to be multiple modules like `chrono::datetime` which only provide a single type `DateTime`. In retrospect, this module structure never reflected how people use those types; with the release of 0.3.0 `chrono::prelude` is a preferred way to glob-import types, and due to reexports `chrono::DateTime` and likes are also common enough. Therefore this commit removes those implementation modules and flattens the module structure. Specifically: Before After ---------------------------------- ---------------------------- chrono::date::Date chrono::Date chrono::date::MIN chrono::MIN_DATE chrono::date::MAX chrono::MAX_DATE chrono::datetime::DateTime chrono::DateTime chrono::datetime::TsSeconds chrono::TsSeconds chrono::datetime::serde::* chrono::serde::* chrono::naive::time::NaiveTime chrono::naive::NaiveTime chrono::naive::date::NaiveDate chrono::naive::NaiveDate chrono::naive::date::MIN chrono::naive::MIN_DATE chrono::naive::date::MAX chrono::naive::MAX_DATE chrono::naive::datetime::NaiveDateTime chrono::naive::NaiveDateTime chrono::naive::datetime::TsSeconds chrono::naive::TsSeconds chrono::naive::datetime::serde::* chrono::naive::serde::* chrono::offset::utc::UTC chrono::offset::UTC chrono::offset::fixed::FixedOffset chrono::offset::FixedOffset chrono::offset::local::Local chrono::offset::Local chrono::format::parsed::Parsed chrono::format::Parsed All internal documentation links have been updated (phew!) and verified with LinkChecker [1]. Probably we can automate this check in the future. [1] https://wummel.github.io/linkchecker/ Closes #161. Compared to the original proposal, `chrono::naive` is retained as we had `TsSeconds` types duplicated for `NaiveDateTime` and `DateTime` (legitimately).
2017-06-21 05:03:49 +00:00
//! The time zone, which calculates offsets from the local time to UTC.
//!
//! There are four operations provided by the `TimeZone` trait:
//!
//! 1. Converting the local `NaiveDateTime` to `DateTime<Tz>`
//! 2. Converting the UTC `NaiveDateTime` to `DateTime<Tz>`
//! 3. Converting `DateTime<Tz>` to the local `NaiveDateTime`
//! 4. Constructing `DateTime<Tz>` objects from various offsets
//!
//! 1 is used for constructors. 2 is used for the `with_timezone` method of date and time types.
//! 3 is used for other methods, e.g. `year()` or `format()`, and provided by an associated type
//! which implements `Offset` (which then passed to `TimeZone` for actual implementations).
//! Technically speaking `TimeZone` has a total knowledge about given timescale,
//! but `Offset` is used as a cache to avoid the repeated conversion
//! and provides implementations for 1 and 3.
//! An `TimeZone` instance can be reconstructed from the corresponding `Offset` instance.
2014-07-29 06:41:07 +00:00
use std::fmt;
use Weekday;
Flattened intermediate implementation modules. There used to be multiple modules like `chrono::datetime` which only provide a single type `DateTime`. In retrospect, this module structure never reflected how people use those types; with the release of 0.3.0 `chrono::prelude` is a preferred way to glob-import types, and due to reexports `chrono::DateTime` and likes are also common enough. Therefore this commit removes those implementation modules and flattens the module structure. Specifically: Before After ---------------------------------- ---------------------------- chrono::date::Date chrono::Date chrono::date::MIN chrono::MIN_DATE chrono::date::MAX chrono::MAX_DATE chrono::datetime::DateTime chrono::DateTime chrono::datetime::TsSeconds chrono::TsSeconds chrono::datetime::serde::* chrono::serde::* chrono::naive::time::NaiveTime chrono::naive::NaiveTime chrono::naive::date::NaiveDate chrono::naive::NaiveDate chrono::naive::date::MIN chrono::naive::MIN_DATE chrono::naive::date::MAX chrono::naive::MAX_DATE chrono::naive::datetime::NaiveDateTime chrono::naive::NaiveDateTime chrono::naive::datetime::TsSeconds chrono::naive::TsSeconds chrono::naive::datetime::serde::* chrono::naive::serde::* chrono::offset::utc::UTC chrono::offset::UTC chrono::offset::fixed::FixedOffset chrono::offset::FixedOffset chrono::offset::local::Local chrono::offset::Local chrono::format::parsed::Parsed chrono::format::Parsed All internal documentation links have been updated (phew!) and verified with LinkChecker [1]. Probably we can automate this check in the future. [1] https://wummel.github.io/linkchecker/ Closes #161. Compared to the original proposal, `chrono::naive` is retained as we had `TsSeconds` types duplicated for `NaiveDateTime` and `DateTime` (legitimately).
2017-06-21 05:03:49 +00:00
use naive::{NaiveDate, NaiveTime, NaiveDateTime};
use {Date, DateTime};
use format::{parse, Parsed, ParseResult, StrftimeItems};
2014-07-29 06:41:07 +00:00
/// The conversion result from the local time to the timezone-aware datetime types.
#[derive(Clone, PartialEq, Debug)]
2014-07-29 06:41:07 +00:00
pub enum LocalResult<T> {
/// Given local time representation is invalid.
/// This can occur when, for example, the positive timezone transition.
None,
2014-07-29 06:41:07 +00:00
/// Given local time representation has a single unique result.
Single(T),
/// Given local time representation has multiple results and thus ambiguous.
/// This can occur when, for example, the negative timezone transition.
Ambiguous(T /*min*/, T /*max*/),
}
impl<T> LocalResult<T> {
/// Returns `Some` only when the conversion result is unique, or `None` otherwise.
pub fn single(self) -> Option<T> {
match self { LocalResult::Single(t) => Some(t), _ => None }
2014-07-29 06:41:07 +00:00
}
/// Returns `Some` for the earliest possible conversion result, or `None` if none.
pub fn earliest(self) -> Option<T> {
match self { LocalResult::Single(t) | LocalResult::Ambiguous(t,_) => Some(t), _ => None }
2014-07-29 06:41:07 +00:00
}
/// Returns `Some` for the latest possible conversion result, or `None` if none.
pub fn latest(self) -> Option<T> {
match self { LocalResult::Single(t) | LocalResult::Ambiguous(_,t) => Some(t), _ => None }
2014-07-29 06:41:07 +00:00
}
/// Maps a `LocalResult<T>` into `LocalResult<U>` with given function.
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> LocalResult<U> {
match self {
LocalResult::None => LocalResult::None,
LocalResult::Single(v) => LocalResult::Single(f(v)),
LocalResult::Ambiguous(min, max) => LocalResult::Ambiguous(f(min), f(max)),
}
}
2014-07-29 06:41:07 +00:00
}
impl<Tz: TimeZone> LocalResult<Date<Tz>> {
/// Makes a new `DateTime` from the current date and given `NaiveTime`.
/// The offset in the current date is preserved.
///
/// Propagates any error. Ambiguous result would be discarded.
#[inline]
pub fn and_time(self, time: NaiveTime) -> LocalResult<DateTime<Tz>> {
match self {
LocalResult::Single(d) => d.and_time(time)
.map_or(LocalResult::None, LocalResult::Single),
_ => LocalResult::None,
}
}
/// Makes a new `DateTime` from the current date, hour, minute and second.
/// The offset in the current date is preserved.
///
/// Propagates any error. Ambiguous result would be discarded.
#[inline]
pub fn and_hms_opt(self, hour: u32, min: u32, sec: u32) -> LocalResult<DateTime<Tz>> {
match self {
LocalResult::Single(d) => d.and_hms_opt(hour, min, sec)
.map_or(LocalResult::None, LocalResult::Single),
_ => LocalResult::None,
}
}
/// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
/// The millisecond part can exceed 1,000 in order to represent the leap second.
/// The offset in the current date is preserved.
///
/// Propagates any error. Ambiguous result would be discarded.
#[inline]
pub fn and_hms_milli_opt(self, hour: u32, min: u32, sec: u32,
milli: u32) -> LocalResult<DateTime<Tz>> {
match self {
LocalResult::Single(d) => d.and_hms_milli_opt(hour, min, sec, milli)
.map_or(LocalResult::None, LocalResult::Single),
_ => LocalResult::None,
}
}
/// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
/// The microsecond part can exceed 1,000,000 in order to represent the leap second.
/// The offset in the current date is preserved.
///
/// Propagates any error. Ambiguous result would be discarded.
#[inline]
pub fn and_hms_micro_opt(self, hour: u32, min: u32, sec: u32,
micro: u32) -> LocalResult<DateTime<Tz>> {
match self {
LocalResult::Single(d) => d.and_hms_micro_opt(hour, min, sec, micro)
.map_or(LocalResult::None, LocalResult::Single),
_ => LocalResult::None,
}
}
/// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
/// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
/// The offset in the current date is preserved.
///
/// Propagates any error. Ambiguous result would be discarded.
#[inline]
pub fn and_hms_nano_opt(self, hour: u32, min: u32, sec: u32,
nano: u32) -> LocalResult<DateTime<Tz>> {
match self {
LocalResult::Single(d) => d.and_hms_nano_opt(hour, min, sec, nano)
.map_or(LocalResult::None, LocalResult::Single),
_ => LocalResult::None,
}
}
}
impl<T: fmt::Debug> LocalResult<T> {
/// Returns the single unique conversion result, or panics accordingly.
2014-07-29 06:41:07 +00:00
pub fn unwrap(self) -> T {
match self {
LocalResult::None => panic!("No such local time"),
LocalResult::Single(t) => t,
LocalResult::Ambiguous(t1,t2) => {
panic!("Ambiguous local time, ranging from {:?} to {:?}", t1, t2)
}
2014-07-29 06:41:07 +00:00
}
}
}
/// The offset from the local time to UTC.
pub trait Offset: Sized + Clone + fmt::Debug {
`FixedOffset` is now the official "fixed offset value" type. This may sound strange, but the final type for the offset "value" was originally `time::Duration` (returned by `Offset::local_minus_utc`). This caused a lot of problems becaus adding `Duration` fully interacts with leap seconds and `Duration` itself is somewhat deprecated. This commit entirely replaces this role of `Duration` with `FixedOffset`. So if we had `Offset` and `Duration` to represent the "storage" offset type and the offset "value" in the past, we now have `Offset` and `FixedOffset`. Storage-to-value conversion is called to "fix" the offset---an apt term for the type. The list of actual changes: - The time zone offset is now restricted to UTC-23:59:59 through UTC+23:59:59, and no subsecond value is allowed. As described above, `FixedOffset` is now fully used for this purpose. - One can now add and subtract `FixedOffset` to/from timelike values. Replaces a temporary `chrono::offset::add_with_leapsecond` function. Datelike & non-timelike values are never affected by the offset. - UTC and local views to `Date<Tz>` are now identical. We keep relevant methods for the consistency right now. - `chrono::format::format` now receives `FixedOffset` in place of `(Old)Duration`. - `Offset` now has a `fix` method to resolve, or to "fix" the "storage" offset (`Offset`) to the offset "value" (`FixedOffset`). - `FixedOffset::{local_minus_utc, utc_minus_local}` methods are added. They no longer depend on `Duration` as well.
2017-02-06 18:43:59 +00:00
/// Returns the fixed offset from UTC to the local time stored.
fn fix(&self) -> FixedOffset;
}
/// The time zone.
///
Flattened intermediate implementation modules. There used to be multiple modules like `chrono::datetime` which only provide a single type `DateTime`. In retrospect, this module structure never reflected how people use those types; with the release of 0.3.0 `chrono::prelude` is a preferred way to glob-import types, and due to reexports `chrono::DateTime` and likes are also common enough. Therefore this commit removes those implementation modules and flattens the module structure. Specifically: Before After ---------------------------------- ---------------------------- chrono::date::Date chrono::Date chrono::date::MIN chrono::MIN_DATE chrono::date::MAX chrono::MAX_DATE chrono::datetime::DateTime chrono::DateTime chrono::datetime::TsSeconds chrono::TsSeconds chrono::datetime::serde::* chrono::serde::* chrono::naive::time::NaiveTime chrono::naive::NaiveTime chrono::naive::date::NaiveDate chrono::naive::NaiveDate chrono::naive::date::MIN chrono::naive::MIN_DATE chrono::naive::date::MAX chrono::naive::MAX_DATE chrono::naive::datetime::NaiveDateTime chrono::naive::NaiveDateTime chrono::naive::datetime::TsSeconds chrono::naive::TsSeconds chrono::naive::datetime::serde::* chrono::naive::serde::* chrono::offset::utc::UTC chrono::offset::UTC chrono::offset::fixed::FixedOffset chrono::offset::FixedOffset chrono::offset::local::Local chrono::offset::Local chrono::format::parsed::Parsed chrono::format::Parsed All internal documentation links have been updated (phew!) and verified with LinkChecker [1]. Probably we can automate this check in the future. [1] https://wummel.github.io/linkchecker/ Closes #161. Compared to the original proposal, `chrono::naive` is retained as we had `TsSeconds` types duplicated for `NaiveDateTime` and `DateTime` (legitimately).
2017-06-21 05:03:49 +00:00
/// The methods here are the primarily constructors for [`Date`](../struct.Date.html) and
/// [`DateTime`](../struct.DateTime.html) types.
2015-04-04 11:49:00 +00:00
pub trait TimeZone: Sized + Clone {
/// An associated offset type.
/// This type is used to store the actual offset in date and time types.
/// The original `TimeZone` value can be recovered via `TimeZone::from_offset`.
type Offset: Offset;
/// Makes a new `Date` from year, month, day and the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Panics on the out-of-range date, invalid month and/or day.
///
/// # Example
///
/// ~~~~
/// use chrono::{UTC, TimeZone};
///
/// assert_eq!(UTC.ymd(2015, 5, 15).to_string(), "2015-05-15UTC");
/// ~~~~
2014-07-29 06:41:07 +00:00
fn ymd(&self, year: i32, month: u32, day: u32) -> Date<Self> {
self.ymd_opt(year, month, day).unwrap()
}
/// Makes a new `Date` from year, month, day and the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Returns `None` on the out-of-range date, invalid month and/or day.
///
/// # Example
///
/// ~~~~
/// use chrono::{UTC, LocalResult, TimeZone};
///
/// assert_eq!(UTC.ymd_opt(2015, 5, 15).unwrap().to_string(), "2015-05-15UTC");
/// assert_eq!(UTC.ymd_opt(2000, 0, 0), LocalResult::None);
/// ~~~~
2014-07-29 06:41:07 +00:00
fn ymd_opt(&self, year: i32, month: u32, day: u32) -> LocalResult<Date<Self>> {
match NaiveDate::from_ymd_opt(year, month, day) {
2014-07-29 06:41:07 +00:00
Some(d) => self.from_local_date(&d),
None => LocalResult::None,
2014-07-29 06:41:07 +00:00
}
}
/// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Panics on the out-of-range date and/or invalid DOY.
///
/// # Example
///
/// ~~~~
/// use chrono::{UTC, TimeZone};
///
/// assert_eq!(UTC.yo(2015, 135).to_string(), "2015-05-15UTC");
/// ~~~~
2014-07-29 06:41:07 +00:00
fn yo(&self, year: i32, ordinal: u32) -> Date<Self> {
self.yo_opt(year, ordinal).unwrap()
}
/// Makes a new `Date` from year, day of year (DOY or "ordinal") and the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Returns `None` on the out-of-range date and/or invalid DOY.
fn yo_opt(&self, year: i32, ordinal: u32) -> LocalResult<Date<Self>> {
match NaiveDate::from_yo_opt(year, ordinal) {
2014-07-29 06:41:07 +00:00
Some(d) => self.from_local_date(&d),
None => LocalResult::None,
2014-07-29 06:41:07 +00:00
}
}
/// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
/// the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
/// The resulting `Date` may have a different year from the input year.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Panics on the out-of-range date and/or invalid week number.
///
/// # Example
///
/// ~~~~
/// use chrono::{UTC, Weekday, TimeZone};
///
/// assert_eq!(UTC.isoywd(2015, 20, Weekday::Fri).to_string(), "2015-05-15UTC");
/// ~~~~
2014-07-29 06:41:07 +00:00
fn isoywd(&self, year: i32, week: u32, weekday: Weekday) -> Date<Self> {
self.isoywd_opt(year, week, weekday).unwrap()
}
/// Makes a new `Date` from ISO week date (year and week number), day of the week (DOW) and
/// the current time zone.
2014-07-29 06:41:07 +00:00
/// This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
/// The resulting `Date` may have a different year from the input year.
///
/// The time zone normally does not affect the date (unless it is between UTC-24 and UTC+24),
2014-07-29 06:41:07 +00:00
/// but it will propagate to the `DateTime` values constructed via this date.
///
/// Returns `None` on the out-of-range date and/or invalid week number.
fn isoywd_opt(&self, year: i32, week: u32, weekday: Weekday) -> LocalResult<Date<Self>> {
match NaiveDate::from_isoywd_opt(year, week, weekday) {
2014-07-29 06:41:07 +00:00
Some(d) => self.from_local_date(&d),
None => LocalResult::None,
2014-07-29 06:41:07 +00:00
}
}
/// Makes a new `DateTime` from the number of non-leap seconds
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
/// and the number of nanoseconds since the last whole non-leap second.
///
/// Panics on the out-of-range number of seconds and/or invalid nanosecond.
///
/// # Example
///
/// ~~~~
/// use chrono::{UTC, TimeZone};
///
/// assert_eq!(UTC.timestamp(1431648000, 0).to_string(), "2015-05-15 00:00:00 UTC");
/// ~~~~
fn timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self> {
self.timestamp_opt(secs, nsecs).unwrap()
}
/// Makes a new `DateTime` from the number of non-leap seconds
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
/// and the number of nanoseconds since the last whole non-leap second.
///
/// Returns `None` on the out-of-range number of seconds and/or invalid nanosecond.
fn timestamp_opt(&self, secs: i64, nsecs: u32) -> LocalResult<DateTime<Self>> {
match NaiveDateTime::from_timestamp_opt(secs, nsecs) {
Some(dt) => LocalResult::Single(self.from_utc_datetime(&dt)),
None => LocalResult::None,
}
}
/// Parses a string with the specified format string and
/// returns a `DateTime` with the current offset.
/// See the [`format::strftime` module](../format/strftime/index.html)
/// on the supported escape sequences.
///
/// If the format does not include offsets, the current offset is assumed;
/// otherwise the input should have a matching UTC offset.
///
/// See also `DateTime::parse_from_str` which gives a local `DateTime`
/// with parsed `FixedOffset`.
fn datetime_from_str(&self, s: &str, fmt: &str) -> ParseResult<DateTime<Self>> {
let mut parsed = Parsed::new();
try!(parse(&mut parsed, s, StrftimeItems::new(fmt)));
2015-02-18 16:48:29 +00:00
parsed.to_datetime_with_timezone(self)
2014-07-29 06:41:07 +00:00
}
/// Reconstructs the time zone from the offset.
fn from_offset(offset: &Self::Offset) -> Self;
/// Creates the offset(s) for given local `NaiveDate` if possible.
fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<Self::Offset>;
2014-07-29 06:41:07 +00:00
/// Creates the offset(s) for given local `NaiveDateTime` if possible.
fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<Self::Offset>;
2014-07-29 06:41:07 +00:00
/// Converts the local `NaiveDate` to the timezone-aware `Date` if possible.
fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Self>> {
self.offset_from_local_date(local).map(|offset| {
`FixedOffset` is now the official "fixed offset value" type. This may sound strange, but the final type for the offset "value" was originally `time::Duration` (returned by `Offset::local_minus_utc`). This caused a lot of problems becaus adding `Duration` fully interacts with leap seconds and `Duration` itself is somewhat deprecated. This commit entirely replaces this role of `Duration` with `FixedOffset`. So if we had `Offset` and `Duration` to represent the "storage" offset type and the offset "value" in the past, we now have `Offset` and `FixedOffset`. Storage-to-value conversion is called to "fix" the offset---an apt term for the type. The list of actual changes: - The time zone offset is now restricted to UTC-23:59:59 through UTC+23:59:59, and no subsecond value is allowed. As described above, `FixedOffset` is now fully used for this purpose. - One can now add and subtract `FixedOffset` to/from timelike values. Replaces a temporary `chrono::offset::add_with_leapsecond` function. Datelike & non-timelike values are never affected by the offset. - UTC and local views to `Date<Tz>` are now identical. We keep relevant methods for the consistency right now. - `chrono::format::format` now receives `FixedOffset` in place of `(Old)Duration`. - `Offset` now has a `fix` method to resolve, or to "fix" the "storage" offset (`Offset`) to the offset "value" (`FixedOffset`). - `FixedOffset::{local_minus_utc, utc_minus_local}` methods are added. They no longer depend on `Duration` as well.
2017-02-06 18:43:59 +00:00
// since FixedOffset is within +/- 1 day, the date is never affected
Date::from_utc(*local, offset)
})
2014-07-31 02:02:20 +00:00
}
/// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible.
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Self>> {
self.offset_from_local_datetime(local).map(|offset| {
`FixedOffset` is now the official "fixed offset value" type. This may sound strange, but the final type for the offset "value" was originally `time::Duration` (returned by `Offset::local_minus_utc`). This caused a lot of problems becaus adding `Duration` fully interacts with leap seconds and `Duration` itself is somewhat deprecated. This commit entirely replaces this role of `Duration` with `FixedOffset`. So if we had `Offset` and `Duration` to represent the "storage" offset type and the offset "value" in the past, we now have `Offset` and `FixedOffset`. Storage-to-value conversion is called to "fix" the offset---an apt term for the type. The list of actual changes: - The time zone offset is now restricted to UTC-23:59:59 through UTC+23:59:59, and no subsecond value is allowed. As described above, `FixedOffset` is now fully used for this purpose. - One can now add and subtract `FixedOffset` to/from timelike values. Replaces a temporary `chrono::offset::add_with_leapsecond` function. Datelike & non-timelike values are never affected by the offset. - UTC and local views to `Date<Tz>` are now identical. We keep relevant methods for the consistency right now. - `chrono::format::format` now receives `FixedOffset` in place of `(Old)Duration`. - `Offset` now has a `fix` method to resolve, or to "fix" the "storage" offset (`Offset`) to the offset "value" (`FixedOffset`). - `FixedOffset::{local_minus_utc, utc_minus_local}` methods are added. They no longer depend on `Duration` as well.
2017-02-06 18:43:59 +00:00
DateTime::from_utc(*local - offset.fix(), offset)
})
2014-07-31 02:02:20 +00:00
}
/// Creates the offset for given UTC `NaiveDate`. This cannot fail.
fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset;
2014-07-31 02:02:20 +00:00
/// Creates the offset for given UTC `NaiveDateTime`. This cannot fail.
fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset;
2014-07-31 02:02:20 +00:00
/// Converts the UTC `NaiveDate` to the local time.
/// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
fn from_utc_date(&self, utc: &NaiveDate) -> Date<Self> {
Date::from_utc(*utc, self.offset_from_utc_date(utc))
2014-07-31 02:02:20 +00:00
}
/// Converts the UTC `NaiveDateTime` to the local time.
/// The UTC is continuous and thus this cannot fail (but can give the duplicate local time).
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Self> {
DateTime::from_utc(*utc, self.offset_from_utc_datetime(utc))
2014-07-31 02:02:20 +00:00
}
}
Flattened intermediate implementation modules. There used to be multiple modules like `chrono::datetime` which only provide a single type `DateTime`. In retrospect, this module structure never reflected how people use those types; with the release of 0.3.0 `chrono::prelude` is a preferred way to glob-import types, and due to reexports `chrono::DateTime` and likes are also common enough. Therefore this commit removes those implementation modules and flattens the module structure. Specifically: Before After ---------------------------------- ---------------------------- chrono::date::Date chrono::Date chrono::date::MIN chrono::MIN_DATE chrono::date::MAX chrono::MAX_DATE chrono::datetime::DateTime chrono::DateTime chrono::datetime::TsSeconds chrono::TsSeconds chrono::datetime::serde::* chrono::serde::* chrono::naive::time::NaiveTime chrono::naive::NaiveTime chrono::naive::date::NaiveDate chrono::naive::NaiveDate chrono::naive::date::MIN chrono::naive::MIN_DATE chrono::naive::date::MAX chrono::naive::MAX_DATE chrono::naive::datetime::NaiveDateTime chrono::naive::NaiveDateTime chrono::naive::datetime::TsSeconds chrono::naive::TsSeconds chrono::naive::datetime::serde::* chrono::naive::serde::* chrono::offset::utc::UTC chrono::offset::UTC chrono::offset::fixed::FixedOffset chrono::offset::FixedOffset chrono::offset::local::Local chrono::offset::Local chrono::format::parsed::Parsed chrono::format::Parsed All internal documentation links have been updated (phew!) and verified with LinkChecker [1]. Probably we can automate this check in the future. [1] https://wummel.github.io/linkchecker/ Closes #161. Compared to the original proposal, `chrono::naive` is retained as we had `TsSeconds` types duplicated for `NaiveDateTime` and `DateTime` (legitimately).
2017-06-21 05:03:49 +00:00
mod utc;
mod fixed;
mod local;
pub use self::utc::UTC;
pub use self::fixed::FixedOffset;
pub use self::local::Local;