Skip to main content

rbdc/types/
date.rs

1use crate::Error;
2use fastdate::DateTime;
3use rbs::Value;
4use std::fmt::{Debug, Display, Formatter};
5use std::str::FromStr;
6
7#[derive(serde::Serialize, serde::Deserialize, Clone, Eq, PartialEq, Hash)]
8#[serde(rename = "Date")]
9pub struct Date(pub fastdate::Date);
10
11impl Display for Date {
12    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13        write!(f, "{}", self.0)
14    }
15}
16
17impl Debug for Date {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        write!(f, "Date({})", self.0)
20    }
21}
22
23impl From<Date> for Value {
24    fn from(arg: Date) -> Self {
25        Value::Ext("Date", Box::new(Value::String(arg.0.to_string())))
26    }
27}
28
29impl FromStr for Date {
30    type Err = Error;
31
32    fn from_str(s: &str) -> Result<Self, Self::Err> {
33        Ok(Date(
34            fastdate::Date::from_str(s).map_err(|e| Error::from(e.to_string()))?,
35        ))
36    }
37}
38
39impl From<Date> for fastdate::Date {
40    fn from(value: Date) -> Self {
41        value.0
42    }
43}
44
45impl From<DateTime> for Date {
46    fn from(value: DateTime) -> Self {
47        Date(value.into())
48    }
49}
50
51impl Default for Date {
52    fn default() -> Self {
53        Date(fastdate::Date {
54            day: 1,
55            mon: 1,
56            year: 1970,
57        })
58    }
59}