Skip to content
This repository
Browse code

Fix bug that causes TimeZone.seconds_to_utc_offset to returns wrong o…

…ffset when hour < 0 and not in hundreds [#3741 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 6e62e89737c991de712a13b67a282ce599710ec9 1 parent d2759d1
Prem Sichanugrist authored January 19, 2010 josevalim committed January 19, 2010
7  activesupport/lib/active_support/values/time_zone.rb
@@ -172,7 +172,7 @@ class TimeZone
172 172
       MAPPING.freeze
173 173
     end
174 174
 
175  
-    UTC_OFFSET_WITH_COLON = '%+03d:%02d'
  175
+    UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
176 176
     UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')
177 177
 
178 178
     # Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset)
@@ -181,9 +181,10 @@ class TimeZone
181 181
     #   TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
182 182
     def self.seconds_to_utc_offset(seconds, colon = true)
183 183
       format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
184  
-      hours = seconds / 3600
  184
+      sign = (seconds < 0 ? '-' : '+')
  185
+      hours = seconds.abs / 3600
185 186
       minutes = (seconds.abs % 3600) / 60
186  
-      format % [hours, minutes]
  187
+      format % [sign, hours, minutes]
187 188
     end
188 189
 
189 190
     include Comparable
6  activesupport/test/time_zone_test.rb
@@ -208,6 +208,12 @@ def test_seconds_to_utc_offset_without_colon
208 208
     assert_equal "+0000", ActiveSupport::TimeZone.seconds_to_utc_offset(0, false)
209 209
     assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false)
210 210
   end
  211
+  
  212
+  def test_seconds_to_utc_offset_with_negative_offset
  213
+    assert_equal "-01:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_600)
  214
+    assert_equal "-00:59", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_599)
  215
+    assert_equal "-05:30", ActiveSupport::TimeZone.seconds_to_utc_offset(-19_800)
  216
+  end
211 217
 
212 218
   def test_formatted_offset_positive
213 219
     zone = ActiveSupport::TimeZone['Moscow']

0 notes on commit 6e62e89

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