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

Fix dialyzer errors when using from_* functions #748

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/time/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ defmodule Timex.Duration do
@doc """
Converts an integer value representing microseconds to a Duration
"""
@spec from_microseconds(integer) :: __MODULE__.t()
@spec from_microseconds(integer | float) :: __MODULE__.t()
def from_microseconds(us) when is_integer(us) do
mega = div(us, 1_000_000_000_000)
sec = div(rem(us, 1_000_000_000_000), 1_000_000)
Expand Down
12 changes: 11 additions & 1 deletion test/duration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule DurationTests do
assert Duration.to_seconds(13, :hours) == 13 * 3600
end

test "from_*" do
test "from_* with integers" do
assert Duration.to_erl(Duration.from_microseconds(1)) == {0, 0, 1}
assert Duration.to_erl(Duration.from_milliseconds(1)) == {0, 0, 1000}
assert Duration.to_erl(Duration.from_seconds(1)) == {0, 1, 0}
Expand All @@ -107,6 +107,16 @@ defmodule DurationTests do
assert Duration.to_erl(Duration.from_weeks(1)) == {0, 604_800, 0}
end

test "from_* with floats" do
assert Duration.to_erl(Duration.from_microseconds(1.5)) == {0, 0, 1}
assert Duration.to_erl(Duration.from_milliseconds(1.5)) == {0, 0, 1500}
assert Duration.to_erl(Duration.from_seconds(1.5)) == {0, 1, 500_000}
assert Duration.to_erl(Duration.from_minutes(1.5)) == {0, 90, 0}
assert Duration.to_erl(Duration.from_hours(1.5)) == {0, 5400, 0}
assert Duration.to_erl(Duration.from_days(1.5)) == {0, 129_600, 0}
assert Duration.to_erl(Duration.from_weeks(1.5)) == {0, 907_200, 0}
end

test "elapsed" do
previous_time = Duration.from_erl({1362, 568_902, 363_960})
now = Duration.from_erl({1362, 568_903, 363_960})
Expand Down