diff --git a/src/core/time.rs b/src/core/time.rs index bb37d2f..cbe0859 100644 --- a/src/core/time.rs +++ b/src/core/time.rs @@ -17,21 +17,27 @@ pub fn now_ms() -> i64 { } pub fn parse_since(input: &str) -> Option { + parse_since_from(input, now_ms()) +} + +/// Like `parse_since` but durations are relative to `reference_ms` instead of now. +/// Absolute dates/timestamps are returned as-is regardless of `reference_ms`. +pub fn parse_since_from(input: &str, reference_ms: i64) -> Option { let input = input.trim(); if let Some(num_str) = input.strip_suffix('d') { let days: i64 = num_str.parse().ok()?; - return Some(now_ms() - (days * 24 * 60 * 60 * 1000)); + return Some(reference_ms - (days * 24 * 60 * 60 * 1000)); } if let Some(num_str) = input.strip_suffix('w') { let weeks: i64 = num_str.parse().ok()?; - return Some(now_ms() - (weeks * 7 * 24 * 60 * 60 * 1000)); + return Some(reference_ms - (weeks * 7 * 24 * 60 * 60 * 1000)); } if let Some(num_str) = input.strip_suffix('m') { let months: i64 = num_str.parse().ok()?; - return Some(now_ms() - (months * 30 * 24 * 60 * 60 * 1000)); + return Some(reference_ms - (months * 30 * 24 * 60 * 60 * 1000)); } if input.len() == 10 && input.chars().filter(|&c| c == '-').count() == 2 {