1use core::fmt;
7#[cfg(all(
8 feature = "now",
9 not(all(
10 target_arch = "wasm32",
11 feature = "wasmbind",
12 not(any(target_os = "emscripten", target_os = "wasi"))
13 ))
14))]
15use std::time::{SystemTime, UNIX_EPOCH};
16
17#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
18use rkyv::{Archive, Deserialize, Serialize};
19
20use super::{FixedOffset, MappedLocalTime, Offset, TimeZone};
21use crate::naive::{NaiveDate, NaiveDateTime};
22#[cfg(feature = "now")]
23#[allow(deprecated)]
24use crate::{Date, DateTime};
25
26#[derive(Copy, Clone, PartialEq, Eq, Hash)]
44#[cfg_attr(
45 any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"),
46 derive(Archive, Deserialize, Serialize),
47 archive(compare(PartialEq)),
48 archive_attr(derive(Clone, Copy, PartialEq, Eq, Debug, Hash))
49)]
50#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
51#[cfg_attr(all(feature = "arbitrary", feature = "std"), derive(arbitrary::Arbitrary))]
52pub struct Utc;
53
54#[cfg(feature = "now")]
55impl Utc {
56 #[deprecated(
58 since = "0.4.23",
59 note = "use `Utc::now()` instead, potentially with `.date_naive()`"
60 )]
61 #[allow(deprecated)]
62 #[must_use]
63 pub fn today() -> Date<Utc> {
64 Utc::now().date()
65 }
66
67 #[cfg(not(all(
90 target_arch = "wasm32",
91 feature = "wasmbind",
92 not(any(target_os = "emscripten", target_os = "wasi"))
93 )))]
94 #[must_use]
95 pub fn now() -> DateTime<Utc> {
96 let now =
97 SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
98 DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos()).unwrap()
99 }
100
101 #[cfg(all(
103 target_arch = "wasm32",
104 feature = "wasmbind",
105 not(any(target_os = "emscripten", target_os = "wasi"))
106 ))]
107 #[must_use]
108 pub fn now() -> DateTime<Utc> {
109 let now = js_sys::Date::new_0();
110 DateTime::<Utc>::from(now)
111 }
112}
113
114impl TimeZone for Utc {
115 type Offset = Utc;
116
117 fn from_offset(_state: &Utc) -> Utc {
118 Utc
119 }
120
121 fn offset_from_local_date(&self, _local: &NaiveDate) -> MappedLocalTime<Utc> {
122 MappedLocalTime::Single(Utc)
123 }
124 fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> MappedLocalTime<Utc> {
125 MappedLocalTime::Single(Utc)
126 }
127
128 fn offset_from_utc_date(&self, _utc: &NaiveDate) -> Utc {
129 Utc
130 }
131 fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> Utc {
132 Utc
133 }
134}
135
136impl Offset for Utc {
137 fn fix(&self) -> FixedOffset {
138 FixedOffset::east_opt(0).unwrap()
139 }
140}
141
142impl fmt::Debug for Utc {
143 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144 write!(f, "Z")
145 }
146}
147
148impl fmt::Display for Utc {
149 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150 write!(f, "UTC")
151 }
152}