diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/conversions.rb | 12 |
2 files changed, 15 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 579079d4f1..28fec5394f 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -57,19 +57,19 @@ class Date # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then subtracts the specified number of seconds def ago(seconds) - to_time.since(-seconds) + to_time_in_current_zone.since(-seconds) end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then adds the specified number of seconds def since(seconds) - to_time.since(seconds) + to_time_in_current_zone.since(seconds) end alias :in :since # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) def beginning_of_day - to_time + to_time_in_current_zone end alias :midnight :beginning_of_day alias :at_midnight :beginning_of_day @@ -77,7 +77,7 @@ class Date # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59) def end_of_day - to_time.end_of_day + to_time_in_current_zone.end_of_day end def plus_with_duration(other) #:nodoc: diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 13ef703f49..ba17b0a06b 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -81,6 +81,16 @@ class Date def to_time(form = :local) ::Time.send("#{form}_time", year, month, day) end + + # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set, + # otherwise converts Date to a Time via Date#to_time + def to_time_in_current_zone + if ::Time.zone_default + ::Time.zone.local(year, month, day) + else + to_time + end + end # Converts a Date instance to a DateTime, where the time is set to the beginning of the day # and UTC offset is set to 0. @@ -94,6 +104,6 @@ class Date end if RUBY_VERSION < '1.9' def xmlschema - to_time.xmlschema + to_time_in_current_zone.xmlschema end end |