November 27, 2014
Hot Topics:

Telling Time with Ruby

  • February 20, 2008
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Next, follow along with these examples to learn more about what Chronic has to offer:

irb>Chronic.parse('yesterday')
=> Wed 13 Feb 2008 12:00:00 -0500 2008
irb>Chronic.parse('yesterday').to_date
=> Wed, 13 Feb 2008
irb>Chronic.parse('last monday').to_date
=> Mon, 11 Feb 2008
irb>Chronic.parse('next month').to_date
=> Sun, 16 Mar 2008
irb> Chronic.parse('3 months ago this friday at 3:45pm')
=> Thu Nov 15 15:45:00 -0500 2007

Chronic offers a second benefit that can be immensely useful if you're dealing with free-form dates. Suppose you wanted to analyze a data file that contains a bunch of dates, converting each to a Date object. These dates used inconsistent formatting, however, with some using dash separators, and others using slash separators. Chronic recognizes both:

irb>Chronic.parse('07-04-2008').to_date
=> Fri, 04 Jul 2008
irb>Chronic.parse('07/04/2008').to_date
=> Fri, 04 Jul 2008

Suppose the inconsistency problem was worse than you expected, and you found an occasional date used a dash to separate the month and day, but a slash to separate the day and year? Chronic is capable of handling these too:

irb>Chronic.parse('07-04/2008').to_date
=> Fri, Jul 04 2008

Rails and Dates

At the beginning of this tutorial, I referenced the great date-related features available through the Active Support gem. Active Support is an integral part of the Rails framework, meaning you can take advantage of all Active Support has to offer within your Rails applications. For instance, suppose you wanted to retrieve a list of users who have logged in within the last seven days:

@users_recently_active = User.find(:all,
   :conditions => ["users.last_login < ?", 7.days.ago],
   :order => "last_login DESC")

User-Friendly Temporal Messages

Within Rails views, you can take advantage of other date/time-specific features introduced by another key gem used by Rails, named ActionView. For instance, suppose you wanted to provide users with a somewhat more friendly message indicating the last time a specific user logged in. The message might look something like this:

This user was last visible 4 days ago

To implement this, all you need to do is use the ActionView helper time_ago_in_words:

This user was last visible <%=
   time_ago_in_words(user.last_login)%> ago

Conclusion

I hope this brief tutorial regarding Ruby's amazing date and time manipulation capabilities gives you cause to consider exploring this powerful language! If you'd like to share a tip, email me at jasonATwjgilmore.com!

About the Author

W. Jason Gilmore is a freelance Web developer, consultant, and technical writer. He's the author of several books, including the best-selling Beginning PHP and MySQL 5: Novice to Professional, Second Edition (Apress, 2006. 913pp.).





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel