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

Ticket #7706: active_support_time_calculations_fallback_to_datetime.diff

File active_support_time_calculations_fallback_to_datetime.diff, 4.1 kB (added by gbuesing, 3 years ago)
  • test/core_ext/time_ext_test.rb

    old new  
    314314    assert_equal 31, Time.days_in_month(12, 2005) 
    315315  end 
    316316 
     317  def test_time_with_datetime_fallback 
     318    assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) 
     319    assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) 
     320    assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0) 
     321    assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.now.offset, 0) 
     322    assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0, 0) 
     323    assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, DateTime.now.offset, 0) 
     324    assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005) 
     325    assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0, 0) 
     326    assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30, 1), Time.utc(2005, 2, 21, 17, 44, 30, 1) #with usec 
     327    assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1), DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0) 
     328  end 
     329 
     330  def test_utc_time 
     331    assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) 
     332    assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0) 
     333    assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0, 0) 
     334  end 
     335 
     336  def test_local_time 
     337    assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) 
     338    assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.now.offset, 0) 
     339    assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, DateTime.now.offset, 0) 
     340  end 
     341 
    317342  def test_next_month_on_31st 
    318343    assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month 
    319344  end 
  • lib/active_support/core_ext/time/calculations.rb

    old new  
    2626              month % 2 == 0 ? 31 : 30 
    2727            end 
    2828          end 
     29 
     30          # Returns a new Time if requested year can be accomodated by Ruby's Time class 
     31          # (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); 
     32          # otherwise returns a DateTime 
     33          def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) 
     34            ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) 
     35          rescue 
     36            offset = if utc_or_local.to_sym == :utc then 0 else ::DateTime.now.offset end 
     37            ::DateTime.civil(year, month, day, hour, min, sec, offset, 0) 
     38          end 
     39           
     40          # wraps class method time_with_datetime_fallback with utc_or_local == :utc 
     41          def utc_time(*args) 
     42            time_with_datetime_fallback(:utc, *args) 
     43          end 
     44           
     45          # wraps class method time_with_datetime_fallback with utc_or_local == :local 
     46          def local_time(*args) 
     47            time_with_datetime_fallback(:local, *args) 
     48          end           
    2949        end 
    3050 
    3151        # Seconds since midnight: Time.now.seconds_since_midnight 
     
    3858        # minute is passed, then sec and usec is set to 0. 
    3959        def change(options) 
    4060          ::Time.send( 
    41             self.utc? ? :utc : :local,  
     61            self.utc? ? :utc_time : :local_time,  
    4262            options[:year]  || self.year,  
    4363            options[:month] || self.month,  
    4464            options[:mday]  || self.mday,