Timeago.rb

From EggeWiki

Jump to: navigation, search

Here's yet another timeago function which I found, for those who don't want to include a full Rails framework in your Ruby script. Sorry - I don't remember where I copied this function from.

def pluralize(count, s)
  if count == 1 then
    count.to_s + ' ' + s
  else
    count.to_s + ' ' + s + 's'
  end
end
 
# options
# :start_date, sets the time to measure against, defaults to now
# :later, changes the adjective and measures time forward
# :round, sets the unit of measure 1 = seconds, 2 = minutes, 3 hours, 4 days, 5 weeks, 6 months, 7 years (yuck!)
# :max_seconds, sets the maximimum practical number of seconds before just referring to the actual time
# :date_format, used with <tt>to_formatted_s<tt>
def timeago(original, options = {})
  start_date = options.delete(:start_date) || Time.now
  later = options.delete(:later) || false
  round = options.delete(:round) || 7
  max_seconds = options.delete(:max_seconds) || 32556926
  date_format = options.delete(:date_format) || :default
 
  # array of time period chunks
  chunks = [
    [60 * 60 * 24 * 365 , "year"],
    [60 * 60 * 24 * 30 , "month"],
    [60 * 60 * 24 * 7, "week"],
    [60 * 60 * 24 , "day"],
    [60 * 60 , "hour"],
    [60 , "minute"],
    [1 , "second"]
  ]
 
  if later
    since = original.to_i - start_date.to_i
  else
    since = start_date.to_i - original.to_i
  end
  time = []
 
  if since < max_seconds
    # Loop trough all the chunks
    totaltime = 0
 
    for chunk in chunks[0..round]
      seconds    = chunk[0]
      name       = chunk[1]
 
      count = ((since - totaltime) / seconds).floor
      time << pluralize(count, name) unless count == 0
 
      if time.size == 2 then
        break
      end
      totaltime += count * seconds
    end
 
    if time.empty?
      "less than a #{chunks[round-1][1]} ago"
    else
      "#{time.join(', ')} #{later ? 'later' : 'ago'}"
    end
  else
    original.to_formatted_s(date_format)
  end
end
Personal tools
Travelling Salesman

Get the app!