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(fastdate::Date::from_str(s).map_err(|e|Error::from(e.to_string()))?))
34    }
35}
36
37impl From<Date> for fastdate::Date {
38    fn from(value: Date) -> Self {
39        value.0
40    }
41}
42
43impl From<DateTime> for Date {
44    fn from(value: DateTime) -> Self {
45        Date(value.into())
46    }
47}
48
49impl Default for Date {
50    fn default() -> Self {
51        Date(fastdate::Date {
52            day: 1,
53            mon: 1,
54            year: 1970,
55        })
56    }
57}