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 
6 use core::fmt;
7 
8 use super::{FixedOffset, LocalResult, Offset, TimeZone};
9 use naive::{NaiveDate, NaiveDateTime};
10 #[cfg(all(
11     feature = "clock",
12     not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))
13 ))]
14 use std::time::{SystemTime, UNIX_EPOCH};
15 #[cfg(feature = "clock")]
16 use {Date, DateTime};
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)]
36 pub struct Utc;
37 
38 #[cfg(feature = "clock")]
39 impl Utc {
40     /// Returns a `Date` which corresponds to the current date.
today() -> Date<Utc>41     pub fn today() -> Date<Utc> {
42         Utc::now().date()
43     }
44 
45     /// Returns a `DateTime` which corresponds to the current date.
46     #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
now() -> DateTime<Utc>47     pub fn now() -> DateTime<Utc> {
48         let now =
49             SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
50         let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32);
51         DateTime::from_utc(naive, Utc)
52     }
53 
54     /// Returns a `DateTime` which corresponds to the current date.
55     #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
now() -> DateTime<Utc>56     pub fn now() -> DateTime<Utc> {
57         let now = js_sys::Date::new_0();
58         DateTime::<Utc>::from(now)
59     }
60 }
61 
62 impl TimeZone for Utc {
63     type Offset = Utc;
64 
from_offset(_state: &Utc) -> Utc65     fn from_offset(_state: &Utc) -> Utc {
66         Utc
67     }
68 
offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<Utc>69     fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<Utc> {
70         LocalResult::Single(Utc)
71     }
offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<Utc>72     fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<Utc> {
73         LocalResult::Single(Utc)
74     }
75 
offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc76     fn offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc {
77         Utc
78     }
offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc79     fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc {
80         Utc
81     }
82 }
83 
84 impl Offset for Utc {
fix(&self) -> FixedOffset85     fn fix(&self) -> FixedOffset {
86         FixedOffset::east(0)
87     }
88 }
89 
90 impl fmt::Debug for Utc {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result91     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92         write!(f, "Z")
93     }
94 }
95 
96 impl fmt::Display for Utc {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result97     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98         write!(f, "UTC")
99     }
100 }
101