Skip to content

Commit e0bdd3e

Browse files
author
José Valim
committed
Inline comparisons on same calendar for date and time
1 parent b0218fa commit e0bdd3e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/elixir/lib/calendar/date.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ defmodule Date do
354354
355355
"""
356356
@spec compare(Calendar.date, Calendar.date) :: :lt | :eq | :gt
357+
def compare(%{calendar: calendar, year: year1, month: month1, day: day1},
358+
%{calendar: calendar, year: year2, month: month2, day: day2}) do
359+
case {{year1, month1, day1}, {year2, month2, day2}} do
360+
{first, second} when first > second -> :gt
361+
{first, second} when first < second -> :lt
362+
_ -> :eq
363+
end
364+
end
357365
def compare(date1, date2) do
358366
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
359367
case {to_rata_die(date1), to_rata_die(date2)} do

lib/elixir/lib/calendar/time.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ defmodule Time do
338338
339339
"""
340340
@spec compare(Calendar.time, Calendar.time) :: :lt | :eq | :gt
341+
def compare(%{calendar: calendar, hour: hour1, minute: minute1, second: second1, microsecond: {microsecond1, _}},
342+
%{calendar: calendar, hour: hour2, minute: minute2, second: second2, microsecond: {microsecond2, _}}) do
343+
case {{hour1, minute1, second1, microsecond1}, {hour2, minute2, second2, microsecond2}} do
344+
{first, second} when first > second -> :gt
345+
{first, second} when first < second -> :lt
346+
_ -> :eq
347+
end
348+
end
349+
341350
def compare(time1, time2) do
342351
{parts1, ppd1} = to_day_fraction(time1)
343352
{parts2, ppd2} = to_day_fraction(time2)

0 commit comments

Comments
 (0)