humanize_proper 0.1.2

Dependency-free, no_std formatting of numbers with SI/binary unit prefixes and of durations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Duration formatting in the style `32y007d_05h01m05s`.

use core::fmt::{self, Display, Formatter, Write};

use crate::buf::{pad_rendered, write_zero_padded, StackStr, POW10};

/// Attoseconds per second, the resolution of [`Dur`].
pub const ATTOS_PER_SEC: u64 = 1_000_000_000_000_000_000;

const SECS_PER_GREGORIAN_YEAR: u64 = 31_556_952;
const UNIT_COUNT: usize = 5;
const LETTERS: [char; UNIT_COUNT] = ['y', 'd', 'h', 'm', 's'];
/// Natural width of each unit. Years are unbounded, so they are never padded.
const WIDTHS: [usize; UNIT_COUNT] = [1, 3, 2, 2, 2];
/// Seconds per unit below years, whose length is configurable.
const SECS_PER_UNIT: [u64; UNIT_COUNT - 1] = [86_400, 3_600, 60, 1];
/// First unit of the time group, the one the separator is written in front of.
const TIME_GROUP: usize = TimeUnit::Hour.index();
const SEC: usize = TimeUnit::Sec.index();
/// Fraction digits of the finest precision, i.e. attoseconds.
const MAX_FRAC_DIGITS: u8 = Frac::Attos.digits();

/// A duration with attosecond resolution and a sign.
///
/// [`core::time::Duration`] only reaches nanoseconds, so the crate carries its own type to make
/// [`Frac::Picos`] and finer reachable losslessly.
///
/// ```
/// use humanize_proper::{Dur, DurFmt, Frac};
///
/// let d = Dur::from_attos(1);
/// assert_eq!(DurFmt::new().frac(Frac::Attos).fmt(d).to_string(), "00.000000000000000001s");
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[must_use]
pub struct Dur {
    neg: bool,
    secs: u64,
    attos: u64,
}

const fn saturate(v: u128) -> u64 {
    if v > u64::MAX as u128 {
        u64::MAX
    } else {
        v as u64
    }
}

impl Dur {
    /// The zero duration.
    pub const ZERO: Self = Self { neg: false, secs: 0, attos: 0 };

    /// Builds a duration from its parts. Attoseconds beyond a full second carry into the seconds.
    pub const fn from_parts(neg: bool, secs: u64, attos: u64) -> Self {
        Self { neg, secs: secs.saturating_add(attos / ATTOS_PER_SEC), attos: attos % ATTOS_PER_SEC }
            .normalized()
    }

    /// Zero has no sign, so that equality agrees with what is printed.
    const fn normalized(mut self) -> Self {
        self.neg = self.neg && !self.is_zero();
        self
    }

    /// ```
    /// # use humanize_proper::{Dur, dur};
    /// assert_eq!(dur(Dur::from_secs(90)).to_string(), "01m30s");
    /// ```
    pub const fn from_secs(secs: u64) -> Self {
        Self { neg: false, secs, attos: 0 }
    }

    /// From whole milliseconds.
    pub const fn from_millis(millis: u64) -> Self {
        Self::from_subsec(millis as u128, 1_000)
    }

    /// From whole microseconds.
    pub const fn from_micros(micros: u64) -> Self {
        Self::from_subsec(micros as u128, 1_000_000)
    }

    /// From whole nanoseconds.
    pub const fn from_nanos(nanos: u64) -> Self {
        Self::from_subsec(nanos as u128, 1_000_000_000)
    }

    /// From whole picoseconds.
    pub const fn from_picos(picos: u128) -> Self {
        Self::from_subsec(picos, 1_000_000_000_000)
    }

    /// From whole femtoseconds.
    pub const fn from_femtos(femtos: u128) -> Self {
        Self::from_subsec(femtos, 1_000_000_000_000_000)
    }

    /// From whole attoseconds, the finest resolution this type holds.
    pub const fn from_attos(attos: u128) -> Self {
        Self::from_subsec(attos, ATTOS_PER_SEC as u128)
    }

    /// Shared body of the sub-second constructors. `per_sec` is how many units make up a second.
    const fn from_subsec(value: u128, per_sec: u128) -> Self {
        Self {
            neg: false,
            secs: saturate(value / per_sec),
            attos: (value % per_sec) as u64 * (ATTOS_PER_SEC / per_sec as u64),
        }
    }

    /// Converts from seconds given as a float. Sub-attosecond parts are truncated.
    pub fn from_secs_f64(secs: f64) -> Self {
        if secs.is_nan() {
            return Self::ZERO;
        }
        let neg = secs < 0.0;
        let a = if neg { -secs } else { secs };
        let whole = a as u64;
        let frac = (a - whole as f64) * ATTOS_PER_SEC as f64;
        Self { neg, secs: whole, attos: (frac as u64).min(ATTOS_PER_SEC - 1) }.normalized()
    }

    /// Flips the sign.
    ///
    /// ```
    /// # use humanize_proper::{Dur, dur};
    /// assert_eq!(dur(Dur::from_secs(90).neg()).to_string(), "-01m30s");
    /// ```
    pub const fn neg(mut self) -> Self {
        self.neg = !self.neg;
        self.normalized()
    }

    /// Whether this is the zero duration.
    pub const fn is_zero(&self) -> bool {
        self.secs == 0 && self.attos == 0
    }
}

/// Types accepted by the duration formatter.
///
/// Implemented for [`Dur`], [`core::time::Duration`], the integer types `u32`, `u64`, `i32`, `i64`
/// (seconds) and the float types `f32`, `f64` (seconds).
pub trait ToDuration {
    /// Converts to the crate's duration type.
    fn to_duration(self) -> Dur;
}

impl ToDuration for Dur {
    fn to_duration(self) -> Dur {
        self
    }
}

impl ToDuration for core::time::Duration {
    fn to_duration(self) -> Dur {
        Dur { neg: false, secs: self.as_secs(), attos: self.subsec_nanos() as u64 * 1_000_000_000 }
    }
}

macro_rules! impl_unsigned_secs {
    ($($t:ty),*) => {$(
        impl ToDuration for $t {
            fn to_duration(self) -> Dur {
                Dur::from_secs(self as u64)
            }
        }
    )*};
}

macro_rules! impl_signed_secs {
    ($($t:ty),*) => {$(
        impl ToDuration for $t {
            fn to_duration(self) -> Dur {
                Dur { neg: self < 0, secs: (self as i64).unsigned_abs(), attos: 0 }
            }
        }
    )*};
}

impl_unsigned_secs!(u32, u64);
impl_signed_secs!(i32, i64);

impl ToDuration for f64 {
    fn to_duration(self) -> Dur {
        Dur::from_secs_f64(self)
    }
}

impl ToDuration for f32 {
    fn to_duration(self) -> Dur {
        Dur::from_secs_f64(self as f64)
    }
}

/// The smallest unit a duration is printed down to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TimeUnit {
    /// Years, `y`.
    Year,
    /// Days, `d`.
    Day,
    /// Hours, `h`.
    Hour,
    /// Minutes, `m`.
    Min,
    /// Seconds, `s`, the default.
    Sec,
}

impl TimeUnit {
    const fn index(self) -> usize {
        match self {
            Self::Year => 0,
            Self::Day => 1,
            Self::Hour => 2,
            Self::Min => 3,
            Self::Sec => 4,
        }
    }
}

/// How leading zeros of a printed quantity are handled.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Zeros {
    /// Every unit is padded to its natural width: `d` to 3, `h`, `m` and `s` to 2 digits. Years are
    /// unbounded and never padded. This is the default.
    Pad,
    /// No padding at all, e.g. `5h1m5s`.
    TrimAll,
    /// Only the largest printed unit is unpadded, e.g. `5h01m05s`.
    TrimLargest,
}

/// Precision of the fractional part of the seconds field.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Frac {
    /// No fraction, the default.
    Secs,
    /// 3 digits, e.g. `05.123s`.
    Millis,
    /// 6 digits.
    Micros,
    /// 9 digits.
    Nanos,
    /// 12 digits.
    Picos,
    /// 15 digits.
    Femtos,
    /// 18 digits, the resolution of [`Dur`].
    Attos,
}

impl Frac {
    /// The number of fraction digits this precision prints.
    ///
    /// ```
    /// # use humanize_proper::Frac;
    /// assert_eq!(Frac::Nanos.digits(), 9);
    /// ```
    pub const fn digits(self) -> u8 {
        match self {
            Self::Secs => 0,
            Self::Millis => 3,
            Self::Micros => 6,
            Self::Nanos => 9,
            Self::Picos => 12,
            Self::Femtos => 15,
            Self::Attos => 18,
        }
    }
}

/// Configuration of the duration formatter.
///
/// The type is `Copy` and all builder methods are `const`, so configurations can be constants:
///
/// ```
/// use humanize_proper::{DurFmt, Zeros};
///
/// const CFG: DurFmt = DurFmt::new().zeros(Zeros::TrimAll);
/// assert_eq!(CFG.fmt(3665u64).to_string(), "1h1m5s");
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[must_use]
pub struct DurFmt {
    zeros: Zeros,
    frac_digits: u8,
    frac_sep: Option<&'static str>,
    sep: &'static str,
    smallest: TimeUnit,
    secs_per_year: u64,
}

impl Default for DurFmt {
    fn default() -> Self {
        Self::new()
    }
}

impl DurFmt {
    /// The default configuration, e.g. `32y007d_05h01m05s`.
    ///
    /// ```
    /// # use humanize_proper::DurFmt;
    /// assert_eq!(DurFmt::new().fmt(1_000_000_000u64).to_string(), "31y251d_13h21m28s");
    /// ```
    pub const fn new() -> Self {
        Self {
            zeros: Zeros::Pad,
            frac_digits: 0,
            frac_sep: None,
            sep: "_",
            smallest: TimeUnit::Sec,
            secs_per_year: SECS_PER_GREGORIAN_YEAR,
        }
    }

    /// Selects the zero padding mode.
    ///
    /// ```
    /// # use humanize_proper::{DurFmt, Zeros};
    /// assert_eq!(DurFmt::new().zeros(Zeros::TrimLargest).fmt(18065u64).to_string(), "5h01m05s");
    /// ```
    pub const fn zeros(mut self, zeros: Zeros) -> Self {
        self.zeros = zeros;
        self
    }

    /// Selects the precision of the fractional seconds.
    ///
    /// ```
    /// # use humanize_proper::{Dur, DurFmt, Frac};
    /// let d = Dur::from_micros(65_000_500);
    /// assert_eq!(DurFmt::new().frac(Frac::Millis).fmt(d).to_string(), "01m05.000s");
    /// assert_eq!(DurFmt::new().frac(Frac::Micros).fmt(d).to_string(), "01m05.000500s");
    /// ```
    pub const fn frac(self, frac: Frac) -> Self {
        self.frac_digits(frac.digits())
    }

    /// Sets an arbitrary number of fraction digits, clamped to `0..=18`.
    ///
    /// The fraction is truncated, not rounded, and is only printed when the smallest printed unit
    /// is [`TimeUnit::Sec`].
    ///
    /// ```
    /// # use humanize_proper::{Dur, DurFmt};
    /// assert_eq!(DurFmt::new().frac_digits(1).fmt(Dur::from_millis(5500)).to_string(), "05.5s");
    /// ```
    pub const fn frac_digits(mut self, digits: u8) -> Self {
        self.frac_digits = if digits > MAX_FRAC_DIGITS { MAX_FRAC_DIGITS } else { digits };
        self
    }

    /// Groups the fraction digits in threes for readability.
    ///
    /// ```
    /// # use humanize_proper::{Dur, DurFmt, Frac};
    /// let d = Dur::from_nanos(1_123_456_789);
    /// let cfg = DurFmt::new().frac(Frac::Nanos).frac_sep(Some("_"));
    /// assert_eq!(cfg.fmt(d).to_string(), "01.123_456_789s");
    /// ```
    pub const fn frac_sep(mut self, sep: Option<&'static str>) -> Self {
        self.frac_sep = sep;
        self
    }

    /// Sets the separator between the date group (`y`, `d`) and the time group (`h`, `m`, `s`).
    pub const fn sep(mut self, sep: &'static str) -> Self {
        self.sep = sep;
        self
    }

    /// Drops the separator between the date and the time group.
    ///
    /// ```
    /// # use humanize_proper::DurFmt;
    /// assert_eq!(DurFmt::new().no_sep().fmt(86400u64).to_string(), "001d00h00m00s");
    /// ```
    pub const fn no_sep(self) -> Self {
        self.sep("")
    }

    /// Sets the smallest printed unit. Smaller parts are truncated.
    ///
    /// ```
    /// # use humanize_proper::{DurFmt, TimeUnit};
    /// assert_eq!(DurFmt::new().smallest(TimeUnit::Min).fmt(3665u64).to_string(), "01h01m");
    /// ```
    pub const fn smallest(mut self, unit: TimeUnit) -> Self {
        self.smallest = unit;
        self
    }

    /// Sets the length of a year in seconds, clamped to at least 1.
    ///
    /// The default is `31_556_952`, which is 365.2425 days, the mean Gregorian year.
    pub const fn secs_per_year(mut self, secs: u64) -> Self {
        self.secs_per_year = if secs == 0 { 1 } else { secs };
        self
    }

    /// Binds this configuration to a duration. The result implements [`Display`].
    pub fn fmt<D: ToDuration>(self, duration: D) -> FmtDur {
        FmtDur { cfg: self, dur: duration.to_duration() }
    }
}

/// A duration bound to a [`DurFmt`], ready to be displayed.
#[derive(Clone, Copy, Debug)]
#[must_use]
pub struct FmtDur {
    cfg: DurFmt,
    dur: Dur,
}

impl Display for FmtDur {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        pad_rendered(f, |out| render(&self.cfg, self.dur, out))
    }
}

/// Splits the seconds into years, days, hours, minutes and seconds.
fn split(cfg: &DurFmt, secs: u64) -> [u64; UNIT_COUNT] {
    let mut values = [0u64; UNIT_COUNT];
    let mut rest = secs;
    for (i, per_unit) in core::iter::once(cfg.secs_per_year).chain(SECS_PER_UNIT).enumerate() {
        values[i] = rest / per_unit;
        rest %= per_unit;
    }
    values
}

fn render(cfg: &DurFmt, d: Dur, out: &mut dyn Write) -> fmt::Result {
    let values = split(cfg, d.secs);
    let last = cfg.smallest.index();
    let first = values[..=last].iter().position(|value| *value != 0).unwrap_or(last);

    if d.neg && !d.is_zero() {
        out.write_char('-')?;
    }
    for i in first..=last {
        if i == TIME_GROUP && first < TIME_GROUP {
            out.write_str(cfg.sep)?;
        }
        let width = match cfg.zeros {
            Zeros::TrimAll => 1,
            Zeros::TrimLargest if i == first => 1,
            _ => WIDTHS[i],
        };
        write_zero_padded(out, values[i] as u128, width)?;
        if i == SEC {
            write_frac(cfg, d.attos, out)?;
        }
        out.write_char(LETTERS[i])?;
    }
    Ok(())
}

fn write_frac(cfg: &DurFmt, attos: u64, out: &mut dyn Write) -> fmt::Result {
    let digits = cfg.frac_digits as usize;
    if digits == 0 {
        return Ok(());
    }
    out.write_char('.')?;
    let value = attos as u128 / POW10[MAX_FRAC_DIGITS as usize - digits];
    let mut buf = StackStr::<{ MAX_FRAC_DIGITS as usize }>::new();
    write_zero_padded(&mut buf, value, digits)?;
    match cfg.frac_sep {
        None => out.write_str(buf.as_str()),
        Some(sep) => {
            for (i, group) in buf.as_str().as_bytes().chunks(3).enumerate() {
                if i > 0 {
                    out.write_str(sep)?;
                }
                out.write_str(core::str::from_utf8(group).unwrap_or(""))?;
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    extern crate std;
    use std::string::ToString;

    fn f(secs: u64) -> std::string::String {
        DurFmt::new().fmt(secs).to_string()
    }

    #[test]
    fn default_style() {
        assert_eq!(f(1_000_000_000), "31y251d_13h21m28s");
        assert_eq!(f(0), "00s");
        assert_eq!(f(5), "05s");
        assert_eq!(f(90), "01m30s");
        assert_eq!(f(3665), "01h01m05s");
        assert_eq!(f(86_400), "001d_00h00m00s");
        assert_eq!(f(SECS_PER_GREGORIAN_YEAR), "1y000d_00h00m00s");
        assert_eq!(f(SECS_PER_GREGORIAN_YEAR - 1), "365d_05h49m11s");
    }

    #[test]
    fn unit_boundaries() {
        assert_eq!(f(59), "59s");
        assert_eq!(f(60), "01m00s");
        assert_eq!(f(3_599), "59m59s");
        assert_eq!(f(3_600), "01h00m00s");
        assert_eq!(f(86_399), "23h59m59s");
    }

    #[test]
    fn zero_modes() {
        let d = 5 * 3600 + 65;
        assert_eq!(DurFmt::new().zeros(Zeros::Pad).fmt(d as u64).to_string(), "05h01m05s");
        assert_eq!(DurFmt::new().zeros(Zeros::TrimAll).fmt(d as u64).to_string(), "5h1m5s");
        assert_eq!(DurFmt::new().zeros(Zeros::TrimLargest).fmt(d as u64).to_string(), "5h01m05s");
        assert_eq!(DurFmt::new().zeros(Zeros::TrimAll).fmt(86_400u64).to_string(), "1d_0h0m0s");
        assert_eq!(DurFmt::new().zeros(Zeros::TrimLargest).fmt(0u64).to_string(), "0s");
    }

    #[test]
    fn all_frac_precisions() {
        let d = Dur::from_parts(false, 65, 123_456_789_012_345_678);
        let cases = [
            (Frac::Secs, "01m05s"),
            (Frac::Millis, "01m05.123s"),
            (Frac::Micros, "01m05.123456s"),
            (Frac::Nanos, "01m05.123456789s"),
            (Frac::Picos, "01m05.123456789012s"),
            (Frac::Femtos, "01m05.123456789012345s"),
            (Frac::Attos, "01m05.123456789012345678s"),
        ];
        for (frac, expected) in cases {
            assert_eq!(DurFmt::new().frac(frac).fmt(d).to_string(), expected, "{frac:?}");
        }
    }

    #[test]
    fn frac_constructors_are_exact() {
        assert_eq!(Dur::from_millis(1_500), Dur::from_parts(false, 1, 500_000_000_000_000_000));
        assert_eq!(Dur::from_micros(1_500_000), Dur::from_millis(1_500));
        assert_eq!(Dur::from_nanos(1_500_000_000), Dur::from_millis(1_500));
        assert_eq!(Dur::from_picos(1_500_000_000_000), Dur::from_millis(1_500));
        assert_eq!(Dur::from_femtos(1_500_000_000_000_000), Dur::from_millis(1_500));
        assert_eq!(Dur::from_attos(1_500_000_000_000_000_000), Dur::from_millis(1_500));
        assert_eq!(Dur::from_secs_f64(1.5), Dur::from_millis(1_500));
    }

    #[test]
    fn frac_is_truncated_not_rounded() {
        let d = Dur::from_millis(1_999);
        assert_eq!(DurFmt::new().frac_digits(1).fmt(d).to_string(), "01.9s");
        assert_eq!(DurFmt::new().fmt(d).to_string(), "01s");
    }

    #[test]
    fn frac_is_ignored_above_seconds() {
        let cfg = DurFmt::new().frac(Frac::Millis).smallest(TimeUnit::Min);
        assert_eq!(cfg.fmt(Dur::from_millis(90_500)).to_string(), "01m");
    }

    #[test]
    fn smallest_unit_truncates() {
        assert_eq!(DurFmt::new().smallest(TimeUnit::Min).fmt(3_665u64).to_string(), "01h01m");
        assert_eq!(DurFmt::new().smallest(TimeUnit::Hour).fmt(3_665u64).to_string(), "01h");
        assert_eq!(DurFmt::new().smallest(TimeUnit::Day).fmt(3_665u64).to_string(), "000d");
        assert_eq!(DurFmt::new().smallest(TimeUnit::Year).fmt(3_665u64).to_string(), "0y");
    }

    #[test]
    fn zero_has_no_sign() {
        assert_eq!(Dur::ZERO.neg(), Dur::ZERO);
        assert_eq!(Dur::from_parts(true, 0, 0), Dur::ZERO);
        assert_eq!(Dur::from_secs_f64(-0.0), Dur::ZERO);
        assert_eq!(DurFmt::new().fmt(Dur::from_parts(true, 0, 0)).to_string(), "00s");
    }

    #[test]
    fn signs() {
        assert_eq!(DurFmt::new().fmt(-90i64).to_string(), "-01m30s");
        assert_eq!(DurFmt::new().fmt(Dur::ZERO.neg()).to_string(), "00s");
        assert_eq!(DurFmt::new().fmt(-1.5f64).to_string(), "-01s");
    }

    #[test]
    fn separator_is_configurable() {
        assert_eq!(DurFmt::new().no_sep().fmt(86_400u64).to_string(), "001d00h00m00s");
        assert_eq!(DurFmt::new().sep(" ").fmt(86_400u64).to_string(), "001d 00h00m00s");
        assert_eq!(DurFmt::new().sep(" ").fmt(3_600u64).to_string(), "01h00m00s");
    }

    #[test]
    fn year_length_is_configurable() {
        let cfg = DurFmt::new().secs_per_year(365 * 86_400);
        assert_eq!(cfg.fmt(365 * 86_400u64).to_string(), "1y000d_00h00m00s");
        assert_eq!(cfg.fmt(364 * 86_400u64).to_string(), "364d_00h00m00s");
    }

    #[test]
    fn display_padding_is_honored() {
        assert_eq!(std::format!("{:>10}", DurFmt::new().fmt(90u64)), "    01m30s");
    }

    #[test]
    fn std_duration_is_accepted() {
        let d = core::time::Duration::from_millis(90_500);
        assert_eq!(DurFmt::new().frac(Frac::Millis).fmt(d).to_string(), "01m30.500s");
    }
}