Skip to content

Commit 1e4fc30

Browse files
committed
relative: Support parsing floating relative values with spaces
In case a string such as "now + 1.5 seconds" was parsed we were failing. This happened because after processing now, the parser was getting to the point in which the string contained "+ 1.5", and once the sign was processed, the remaining " 1.5" string conained a space that was causing sec_and_nsec to fail. Instead of failing at this point, just strip the spaces after the sign has been processed. Note in fact that "0+0.0 seconds" was working fine Closes: uutils/coreutils#8618
1 parent 9f6605f commit 1e4fc30

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/items/relative.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub(super) fn parse(input: &mut &str) -> ModalResult<Relative> {
8383
fn seconds(input: &mut &str) -> ModalResult<Relative> {
8484
(
8585
opt(alt((s('+').value(1), s('-').value(-1)))),
86-
sec_and_nsec,
86+
s(sec_and_nsec),
8787
s(alpha1).verify(|s: &str| matches!(s, "seconds" | "second" | "sec" | "secs")),
8888
ago,
8989
)
@@ -138,11 +138,13 @@ mod tests {
138138
("secs", Relative::Seconds(1, 0)),
139139
("second ago", Relative::Seconds(-1, 0)),
140140
("3 seconds", Relative::Seconds(3, 0)),
141+
("+ 3 seconds", Relative::Seconds(3, 0)),
141142
("3.5 seconds", Relative::Seconds(3, 500_000_000)),
142143
("-3.5 seconds", Relative::Seconds(-4, 500_000_000)),
143144
("+3.5 seconds", Relative::Seconds(3, 500_000_000)),
145+
("+ 3.5 seconds", Relative::Seconds(3, 500_000_000)),
144146
("3.5 seconds ago", Relative::Seconds(-4, 500_000_000)),
145-
("-3.5 seconds ago", Relative::Seconds(3, 500_000_000)),
147+
("- 3.5 seconds ago", Relative::Seconds(3, 500_000_000)),
146148
// Minutes
147149
("minute", Relative::Minutes(1)),
148150
("minutes", Relative::Minutes(1)),

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,42 @@ mod tests {
307307
assert!(parse_datetime(relative_time).is_ok());
308308
}
309309
}
310+
311+
#[test]
312+
fn integer_seconds_offset() {
313+
let dt = "0 + 0 seconds";
314+
assert!(parse_datetime(dt).is_ok());
315+
}
316+
317+
#[test]
318+
fn integer_seconds_offset_spaceless() {
319+
let dt = "0+0 seconds";
320+
assert!(parse_datetime(dt).is_ok());
321+
}
322+
323+
#[test]
324+
fn floating_seconds_offset() {
325+
let dt = "0 + 0.0 seconds";
326+
assert!(parse_datetime(dt).is_ok());
327+
}
328+
329+
#[test]
330+
fn floating_seconds_offset_spaceless() {
331+
let dt = "0+0.0 seconds";
332+
assert!(parse_datetime(dt).is_ok());
333+
}
334+
335+
#[test]
336+
fn floating_seconds_offset_from_now() {
337+
let dt = "now + 1.5 seconds";
338+
assert!(parse_datetime(dt).is_ok());
339+
}
340+
341+
#[test]
342+
fn floating_seconds_offset_from_tomorrow_spaceless() {
343+
let dt = "tomorrow+1.5 seconds";
344+
assert!(parse_datetime(dt).is_ok());
345+
}
310346
}
311347

312348
#[cfg(test)]

0 commit comments

Comments
 (0)