aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/time/calculations.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-05 00:30:15 +0200
committerXavier Noria <fxn@hashref.com>2010-05-05 00:35:06 +0200
commit53c1cd6cde6e34996ff85960f06749b681200908 (patch)
tree9d69ca78887aa9222a09b945cae581eb70b0d11c /activesupport/lib/active_support/core_ext/time/calculations.rb
parent38da0ace772e6f9f5e2fff74db76237ab31790fa (diff)
downloadrails-53c1cd6cde6e34996ff85960f06749b681200908.tar.gz
rails-53c1cd6cde6e34996ff85960f06749b681200908.tar.bz2
rails-53c1cd6cde6e34996ff85960f06749b681200908.zip
let Time.time_with_datetime_fallback handle properly years in the range 0..138
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time/calculations.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb8
1 files changed, 5 insertions, 3 deletions
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>.