2017-02-05 21:15:57 +00:00
|
|
|
// This is a part of Chrono.
|
2015-01-12 16:33:53 +00:00
|
|
|
// See README.md and LICENSE.txt for details.
|
|
|
|
|
2017-02-06 18:43:59 +00:00
|
|
|
//! The UTC (Coordinated Universal Time) time zone.
|
2015-01-12 16:33:53 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
2017-02-05 23:17:01 +00:00
|
|
|
use oldtime;
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2017-06-21 05:03:49 +00:00
|
|
|
use naive::{NaiveDate, NaiveDateTime};
|
|
|
|
use {Date, DateTime};
|
|
|
|
use super::{TimeZone, Offset, LocalResult, FixedOffset};
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
/// 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).
|
2016-11-12 21:09:08 +00:00
|
|
|
///
|
2017-06-21 12:58:42 +00:00
|
|
|
/// Using the [`TimeZone`](./trait.TimeZone.html) methods
|
2017-06-21 15:33:17 +00:00
|
|
|
/// on the UTC struct is the preferred way to construct `DateTime<Utc>`
|
2016-11-12 21:09:08 +00:00
|
|
|
/// instances.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~~
|
2017-06-21 15:33:17 +00:00
|
|
|
/// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
|
2016-11-12 21:09:08 +00:00
|
|
|
///
|
2017-06-21 15:33:17 +00:00
|
|
|
/// let dt = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
|
2016-11-12 21:09:08 +00:00
|
|
|
///
|
2017-06-21 15:33:17 +00:00
|
|
|
/// assert_eq!(Utc.timestamp(61, 0), dt);
|
|
|
|
/// assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 1, 1), dt);
|
2016-11-12 21:09:08 +00:00
|
|
|
/// ~~~~
|
2015-01-12 16:33:53 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2017-06-21 15:33:17 +00:00
|
|
|
pub struct Utc;
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
impl Utc {
|
2015-01-12 16:33:53 +00:00
|
|
|
/// Returns a `Date` which corresponds to the current date.
|
2017-06-21 15:33:17 +00:00
|
|
|
pub fn today() -> Date<Utc> { Utc::now().date() }
|
2015-01-12 16:33:53 +00:00
|
|
|
|
|
|
|
/// Returns a `DateTime` which corresponds to the current date.
|
2017-06-21 15:33:17 +00:00
|
|
|
pub fn now() -> DateTime<Utc> {
|
2017-02-05 23:17:01 +00:00
|
|
|
let spec = oldtime::get_time();
|
2015-02-18 17:45:29 +00:00
|
|
|
let naive = NaiveDateTime::from_timestamp(spec.sec, spec.nsec as u32);
|
2017-06-21 15:33:17 +00:00
|
|
|
DateTime::from_utc(naive, Utc)
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
impl TimeZone for Utc {
|
|
|
|
type Offset = Utc;
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
fn from_offset(_state: &Utc) -> Utc { Utc }
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<Utc> {
|
|
|
|
LocalResult::Single(Utc)
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
2017-06-21 15:33:17 +00:00
|
|
|
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<Utc> {
|
|
|
|
LocalResult::Single(Utc)
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
fn offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc { Utc }
|
|
|
|
fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc { Utc }
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
impl Offset for Utc {
|
2017-02-06 18:43:59 +00:00
|
|
|
fn fix(&self) -> FixedOffset { FixedOffset::east(0) }
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
impl fmt::Debug for Utc {
|
2015-01-12 16:33:53 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Z") }
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:33:17 +00:00
|
|
|
impl fmt::Display for Utc {
|
2015-01-12 16:33:53 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UTC") }
|
|
|
|
}
|
|
|
|
|