Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix bug that causes to_utf_offset_s to returns wrong offset when hour…

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

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit ef10988fdd2fa2dfb92fa731b139cadb32e9811a 1 parent 6012e57
@sikachu sikachu authored josevalim committed
View
4 activesupport/lib/active_support/core_ext/numeric/conversions.rb
@@ -8,10 +8,10 @@ module Conversions
# -21_600.to_utc_offset_s # => "-06:00"
def to_utc_offset_s(colon=true)
seconds = self
- sign = (seconds < 0 ? -1 : 1)
+ sign = (seconds < 0 ? '-' : '+')
hours = seconds.abs / 3600
minutes = (seconds.abs % 3600) / 60
- "%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ]
+ "%s%02d%s%02d" % [ sign, hours, colon ? ":" : "", minutes ]
end
end
end
View
6 activesupport/test/core_ext/numeric_ext_test.rb
@@ -159,4 +159,10 @@ def test_to_utc_offset_s_without_colon
assert_equal "+0000", 0.to_utc_offset_s(false)
assert_equal "+0500", 18_000.to_utc_offset_s(false)
end
+
+ def test_to_utc_offset_s_with_negative_offset
+ assert_equal "-01:00", -3_600.to_utc_offset_s
+ assert_equal "-00:59", -3_599.to_utc_offset_s
+ assert_equal "-05:30", -19_800.to_utc_offset_s
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.