Skip to content

Time can't be roundtrip formatted/parsed using ISO 8601 format #759

@Anfid

Description

@Anfid

When using ISO 8601 formatter, time is formatted having 'T' prefix, for example "T00:23:23.078374000". ISO 8691 parser, however, only allows T if the date component is present. I was able to trace it down to this bit of code:

input = ascii_char::<b'T'>(input)

Unfortunately I don't have an access to the full original ISO spec, but at least the comment in the linked function and the Wikipedia article on ISO both state that T component CAN be ommitted (as opposed to MUST) in non-ambiguous scenarios. So from my understanding, T prefix should be allowed for times even without dates, at least with later standard iterations.

Example code to reproduce:

main.rs

fn main() {
    let now = time::OffsetDateTime::now_utc();
    let now_time = now.time();
    let now_time_str = now_time
        .format(&time::format_description::well_known::Iso8601::TIME)
        .unwrap();
    let parsed = time::Time::parse(
        &now_time_str,
        &time::format_description::well_known::Iso8601::TIME,
    )
    .unwrap(); // Currently fails here
    assert_eq!(now_time, parsed);
}

Cargo.toml

[package]
name = "timetest"
version = "0.1.0"
edition = "2024"

[dependencies]
time = { version = "0.3.44", features = ["formatting", "parsing"] }

The fix seems straightforward enough, so I could probably try and work on it myself. Let me know if I should.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions