Time#advance now uses :weeks, :hours, :minutes, :seconds options - Rails 2.0 a feature a day #1

Time#advance and DateTime#advance just got, erm, more betterer in Rails 2.0.

In Rails 1.2.6 and earlier, Time#advance and DateTime#advance only accepted :year, :month and :day options. You’d think :hours, :minutes and :seconds would work too but due to a bug in the code (see ticket #9818), you get something like this instead:

time = Time.utc(2000,10,1,10,30,45)
# => Sun Oct 01 10:30:45 UTC 2000

time.advance(:days =>3, :hour => 2)
# => Wed Oct 04 02:00:00 UTC 2000

Notice how the hour is changed instead of being advanced.

Now, thanks to Geoff Buesing’s efforts, passing :hours, :minutes and :seconds options work as you’d expect:

time = Time.local(2005,2,28,15,15,10)
# => Mon Feb 28 15:15:10 0800 2005

time.advance(:hours => 5, :minutes => 20, :seconds => 25)
# => Mon Feb 28 20:35:35 0800 2005

Hell, you can even #advance by :weeks (see ticket #9866):

time = Time.local(2005,2,28,15,15,10)
# => Mon Feb 28 15:15:10 0800 2005

time.advance(:weeks => 2)
# => Mon Mar 14 15:15:10 0800 2005

+1 for consistency and properly behaving code. Thanks to this #advance patch, Geoff was also able to re-use #advance to refactor some other parts of ActiveSupport’s time-related extensions.

About the contributor, Geoff Buesing

Geoff Buesing is a regular Rails contributor and I’ve known him to be the date/datetime/time guru - just look at his submitted patches if you’re not convinced. Not too long ago Geoff was given well-deserved commit rights (congrats Geoff)! You won’t find him on the Ruby on Rails Core team page yet though - it’s probably out of date.

Check out his blog at mad.ly where he writes mostly his adventures with on unobtrusive JavaScript in Rails.

4 Comments & TrackBacks ()

Paper doll icon
» The Links » roarin’ reporter's Gravatar

[…] Time#advance now uses :weeks, :hours, :minutes, :seconds options - Rails 2.0 a feature a day #1 […]

Posted by: » The Links » roarin’ reporter on December 10, 2007 9am

Paper doll icon
choonkeat's Gravatar

nicely done! good idea on the “about contributor”. looks like i’ll have to look for something to patch!

and i can finally stop following http://twitter.com/railsedge (which i fail to read everything anyways)

Posted by: choonkeat on December 10, 2007 11am

Paper doll icon
Chu Yeow's Gravatar

Well I’ll feature you someday Choon Keat! Anyway, this “column” is about stuff that’s already in gem Rails 2.0.1 - I think you still need to follow railsedge’s Twitter or the Trac timeline/changeset RSS!

Posted by: Chu Yeow on December 10, 2007 10pm

Paper doll icon
Tecker.LOG » Blog Archive » Time#advance???:weeks, :hours, :minutes, :seconds???????Rails 2.0?? #1(??)'s Gravatar

[…] ???Time#advance now uses :weeks, :hours, :minutes, :seconds options - Rails 2.0 a feature a da… […]

Posted by: Tecker.LOG » Blog Archive » Time#advance???:weeks, :hours, :minutes, :seconds???????Rails 2.0?? #1(??) on June 3, 2008 12pm

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.