Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ISO8601 durations in TimeSpan #28862

Closed
jeremyVignelles opened this issue Mar 4, 2019 · 7 comments
Closed

Support for ISO8601 durations in TimeSpan #28862

jeremyVignelles opened this issue Mar 4, 2019 · 7 comments
Labels
area-System.DateTime enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@jeremyVignelles
Copy link

Rationale

As far as I know, there is currently no support for ISO8601 durations for TimeSpan.Parse and TimeSpan.ToString.

Per wikipedia's definition , it would allow to write durations like P10M1D for a duration of ten month and one day, P1DT2H10M for 1 day, 2 hours and 10 minutes, or PT0,5M for 30 seconds.

Proposition

  • Make TimeSpan.Parse() accept arguments like the values quoted above
  • Create a new ToString() format specifier to output values like P1DT2H or, if it is not possible for compatibility reasons, add a new .ToISOString() method.

Use cases

@Clockwork-Muse
Copy link
Contributor

Implementation note: we'd have to adopt a non-quite-conforming parser/formatter (similar to Java's Duration.parse(...), because ISO8601 durations are positive only, and TimeSpan allows for negative values

@bartonjs
Copy link
Member

bartonjs commented Mar 6, 2019

TimeSpan also doesn't know what "months" are, since "2 months and a day" is anywhere from 60 days (Jan/Feb or Feb/Mar in a non-leap-year) to 63 (Dec/Jan); which is a pretty large range for a single-value with a 100ns precision.

P63D would be possible input/output; if the negative problem is ignored.

@khellang
Copy link
Member

This would be a great first step for https://github.com/dotnet/corefx/issues/38641.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the Future milestone Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@joperezr joperezr removed the untriaged New issue has not been triaged by the area owner label Jul 2, 2020
@lomholdt
Copy link

lomholdt commented Jun 6, 2021

Is there any timeline on this suggestion, or is it currently only a thing on the backlog?
Also, does anyone have a recommended/good way to handle this until support has been added natively?
I have searched but only found some relatively unused libs.

@eiriktsarpalis
Copy link
Member

Related to #29932. In the context of System.Text.Json we have decided that ISO8601 durations (which represent duration relative to a point in time) are somewhat incompatible with TimeSpan (which represents an absolute timespan counted in ticks).

@tthiery
Copy link

tthiery commented Oct 9, 2022

Also interested. I understand that it conflicts with TimeSpan but there are tons of use cases to express duratoins in something else than days (and hours/minutes). Especially since DateTime itself has suitable AddMonths/AddYears/... which are adhering the difference between "P1M" and "P30D" (checked at least for months). I have two cases, one at home and one at work in completely different fields.

Would not be there a case for a Iso8601TimeSpan?

.NET 8 maybe? 😉 I know it is painful since TimeSpan is so ... fundamental ... but so ISO8601 compliance ;)

FYI: @terrajobst

@eiriktsarpalis
Copy link
Member

Would not be there a case for a Iso8601TimeSpan?

I would recommend filing a separate API proposal for a potential System.Duration type. In the meantime, I'm going to close this issue as we made the definitive decision back in .NET 6 to not support the ISO 8601 duration format for System.TimeSpan.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.DateTime enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests