chrono/offset/
utc.rs

1// This is a part of Chrono.
2// See README.md and LICENSE.txt for details.
3
4//! The UTC (Coordinated Universal Time) time zone.
5
6use core::fmt;
7#[cfg(all(
8    feature = "clock",
9    not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))
10))]
11use oldtime;
12
13use naive::{NaiveDate, NaiveDateTime};
14#[cfg(feature="clock")]
15use {Date, DateTime};
16use super::{TimeZone, Offset, LocalResult, FixedOffset};
17
18/// The UTC time zone. This is the most efficient time zone when you don't need the local time.
19/// It is also used as an offset (which is also a dummy type).
20///
21/// Using the [`TimeZone`](./trait.TimeZone.html) methods
22/// on the UTC struct is the preferred way to construct `DateTime<Utc>`
23/// instances.
24///
25/// # Example
26///
27/// ~~~~
28/// use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
29///
30/// let dt = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
31///
32/// assert_eq!(Utc.timestamp(61, 0), dt);
33/// assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 1, 1), dt);
34/// ~~~~
35#[derive(Copy, Clone, PartialEq, Eq)]
36pub struct Utc;
37
38#[cfg(feature="clock")]
39impl Utc {
40    /// Returns a `Date` which corresponds to the current date.
41    pub fn today() -> Date<Utc> { Utc::now().date() }
42
43    /// Returns a `DateTime` which corresponds to the current date.
44    #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
45    pub fn now() -> DateTime<Utc> {
46        let spec = oldtime::get_time();
47        let naive = NaiveDateTime::from_timestamp(spec.sec, spec.nsec as u32);
48        DateTime::from_utc(naive, Utc)
49    }
50
51    /// Returns a `DateTime` which corresponds to the current date.
52    #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
53    pub fn now() -> DateTime<Utc> {
54        let now = js_sys::Date::new_0();
55        let millisecs_since_unix_epoch: u64 = now.get_time() as u64;
56        let secs  = millisecs_since_unix_epoch / 1000;
57        let nanos = 1_000_000 * (millisecs_since_unix_epoch - 1000 * secs);
58        let naive = NaiveDateTime::from_timestamp(secs as i64, nanos as u32);
59        DateTime::from_utc(naive, Utc)
60    }
61}
62
63impl TimeZone for Utc {
64    type Offset = Utc;
65
66    fn from_offset(_state: &Utc) -> Utc { Utc }
67
68    fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<Utc> {
69        LocalResult::Single(Utc)
70    }
71    fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<Utc> {
72        LocalResult::Single(Utc)
73    }
74
75    fn offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc { Utc }
76    fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc { Utc }
77}
78
79impl Offset for Utc {
80    fn fix(&self) -> FixedOffset { FixedOffset::east(0) }
81}
82
83impl fmt::Debug for Utc {
84    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Z") }
85}
86
87impl fmt::Display for Utc {
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "UTC") }
89}