Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6937

Show
Ignore:
Timestamp:
06/04/07 22:00:53 (1 year ago)
Author:
bitsweat
Message:

Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. Closes #8575.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r6935 r6937  
    11*SVN* 
     2 
     3* Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now.  #8575 [Geoff Buesing] 
    24 
    35* 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  
    2424        end 
    2525         
     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         
    2652        def plus_with_duration(other) #:nodoc: 
    2753          if ActiveSupport::Duration === other 
  • trunk/activesupport/lib/active_support/duration.rb

    r6901 r6937  
    6666    def sum(sign, time = ::Time.now) #:nodoc: 
    6767      parts.inject(time) do |t,(type,number)| 
    68         if t.acts_like?(:time) 
     68        if t.acts_like?(:time) || t.acts_like?(:date) 
    6969          if type == :seconds 
    7070            t.since(sign * number) 
     
    7272            t.advance(type => sign * number) 
    7373          end 
    74         elsif t.acts_like?(:date) 
    75           raise ArgumentError, "Adding seconds to a Date does not make sense" if type == :seconds 
    76           t.advance(type => sign * number) 
    7774        else 
    7875          raise ArgumentError, "expected a time or date, got #{time.inspect}" 
  • trunk/activesupport/test/core_ext/date_ext_test.rb

    r6934 r6937  
    140140    assert_equal Date.today + 1, Date.tomorrow 
    141141  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 
    142158end 
  • trunk/activesupport/test/core_ext/numeric_ext_test.rb

    r6906 r6937  
    9191    assert_equal @today + 1, @today + 1.day 
    9292    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 
    9496  end 
    9597