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.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* The UTC (Coordinated Universal Time) time zone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use std::fmt;
|
2017-02-05 23:17:01 +00:00
|
|
|
use oldtime;
|
|
|
|
use oldtime::Duration as OldDuration;
|
2015-01-12 16:33:53 +00:00
|
|
|
|
|
|
|
use naive::date::NaiveDate;
|
|
|
|
use naive::datetime::NaiveDateTime;
|
|
|
|
use date::Date;
|
|
|
|
use datetime::DateTime;
|
2015-01-12 17:03:12 +00:00
|
|
|
use super::{TimeZone, Offset, LocalResult};
|
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
|
|
|
///
|
|
|
|
/// 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);
|
|
|
|
/// ~~~~
|
2015-01-12 16:33:53 +00:00
|
|
|
#[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> {
|
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);
|
2015-01-12 16:33:53 +00:00
|
|
|
DateTime::from_utc(naive, UTC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl TimeZone for UTC {
|
|
|
|
type Offset = UTC;
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
fn from_offset(_state: &UTC) -> UTC { UTC }
|
2015-01-12 16:33:53 +00:00
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<UTC> {
|
2015-01-12 16:33:53 +00:00
|
|
|
LocalResult::Single(UTC)
|
|
|
|
}
|
2015-01-12 17:03:12 +00:00
|
|
|
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<UTC> {
|
2015-01-12 16:33:53 +00:00
|
|
|
LocalResult::Single(UTC)
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +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
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl Offset for UTC {
|
2017-02-05 23:17:01 +00:00
|
|
|
fn local_minus_utc(&self) -> OldDuration { OldDuration::zero() }
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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") }
|
|
|
|
}
|
|
|
|
|