Skip to content

Support for months when serializing Duration to ISO8601 string #100

Description

@ThorodanBrom

Hello,

Is it possible to get support for months is not present in the Duration to ISO8601 string here. The same scaling logic of 30 days in a month when converting the string to Duration could be used.

speedate/src/duration.rs

Lines 61 to 101 in 6fafc2c

impl fmt::Display for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.positive {
write!(f, "-")?;
}
write!(f, "P")?;
if self.day != 0 {
let year = self.day / 365;
if year != 0 {
write!(f, "{year}Y")?;
}
let day = self.day % 365;
if day != 0 {
write!(f, "{day}D")?;
}
}
if self.second != 0 || self.microsecond != 0 {
let (hour, minute, sec) = self.to_hms();
write!(f, "T")?;
if hour != 0 {
write!(f, "{hour}H")?;
}
if minute != 0 {
write!(f, "{minute}M")?;
}
if sec != 0 || self.microsecond != 0 {
write!(f, "{sec}")?;
if self.microsecond != 0 {
let mut s = arrayvec::ArrayString::<6>::new();
write!(s, "{:06}", self.microsecond)?;
write!(f, ".{}", s.trim_end_matches('0'))?;
}
write!(f, "S")?;
}
}
if self.second == 0 && self.microsecond == 0 && self.day == 0 {
write!(f, "T0S")?;
}
Ok(())
}
}

Or is there a specific reason that this was never implemented?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions