From ba944d31813255ac5d44b811729f85b3bf3cae30 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 00:02:33 +0100 Subject: [PATCH 1/2] fix(core): roll sizes and durations over to the next unit when they round up Bytes and Elapsed picked their unit before rounding, so 1,048,525 bytes read 1024.0 KiB and 3,599,500 ms read 60 min 0 s. Both now round first and then pick the unit, as the compact Count does. Fixes PQD-152 --- crates/piqueld-core/src/observability/time.rs | 12 ++++++++---- crates/piqueld-core/src/observability/units.rs | 14 +++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/crates/piqueld-core/src/observability/time.rs b/crates/piqueld-core/src/observability/time.rs index 23ba1fb16..f9cce6720 100644 --- a/crates/piqueld-core/src/observability/time.rs +++ b/crates/piqueld-core/src/observability/time.rs @@ -136,20 +136,22 @@ impl Elapsed { impl fmt::Display for Elapsed { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { let milliseconds = self.0.as_millis(); + // Rounded first, so the unit is picked after rounding: 3,599.5 s + // reads as an hour rather than 60 minutes. + let seconds = (milliseconds + 500) / 1_000; match milliseconds { 0..=999 => write!(formatter, "{milliseconds} ms"), // Tenths of a second, below the 60.0 s that is shown as a // minute. 1_000..=59_949 => write!(formatter, "{} s", Tenths::of(milliseconds, 1_000)), - 59_950..=3_599_999 => { - let seconds = (milliseconds + 500) / 1_000; + _ if seconds < 3_600 => { write!(formatter, "{} min {} s", seconds / 60, seconds % 60) } _ => write!( formatter, "{} h {} min", - milliseconds / 3_600_000, - (milliseconds % 3_600_000) / 60_000 + seconds / 3_600, + seconds % 3_600 / 60 ), } } @@ -255,6 +257,8 @@ mod tests { (59_949, "59.9 s"), (59_950, "1 min 0 s"), (252_000, "4 min 12 s"), + (3_599_499, "59 min 59 s"), + (3_599_500, "1 h 0 min"), (7_500_000, "2 h 5 min"), ] { assert_eq!(Elapsed::from_millis(milliseconds).to_string(), text); diff --git a/crates/piqueld-core/src/observability/units.rs b/crates/piqueld-core/src/observability/units.rs index c668c9915..94ef9b6a8 100644 --- a/crates/piqueld-core/src/observability/units.rs +++ b/crates/piqueld-core/src/observability/units.rs @@ -5,7 +5,7 @@ use std::fmt; /// A quantity in tenths of a unit, rounded half up from `value` of a `unit`: /// `Tenths::of(1536, 1024)` reads `1.5`. With `{:#}`, a whole number drops /// its `.0`. Sizes, durations and compact counts round this way. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] pub(super) struct Tenths(u128); impl Tenths { @@ -43,10 +43,12 @@ impl Bytes { impl fmt::Display for Bytes { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { const UNITS: [&str; 4] = ["KiB", "MiB", "GiB", "TiB"]; - // The largest unit it reaches, of 2^(10 × power) bytes. + // The largest unit it reaches once rounded, of 2^(10 × power) bytes: + // the one whose previous unit rounds to 1024.0, so 1023.95 KiB + // reads as 1.0 MiB. match (1..=UNITS.len()) .rev() - .find(|power| self.0 >> (10 * power) > 0) + .find(|power| Tenths::of(u128::from(self.0), 1 << (10 * (power - 1))) >= Tenths(10_240)) { None => write!(formatter, "{} B", self.0), Some(power) => { @@ -115,6 +117,12 @@ mod tests { (1023, "1023 B"), (1024, "1.0 KiB"), (1536, "1.5 KiB"), + (1_048_524, "1023.9 KiB"), + (1_048_525, "1.0 MiB"), + (1_073_689_395, "1023.9 MiB"), + (1_073_689_396, "1.0 GiB"), + (1_099_457_940_684, "1023.9 GiB"), + (1_099_457_940_685, "1.0 TiB"), (52_428_800, "50.0 MiB"), (127_026_911_232, "118.3 GiB"), (u64::MAX, "16777216.0 TiB"), From d2eb21fce0fc6c23753c0f4529d1138ad1396382 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 13:24:00 +0100 Subject: [PATCH 2/2] docs(core): say the previous unit rounds to 1024.0 or more --- crates/piqueld-core/src/observability/units.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/piqueld-core/src/observability/units.rs b/crates/piqueld-core/src/observability/units.rs index 94ef9b6a8..3a8849398 100644 --- a/crates/piqueld-core/src/observability/units.rs +++ b/crates/piqueld-core/src/observability/units.rs @@ -44,7 +44,7 @@ impl fmt::Display for Bytes { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { const UNITS: [&str; 4] = ["KiB", "MiB", "GiB", "TiB"]; // The largest unit it reaches once rounded, of 2^(10 × power) bytes: - // the one whose previous unit rounds to 1024.0, so 1023.95 KiB + // the one whose previous unit rounds to 1024.0 or more, so 1023.95 KiB // reads as 1.0 MiB. match (1..=UNITS.len()) .rev()