diff options
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 26 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 5 |
2 files changed, 27 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 5c4ed65b28..637c9ea02a 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -23,6 +23,32 @@ module ActiveSupport #:nodoc: end 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 subtracts the specified number of seconds + def ago(seconds) + to_time.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) + 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 + end + alias :midnight :beginning_of_day + alias :at_midnight :beginning_of_day + alias :at_beginning_of_day :beginning_of_day + + # 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 + end + def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other other.since(self) diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 721bc18795..0d3016a690 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -65,15 +65,12 @@ module ActiveSupport def sum(sign, time = ::Time.now) #:nodoc: parts.inject(time) do |t,(type,number)| - if t.acts_like?(:time) + if t.acts_like?(:time) || t.acts_like?(:date) if type == :seconds t.since(sign * number) else t.advance(type => sign * number) end - elsif t.acts_like?(:date) - raise ArgumentError, "Adding seconds to a Date does not make sense" if type == :seconds - t.advance(type => sign * number) else raise ArgumentError, "expected a time or date, got #{time.inspect}" end |