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