Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions lib/elixir/lib/calendar/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,12 @@ defmodule Time do
Calendar.microsecond(),
Calendar.calendar()
) :: t
def from_seconds_after_midnight(seconds, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
when is_integer(seconds) do
def from_seconds_after_midnight(
seconds,
{microsecond, precision} \\ {0, 0},
calendar \\ Calendar.ISO
)
when is_integer(seconds) and microsecond in 0..999_999 and precision in 0..6 do
seconds_in_day = Integer.mod(seconds, @seconds_per_day)

{hour, minute, second, {_, _}} =
Expand All @@ -479,7 +483,7 @@ defmodule Time do
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
microsecond: {microsecond, precision}
}
end

Expand Down
12 changes: 12 additions & 0 deletions lib/elixir/test/elixir/calendar/time_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,16 @@ defmodule TimeTest do
~r/unsupported value 1 for :microsecond. Expected a tuple \{ms, precision\}/,
fn -> Time.shift(time, microsecond: 1) end
end

test "from_seconds_after_midnight/3" do
assert Time.from_seconds_after_midnight(86399, {999_999, 6}) == ~T[23:59:59.999999]

assert_raise FunctionClauseError, fn ->
Time.from_seconds_after_midnight(0, {1_000_000, 6})
end

assert_raise FunctionClauseError, fn ->
Time.from_seconds_after_midnight(0, {999_999, 7})
end
end
end
Loading