microsoft / typespec

https://typespec.io/
MIT License
4.36k stars 202 forks source link

[Bug]: Encoding ISO8601 Duration to Numeric Values Causes Ambiguity #3733

Open joheredi opened 3 months ago

joheredi commented 3 months ago

Describe the bug

Encoding ISO8601 duration to seconds or any other numeric value can be problematic when the duration includes non-zero values for days, weeks, months, or years without additional metadata. For example, encoding P1Y (1 year) to seconds requires a reference point to accurately determine the number of seconds. Depending on the reference point, a year can have 365 or 366 days. Similar issues arise with months, weeks, and days.

Suggested Solutions:

Additional Information: This issue affects any service that relies on precise duration encoding and can lead to significant inaccuracies in time-based calculations.

Reproduction

This is a simple spec

Depending on the client implementation the service may receive different values for the same ISO8601 string P1Y

Checklist

timotheeguerin commented 3 months ago

Technically the duration in tyepspec is not an ISO8601 duration it is just the concept of a duration.

Seeing also what the new JS api does for this is intresting.

image

I am assuming what we are saying here is that if you serialize this duraiton as seconds then you cannot represent it using those y, m w

joheredi commented 3 months ago

Yeah you can't use year, month, week or day without a reference point. I think this is likely to surface ambiguity, the actual encoded value will depend on how emitters interpret and encode durations to seconds.