Wednesday May 14, 2008 at 20:26

I decided to throw together a lifestreaming app to aggregate all various activity feeds that make up my distributed “web identity”. It’s a rails app living over at lifestream.dustinselman.com. Along the way I ended up modifying the default behavior of the rails method time_ago_in_words to work a little  differently. Here’s the code I ended up with: 


def time_ago_in_words(time, options = {})
  start_date = options.delete(:start_date) || Time.new
  date_format = options.delete(:date_format) || :default
  delta_minutes = (start_date.to_i - time.to_i).floor / (60)
  distance = distance_of_time_in_words(delta_minutes);
end

def distance_of_time_in_words(minutes)
  hours = minutes.floor/60
  days = hours.floor/24
  today = Time.today.to_date
  startday = today - days
  starttime = Time.today - minutes*60
  startstr = starttime.strftime("%I").to_i.to_s + 
                   starttime.strftime(":%M %p")
  case
    when hours < 1
      "less than 1 hour ago"
    when hours < 24
      pluralize(hours, "hour") + " ago".downcase
    when days < 2
      "yesterday at #{start_time_str}".downcase
    when days < 7
      "#{(startday).strftime('%A')} at #{startstr}".downcase
    when days < 356
      "#{(startday).strftime('%B %d')} at #{startstr}".downcase
    else
      "#{(startday).strftime('%B %d, %Y')} at #{startstr}".downcase
  end
end
I decided to throw together a lifestreaming app to aggregate all various activity feeds that make up my distributed “web identity”. It’s a rails app living over at lifestream.dustinselman.com. Along the way I ended up modifying the default behavior of the rails method time_ago_in_words to work a little differently. Here’s the code I ended up with:

def time_ago_in_words(time, options = {})
  start_date = options.delete(:start_date) || Time.new
  date_format = options.delete(:date_format) || :default
  delta_minutes = (start_date.to_i - time.to_i).floor / (60)
  distance = distance_of_time_in_words(delta_minutes);
end

def distance_of_time_in_words(minutes)
  hours = minutes.floor/60
  days = hours.floor/24
  today = Time.today.to_date
  startday = today - days
  starttime = Time.today - minutes*60
  startstr = starttime.strftime("%I").to_i.to_s + 
                   starttime.strftime(":%M %p")
  case
    when hours < 1
      "less than 1 hour ago"
    when hours < 24
      pluralize(hours, "hour") + " ago".downcase
    when days < 2
      "yesterday at #{start_time_str}".downcase
    when days < 7
      "#{(startday).strftime('%A')} at #{startstr}".downcase
    when days < 356
      "#{(startday).strftime('%B %d')} at #{startstr}".downcase
    else
      "#{(startday).strftime('%B %d, %Y')} at #{startstr}".downcase
  end
end
Comments (View)
blog comments powered by Disqus