Merge pull request #246 from 3v1n0/fix-parsing-relative-times

relative: Support parsing floating relative values with spaces
This commit is contained in:
Daniel Hofstetter
2025-11-21 09:16:18 +01:00
committed by GitHub
2 changed files with 51 additions and 2 deletions
+15 -2
View File
@@ -83,7 +83,7 @@ pub(super) fn parse(input: &mut &str) -> ModalResult<Relative> {
fn seconds(input: &mut &str) -> ModalResult<Relative> {
(
opt(alt((s('+').value(1), s('-').value(-1)))),
sec_and_nsec,
s(sec_and_nsec),
s(alpha1).verify(|s: &str| matches!(s, "seconds" | "second" | "sec" | "secs")),
ago,
)
@@ -138,11 +138,13 @@ mod tests {
("secs", Relative::Seconds(1, 0)),
("second ago", Relative::Seconds(-1, 0)),
("3 seconds", Relative::Seconds(3, 0)),
("+ 3 seconds", Relative::Seconds(3, 0)),
("3.5 seconds", Relative::Seconds(3, 500_000_000)),
("-3.5 seconds", Relative::Seconds(-4, 500_000_000)),
("+3.5 seconds", Relative::Seconds(3, 500_000_000)),
("+ 3.5 seconds", Relative::Seconds(3, 500_000_000)),
("3.5 seconds ago", Relative::Seconds(-4, 500_000_000)),
("-3.5 seconds ago", Relative::Seconds(3, 500_000_000)),
("- 3.5 seconds ago", Relative::Seconds(3, 500_000_000)),
// Minutes
("minute", Relative::Minutes(1)),
("minutes", Relative::Minutes(1)),
@@ -150,29 +152,40 @@ mod tests {
("mins", Relative::Minutes(1)),
("10 minutes", Relative::Minutes(10)),
("-10 minutes", Relative::Minutes(-10)),
("- 10 minutes", Relative::Minutes(-10)),
("10 minutes ago", Relative::Minutes(-10)),
("-10 minutes ago", Relative::Minutes(10)),
("- 10 minutes ago", Relative::Minutes(10)),
("-10 minutes ago", Relative::Minutes(10)),
("- 10 minutes ago", Relative::Minutes(10)),
// Hours
("hour", Relative::Hours(1)),
("hours", Relative::Hours(1)),
("10 hours", Relative::Hours(10)),
("+10 hours", Relative::Hours(10)),
("+ 10 hours", Relative::Hours(10)),
("-10 hours", Relative::Hours(-10)),
("- 10 hours", Relative::Hours(-10)),
("10 hours ago", Relative::Hours(-10)),
("-10 hours ago", Relative::Hours(10)),
("- 10 hours ago", Relative::Hours(10)),
// Days
("day", Relative::Days(1)),
("days", Relative::Days(1)),
("10 days", Relative::Days(10)),
("+10 days", Relative::Days(10)),
("+ 10 days", Relative::Days(10)),
("-10 days", Relative::Days(-10)),
("- 10 days", Relative::Days(-10)),
("10 days ago", Relative::Days(-10)),
("-10 days ago", Relative::Days(10)),
("- 10 days ago", Relative::Days(10)),
// Multiple days
("fortnight", Relative::Days(14)),
("fortnights", Relative::Days(14)),
("2 fortnights ago", Relative::Days(-28)),
("+2 fortnights ago", Relative::Days(-28)),
("+ 2 fortnights ago", Relative::Days(-28)),
("week", Relative::Days(7)),
("weeks", Relative::Days(7)),
("2 weeks ago", Relative::Days(-14)),
+36
View File
@@ -307,6 +307,42 @@ mod tests {
assert!(parse_datetime(relative_time).is_ok());
}
}
#[test]
fn integer_seconds_offset() {
let dt = "0 + 0 seconds";
assert!(parse_datetime(dt).is_ok());
}
#[test]
fn integer_seconds_offset_spaceless() {
let dt = "0+0 seconds";
assert!(parse_datetime(dt).is_ok());
}
#[test]
fn floating_seconds_offset() {
let dt = "0 + 0.0 seconds";
assert!(parse_datetime(dt).is_ok());
}
#[test]
fn floating_seconds_offset_spaceless() {
let dt = "0+0.0 seconds";
assert!(parse_datetime(dt).is_ok());
}
#[test]
fn floating_seconds_offset_from_now() {
let dt = "now + 1.5 seconds";
assert!(parse_datetime(dt).is_ok());
}
#[test]
fn floating_seconds_offset_from_tomorrow_spaceless() {
let dt = "tomorrow+1.5 seconds";
assert!(parse_datetime(dt).is_ok());
}
}
#[cfg(test)]