Skip to content
Open
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
20 changes: 17 additions & 3 deletions src/items/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,23 @@ impl TryFrom<Relative> for jiff::Span {
Relative::Days(days) => jiff::Span::new().try_days(days),
Relative::Hours(hours) => jiff::Span::new().try_hours(hours),
Relative::Minutes(minutes) => jiff::Span::new().try_minutes(minutes),
Relative::Seconds(seconds, nanoseconds) => jiff::Span::new()
.try_seconds(seconds)
.and_then(|span| span.try_nanoseconds(nanoseconds)),
Relative::Seconds(seconds, nanoseconds) => {
// `Relative::Seconds` is a floor decomposition: the value is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 lines of comment for 3 lines of code, nobody will read it :) two lines are enough here

// `seconds + nanoseconds / 1e9` with a non-negative fraction, so
// -0.25 seconds is held as `(-1, 750_000_000)`. A `jiff::Span`
// is sign-uniform, so the fraction has to be rebalanced onto the
// sign of the whole seconds first; combining the fields as they
// are would subtract the fraction instead of adding it.
let (seconds, nanoseconds) = if seconds < 0 && nanoseconds > 0 {
(seconds + 1, -i64::from(1_000_000_000 - nanoseconds))
} else {
(seconds, i64::from(nanoseconds))
};

jiff::Span::new()
.try_seconds(seconds)
.and_then(|span| span.try_nanoseconds(nanoseconds))
}
}
.map_err(|_| "relative value is invalid")
}
Expand Down
36 changes: 36 additions & 0 deletions tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,39 @@ fn test_time_seconds_ago_invalid(#[case] input: &str) {
"Input string '{input}' did not produce an error when parsing"
);
}

// Fractional relative seconds, checked against GNU date 9.11 with

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the header at the top of the file says 8.32, maybe worth aligning the versions to 9.11

// TZ=UTC date --date="2026-08-27 12:00:00 <input>" +"%H:%M:%S.%N"
#[rstest]
#[case::plus_half("+0.5 sec", "12:00:00.500000000")]
#[case::minus_half("-0.5 sec", "11:59:59.500000000")]
#[case::plus_one_and_a_half("+1.5 sec", "12:00:01.500000000")]
#[case::minus_one_and_a_half("-1.5 sec", "11:59:58.500000000")]
#[case::plus_quarter("+0.25 sec", "12:00:00.250000000")]
#[case::minus_quarter("-0.25 sec", "11:59:59.750000000")]
#[case::plus_one_and_three_quarters("+1.75 sec", "12:00:01.750000000")]
#[case::minus_one_and_three_quarters("-1.75 sec", "11:59:58.250000000")]
#[case::plus_two_and_an_eighth("+2.125 sec", "12:00:02.125000000")]
#[case::minus_two_and_an_eighth("-2.125 sec", "11:59:57.875000000")]
#[case::minus_nine_nanoseconds("-0.000000009 sec", "11:59:59.999999991")]
#[case::minus_almost_one("-0.999999999 sec", "11:59:59.000000001")]
#[case::plus_half_ago("+0.5 sec ago", "11:59:59.500000000")]
#[case::minus_half_ago("-0.5 sec ago", "12:00:00.500000000")]
#[case::plus_one_and_a_half_ago("+1.5 sec ago", "11:59:58.500000000")]
#[case::unsigned_half_ago("0.5 sec ago", "11:59:59.500000000")]
#[case::unsigned_half("0.5 sec", "12:00:00.500000000")]
#[case::plus_zero_fraction("+0.0 sec", "12:00:00.000000000")]
#[case::minus_zero_fraction("-0.0 sec", "12:00:00.000000000")]
#[case::minus_whole_with_fraction_zero("-1.0 sec", "11:59:59.000000000")]
#[case::minus_whole("-1 second ago", "12:00:01.000000000")]
#[case::plural_minus_half("-0.5 seconds", "11:59:59.500000000")]
#[case::abbreviated_minus_half("-0.5 secs", "11:59:59.500000000")]
fn test_relative_fractional_seconds(#[case] input: &str, #[case] expected: &str) {
let base = "2026-08-27 12:00:00"
.parse::<DateTime>()
.unwrap()
.to_zoned(TimeZone::UTC)
.unwrap();

check_time(input, expected, "%H:%M:%S.%N", Some(base));
}