sea_query/value/
with_time.rs

1use super::*;
2
3type_to_value!(time::Date, TimeDate, Date);
4type_to_value!(time::Time, TimeTime, Time);
5type_to_value!(PrimitiveDateTime, TimeDateTime, DateTime);
6
7impl DateLikeValue for time::Date {}
8impl TimeLikeValue for time::Time {}
9impl DateTimeLikeValue for time::PrimitiveDateTime {}
10impl DateTimeLikeValue for time::OffsetDateTime {}
11
12impl DateLikeValueNullable for Option<time::Date> {}
13impl TimeLikeValueNullable for Option<time::Time> {}
14impl DateTimeLikeValueNullable for Option<time::PrimitiveDateTime> {}
15impl DateTimeLikeValueNullable for Option<time::OffsetDateTime> {}
16
17impl From<OffsetDateTime> for Value {
18    fn from(v: OffsetDateTime) -> Value {
19        Value::TimeDateTimeWithTimeZone(Some(v))
20    }
21}
22
23impl Nullable for OffsetDateTime {
24    fn null() -> Value {
25        Value::TimeDateTimeWithTimeZone(None)
26    }
27}
28
29impl ValueType for OffsetDateTime {
30    fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
31        match v {
32            Value::TimeDateTimeWithTimeZone(Some(x)) => Ok(x),
33            _ => Err(ValueTypeErr),
34        }
35    }
36
37    fn type_name() -> String {
38        stringify!(OffsetDateTime).to_owned()
39    }
40
41    fn array_type() -> ArrayType {
42        ArrayType::TimeDateTimeWithTimeZone
43    }
44
45    fn column_type() -> ColumnType {
46        ColumnType::TimestampWithTimeZone
47    }
48}
49
50impl Value {
51    pub fn is_time_date(&self) -> bool {
52        matches!(self, Self::TimeDate(_))
53    }
54
55    pub fn as_ref_time_date(&self) -> Option<&time::Date> {
56        match self {
57            Self::TimeDate(v) => v.as_ref(),
58            _ => panic!("not Value::TimeDate"),
59        }
60    }
61}
62
63impl Value {
64    pub fn is_time_time(&self) -> bool {
65        matches!(self, Self::TimeTime(_))
66    }
67
68    pub fn as_ref_time_time(&self) -> Option<&time::Time> {
69        match self {
70            Self::TimeTime(v) => v.as_ref(),
71            _ => panic!("not Value::TimeTime"),
72        }
73    }
74}
75
76impl Value {
77    pub fn is_time_date_time(&self) -> bool {
78        matches!(self, Self::TimeDateTime(_))
79    }
80
81    pub fn as_ref_time_date_time(&self) -> Option<&PrimitiveDateTime> {
82        match self {
83            Self::TimeDateTime(v) => v.as_ref(),
84            _ => panic!("not Value::TimeDateTime"),
85        }
86    }
87}
88
89impl Value {
90    pub fn is_time_date_time_with_time_zone(&self) -> bool {
91        matches!(self, Self::TimeDateTimeWithTimeZone(_))
92    }
93
94    pub fn as_ref_time_date_time_with_time_zone(&self) -> Option<&OffsetDateTime> {
95        match self {
96            Self::TimeDateTimeWithTimeZone(v) => v.as_ref(),
97            _ => panic!("not Value::TimeDateTimeWithTimeZone"),
98        }
99    }
100}
101
102impl Value {
103    pub fn time_as_naive_utc_in_string(&self) -> Option<String> {
104        match self {
105            Self::TimeDate(v) => v
106                .as_ref()
107                .and_then(|v| v.format(time_format::FORMAT_DATE).ok()),
108            Self::TimeTime(v) => v
109                .as_ref()
110                .and_then(|v| v.format(time_format::FORMAT_TIME).ok()),
111            Self::TimeDateTime(v) => v
112                .as_ref()
113                .and_then(|v| v.format(time_format::FORMAT_DATETIME).ok()),
114            Self::TimeDateTimeWithTimeZone(v) => v.as_ref().and_then(|v| {
115                v.to_offset(time::macros::offset!(UTC))
116                    .format(time_format::FORMAT_DATETIME_TZ)
117                    .ok()
118            }),
119            _ => panic!("not time Value"),
120        }
121    }
122}