haystack_types/
h_time.rs

1use num::Float;
2use crate::{HType, HVal, NumTrait};
3use std::fmt::{self,Write,Display};
4use std::str::FromStr;
5
6use chrono::naive::NaiveTime;
7use chrono::Timelike;
8
9#[derive(Debug,PartialEq)]
10pub struct HTime {
11    inner: NaiveTime,
12}
13
14pub type Time = HTime;
15
16const THIS_TYPE: HType = HType::Time;
17
18impl HTime {
19    pub fn new(hour: u32, minute: u32, second: u32, nano: u32) -> Self {
20        Self { inner: NaiveTime::from_hms_nano(hour, minute, second, nano) }
21    }
22}
23
24impl <'a,T: NumTrait + 'a>HVal<'a,T> for HTime {
25    fn to_zinc(&self, buf: &mut String) -> fmt::Result {
26        write!(buf,"{:0>2}:{:0>2}:{:0>2}.{}",self.inner.hour(),self.inner.minute(),
27        self.inner.second(),self.inner.nanosecond())?;
28        Ok(())
29    }
30    fn to_json(&self, buf: &mut String) -> fmt::Result {
31        write!(buf,"h:")?;
32        let it: &dyn HVal<T> = self;
33        it.to_zinc(buf)
34    }
35    fn haystack_type(&self) -> HType { THIS_TYPE }
36
37    set_trait_eq_method!(get_time_val,'a,T);
38    set_get_method!(get_time_val, HTime);
39}