Skip to content

Commit 80b803f

Browse files
committed
Added overload to distance_of_time_in_words for Time::Span. Fixes #1859
1 parent ca4ee3e commit 80b803f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

spec/lucky/time_helpers_spec.cr

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ describe Lucky::TimeHelpers do
3030
view.distance_of_time_in_words(from_time, from_time + 2.years).should eq "over 2 years"
3131
view.distance_of_time_in_words(from_time, from_time + 10.years).should eq "almost 10 years"
3232
end
33+
34+
it "takes a Time::Span" do
35+
span = 4.minutes
36+
view.distance_of_time_in_words(span).should eq "4 minutes"
37+
end
3338
end
3439

3540
describe "time_ago_in_words" do

src/lucky/page_helpers/time_helpers.cr

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ module Lucky::TimeHelpers
1616
# # => "almost 42 years"
1717
# ```
1818
def distance_of_time_in_words(from : Time, to : Time) : String
19-
minutes = (to - from).minutes
20-
seconds = (to - from).seconds
21-
hours = (to - from).hours
22-
days = (to - from).days
19+
distance_of_time_in_words(to - from)
20+
end
21+
22+
# :ditto:
23+
def distance_of_time_in_words(span : Time::Span) : String
24+
minutes = span.minutes
25+
seconds = span.seconds
26+
hours = span.hours
27+
days = span.days
2328

2429
return distance_in_days(days) if days != 0
2530
return distance_in_hours(hours, minutes) if hours != 0

0 commit comments

Comments
 (0)