use chrono::{DateTime, Utc};
use crate::errors::CliError;
pub const FORMS: &str =
"want a duration (30m, 4h, 3d, 2w), 'today', 'yesterday', a date (2026-07-25), \
or an RFC3339 timestamp";
pub fn parse(s: &str, flag: &str) -> Result<DateTime<Utc>, CliError> {
cliban_core::time::parse_since(s, Utc::now())
.ok_or_else(|| CliError::validation(format!("invalid {flag} {s:?} ({FORMS})")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_error_names_the_flag_and_lists_the_forms() {
let err = parse("last tuesday", "--since").unwrap_err().message();
assert!(err.contains("--since"), "{err}");
assert!(err.contains("\"last tuesday\""), "{err}");
assert!(err.contains("3d"), "the error teaches the syntax: {err}");
assert!(err.contains("yesterday"), "{err}");
}
#[test]
fn day_and_week_units_parse() {
assert!(parse("1d", "--since").is_ok());
assert!(parse("2w", "--since").is_ok());
assert!(parse("yesterday", "--since").is_ok());
assert!(parse("2026-07-25", "--since").is_ok());
}
}