File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -299,6 +299,34 @@ defmodule Duration do
299
299
}
300
300
end
301
301
302
+ @ doc """
303
+ Compares two durations.
304
+
305
+ Returns `:eq` if the durations are equal, `:lt` if the first duration is less than the second, and `:gt` if the first
306
+ duration is greater than the second.
307
+
308
+ ## Examples
309
+
310
+ iex> Duration.compare(Duration.new!(second: 10), Duration.new!(second: 20))
311
+ :lt
312
+ iex> Duration.compare(Duration.new!(second: 20), Duration.new!(second: 10))
313
+ :gt
314
+ iex> Duration.compare(Duration.new!(second: 10), Duration.new!(second: 10))
315
+ :eq
316
+ """
317
+ @ doc since: "1.19.0"
318
+ @ spec compare ( t , t ) :: :eq | :lt | :gt
319
+ def compare ( % Duration { } = d1 , % Duration { } = d2 ) do
320
+ d1_timeout = Kernel . to_timeout ( d1 )
321
+ d2_timeout = Kernel . to_timeout ( d2 )
322
+
323
+ cond do
324
+ d1_timeout == d2_timeout -> :eq
325
+ d1_timeout < d2_timeout -> :lt
326
+ d1_timeout > d2_timeout -> :gt
327
+ end
328
+ end
329
+
302
330
@ doc """
303
331
Parses an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) formatted duration string to a `Duration` struct.
304
332
You can’t perform that action at this time.
0 commit comments