From 9786ef27f5f64479158173df3aa0f937c96ce1da Mon Sep 17 00:00:00 2001 From: Taylor Eernisse Date: Fri, 13 Feb 2026 10:54:20 -0500 Subject: [PATCH] refactor(core/time): extract parse_since_from for deterministic time parsing Factor out parse_since_from(input, reference_ms) so callers can compute relative durations against a fixed reference timestamp instead of always using now(). The existing parse_since() now delegates to it with now_ms(). Enables testable and reproducible time-relative queries for features like timeline --as-of and who --as-of. Co-Authored-By: Claude Opus 4.6 --- src/core/time.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 {