chrono/src/offset/utc.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

// This is a part of Chrono.
// See README.md and LICENSE.txt for details.
`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
//! The UTC (Coordinated Universal Time) time zone.
use std::fmt;
use oldtime;
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, NaiveDateTime};
use {Date, DateTime};
use super::{TimeZone, Offset, LocalResult, FixedOffset};
/// The UTC time zone. This is the most efficient time zone when you don't need the local time.
/// It is also used as an offset (which is also a dummy type).
///
/// Using the [`TimeZone`](../../../chrono/offset/trait.TimeZone.html) methods
/// on the UTC struct is the preferred way to construct `DateTime<UTC>`
/// instances.
///
/// # Example
///
/// ~~~~
/// use chrono::{DateTime, TimeZone, NaiveDateTime, UTC};
///
/// let dt = DateTime::<UTC>::from_utc(NaiveDateTime::from_timestamp(61, 0), UTC);
///
/// assert_eq!(UTC.timestamp(61, 0), dt);
/// assert_eq!(UTC.ymd(1970, 1, 1).and_hms(0, 1, 1), dt);
/// ~~~~
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct UTC;
impl UTC {
/// Returns a `Date` which corresponds to the current date.
pub fn today() -> Date<UTC> { UTC::now().date() }
/// Returns a `DateTime` which corresponds to the current date.
pub fn now() -> DateTime<UTC> {
let spec = oldtime::get_time();
let naive = NaiveDateTime::from_timestamp(spec.sec, spec.nsec as u32);
DateTime::from_utc(naive, UTC)
}
}
impl TimeZone for UTC {
type Offset = UTC;
fn from_offset(_state: &UTC) -> UTC { UTC }
fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<UTC> {
LocalResult::Single(UTC)
}
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<UTC> {
LocalResult::Single(UTC)
}
fn offset_from_utc_date(&self, _utc: &NaiveDate) -> UTC { UTC }
fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> UTC { UTC}
}
impl Offset for UTC {
`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
fn fix(&self) -> FixedOffset { FixedOffset::east(0) }
}
impl fmt::Debug for UTC {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Z") }
}
impl fmt::Display for UTC {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UTC") }
}