Revision: 66178
Updated Code
at April 5, 2014 03:20 by uberdragon
Updated Code
def time_spent_in_words seconds, params={} time_periods_shown = params[:time_periods_shown] || 3 use_short_names = params[:use_short_names] || false return "0 seconds" if seconds < 1 short_name = {:second => :sec, :minute => :min, :hour => :hr, :day => :day, :week => :wk, :year => :yr} [[60, :second], [60, :minute], [24, :hour], [7, :day], [52, :week], [1000, :year]].map{ |count, name| if seconds > 0 seconds, n = seconds.divmod(count) name = short_name[name] if use_short_names "#{n.to_i} #{name}".pluralize(n.to_i) if n.to_i > 0 end }.compact.last(time_periods_shown).reverse.join(' ') end
Revision: 66177
Updated Code
at April 5, 2014 03:19 by uberdragon
Updated Code
def time_spent_in_words seconds, params={} time_periods_shown = params[:time_periods_shown] || 3 use_short_names = params[:use_short_names] || false return "0 seconds" if seconds < 1 short_name = {:second => :sec, :minute => :min, :hour => :hr, :day => :day, :week => :wk, :year => :yr} [[60, :second], [60, :minute], [24, :hour], [7, :day], [52, :week], [1000, :year]].map{ |count, name| if seconds > 0 seconds, n = seconds.divmod(count) puts name.inspect name = short_name[name] if use_short_names puts name.inspect "#{n.to_i} #{name}".pluralize(n.to_i) if n.to_i > 0 end }.compact.last(time_periods_shown).reverse.join(' ') end
Revision: 66176
Updated Code
at March 26, 2014 08:52 by uberdragon
Updated Code
def time_spent_in_words seconds, time_periods_shown=3 return "0 seconds" if seconds < 1 [[60, :second], [60, :minute], [24, :hour], [7, :day], [52, :week], [1000, :year]].map{ |count, name| if seconds > 0 seconds, n = seconds.divmod(count) "#{n.to_i} #{name}".pluralize(n.to_i) if n.to_i > 0 end }.compact.last(time_periods_shown).reverse.join(' ') end
Revision: 66175
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 25, 2014 07:39 by uberdragon
Initial Code
def time_spent_in_words seconds, time_periods_shown=3 return "0 seconds" if seconds < 1 [[60, :second], [60, :minute], [24, :hour], [7, :day], [52, :week], [1000, :year]].map{ |count, name| if seconds > 0 seconds, n = seconds.divmod(count) "#{n.to_i} #{name}".pluralize(n.to_i) if n.to_i > 0 end }.compact.last(time_periods_shown).reverse.join(' ') end
Initial URL
Initial Description
A very concise ruby on rails helper method that converts seconds into human readable format. It will not display any time periods that have 0 (ie: 1 hour 13 seconds - skipping minutes as there were none) Due to complexity, months were not factored into this converter, the function will go from weeks to years. I welcome anyone that feels the urge to fork this with month's included. ;)
Initial Title
human readable time from seconds (helper)
Initial Tags
rails, ruby
Initial Language
Rails