easyofd_core/model/
creation_date.rs1use chrono::NaiveDateTime;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct CreationDate {
13 pub value: NaiveDateTime,
15}
16
17impl CreationDate {
18 pub fn new(value: NaiveDateTime) -> Self {
20 Self { value }
21 }
22
23 pub fn parse(s: &str) -> Option<Self> {
27 let value = NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
28 .or_else(|_| {
29 chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d")
30 .map(|d| d.and_hms_opt(0, 0, 0).expect("00:00:00 是有效时间"))
31 })
32 .ok()?;
33 Some(Self { value })
34 }
35
36 #[must_use]
38 pub fn to_iso(&self) -> String {
39 self.value.format("%Y-%m-%dT%H:%M:%S").to_string()
40 }
41}