Skip to content
This repository
Browse code

Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC z…

…one with nil

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8720 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4b2ac708b7666510d522fc706936d3c2ea5cbb80 1 parent a7adca3
Geoff Buesing authored January 25, 2008
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil [Geoff Buesing]
  4
+
3 5
 * Time.get_zone refactored to private method, given that the encapsulated logic is only useful internally [Geoff Buesing]
4 6
 
5 7
 * Time.zone uses thread-local variable for thread safety. Adding Time.use_zone, for overriding Time.zone locally inside a block. Removing unneeded Time.zone_reset! [Geoff Buesing]
15  activesupport/lib/active_support/time_with_zone.rb
@@ -5,7 +5,7 @@ class TimeWithZone
5 5
     include Comparable
6 6
     attr_reader :time_zone
7 7
   
8  
-    def initialize(utc_time, time_zone = nil, local_time = nil)
  8
+    def initialize(utc_time, time_zone, local_time = nil)
9 9
       @utc = utc_time
10 10
       @time = local_time
11 11
       @time_zone = time_zone
@@ -13,12 +13,12 @@ def initialize(utc_time, time_zone = nil, local_time = nil)
13 13
   
14 14
     # Returns a Time instance that represents the time in time_zone
15 15
     def time
16  
-      @time ||= utc? ? @utc : time_zone.utc_to_local(@utc)
  16
+      @time ||= time_zone.utc_to_local(@utc)
17 17
     end
18 18
 
19 19
     # Returns a Time instance that represents the time in UTC
20 20
     def utc
21  
-      @utc ||= utc? ? @time : time_zone.local_to_utc(@time)
  21
+      @utc ||= time_zone.local_to_utc(@time)
22 22
     end
23 23
     alias_method :comparable_time, :utc
24 24
   
@@ -53,16 +53,15 @@ def localtime
53 53
     end
54 54
   
55 55
     def dst?
56  
-      utc? ? false : period.dst?
  56
+      period.dst?
57 57
     end
58 58
   
59  
-    # The TimeZone class has no zone for UTC, so this class uses the absence of a time zone to indicate UTC
60 59
     def utc?
61  
-      !time_zone
  60
+      time_zone.name == 'UTC'
62 61
     end
63 62
   
64 63
     def utc_offset
65  
-      utc? ? 0 : period.utc_total_offset
  64
+      period.utc_total_offset
66 65
     end
67 66
   
68 67
     def formatted_offset(colon = true, alternate_utc_string = nil)
@@ -71,7 +70,7 @@ def formatted_offset(colon = true, alternate_utc_string = nil)
71 70
   
72 71
     # Time uses #zone to display the time zone abbreviation, so we're duck-typing it
73 72
     def zone
74  
-      utc? ? 'UTC' : period.abbreviation.to_s
  73
+      period.abbreviation.to_s
75 74
     end
76 75
   
77 76
     def inspect
3  activesupport/lib/active_support/values/time_zone.rb
@@ -40,6 +40,7 @@ class TimeZone
40 40
     "London"                       => "Europe/London",
41 41
     "Casablanca"                   => "Africa/Casablanca",
42 42
     "Monrovia"                     => "Africa/Monrovia",
  43
+    "UTC"                          => "UTC",
43 44
     "Belgrade"                     => "Europe/Belgrade",
44 45
     "Bratislava"                   => "Europe/Bratislava",
45 46
     "Budapest"                     => "Europe/Budapest",
@@ -259,7 +260,7 @@ def all
259 260
          [ -7_200, "Mid-Atlantic" ],
260 261
          [ -3_600, "Azores", "Cape Verde Is." ],
261 262
          [      0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca",
262  
-                   "Monrovia" ],
  263
+                   "Monrovia", "UTC" ],
263 264
          [  3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague",
264 265
                    "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels",
265 266
                    "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin",
6  activesupport/test/core_ext/date_time_ext_test.rb
@@ -267,9 +267,9 @@ def test_compare_with_datetime
267 267
   end
268 268
   
269 269
   def test_compare_with_time_with_zone
270  
-    assert_equal  1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
271  
-    assert_equal  0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
272  
-    assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
  270
+    assert_equal  1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
  271
+    assert_equal  0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
  272
+    assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
273 273
   end
274 274
 
275 275
   protected
6  activesupport/test/core_ext/time_ext_test.rb
@@ -450,9 +450,9 @@ def test_compare_with_datetime
450 450
   end
451 451
   
452 452
   def test_compare_with_time_with_zone
453  
-    assert_equal  1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
454  
-    assert_equal  0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
455  
-    assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
  453
+    assert_equal  1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
  454
+    assert_equal  0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
  455
+    assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
456 456
   end
457 457
 
458 458
   protected
16  activesupport/test/core_ext/time_with_zone_test.rb
@@ -46,7 +46,7 @@ def test_change_time_zone_to_current
46 46
   
47 47
     def test_utc?
48 48
       assert_equal false, @twz.utc?
49  
-      assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000)).utc?
  49
+      assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc?
50 50
     end
51 51
       
52 52
     def test_formatted_offset
@@ -101,9 +101,9 @@ def test_compare_with_datetime
101 101
     end
102 102
 
103 103
     def test_compare_with_time_with_zone
104  
-      assert_equal  1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
105  
-      assert_equal  0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
106  
-      assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
  104
+      assert_equal  1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
  105
+      assert_equal  0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
  106
+      assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
107 107
     end
108 108
       
109 109
     def test_plus
@@ -159,11 +159,13 @@ def teardown
159 159
 
160 160
     def test_in_time_zone
161 161
       silence_warnings do # silence warnings raised by tzinfo gem
162  
-        Time.use_zone 'Eastern Time (US & Canada)' do
  162
+        Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
163 163
           assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
164 164
           assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
165 165
           assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
166 166
           assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
  167
+          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
  168
+          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
167 169
         end
168 170
       end
169 171
     end
@@ -185,11 +187,13 @@ def test_in_current_time_zone
185 187
     
186 188
     def test_change_time_zone
187 189
       silence_warnings do # silence warnings raised by tzinfo gem
188  
-        Time.use_zone 'Eastern Time (US & Canada)' do
  190
+        Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #change_time_zone(zone)
189 191
           assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone('Alaska').inspect
190 192
           assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone('Alaska').inspect
191 193
           assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @t.change_time_zone('Hawaii').inspect
192 194
           assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @dt.change_time_zone('Hawaii').inspect
  195
+          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.change_time_zone('UTC').inspect
  196
+          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.change_time_zone('UTC').inspect
193 197
         end
194 198
       end
195 199
     end

0 notes on commit 4b2ac70

Please sign in to comment.
Something went wrong with that request. Please try again.