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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The `parse_datetime` and `parse_datetime_at_date` functions support absolute dat
- "now" or "today"
- "yesterday"
- "tomorrow"
- use "ago" for the past
- use "ago" for the past or "hence" for the future
- use "next" or "last" with `unit` (e.g., "next week", "last year")
- unix timestamps (for example "@0" "@1344000")

Expand Down
2 changes: 1 addition & 1 deletion src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub(crate) fn parse_at_local<S: AsRef<str> + Clone>(input: S) -> Result<ParsedDa
///
/// time_offset = ( "+" | "-" ) , dec_uint , [ ":" , dec_uint ] ;
///
/// relative = [ numeric_ordinal ] , unit , [ "ago" ] | day_shift ;
/// relative = [ numeric_ordinal ] , unit , [ "ago" | "hence" ] | day_shift ;
///
/// unit = "year" | "years"
/// | "month" | "months"
Expand Down
10 changes: 6 additions & 4 deletions src/items/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn seconds(input: &mut &str) -> ModalResult<Relative> {
opt(alt((s('+').value(1), s('-').value(-1)))),
s(sec_and_nsec),
s(alpha1).verify(|s: &str| matches!(s, "seconds" | "second" | "sec" | "secs")),
ago,
direction,
)
.verify_map(|(sign, (sec, nsec), _, ago)| {
Comment on lines +88 to 90
let sec = i64::try_from(sec).ok()?;
Expand All @@ -102,7 +102,7 @@ fn seconds(input: &mut &str) -> ModalResult<Relative> {
}

fn displacement(input: &mut &str) -> ModalResult<Relative> {
(opt(ordinal), s(alpha1), ago)
(opt(ordinal), s(alpha1), direction)
.verify_map(|(n, unit, ago): (Option<i32>, &str, bool)| {
let multiplier = n.unwrap_or(1) * if ago { -1 } else { 1 };
Some(match unit.strip_suffix('s').unwrap_or(unit) {
Expand All @@ -120,8 +120,10 @@ fn displacement(input: &mut &str) -> ModalResult<Relative> {
.parse_next(input)
}

fn ago(input: &mut &str) -> ModalResult<bool> {
opt(s("ago")).map(|o| o.is_some()).parse_next(input)
fn direction(input: &mut &str) -> ModalResult<bool> {
opt(alt((s("ago").value(true), s("hence").value(false))))
.map(|backward| backward.unwrap_or(false))
.parse_next(input)
}

#[cfg(test)]
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,30 @@ mod tests {
mod test_relative {
use crate::parse_datetime;

#[test]
fn test_hence_suffix() {
let now: jiff::Zoned = "2026-01-15T12:00:00Z[UTC]".parse().unwrap();
for relative in [
"2 months",
"year",
"-2 days",
"next week",
"0 hours",
"1.5 seconds",
] {
let expected = crate::parse_datetime_at_date(now.clone(), relative).unwrap();
for suffix in ["hence", "HENCE"] {
assert_eq!(
crate::parse_datetime_at_date(now.clone(), format!("{relative} {suffix}"))
.unwrap(),
expected,
);
}
}
assert!(crate::parse_datetime_at_date(now.clone(), "hence").is_err());
assert!(crate::parse_datetime_at_date(now, "2 days henceforth").is_err());
}

#[test]
fn test_month() {
assert_eq!(
Expand Down
Loading