"Fossies" - the Fresh Open Source Software Archive

Member "fd-8.1.1/src/filter/time.rs" (25 May 2020, 2383 Bytes) of package /linux/privat/fd-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 use std::time::SystemTime;
    2 
    3 /// Filter based on time ranges.
    4 #[derive(Debug, PartialEq)]
    5 pub enum TimeFilter {
    6     Before(SystemTime),
    7     After(SystemTime),
    8 }
    9 
   10 impl TimeFilter {
   11     fn from_str(ref_time: &SystemTime, s: &str) -> Option<SystemTime> {
   12         humantime::parse_duration(s)
   13             .map(|duration| *ref_time - duration)
   14             .or_else(|_| humantime::parse_rfc3339_weak(s))
   15             .ok()
   16     }
   17 
   18     pub fn before(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
   19         TimeFilter::from_str(ref_time, s).map(TimeFilter::Before)
   20     }
   21 
   22     pub fn after(ref_time: &SystemTime, s: &str) -> Option<TimeFilter> {
   23         TimeFilter::from_str(ref_time, s).map(TimeFilter::After)
   24     }
   25 
   26     pub fn applies_to(&self, t: &SystemTime) -> bool {
   27         match self {
   28             TimeFilter::Before(limit) => t <= limit,
   29             TimeFilter::After(limit) => t >= limit,
   30         }
   31     }
   32 }
   33 
   34 #[cfg(test)]
   35 mod tests {
   36     use super::*;
   37     use std::time::Duration;
   38 
   39     #[test]
   40     fn is_time_filter_applicable() {
   41         let ref_time = humantime::parse_rfc3339("2010-10-10T10:10:10Z").unwrap();
   42         assert!(TimeFilter::after(&ref_time, "1min")
   43             .unwrap()
   44             .applies_to(&ref_time));
   45         assert!(!TimeFilter::before(&ref_time, "1min")
   46             .unwrap()
   47             .applies_to(&ref_time));
   48 
   49         let t1m_ago = ref_time - Duration::from_secs(60);
   50         assert!(!TimeFilter::after(&ref_time, "30sec")
   51             .unwrap()
   52             .applies_to(&t1m_ago));
   53         assert!(TimeFilter::after(&ref_time, "2min")
   54             .unwrap()
   55             .applies_to(&t1m_ago));
   56 
   57         assert!(TimeFilter::before(&ref_time, "30sec")
   58             .unwrap()
   59             .applies_to(&t1m_ago));
   60         assert!(!TimeFilter::before(&ref_time, "2min")
   61             .unwrap()
   62             .applies_to(&t1m_ago));
   63 
   64         let t10s_before = "2010-10-10 10:10:00";
   65         assert!(!TimeFilter::before(&ref_time, t10s_before)
   66             .unwrap()
   67             .applies_to(&ref_time));
   68         assert!(TimeFilter::before(&ref_time, t10s_before)
   69             .unwrap()
   70             .applies_to(&t1m_ago));
   71 
   72         assert!(TimeFilter::after(&ref_time, t10s_before)
   73             .unwrap()
   74             .applies_to(&ref_time));
   75         assert!(!TimeFilter::after(&ref_time, t10s_before)
   76             .unwrap()
   77             .applies_to(&t1m_ago));
   78     }
   79 }