File tree Expand file tree Collapse file tree 2 files changed +10
-3
lines changed
lib/active_support/values Expand file tree Collapse file tree 2 files changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -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
Original file line number Diff line number Diff line change @@ -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' ]
You can’t perform that action at this time.
0 commit comments