1use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
9pub struct ParseDate {
10 #[serde(rename = "__type")]
11 pub __type: String, pub iso: String, }
14
15impl fmt::Display for ParseDate {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 write!(f, "{}", self.iso)
18 }
19}
20
21impl ParseDate {
22 pub fn new(iso_string: impl Into<String>) -> Self {
25 ParseDate {
26 __type: "Date".to_string(),
27 iso: iso_string.into(),
28 }
29 }
30
31 pub fn now() -> Self {
33 Self::from_datetime(Utc::now())
34 }
35
36 pub fn from_datetime(dt: DateTime<Utc>) -> Self {
38 ParseDate {
39 __type: "Date".to_string(),
40 iso: dt.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
41 }
42 }
43
44 pub fn to_datetime(&self) -> Result<DateTime<Utc>, chrono::ParseError> {
46 DateTime::parse_from_rfc3339(&self.iso).map(|dt| dt.with_timezone(&Utc))
47 }
48}