What's New in Edge Rails: 1.month.from_now.no_longer.effed

Posted by ryan
at 10:10 AM on Friday, January 26, 2007

Brian Donovan has a great writeup of his new Duration fix to the Rails’ Time convenience methods that, until now, have been slightly broken.

Here’s the summary. Before (bad):

1
2
3
4
Time.now
#=> Thu Jan 25 21:01:31 -0800 2007
1.month.from_now
#=> Sat Feb 24 21:01:34 -0800 2007

Oops – apparently one month only equals 30 days, not really one month. But now, in edge:

1
2
3
4
Time.now
#=> Thu Jan 25 21:01:31 -0800 2007
1.month.from_now
#=> Sun Feb 25 21:01:34 -0800 2007

And check out this little inspect goodie:

1
2
>> 3.weeks
#=> 21 days

And just so you’re not mislead by this post’s title, this fix applies to all the time helpers, not just month.from_now. Read the full rundown to become fully enlightened.

tags: rubyonrails, rails

Comments

Leave a response

  1. JeremyJanuary 26, 2007 @ 10:32 AM
    I hope you copied that wrong, because both of those examples have the wrong day of the week. Feb 24 is a Saturday and Feb 25 is a Sunday. I haven't downloaded and tested yet, but it still looks broken to me.
  2. RyanJanuary 26, 2007 @ 10:40 AM
    Indeed I did copy wrong, Jeremy, tx for the heads up.
  3. Nathan ColgateJanuary 27, 2007 @ 01:18 AM
    Man. Where was this 3 Months Ago when I was writing the code for Odd Anniversaries. Awesome stuff.
  4. Hendy IrawanJanuary 27, 2007 @ 04:25 AM
    No... This is exactly what I've wanted since childbirth. I develop a contact management app add do lots of calculations with dates. Zodiacs. And all these astronomiclogic stuff. Yaay!!! :-)