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 6 6 def self.now 7 7 Time.utc( 2004, 7, 25, 14, 49, 00 ) 8 8 end 9 10 def self.local(*args) 11 Time.utc(*args) 12 end 9 13 end 10 14 11 15 TimeZone::Time = MockTime … … 25 29 assert_equal Time.local(2004,7,25,15,59,00).to_a[0,6], zone.now.to_a[0,6] 26 30 end 27 31 32 def test_today 33 zone = TimeZone.create( "Test", 43200 ) 34 assert_equal Date.new(2004,7,26), zone.today 35 end 36 28 37 def test_adjust_negative 29 38 zone = TimeZone.create( "Test", -4200 ) 30 39 assert_equal Time.utc(2004,7,24,23,55,0), zone.adjust(Time.utc(2004,7,25,1,5,0)) … … 35 44 assert_equal Time.utc(2004,7,26,1,5,0), zone.adjust(Time.utc(2004,7,25,23,55,0)) 36 45 end 37 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 52 end 53 38 54 def test_zone_compare 39 55 zone1 = TimeZone.create( "Test1", 4200 ) 40 56 zone2 = TimeZone.create( "Test1", 5600 ) -
activesupport/lib/active_support/values/time_zone.rb
old new 22 22 23 23 # Returns the offset of this time zone as a formatted string, of the 24 24 # 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 ) 27 28 return "" if utc_offset == 0 28 29 sign = (utc_offset < 0 ? -1 : 1) 29 30 hours = utc_offset.abs / 3600 30 31 minutes = (utc_offset.abs % 3600) / 60 31 "%+03d :%02d" % [ hours * sign, minutes ]32 "%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ] 32 33 end 33 34 34 35 # Compute and return the current time, in the time zone represented by … … 45 46 # Adjust the given time to the time zone represented by +self+. 46 47 def adjust(time) 47 48 time = time.to_time 48 offset = time.utc_offset 49 time + utc_offset - offset 49 time + utc_offset - time.utc_offset 50 50 end 51 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 58 end 59 52 60 # Compare this time zone to the parameter. The two are comapred first on 53 61 # their offsets, and then by name. 54 62 def <=>(zone) … … 142 150 @@zones 143 151 end 144 152 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 149 168 end 150 169 151 170 # A regular expression that matches the names of all time zones in