Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmake 4.4.5
duckdb 1.5.0
nodejs 24.22.0
rust 1.99.1
15 changes: 10 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ pub fn open_db(path: &Path) -> Result<duckdb::Connection> {
/// `-P2W`, `PT24H`, `PT30M`), returns the UTC instant that far in the past as RFC 3339. A month
/// counts as 30 days. Otherwise returns `s` unchanged.
pub fn resolve_relative_datetime(s: &str) -> String {
let normalized = s.to_ascii_lowercase();
let normalized = normalized.strip_prefix('-').unwrap_or(&normalized);
let unsigned = s.strip_prefix('-').unwrap_or(s);
// Compact units are lowercase only, so that 3M (often meant as months) is not read as 3 minutes
let normalized = if unsigned.starts_with(['p', 'P']) {
unsigned.to_ascii_lowercase()
} else {
unsigned.to_owned()
};
let (designator, amount) = if let Some(amount) = normalized.strip_prefix("pt") {
("pt", amount)
} else if let Some(amount) = normalized.strip_prefix('p') {
("p", amount)
} else {
("", normalized)
("", normalized.as_str())
};
let Some(unit) = amount.bytes().last() else {
return s.to_owned();
Expand Down Expand Up @@ -221,15 +226,15 @@ mod tests {
assert_approximately_now_minus(&resolve_relative_datetime("P14D"), 14 * 86_400);
assert_approximately_now_minus(&resolve_relative_datetime("-P14D"), 14 * 86_400);
assert_approximately_now_minus(&resolve_relative_datetime("p14d"), 14 * 86_400);
assert_approximately_now_minus(&resolve_relative_datetime("-2H"), 2 * 3600);
assert_approximately_now_minus(&resolve_relative_datetime("-2h"), 2 * 3600);
assert_approximately_now_minus(&resolve_relative_datetime("PT24H"), 24 * 3600);
assert_approximately_now_minus(&resolve_relative_datetime("PT30M"), 30 * 60);
assert_approximately_now_minus(&resolve_relative_datetime("-PT30M"), 30 * 60);

assert_approximately_now_minus(&resolve_relative_datetime("P1M"), 30 * 86_400);
assert_approximately_now_minus(&resolve_relative_datetime("-P3M"), 90 * 86_400);

for unsupported in ["P1H", "PT1D", "TP1M", "T5M", "PP5M"] {
for unsupported in ["P1H", "PT1D", "TP1M", "T5M", "PP5M", "3M", "7D"] {
assert_eq!(resolve_relative_datetime(unsupported), unsupported);
}
}
Expand Down
Loading