1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix bug that causes TimeZone.seconds_to_utc_offset to returns wrong offset when hour < 0 and not in hundreds [#3741 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Prem Sichanugrist 2010-01-19 01:55:48 +07:00 committed by José Valim
parent d2759d125a
commit 6e62e89737
2 changed files with 10 additions and 3 deletions

View file

@ -172,7 +172,7 @@ module ActiveSupport
MAPPING.freeze MAPPING.freeze
end end
UTC_OFFSET_WITH_COLON = '%+03d:%02d' UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '') UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')
# Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset) # Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset)
@ -181,9 +181,10 @@ module ActiveSupport
# TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00" # TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
def self.seconds_to_utc_offset(seconds, colon = true) def self.seconds_to_utc_offset(seconds, colon = true)
format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
hours = seconds / 3600 sign = (seconds < 0 ? '-' : '+')
hours = seconds.abs / 3600
minutes = (seconds.abs % 3600) / 60 minutes = (seconds.abs % 3600) / 60
format % [hours, minutes] format % [sign, hours, minutes]
end end
include Comparable include Comparable

View file

@ -209,6 +209,12 @@ class TimeZoneTest < Test::Unit::TestCase
assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false) assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false)
end end
def test_seconds_to_utc_offset_with_negative_offset
assert_equal "-01:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_600)
assert_equal "-00:59", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_599)
assert_equal "-05:30", ActiveSupport::TimeZone.seconds_to_utc_offset(-19_800)
end
def test_formatted_offset_positive def test_formatted_offset_positive
zone = ActiveSupport::TimeZone['Moscow'] zone = ActiveSupport::TimeZone['Moscow']
assert_equal "+03:00", zone.formatted_offset assert_equal "+03:00", zone.formatted_offset