rust_dynamic/
timestamp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use unix_ts::Timestamp;
use chrono::{DateTime, Utc};
use crate::value::{Value, timestamp_ns};
use crate::types::*;

impl Value {
    pub fn get_timestamp(&self) -> f64 {
        self.stamp
    }
    pub fn get_timestamp_as_datetime(&self) -> DateTime<Utc> {
        match self.data {
            Val::Time(u_val) => {
                let ts = Timestamp::from_nanos(u_val as i128);
                return DateTime::from_timestamp_nanos(ts.at_precision(9) as i64);
            }
            _ => {
                return Utc::now();
            }
        }
    }
    pub fn timestamp_diff(&self, other: Self) -> f64 {
        self.stamp - other.get_timestamp()
    }
    pub fn elapsed(&self) -> Result<u128, Box<dyn std::error::Error>> {
        if self.dt != TIME {
            return Err("Incorrect type for the method, TIME required".into());
        }
        match self.data {
            Val::Time(u_val) => {
                return Result::Ok(timestamp_ns() - u_val);
            }
            _ => Err("Value of type TIME is corrupted".into()),
        }
    }
    pub fn elapsed_value(&self) -> Result<Self, Box<dyn std::error::Error>> {
        if self.dt != TIME {
            return Err("Incorrect type for the method, TIME required".into());
        }
        match self.data {
            Val::Time(u_val) => {
                return Result::Ok(Value::from_timestamp(timestamp_ns() - u_val));
            }
            _ => Err("Value of type TIME is corrupted".into()),
        }
    }
    pub fn get_time_as_datetime(&self) -> Result<DateTime<Utc>, Box<dyn std::error::Error>> {
        if self.dt != TIME {
            return Err("Value must be of type TIME".into());
        }
        match self.data {
            Val::Time(u_val) => {
                let ts = Timestamp::from_nanos(u_val as i128);
                return Ok(DateTime::from_timestamp_nanos(ts.at_precision(9) as i64));
            }
            _ => Err("Value of type TIME is corrupted".into()),
        }
    }
}