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

Implement Duration.to_iso8601/1 and Duration.from_iso8601/1 #13599

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions lib/elixir/lib/calendar/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,77 @@ defmodule Duration do
microsecond: {-ms, p}
}
end

@doc """
Converts the given duration to ISO 8601:TODO format.

## Examples

iex> Duration.new!([]) |> Duration.to_iso8601()
"PT0S"
iex> Duration.new!(year: 3) |> Duration.to_iso8601()
"P3Y"
iex> Duration.new!(year: 3, day: 6, minute: 9) |> Duration.to_iso8601()
"P3Y6DT9M"
iex> Duration.new!(second: 30) |> Duration.to_iso8601()
"PT30S"
iex> Duration.new!(hour: 2, microsecond: {1000, 6}) |> Duration.to_iso8601()
"PT2H0.001S"
Copy link
Member

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.

"""

@spec to_iso8601(t) :: String.t()
def to_iso8601(%Duration{
year: 0,
month: 0,
week: 0,
day: 0,
hour: 0,
minute: 0,
second: 0,
microsecond: {0, 0}
tanguilp marked this conversation as resolved.
Show resolved Hide resolved
}) do
"PT0S"
end

def to_iso8601(%Duration{} = d) do
"P#{to_iso8601_left_part(d)}#{to_iso8601_right_part(d)}"
end

defp to_iso8601_left_part(d) do
year = unless d.year == 0, do: "#{d.year}Y"
month = unless d.month == 0, do: "#{d.month}M"
week = unless d.week == 0, do: "#{d.week}W"
day = unless d.day == 0, do: "#{d.day}D"

"#{year}#{month}#{week}#{day}"
end

defp to_iso8601_right_part(%Duration{hour: 0, minute: 0, second: 0, microsecond: {0, 0}}) do
Copy link
Member

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.

""
end

defp to_iso8601_right_part(d) do
hour = unless d.hour == 0, do: "#{d.hour}H"
minute = unless d.minute == 0, do: "#{d.minute}M"

second =
case d do
%Duration{second: 0, microsecond: {0, 0}} ->
""

%Duration{microsecond: {0, _}} ->
"#{d.second}S"

%Duration{microsecond: {microsecond, _}} ->
microsecond =
microsecond
|> to_string()
|> String.pad_leading(6, "0")
|> String.trim_trailing("0")

"#{d.second}.#{microsecond}S"
end

"T#{hour}#{minute}#{second}"
end
end