1use 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#[derive(Copy, Clone, PartialEq, Eq)]
36pub struct Utc;
37
38#[cfg(feature="clock")]
39impl Utc {
40 pub fn today() -> Date<Utc> { Utc::now().date() }
42
43 #[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 #[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}