sea_query/value/
with_chrono.rs1use super::*;
2use chrono::{Local, Offset, Utc};
3
4type_to_value!(NaiveDate, ChronoDate, Date);
5type_to_value!(NaiveTime, ChronoTime, Time);
6type_to_value!(NaiveDateTime, ChronoDateTime, DateTime);
7
8impl DateLikeValue for NaiveDate {}
9impl TimeLikeValue for NaiveTime {}
10impl DateTimeLikeValue for NaiveDateTime {}
11impl DateTimeLikeValue for DateTime<Utc> {}
12impl DateTimeLikeValue for DateTime<Local> {}
13impl DateTimeLikeValue for DateTime<FixedOffset> {}
14
15impl From<DateTime<Utc>> for Value {
16 fn from(v: DateTime<Utc>) -> Value {
17 Value::ChronoDateTimeUtc(Some(v))
18 }
19}
20
21impl From<DateTime<Local>> for Value {
22 fn from(v: DateTime<Local>) -> Value {
23 Value::ChronoDateTimeLocal(Some(v))
24 }
25}
26
27impl From<DateTime<FixedOffset>> for Value {
28 fn from(x: DateTime<FixedOffset>) -> Value {
29 let v = DateTime::<FixedOffset>::from_naive_utc_and_offset(x.naive_utc(), x.offset().fix());
30 Value::ChronoDateTimeWithTimeZone(Some(v))
31 }
32}
33
34impl Nullable for DateTime<Utc> {
35 fn null() -> Value {
36 Value::ChronoDateTimeUtc(None)
37 }
38}
39
40impl ValueType for DateTime<Utc> {
41 fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
42 match v {
43 Value::ChronoDateTimeUtc(Some(x)) => Ok(x),
44 _ => Err(ValueTypeErr),
45 }
46 }
47
48 fn type_name() -> String {
49 stringify!(DateTime<Utc>).to_owned()
50 }
51
52 fn array_type() -> ArrayType {
53 ArrayType::ChronoDateTimeUtc
54 }
55
56 fn column_type() -> ColumnType {
57 ColumnType::TimestampWithTimeZone
58 }
59}
60
61impl Nullable for DateTime<Local> {
62 fn null() -> Value {
63 Value::ChronoDateTimeLocal(None)
64 }
65}
66
67impl ValueType for DateTime<Local> {
68 fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
69 match v {
70 Value::ChronoDateTimeLocal(Some(x)) => Ok(x),
71 _ => Err(ValueTypeErr),
72 }
73 }
74
75 fn type_name() -> String {
76 stringify!(DateTime<Local>).to_owned()
77 }
78
79 fn array_type() -> ArrayType {
80 ArrayType::ChronoDateTimeLocal
81 }
82
83 fn column_type() -> ColumnType {
84 ColumnType::TimestampWithTimeZone
85 }
86}
87
88impl Nullable for DateTime<FixedOffset> {
89 fn null() -> Value {
90 Value::ChronoDateTimeWithTimeZone(None)
91 }
92}
93
94impl ValueType for DateTime<FixedOffset> {
95 fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
96 match v {
97 Value::ChronoDateTimeWithTimeZone(Some(x)) => Ok(x),
98 _ => Err(ValueTypeErr),
99 }
100 }
101
102 fn type_name() -> String {
103 stringify!(DateTime<FixedOffset>).to_owned()
104 }
105
106 fn array_type() -> ArrayType {
107 ArrayType::ChronoDateTimeWithTimeZone
108 }
109
110 fn column_type() -> ColumnType {
111 ColumnType::TimestampWithTimeZone
112 }
113}
114
115impl Value {
116 pub fn is_chrono_date(&self) -> bool {
117 matches!(self, Self::ChronoDate(_))
118 }
119
120 pub fn is_chrono_time(&self) -> bool {
121 matches!(self, Self::ChronoTime(_))
122 }
123
124 pub fn is_chrono_date_time(&self) -> bool {
125 matches!(self, Self::ChronoDateTime(_))
126 }
127
128 pub fn is_chrono_date_time_utc(&self) -> bool {
129 matches!(self, Self::ChronoDateTimeUtc(_))
130 }
131
132 pub fn is_chrono_date_time_with_time_zone(&self) -> bool {
133 matches!(self, Self::ChronoDateTimeWithTimeZone(_))
134 }
135
136 pub fn is_chrono_date_time_local(&self) -> bool {
137 matches!(self, Self::ChronoDateTimeLocal(_))
138 }
139
140 pub fn as_ref_chrono_date(&self) -> Option<&NaiveDate> {
141 match self {
142 Self::ChronoDate(v) => v.as_ref(),
143 _ => panic!("not Value::ChronoDate"),
144 }
145 }
146
147 pub fn as_ref_chrono_time(&self) -> Option<&NaiveTime> {
148 match self {
149 Self::ChronoTime(v) => v.as_ref(),
150 _ => panic!("not Value::ChronoTime"),
151 }
152 }
153
154 pub fn as_ref_chrono_date_time(&self) -> Option<&NaiveDateTime> {
155 match self {
156 Self::ChronoDateTime(v) => v.as_ref(),
157 _ => panic!("not Value::ChronoDateTime"),
158 }
159 }
160
161 pub fn as_ref_chrono_date_time_utc(&self) -> Option<&DateTime<Utc>> {
162 match self {
163 Self::ChronoDateTimeUtc(v) => v.as_ref(),
164 _ => panic!("not Value::ChronoDateTimeUtc"),
165 }
166 }
167
168 pub fn as_ref_chrono_date_time_with_time_zone(&self) -> Option<&DateTime<FixedOffset>> {
169 match self {
170 Self::ChronoDateTimeWithTimeZone(v) => v.as_ref(),
171 _ => panic!("not Value::ChronoDateTimeWithTimeZone"),
172 }
173 }
174
175 pub fn as_ref_chrono_date_time_local(&self) -> Option<&DateTime<Local>> {
176 match self {
177 Self::ChronoDateTimeLocal(v) => v.as_ref(),
178 _ => panic!("not Value::ChronoDateTimeLocal"),
179 }
180 }
181
182 pub fn chrono_as_naive_utc_in_string(&self) -> Option<String> {
183 match self {
184 Self::ChronoDate(v) => v.as_ref().map(|v| v.to_string()),
185 Self::ChronoTime(v) => v.as_ref().map(|v| v.to_string()),
186 Self::ChronoDateTime(v) => v.as_ref().map(|v| v.to_string()),
187 Self::ChronoDateTimeUtc(v) => v.as_ref().map(|v| v.naive_utc().to_string()),
188 Self::ChronoDateTimeLocal(v) => v.as_ref().map(|v| v.naive_utc().to_string()),
189 Self::ChronoDateTimeWithTimeZone(v) => v.as_ref().map(|v| v.naive_utc().to_string()),
190 _ => panic!("not chrono Value"),
191 }
192 }
193}