diff options
author | Daniel <kingdread@gmx.de> | 2020-06-12 15:55:19 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-06-12 15:55:19 +0200 |
commit | 4a3e7137334601828f56a3ee614f01d84bada4ce (patch) | |
tree | 3cf7fc84dfebeff11bd5d12230eb39368f45423f /src/filters/values.rs | |
parent | 6e52b5f2ac6154eca35355b320b7fb8bbc8f23ee (diff) | |
download | raidgrep-4a3e7137334601828f56a3ee614f01d84bada4ce.tar.gz raidgrep-4a3e7137334601828f56a3ee614f01d84bada4ce.tar.bz2 raidgrep-4a3e7137334601828f56a3ee614f01d84bada4ce.zip |
implement -after/-before in terms of -time
It makes sense to unify this implementation to avoid code duplication
and bugs that might be hidden. -after and -before can stay for now, as
shortcuts for -time < and -time >, the same way we have other shortcuts
as well.
Diffstat (limited to 'src/filters/values.rs')
-rw-r--r-- | src/filters/values.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/filters/values.rs b/src/filters/values.rs index df5b805..a523dad 100644 --- a/src/filters/values.rs +++ b/src/filters/values.rs @@ -14,15 +14,21 @@ use std::{ cmp::Ordering, convert::TryFrom, + ffi::OsStr, fmt::{self, Debug}, }; -use chrono::{DateTime, Duration, Utc}; +use chrono::{DateTime, Duration, Local, TimeZone, Utc}; use evtclib::Agent; +use once_cell::sync::Lazy; +use regex::Regex; use super::{log::LogFilter, player::PlayerFilter, Filter, Inclusion}; use crate::{EarlyLogResult, LogResult}; +/// The regular expression used to extract datetimes from filenames. +static DATE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\d{8}-\d{6}").unwrap()); + /// A producer for a given value. /// /// A producer is something that produces a value of a certain type from a log, which can then be @@ -167,7 +173,7 @@ impl Producer for TimeProducer { early_log .log_file .file_name() - .and_then(super::log::datetime_from_filename) + .and_then(datetime_from_filename) } fn produce(&self, log: &LogResult) -> Self::Output { @@ -175,6 +181,18 @@ impl Producer for TimeProducer { } } +/// Try to extract the log time from the filename. +/// +/// This expects the filename to have the datetime in the pattern `YYYYmmdd-HHMMSS` somewhere in +/// it. +fn datetime_from_filename(name: &OsStr) -> Option<DateTime<Utc>> { + let date_match = DATE_REGEX.find(name.to_str()?)?; + let local_time = Local + .datetime_from_str(date_match.as_str(), "%Y%m%d-%H%M%S") + .ok()?; + Some(local_time.with_timezone(&Utc)) +} + /// A producer that produces the time at which a log was created. pub fn time() -> Box<dyn Producer<Output = DateTime<Utc>>> { Box::new(TimeProducer) |