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
59
60
61
62
63
64
65
use chrono::prelude::*;
use chrono::DateTime;
use chrono_tz::Tz;
use serde::{Deserialize, Serialize};
use std::error::Error;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TimeSpan {
start_ts: i64,
end_ts: i64,
duration: i64,
}
impl TimeSpan {
pub fn new(start_ts: i64, end_ts: i64) -> Self {
Self {
start_ts,
end_ts,
duration: end_ts - start_ts,
}
}
pub fn greater_than(&self, duration: i64) -> bool {
self.duration > duration
}
fn create_datetime_from_millis(timestamp_millis: i64, tz: &Tz) -> DateTime<Tz> {
tz.timestamp_millis(timestamp_millis)
}
pub fn as_datetime(&self, tz: &Tz) -> TimeSpanDateTime {
TimeSpanDateTime {
start: TimeSpan::create_datetime_from_millis(self.start_ts, tz),
end: TimeSpan::create_datetime_from_millis(self.end_ts, tz),
}
}
pub fn start(&self) -> i64 {
self.start_ts
}
pub fn end(&self) -> i64 {
self.end_ts
}
}
#[derive(Debug)]
pub struct InvalidTimeSpanError(i64, i64);
impl Error for InvalidTimeSpanError {}
impl std::fmt::Display for InvalidTimeSpanError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Provided timespan start_ts: {} and end_ts: {} is invalid. It should be between 1 hour and 40 days.", self.0, self.1)
}
}
#[derive(Debug)]
pub struct TimeSpanDateTime {
pub start: DateTime<Tz>,
pub end: DateTime<Tz>,
}