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