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

Changeset 787

Show
Ignore:
Timestamp:
02/24/05 11:56:09 (4 years ago)
Author:
david
Message:

Made TimeZone even more delicious #709

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r776 r787  
    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 
     
    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 
     50  end 
     51 
     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 
    5058  end 
    5159 
     
    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 
  • trunk/activesupport/test/time_zone_test.rb

    r770 r787  
    66    def self.now 
    77      Time.utc( 2004, 7, 25, 14, 49, 00 ) 
     8    end 
     9 
     10    def self.local(*args) 
     11      Time.utc(*args) 
    812    end 
    913  end 
     
    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 ) 
     
    3443    zone = TimeZone.create( "Test", 4200 ) 
    3544    assert_equal Time.utc(2004,7,26,1,5,0), zone.adjust(Time.utc(2004,7,25,23,55,0)) 
     45  end 
     46 
     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 
    3652  end 
    3753