xcell_types/value/
time.rs

1use super::*;
2
3#[derive(Debug, Default, Clone, Serialize, Deserialize)]
4pub struct TimeDescription {
5    /// `new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc)`
6    pub default: Option<DateTime>,
7}
8
9impl TimeDescription {
10    pub fn with_default(mut self, time: &str) -> Self {
11        if let Ok(o) = DateTime::from_str(time) {
12            self.default = Some(o)
13        }
14        self
15    }
16}
17
18impl TimeDescription {
19    pub fn parse_cell(&self, cell: &DataType) -> XResult<XCellValue> {
20        let _ = self.parse_value(cell)?;
21        todo!()
22    }
23
24    fn parse_value(&self, cell: &DataType) -> XResult<DateTime> {
25        match cell {
26            DataType::DateTime(time) => {
27                let ntv = NaiveDateTime::from_timestamp_opt(*time as i64, 0).unwrap_or_default();
28                let utc = Utc.from_utc_datetime(&ntv);
29                Ok(utc)
30            }
31            DataType::String(s) => match DateTime::from_str(s) {
32                Ok(o) => Ok(o),
33                Err(_) => syntax_error(format!("{} 无法解析为 time 类型", s)),
34            },
35            DataType::Empty => match &self.default {
36                Some(s) => Ok(*s),
37                None => Ok(DateTime::default()),
38            },
39            _ => syntax_error(format!("{} 无法解析为 time 类型", cell)),
40        }
41    }
42}