9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/silverball/date_time.rb', line 9
def timespan_in_words(seconds, unit: nil, round: 2)
seconds = seconds.to_f unless unit.nil?
if (seconds >= SECONDS_PER_DAY && unit.nil?) || unit == :days
days = seconds / SECONDS_PER_DAY
seconds -= days * SECONDS_PER_DAY
if unit == :days
if days == days.to_i
days = days.to_i
else
days - days.round(round)
end
end
else
days = 0
end
if (seconds >= SECONDS_PER_HOUR && unit.nil?) || unit == :hours
hours = seconds / SECONDS_PER_HOUR
seconds -= hours * SECONDS_PER_HOUR
if unit == :hours
if hours == hours.to_i
hours = hours.to_i
else
hours = hours.round(round)
end
end
else
hours = 0
end
if (seconds >= SECONDS_PER_MINUTE && unit.nil?) || unit == :minutes
minutes = seconds / SECONDS_PER_MINUTE
seconds -= minutes * SECONDS_PER_MINUTE
if unit == :minutes
if minutes == minutes.to_i
minutes = minutes.to_i
else
minutes = minutes.round(round)
end
end
else
minutes = 0
end
if unit == :seconds
if seconds == seconds.to_i
seconds = seconds.to_i
else
seconds = seconds.round(round)
end
end
if unit == :days
"#{ days }d"
elsif unit == :hours
"#{ hours }h"
elsif unit == :minutes
"#{ minutes }m"
else
out = []
out << "#{ days }d" if days > 0
out << "#{ hours }h" if hours > 0
out << "#{ minutes }m" if minutes > 0
out << "#{ seconds }s" if seconds > 0 || (seconds == 0 && minutes == 0 && hours == 0 && days == 0)
out.join(' ')
end
end
|