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