tags:

views:

370

answers:

3

How do I form "fuzzy" date/time from RFC 2822 formatted (Sat, 18 Jul 2009 10:57:43 +0300) timestamp?

With fuzzy date I mean like: "5 minutes ago", "2 days, 15 minutes ago".

+7  A: 

Rails gives your views a helper function called time_ago_in_words that you can call to output just such a format from a Time object.

Jim Puls
you don't have to be using all of rails for this. You could just use the actionview gem: `require 'rubygems'; require 'actionview'`
rampion
+3  A: 
def fuzzy_date(date)
  date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
  days = (date - Date.today).to_i
  return 'today'     if days >= 0 and days < 1
  return 'tomorrow'  if days >= 1 and days < 2
  return 'yesterday' if days >= -1 and days < 0
  return "in #{days} days"      if days.abs < 60 and days > 0
  return "#{days.abs} days ago" if days.abs < 60 and days < 0
  return date.strftime('%A, %B %e') if days.abs < 182
  return date.strftime('%A, %B %e, %Y')
end
Robert Smith
+2  A: 

There's some really powerful date/time parsers in ruby (unfortunately kind of hard to google(TM) for)

http://chronic.rubyforge.org/

http://www.ahabman.com/blog/2009/06/ruby-duration/

http://github.com/flogic/timely/tree/master

Gene T