diff options
author | Xavier Noria <fxn@hashref.com> | 2010-05-09 11:46:45 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-05-09 11:46:45 +0200 |
commit | e1a0d86fe0355ddff8c86db0f42f3824ffe14c02 (patch) | |
tree | 5833022ca41905f243c15c8a5ffdf864de69bfa6 /activesupport/lib/active_support/core_ext | |
parent | 1ff3d951e620ddeeb97e87e2024391470e886467 (diff) | |
parent | df508bd97062b871fe25eda8d1bb61cd43d79bc4 (diff) | |
download | rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.gz rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.bz2 rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
3 files changed, 30 insertions, 3 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..3038729d34 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 @@ -163,6 +181,7 @@ class Date result = self + days_to_sunday.days self.acts_like?(:time) ? result.end_of_day : result end + alias :sunday :end_of_week alias :at_end_of_week :end_of_week # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday). diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index a9f821b01e..24168c7825 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -1,5 +1,6 @@ require 'active_support/inflector' require 'active_support/core_ext/time/conversions' +require 'active_support/core_ext/date_time/calculations' class DateTime # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows @@ -72,6 +73,11 @@ class DateTime self end unless method_defined?(:to_datetime) + def self.civil_from_format(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0) + offset = utc_or_local.to_sym == :local ? local_offset : 0 + civil(year, month, day, hour, min, sec, offset) + end + # Converts datetime to an appropriate format for use in XML def xmlschema strftime("%Y-%m-%dT%H:%M:%S%Z") diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 98906bc5c0..2b47ecd543 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -1,6 +1,7 @@ require 'active_support/duration' require 'active_support/core_ext/date/acts_like' require 'active_support/core_ext/date/calculations' +require 'active_support/core_ext/date_time/conversions' class Time COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -23,10 +24,11 @@ class Time # (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); # otherwise returns a DateTime def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) - ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) + time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) + # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138. + time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec) rescue - offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0 - ::DateTime.civil(year, month, day, hour, min, sec, offset) + ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec) end # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>. |