Changeset 6937
- Timestamp:
- 06/04/07 22:00:53 (1 year ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/date/calculations.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/duration.rb (modified) (2 diffs)
- trunk/activesupport/test/core_ext/date_ext_test.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/numeric_ext_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r6935 r6937 1 1 *SVN* 2 3 * Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. #8575 [Geoff Buesing] 2 4 3 5 * String#to_time overflows to DateTime. Add String#to_datetime. #8572 [Geoff Buesing] trunk/activesupport/lib/active_support/core_ext/date/calculations.rb
r6934 r6937 24 24 end 25 25 26 # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 27 # and then subtracts the specified number of seconds 28 def ago(seconds) 29 to_time.since(-seconds) 30 end 31 32 # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 33 # and then adds the specified number of seconds 34 def since(seconds) 35 to_time.since(seconds) 36 end 37 alias :in :since 38 39 # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 40 def beginning_of_day 41 to_time 42 end 43 alias :midnight :beginning_of_day 44 alias :at_midnight :beginning_of_day 45 alias :at_beginning_of_day :beginning_of_day 46 47 # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59) 48 def end_of_day 49 to_time.end_of_day 50 end 51 26 52 def plus_with_duration(other) #:nodoc: 27 53 if ActiveSupport::Duration === other trunk/activesupport/lib/active_support/duration.rb
r6901 r6937 66 66 def sum(sign, time = ::Time.now) #:nodoc: 67 67 parts.inject(time) do |t,(type,number)| 68 if t.acts_like?(:time) 68 if t.acts_like?(:time) || t.acts_like?(:date) 69 69 if type == :seconds 70 70 t.since(sign * number) … … 72 72 t.advance(type => sign * number) 73 73 end 74 elsif t.acts_like?(:date)75 raise ArgumentError, "Adding seconds to a Date does not make sense" if type == :seconds76 t.advance(type => sign * number)77 74 else 78 75 raise ArgumentError, "expected a time or date, got #{time.inspect}" trunk/activesupport/test/core_ext/date_ext_test.rb
r6934 r6937 140 140 assert_equal Date.today + 1, Date.tomorrow 141 141 end 142 143 def test_since 144 assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45) 145 end 146 147 def test_ago 148 assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45) 149 end 150 151 def test_beginning_of_day 152 assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day 153 end 154 155 def test_end_of_day 156 assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day 157 end 142 158 end trunk/activesupport/test/core_ext/numeric_ext_test.rb
r6906 r6937 91 91 assert_equal @today + 1, @today + 1.day 92 92 assert_equal @today >> 1, @today + 1.month 93 assert_raises(ArgumentError) { @today + 1.second } 93 assert_equal @today.to_time.since(1), @today + 1.second 94 assert_equal @today.to_time.since(60), @today + 1.minute 95 assert_equal @today.to_time.since(60*60), @today + 1.hour 94 96 end 95 97