haystack_types/
h_date.rs

1use std::str::FromStr;
2use core::fmt::Display;
3use num::Float;
4use crate::{HType, HVal, NumTrait};
5use std::fmt::{self,Write};
6
7use chrono::naive::NaiveDate;
8use chrono::Datelike;
9
10#[derive(Debug,PartialEq)]
11pub struct HDate {
12    inner: NaiveDate,
13}
14
15pub type Date = HDate;
16
17const THIS_TYPE: HType = HType::Date;
18
19impl HDate {
20    pub fn new(year: i32, month: u32, day: u32) -> Self {
21        Self { inner: NaiveDate::from_ymd(year, month, day) }
22    }
23}
24
25impl <'a,T: NumTrait + 'a>HVal<'a,T> for HDate {
26    fn to_zinc(&self, buf: &mut String) -> fmt::Result {
27        write!(buf,"{:0>4}-{:0>2}-{:0>2}",
28            self.inner.year(),
29            self.inner.month(),
30            self.inner.day())
31    }
32    fn to_json(&self, buf: &mut String) -> fmt::Result {
33        write!(buf,"d:")?;
34        let it: &dyn HVal<T> = self;
35        it.to_zinc(buf)
36    }
37    fn haystack_type(&self) -> HType { THIS_TYPE }
38
39    set_trait_eq_method!(get_date_val,'a,T);
40    set_get_method!(get_date_val, HDate);
41}