Skip to content

date: strftime strips leading zeros when requested width is narrower than default (%02j on day 1) #11660

@sylvestre

Description

@sylvestre

Summary

%j (day of year) has a default width of 3. When the user requests a narrower width via %02j, GNU pads the value to the requested width (01 for day 1), while uutils strips leading zeros entirely and emits 1.

The same family of bugs likely affects other wide-default specifiers (%Y, %G).

Found by a fuzz_date run.

Reproduction

$ LC_ALL=C TZ=UTC /usr/bin/date -d '2024-01-01' '+%02j'
01

$ LC_ALL=C TZ=UTC target/debug/coreutils date -d '2024-01-01' '+%02j'
1

$ LC_ALL=C TZ=UTC /usr/bin/date -d '2024-01-09' '+%02j'
09

$ LC_ALL=C TZ=UTC target/debug/coreutils date -d '2024-01-09' '+%02j'
9

When the natural representation is already >= the requested width, both agree.

Test

#[test]
fn test_date_strftime_narrow_width_on_wide_default() {
    new_ucmd!()
        .env("LC_ALL", "C")
        .env("TZ", "UTC")
        .arg("-d")
        .arg("2024-01-01")
        .arg("+%02j")
        .succeeds()
        .stdout_is("01\n");
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions