Ensure we check for microseconds in Time.compare

This commit is contained in:
José Valim
2016-10-28 21:15:31 +02:00
parent 46fa15b688
commit d715f8dca8
2 changed files with 11 additions and 1 deletions
+6 -1
View File
@@ -610,10 +610,13 @@ defmodule Time do
iex> Time.compare(~T[16:04:16], ~T[16:04:28])
:lt
iex> Time.compare(~T[16:04:16.01], ~T[16:04:16.001])
:gt
"""
@spec compare(Time.t, Time.t) :: :lt | :eq | :gt
def compare(%Time{} = time1, %Time{} = time2) do
case {to_erl(time1), to_erl(time2)} do
case {to_tuple(time1), to_tuple(time2)} do
{first, second} when first > second -> :gt
{first, second} when first < second -> :lt
_ -> :eq
@@ -1086,6 +1089,8 @@ defmodule NaiveDateTime do
iex> NaiveDateTime.compare(~N[2016-04-16 13:30:15], ~N[2016-04-28 16:19:25])
:lt
iex> NaiveDateTime.compare(~N[2016-04-16 13:30:15.1], ~N[2016-04-16 13:30:15.01])
:gt
"""
@spec compare(NaiveDateTime.t, NaiveDateTime.t) :: :lt | :eq | :gt
def compare(%NaiveDateTime{} = naive_datetime1, %NaiveDateTime{} = naive_datetime2) do
+5
View File
@@ -44,9 +44,11 @@ defmodule TimeTest do
end
test "compare/2" do
time0 = ~T[01:01:01.0]
time1 = ~T[01:01:01.005]
time2 = ~T[01:01:01.0050]
time3 = ~T[23:01:01.0050]
assert Time.compare(time0, time1) == :lt
assert Time.compare(time1, time1) == :eq
assert Time.compare(time1, time2) == :eq
assert Time.compare(time1, time3) == :lt
@@ -76,9 +78,12 @@ defmodule NaiveDateTimeTest do
test "compare/2" do
ndt1 = ~N[2000-04-16 13:30:15.0049]
ndt2 = ~N[2000-04-16 13:30:15.0050]
ndt3 = ~N[2001-04-16 13:30:15.0050]
assert NaiveDateTime.compare(ndt1, ndt1) == :eq
assert NaiveDateTime.compare(ndt1, ndt2) == :lt
assert NaiveDateTime.compare(ndt2, ndt1) == :gt
assert NaiveDateTime.compare(ndt3, ndt1) == :gt
assert NaiveDateTime.compare(ndt3, ndt2) == :gt
end
end