Debian Codegraph @

src/core/timezone_impl.rs

raw symbols SCIP
1 use chrono::{FixedOffset, Local, Offset, TimeZone, Utc};
2
3 use super::Tz;
4
5 impl PartialEq for Tz {
6 fn eq(&self, other: &Self) -> bool {
7 match (self, other) {
8 (Self::Local(_), Self::Local(_)) => true,
9 (Self::Tz(l0), Self::Tz(r0)) => l0 == r0,
10 _ => false,
11 }
12 }
13 }
14
15 impl From<Local> for Tz {
16 fn from(tz: Local) -> Self {
17 Self::Local(tz)
18 }
19 }
20
21 impl From<Utc> for Tz {
22 fn from(_tz: Utc) -> Self {
23 Self::Tz(chrono_tz::UTC)
24 }
25 }
26
27 impl From<chrono_tz::Tz> for Tz {
28 fn from(tz: chrono_tz::Tz) -> Self {
29 Self::Tz(tz)
30 }
31 }
32
33 impl std::fmt::Debug for Tz {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 match self {
36 Self::Local(tz) => tz.fmt(f),
37 Self::Tz(tz) => tz.fmt(f),
38 }
39 }
40 }
41
42 impl std::fmt::Display for Tz {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 match self {
45 Self::Local(_tz) => write!(f, "Local"),
46 Self::Tz(tz) => tz.fmt(f),
47 }
48 }
49 }
50
51 #[derive(Clone, Copy)]
52 pub enum RRuleOffset {
53 Local(FixedOffset),
54 Tz(<chrono_tz::Tz as TimeZone>::Offset),
55 }
56
57 impl std::fmt::Debug for RRuleOffset {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 match self {
60 Self::Local(offset) => offset.fmt(f),
61 Self::Tz(offset) => offset.fmt(f),
62 }
63 }
64 }
65
66 impl std::fmt::Display for RRuleOffset {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 match self {
69 Self::Local(offset) => offset.fmt(f),
70 Self::Tz(offset) => offset.fmt(f),
71 }
72 }
73 }
74
75 impl Offset for RRuleOffset {
76 fn fix(&self) -> FixedOffset {
77 match self {
78 Self::Local(tz) => tz.fix(),
79 Self::Tz(tz) => tz.fix(),
80 }
81 }
82 }
83
84 impl TimeZone for Tz {
85 type Offset = RRuleOffset;
86
87 fn from_offset(offset: &Self::Offset) -> Self {
88 match offset {
89 RRuleOffset::Local(offset) => Self::Local(Local::from_offset(offset)),
90 RRuleOffset::Tz(offset) => Self::Tz(chrono_tz::Tz::from_offset(offset)),
91 }
92 }
93
94 #[allow(deprecated)]
95 fn offset_from_local_date(
96 &self,
97 local: &chrono::NaiveDate,
98 ) -> chrono::LocalResult<Self::Offset> {
99 match self {
100 Self::Local(tz) => tz
101 .from_local_date(local)
102 .map(|date| RRuleOffset::Local(*date.offset())),
103 Self::Tz(tz) => tz
104 .from_local_date(local)
105 .map(|date| RRuleOffset::Tz(*date.offset())),
106 }
107 }
108
109 fn offset_from_local_datetime(
110 &self,
111 local: &chrono::NaiveDateTime,
112 ) -> chrono::LocalResult<Self::Offset> {
113 match self {
114 Self::Local(tz) => tz
115 .from_local_datetime(local)
116 .map(|date| RRuleOffset::Local(*date.offset())),
117 Self::Tz(tz) => tz
118 .from_local_datetime(local)
119 .map(|date| RRuleOffset::Tz(*date.offset())),
120 }
121 }
122
123 #[allow(deprecated)]
124 fn offset_from_utc_date(&self, utc: &chrono::NaiveDate) -> Self::Offset {
125 match self {
126 Self::Local(tz) => RRuleOffset::Local(*tz.from_utc_date(utc).offset()),
127 Self::Tz(tz) => RRuleOffset::Tz(*tz.from_utc_date(utc).offset()),
128 }
129 }
130
131 fn offset_from_utc_datetime(&self, utc: &chrono::NaiveDateTime) -> Self::Offset {
132 match self {
133 Self::Local(tz) => RRuleOffset::Local(*tz.from_utc_datetime(utc).offset()),
134 Self::Tz(tz) => RRuleOffset::Tz(*tz.from_utc_datetime(utc).offset()),
135 }
136 }
137 }