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

Ticket #709: time-zone-enhancements.patch

File time-zone-enhancements.patch, 3.8 kB (added by minam, 4 years ago)
  • activesupport/test/time_zone_test.rb

    old new  
    66    def self.now 
    77      Time.utc( 2004, 7, 25, 14, 49, 00 ) 
    88    end 
     9 
     10    def self.local(*args) 
     11      Time.utc(*args) 
     12    end 
    913  end 
    1014 
    1115  TimeZone::Time = MockTime 
     
    2529    assert_equal Time.local(2004,7,25,15,59,00).to_a[0,6], zone.now.to_a[0,6] 
    2630  end 
    2731 
     32  def test_today 
     33    zone = TimeZone.create( "Test", 43200 ) 
     34    assert_equal Date.new(2004,7,26), zone.today 
     35  end 
     36 
    2837  def test_adjust_negative 
    2938    zone = TimeZone.create( "Test", -4200 ) 
    3039    assert_equal Time.utc(2004,7,24,23,55,0), zone.adjust(Time.utc(2004,7,25,1,5,0)) 
     
    3544    assert_equal Time.utc(2004,7,26,1,5,0), zone.adjust(Time.utc(2004,7,25,23,55,0)) 
    3645  end 
    3746 
     47  def test_unadjust 
     48    zone = TimeZone.create( "Test", 4200 ) 
     49    expect = Time.utc(2004,7,24,23,55,0).to_a[0,6] 
     50    actual = zone.unadjust(Time.utc(2004,7,25,1,5,0)).to_a[0,6] 
     51    assert_equal expect, actual 
     52  end 
     53 
    3854  def test_zone_compare 
    3955    zone1 = TimeZone.create( "Test1", 4200 ) 
    4056    zone2 = TimeZone.create( "Test1", 5600 ) 
  • activesupport/lib/active_support/values/time_zone.rb

    old new  
    2222 
    2323  # Returns the offset of this time zone as a formatted string, of the 
    2424  # format "+HH:MM". If the offset is zero, this returns the empty 
    25   # string. 
    26   def formatted_offset 
     25  # string. If +colon+ is false, a colon will not be inserted into the 
     26  # result. 
     27  def formatted_offset( colon=true ) 
    2728    return "" if utc_offset == 0 
    2829    sign = (utc_offset < 0 ? -1 : 1) 
    2930    hours = utc_offset.abs / 3600 
    3031    minutes = (utc_offset.abs % 3600) / 60 
    31     "%+03d:%02d" % [ hours * sign, minutes ] 
     32    "%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ] 
    3233  end 
    3334 
    3435  # Compute and return the current time, in the time zone represented by 
     
    4546  # Adjust the given time to the time zone represented by +self+. 
    4647  def adjust(time) 
    4748    time = time.to_time 
    48     offset = time.utc_offset 
    49     time + utc_offset - offset 
     49    time + utc_offset - time.utc_offset 
    5050  end 
    5151 
     52  # Reinterprets the given time value as a time in the current time 
     53  # zone, and then adjusts it to return the corresponding time in the 
     54  # local time zone. 
     55  def unadjust(time) 
     56    time = Time.local(*time.to_time.to_a) 
     57    time - utc_offset + time.utc_offset 
     58  end 
     59 
    5260  # Compare this time zone to the parameter. The two are comapred first on 
    5361  # their offsets, and then by name. 
    5462  def <=>(zone) 
     
    142150      @@zones 
    143151    end 
    144152 
    145     # Locate a specific time zone object by the name it was given. Returns 
    146     # +nil+ if no such time zone is known to the system. 
    147     def [](name) 
    148       all.find { |z| z.name == name } 
     153    # Locate a specific time zone object. If the argument is a string, it 
     154    # is interpreted to mean the name of the timezone to locate. If it is a 
     155    # numeric value it is either the hour offset, or the second offset, of the 
     156    # timezone to find. (The first one with that offset will be returned.) 
     157    # Returns +nil+ if no such time zone is known to the system. 
     158    def [](arg) 
     159      case arg 
     160        when String 
     161          all.find { |z| z.name == arg } 
     162        when Numeric 
     163          arg *= 3600 if arg.abs <= 13 
     164          all.find { |z| z.utc_offset == arg.to_i } 
     165        else 
     166          raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" 
     167      end 
    149168    end 
    150169 
    151170    # A regular expression that matches the names of all time zones in