diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-06-07 15:36:26 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@atos.net> | 2021-06-07 15:36:26 +0200 |
commit | 3f3ca1259919cb69f8915b11db9b21d9c03aadbc (patch) | |
tree | c5ca554d61caed8e29547670b02346bb2d2977c7 /src/commands/util.rs | |
parent | dcc8b42810d34b2b906f0307c26c4826d16ad997 (diff) | |
parent | b665c7cd4aa4ea900dc194a6ba5ad9de527f978c (diff) |
Merge branch 'db-release-arg-date-filter'
Diffstat (limited to 'src/commands/util.rs')
-rw-r--r-- | src/commands/util.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/commands/util.rs b/src/commands/util.rs index e892875..acfbdd2 100644 --- a/src/commands/util.rs +++ b/src/commands/util.rs @@ -225,3 +225,36 @@ pub fn display_data<D: Display>( } } +pub fn get_date_filter(name: &str, matches: &ArgMatches) -> Result<Option<chrono::DateTime::<chrono::Local>>> { + matches.value_of(name) + .map(|s| { + trace!("Parsing duration: '{}'", s); + humantime::parse_duration(s) + .map_err(Error::from) + .or_else(|_| { + trace!("Parsing time: '{}'", s); + humantime::parse_rfc3339_weak(s) + .map_err(Error::from) + .and_then(|d| d.elapsed().map_err(Error::from)) + }) + .or_else(|_| { + let s = format!("{} 00:00:00", s); + trace!("Parsing time: '{}'", s); + humantime::parse_rfc3339_weak(&s) + .map_err(Error::from) + .and_then(|d| d.elapsed().map_err(Error::from)) + }) + }) + .transpose()? + .map(chrono::Duration::from_std) + .transpose()? + .map(|dur| { + chrono::offset::Local::now() + .checked_sub_signed(dur) + .ok_or_else(|| anyhow!("Time calculation would overflow")) + .with_context(|| anyhow!("Cannot subtract {} from 'now'", dur)) + .map_err(Error::from) + }) + .transpose() +} + |