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