Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
sikachu authored and josevalim committed Jan 19, 2010
1 parent d2759d1 commit 6e62e89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions activesupport/lib/active_support/values/time_zone.rb
Expand Up @@ -172,7 +172,7 @@ class TimeZone
MAPPING.freeze
end

UTC_OFFSET_WITH_COLON = '%+03d:%02d'
UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')

# Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset)
Expand All @@ -181,9 +181,10 @@ class TimeZone
# TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
def self.seconds_to_utc_offset(seconds, colon = true)
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
format % [hours, minutes]
format % [sign, hours, minutes]
end

include Comparable
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/time_zone_test.rb
Expand Up @@ -208,6 +208,12 @@ def test_seconds_to_utc_offset_without_colon
assert_equal "+0000", ActiveSupport::TimeZone.seconds_to_utc_offset(0, false)
assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false)
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
zone = ActiveSupport::TimeZone['Moscow']
Expand Down

0 comments on commit 6e62e89

Please sign in to comment.