1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use super::*;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TimeDescription {
    /// `new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc)`
    pub default: Option<DateTime>,
}

impl TimeDescription {
    pub fn with_default(mut self, time: &str) -> Self {
        match DateTime::from_str(time) {
            Ok(o) => self.default = Some(o),
            Err(_) => {}
        }
        self
    }
}

impl TimeDescription {
    pub fn parse_cell(&self, cell: &DataType) -> XResult<XCellValue> {
        self.parse_value(cell).map(XCellValue::Time)
    }

    fn parse_value(&self, cell: &DataType) -> XResult<DateTime> {
        match cell {
            DataType::DateTime(time) => {
                let ntv = NaiveDateTime::from_timestamp(*time as i64, 0);
                let utc = Utc.from_utc_datetime(&ntv);
                Ok(utc)
            }
            DataType::String(s) => match DateTime::from_str(s) {
                Ok(o) => Ok(o),
                Err(_) => syntax_error(format!("{} 无法解析为 time 类型", s)),
            },
            DataType::Empty => match &self.default {
                Some(s) => Ok(s.clone()),
                None => Ok(DateTime::default()),
            },
            _ => syntax_error(format!("{} 无法解析为 time 类型", cell.to_string())),
        }
    }
}