-
-
Save blarosen95/613c3e7e7dc0ede351f2716dbe384299 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_duration(n) | |
return "now" if n == 0 | |
return "#{n} second" if n == 1 | |
return "#{n} seconds" if n.between?(2, 59) | |
minutes, seconds = n.divmod(60) | |
hours, minutes = minutes.divmod(60) | |
days, hours = hours.divmod(24) | |
years, days = days.divmod (365) | |
# if [years, days, hours, minutes, seconds].each {|value| value > 1} | |
# str_out = "%d years, %d days, %d hours, %d minutes and %d seconds" % [years, days, hours, minutes, seconds] | |
# end | |
tri_years, tri_days, tri_hours, tri_minutes, tri_seconds = 0 | |
case years | |
when years == 0 | |
tri_years = 0 # the unit, years/year would then not be used for output | |
when years == 1 | |
tri_years = 1 # the unit, year would be used for output | |
else | |
tri_years = 2 # the unit, years would be used for output | |
end | |
case days | |
when days == 0 | |
tri_days = 0 | |
when days == 1 | |
tri_days = 1 | |
else | |
tri_days = 2 | |
end | |
case hours | |
when hours == 0 | |
tri_hours = 0 | |
when hours == 1 | |
tri_hours = 1 | |
else | |
tri_hours = 2 | |
end | |
case minutes | |
when minutes == 0 | |
tri_minutes = 0 | |
when minutes == 1 | |
tri_minutes = 1 | |
else | |
tri_minutes = 2 | |
end | |
case seconds | |
when seconds == 0 | |
tri_seconds = 0 | |
when seconds == 1 | |
tri_seconds = 1 | |
else | |
tri_seconds = 2 | |
end | |
plural_singular_arr = [] | |
plural_singular_arr << "seconds" if tri_seconds == 2 | |
plural_singular_arr << "second" if tri_seconds == 1 | |
plural_singular_arr << "minutes" if tri_minutes == 2 | |
plural_singular_arr << "minute" if tri_minutes == 1 | |
plural_singular_arr << "hours" if tri_hours == 2 | |
plural_singular_arr << "hour" if tri_hours == 1 | |
plural_singular_arr << "days" if tri_days == 2 | |
plural_singular_arr << "day" if tri_days == 1 | |
plural_singular_arr << "years" if tri_years == 2 | |
plural_singular_arr << "year" if tri_years == 1 | |
units_arr = [] | |
units_arr << "seconds" if tri_seconds != 0 | |
units_arr << "minutes" if tri_minutes != 0 | |
units_arr << "hours" if tri_hours != 0 | |
units_arr << "days" if tri_days != 0 | |
units_arr << "years" if tri_years != 0 | |
puts tri_years # Clearly I don't understand Ruby's case syntax, when n == 60 (among tons of other values) this returns 2 | |
if units_arr.length == 1 | |
return "#{units_arr.map {|unit| binding.local_variable_get unit} }" | |
end | |
"" | |
end | |
puts format_duration(63252122) |
baweaver
commented
May 31, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment