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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use anyhow::{anyhow, bail, Result};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::dateutil;
use crate::util;
const MISSING_SAMPLE_WARN_DURATION_S: u64 = 60;
pub fn system_time_from_date(date: &str) -> Result<SystemTime> {
Ok(UNIX_EPOCH
+ Duration::from_secs(
dateutil::HgTime::parse(&date)
.ok_or_else(|| {
anyhow!(
"Unrecognized timestamp format\n\
Input: {}.\n\
Examples:\n\t\
Keywords: now, today, yesterday\n\t\
Relative: \"{{humantime}} ago\", e.g. 2 days 3 hr 15m 10sec ago\n\t\
Relative short: Mixed {{time_digit}}{{time_unit_char}}. E.g. 10m, 3d2H, 5h30s, 10m5h\n\t\
Absolute: \"Jan 01 23:59\", \"01/01/1970 11:59PM\", \"1970-01-01 23:59:59\"\n\t\
Unix Epoch: 1589808367",
&date
)
})?
.unixtime,
))
}
pub fn system_time_from_date_and_adjuster(
date: &str,
days_adjuster: Option<&str>,
) -> Result<SystemTime> {
let mut time = system_time_from_date(date)?;
if let Some(days) = days_adjuster {
if days.is_empty() || days.find(|c: char| c != 'y').is_some() {
bail!("Unrecognized days adjuster format: {}", days);
}
let time_to_deduct = Duration::from_secs(days.chars().count() as u64 * 86400);
time -= time_to_deduct;
}
Ok(time)
}
pub fn system_time_range_from_date_and_adjuster(
start_date: &str,
end_date: Option<&str>,
days_adjuster: Option<&str>,
) -> Result<(SystemTime, SystemTime)> {
let start = system_time_from_date_and_adjuster(start_date, days_adjuster)?;
let end = match end_date {
Some(t) => system_time_from_date_and_adjuster(t, days_adjuster)?,
None => SystemTime::now(),
};
Ok((start, end))
}
pub fn check_initial_sample_time_with_requested_time(
initial_sample_time: SystemTime,
time_begin: SystemTime,
) {
if initial_sample_time > time_begin + Duration::from_secs(MISSING_SAMPLE_WARN_DURATION_S) {
eprintln!(
"Warning: Initial sample found at {} which is over {} seconds \
after the requested start time of {}",
util::systemtime_to_datetime(initial_sample_time),
MISSING_SAMPLE_WARN_DURATION_S,
util::systemtime_to_datetime(time_begin),
);
};
}
pub fn check_initial_sample_time_in_time_range(
initial_sample_time: SystemTime,
time_begin: SystemTime,
time_end: SystemTime,
) -> Result<()> {
if initial_sample_time > time_end {
bail!(
"No samples found in desired time range.\n\
Earliest sample found after {} is at {} which is after the \
requested end time of {}",
util::systemtime_to_datetime(time_begin),
util::systemtime_to_datetime(initial_sample_time),
util::systemtime_to_datetime(time_end),
);
}
check_initial_sample_time_with_requested_time(initial_sample_time, time_begin);
Ok(())
}
pub fn check_final_sample_time_with_requested_time(
final_sample_time: SystemTime,
time_end: SystemTime,
) {
if final_sample_time < time_end - Duration::from_secs(MISSING_SAMPLE_WARN_DURATION_S) {
eprintln!(
"Warning: Final sample processed was for {} which is over {} \
seconds before the requested end time of {}",
util::systemtime_to_datetime(final_sample_time),
MISSING_SAMPLE_WARN_DURATION_S,
util::systemtime_to_datetime(time_end),
);
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_system_time_from_date_fail() {
match system_time_from_date("invalid") {
Ok(_) => panic!("Expected to fail but didn't"),
Err(_) => {}
}
}
#[test]
fn test_system_time_from_date_and_adjuster() {
assert_eq!(
system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", None).unwrap(),
t("2006-02-01 13:00:30 UTC")
);
assert_eq!(
system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("y")).unwrap(),
t("2006-01-31 13:00:30 UTC")
);
assert_eq!(
system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("yy")).unwrap(),
t("2006-01-30 13:00:30 UTC")
);
assert_eq!(
system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("yyy")).unwrap(),
t("2006-01-29 13:00:30 UTC")
);
}
#[test]
fn test_system_time_from_date_and_adjuster_fail() {
match system_time_from_date_and_adjuster("2006-02-01 13:00:30 UTC", Some("invalid")) {
Ok(_) => panic!("Expected fo fail as adjuster is invalid"),
Err(_) => {}
}
}
#[test]
fn test_system_time_range_from_date_and_adjuster() {
assert_eq!(
system_time_range_from_date_and_adjuster(
"2006-02-01 13:00:30 UTC",
Some("2006-02-01 15:00:30 UTC"),
Some("y")
)
.unwrap(),
(t("2006-01-31 13:00:30 UTC"), t("2006-01-31 15:00:30 UTC"))
)
}
fn t(h: &str) -> SystemTime {
system_time_from_date(h).unwrap()
}
}