aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-09 01:20:23 +0200
committerXavier Noria <fxn@hashref.com>2010-05-09 01:20:23 +0200
commit636ffa1f089a51c98fce616191846eaba93d7b87 (patch)
treea8c0178a4eccfb55253f957e5bc2015381fb4975 /activesupport/lib/active_support/core_ext
parent605c6455ac722ed9679e17458a47cc649cdedab0 (diff)
downloadrails-636ffa1f089a51c98fce616191846eaba93d7b87.tar.gz
rails-636ffa1f089a51c98fce616191846eaba93d7b87.tar.bz2
rails-636ffa1f089a51c98fce616191846eaba93d7b87.zip
Backports Date#>> from 1.9 so that calculations do the right thing around the calendar reform
Our next_month gives November for some late dates in September of 1582. Related methods, last_*, and in general advance have the same issues. This commit fixes those, see the test suite for expected behavior, which we still run in 1.9 to ensure it matches as we do with other methods defined in Date in 1.9.
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index fef49e1003..2612dca93a 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -4,6 +4,24 @@ require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/object/acts_like'
class Date
+ if RUBY_VERSION < '1.9'
+ undef :>>
+
+ # Backported from 1.9. The one in 1.8 leads to incorrect next_month and
+ # friends for dates where the calendar reform is involved. It additionally
+ # prevents an infinite loop fixed in r27013.
+ def >>(n)
+ y, m = (year * 12 + (mon - 1) + n).divmod(12)
+ m, = (m + 1) .divmod(1)
+ d = mday
+ until jd2 = self.class.valid_civil?(y, m, d, start)
+ d -= 1
+ raise ArgumentError, 'invalid date' unless d > 0
+ end
+ self + (jd2 - jd)
+ end
+ end
+
class << self
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
def yesterday