-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Implement Duration.to_iso8601/1
and Duration.from_iso8601/1
#13599
Implement Duration.to_iso8601/1
and Duration.from_iso8601/1
#13599
Conversation
We discussed this in the past but, opposite to other ISO formats, this one is quite rare in practice and we would need to make some extensions. Do you have a use case for it? |
I think a great use case is parsing ISO durations in a custom Absinthe scalar, transforming a Duration back and forth to a string, so it can be used on a GraphQl field and read by front-end clients... |
iex> Duration.new!(second: 30) |> Duration.to_iso8601() | ||
"PT30S" | ||
iex> Duration.new!(hour: 2, microsecond: {1000, 6}) |> Duration.to_iso8601() | ||
"PT2H0.001S" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need tests for negative durations.
"#{year}#{month}#{week}#{day}" | ||
end | ||
|
||
defp to_iso8601_right_part(%Duration{hour: 0, minute: 0, second: 0, microsecond: {0, 0}}) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should generally match on {0, _}
for microseconds.
Sounds good. I have reopened a PR for from_iso8501: #13473 so we can tackle these problems individually. |
Had it on a REST API not long ago for flight duration. My bad, I didn't intend to publish this draft on this repo 🙈 Closing it for now, I'll follow the work on the reopened PR and participate if useful. #13473 implements |
@tanguilp it is ok to have this PR introducing to_iso8601 and the other introduces from_iso8601. :) |
No description provided.