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 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-02-13 10:54:20 -05:00
parent 7e0e6a91f2
commit 9786ef27f5

View File

@@ -17,21 +17,27 @@ pub fn now_ms() -> i64 {
} }
pub fn parse_since(input: &str) -> Option<i64> { pub fn parse_since(input: &str) -> Option<i64> {
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<i64> {
let input = input.trim(); let input = input.trim();
if let Some(num_str) = input.strip_suffix('d') { if let Some(num_str) = input.strip_suffix('d') {
let days: i64 = num_str.parse().ok()?; 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') { if let Some(num_str) = input.strip_suffix('w') {
let weeks: i64 = num_str.parse().ok()?; 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') { if let Some(num_str) = input.strip_suffix('m') {
let months: i64 = num_str.parse().ok()?; 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 { if input.len() == 10 && input.chars().filter(|&c| c == '-').count() == 2 {