Skip to content

Duration fmt::Debug impl doesn't honor alignment #88059

Closed
@hawkw

Description

@hawkw

It appears that the fmt::Debug implementation for std::time::Duration ignores alignment/width specifiers.

For example, see this playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d5daccbe4b2e510f4dcee8d85e421b47

I tried this code:

Formatting a Duration with alignment/width specifiers (with a f64 for comparison).

fn main() {
    use std::time::Duration;
    let some_duration = Duration::from_millis(100) + Duration::from_micros(555);
    println!(" left aligned `Duration` = {:>20?} foo", some_duration);
    println!(" left aligned `f64`      = {:>20?} foo", 100.555f64);
    println!("right aligned `Duration` = {:<20?} foo", some_duration);
    println!("right aligned `f64`      = {:<20?} foo", 100.555f64)
}

I expected to see this happen:

The durations should be padded to fill the specified width, and aligned based on the alignment specifier (identically to the f64 value. I would expect to see output that looks like this:

 left aligned `Duration` =            100.555ms foo
 left aligned `f64`      =              100.555 foo
right aligned `Duration` = 100.555ms            foo
right aligned `f64`      = 100.555              foo

Instead, this happened:

The Durations are not padded to the specified width, and are not aligned based on the format specifier. The output looks like this:

 left aligned `Duration` = 100.555ms foo
 left aligned `f64`      =              100.555 foo
right aligned `Duration` = 100.555ms foo
right aligned `f64`      = 100.555              foo

Meta

rustc --version --verbose:
On my machine, the version is:

rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1

In the playground this also occurs on stable 1.54.0, and on 1.56.0-nightly (2021-08-14 8007b506ac5da629f223). Looking at the git blame, though, it looks like Duration's Debug impl has had this behavior since 9eeb13f (prior to that commit, it simply derived Debug).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions