2020-08-29 23:16:33 +00:00
|
|
|
use std::time::{
|
|
|
|
SystemTime,
|
|
|
|
UNIX_EPOCH,
|
|
|
|
};
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
use std::fmt::{
|
|
|
|
Formatter,
|
|
|
|
Display,
|
|
|
|
Result as FmtResult,
|
|
|
|
};
|
|
|
|
|
2020-08-29 23:16:33 +00:00
|
|
|
use chrono_tz::Tz;
|
2020-09-01 16:07:51 +00:00
|
|
|
use chrono::TimeZone;
|
2020-08-29 23:16:33 +00:00
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum InvalidTime {
|
|
|
|
ParseErrorDMY,
|
|
|
|
ParseErrorHMS,
|
|
|
|
ParseErrorDisplacement,
|
|
|
|
ParseErrorChrono,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for InvalidTime {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
|
|
|
write!(f, "InvalidTime: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for InvalidTime {}
|
|
|
|
|
2020-08-29 23:16:33 +00:00
|
|
|
enum ParseType {
|
|
|
|
Explicit,
|
|
|
|
Displacement,
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
pub struct TimeParser {
|
2020-08-29 23:16:33 +00:00
|
|
|
timezone: Tz,
|
|
|
|
inverted: bool,
|
|
|
|
time_string: String,
|
|
|
|
parse_type: ParseType,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimeParser {
|
|
|
|
pub fn new(input: String, timezone: Tz) -> Self {
|
|
|
|
let inverted = if input.starts_with("-") {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let parse_type = if input.contains("/") || input.contains(":") {
|
|
|
|
ParseType::Explicit
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ParseType::Displacement
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
timezone,
|
|
|
|
inverted,
|
|
|
|
time_string: input.trim_start_matches("-").to_string(),
|
|
|
|
parse_type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
pub fn timestamp(&self) -> Result<i64, InvalidTime> {
|
2020-08-29 23:16:33 +00:00
|
|
|
match self.parse_type {
|
|
|
|
ParseType::Explicit => {
|
2020-09-01 14:34:50 +00:00
|
|
|
Ok(self.process_explicit()?)
|
2020-08-29 23:16:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ParseType::Displacement => {
|
|
|
|
let now = SystemTime::now();
|
|
|
|
let since_epoch = now
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Time calculated as going backwards. Very bad");
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
Ok(since_epoch.as_secs() as i64 + self.process_displacement()?)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn displacement(&self) -> Result<i64, InvalidTime> {
|
|
|
|
match self.parse_type {
|
|
|
|
ParseType::Explicit => {
|
|
|
|
let now = SystemTime::now();
|
|
|
|
let since_epoch = now
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Time calculated as going backwards. Very bad");
|
|
|
|
|
|
|
|
Ok(self.process_explicit()? - since_epoch.as_secs() as i64)
|
|
|
|
},
|
|
|
|
|
|
|
|
ParseType::Displacement => {
|
|
|
|
Ok(self.process_displacement()?)
|
2020-08-29 23:16:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
fn process_explicit(&self) -> Result<i64, InvalidTime> {
|
|
|
|
let segments = self.time_string.matches("-").count();
|
|
|
|
|
|
|
|
let parse_string = if segments == 1 {
|
|
|
|
let slashes = self.time_string.matches("/").count();
|
|
|
|
|
|
|
|
match slashes {
|
|
|
|
0 => Ok("%d-".to_string()),
|
|
|
|
1 => Ok("%d/%m-".to_string()),
|
|
|
|
2 => Ok("%d/%m/%Y-".to_string()),
|
|
|
|
_ => Err(InvalidTime::ParseErrorDMY)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok("".to_string())
|
|
|
|
}? + if segments == 1 {
|
|
|
|
let colons = self.time_string.matches(":").count();
|
|
|
|
|
|
|
|
match colons {
|
|
|
|
1 => Ok("%H:%M"),
|
|
|
|
2 => Ok("%H:%M:%S"),
|
|
|
|
_ => Err(InvalidTime::ParseErrorHMS)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok("")
|
|
|
|
}?;
|
|
|
|
|
|
|
|
let dt = self.timezone.datetime_from_str(self.time_string.as_str(), &parse_string).map_err(|_| InvalidTime::ParseErrorChrono)?;
|
|
|
|
|
|
|
|
Ok(dt.timestamp() as i64)
|
2020-08-29 23:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
fn process_displacement(&self) -> Result<i64, InvalidTime> {
|
2020-08-29 23:16:33 +00:00
|
|
|
let mut current_buffer = "0".to_string();
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
let mut seconds = 0 as i64;
|
|
|
|
let mut minutes = 0 as i64;
|
|
|
|
let mut hours = 0 as i64;
|
|
|
|
let mut days = 0 as i64;
|
2020-08-29 23:16:33 +00:00
|
|
|
|
|
|
|
for character in self.time_string.chars() {
|
|
|
|
match character {
|
|
|
|
|
|
|
|
's' => {
|
2020-09-01 14:34:50 +00:00
|
|
|
seconds = current_buffer.parse::<i64>().unwrap();
|
2020-08-29 23:16:33 +00:00
|
|
|
current_buffer = String::from("0");
|
|
|
|
},
|
|
|
|
|
|
|
|
'm' => {
|
2020-09-01 14:34:50 +00:00
|
|
|
minutes = current_buffer.parse::<i64>().unwrap();
|
2020-08-29 23:16:33 +00:00
|
|
|
current_buffer = String::from("0");
|
|
|
|
},
|
|
|
|
|
|
|
|
'h' => {
|
2020-09-01 14:34:50 +00:00
|
|
|
hours = current_buffer.parse::<i64>().unwrap();
|
2020-08-29 23:16:33 +00:00
|
|
|
current_buffer = String::from("0");
|
|
|
|
},
|
|
|
|
|
|
|
|
'd' => {
|
2020-09-01 14:34:50 +00:00
|
|
|
days = current_buffer.parse::<i64>().unwrap();
|
2020-08-29 23:16:33 +00:00
|
|
|
current_buffer = String::from("0");
|
|
|
|
},
|
|
|
|
|
|
|
|
c => {
|
|
|
|
if c.is_digit(10) {
|
2020-08-30 20:08:08 +00:00
|
|
|
current_buffer += &c.to_string();
|
2020-08-29 23:16:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-09-01 14:34:50 +00:00
|
|
|
return Err(InvalidTime::ParseErrorDisplacement)
|
2020-08-29 23:16:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::<i64>().unwrap() *
|
2020-08-29 23:16:33 +00:00
|
|
|
if self.inverted { -1 } else { 1 };
|
|
|
|
|
2020-09-01 14:34:50 +00:00
|
|
|
Ok(full)
|
2020-08-29 23:16:33 +00:00
|
|
|
}
|
|
|
|
}
|