From 4e715e33aff6274267446d8dcb63da7f60c683b6 Mon Sep 17 00:00:00 2001 From: kennyj Date: Thu, 20 Sep 2012 00:51:28 +0900 Subject: Fix #6962. AS::TimeWithZone#strftime responds incorrectly to %:z and %::z format strings. --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/time_with_zone.rb | 5 ++++- activesupport/test/time_zone_test.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f823118c8b..04f8568e4d 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +## Rails 3.2.9 (unreleased) + +* Add %:z and %::z format string support to ActiveSupport::TimeWithZone#strftime. [fixes #6962] *kennyj* + ## Rails 3.2.8 (Aug 9, 2012) ## * Fix ActiveSupport integration with Mocha > 0.12.1. *Mike Gunderloy* diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index d3adf671a0..59da6bc760 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -177,7 +177,10 @@ module ActiveSupport # Replaces %Z and %z directives with +zone+ and +formatted_offset+, respectively, before passing to # Time#strftime, so that zone information is correct def strftime(format) - format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false)) + format = format.gsub('%Z', zone) + .gsub('%z', formatted_offset(false)) + .gsub('%:z', formatted_offset(true)) + .gsub('%::z', formatted_offset(true) + ":00") time.strftime(format) end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 3575175517..8ecfc1e47e 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -235,6 +235,14 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal "-0500", zone.formatted_offset(false) end + def test_z_format_strings + zone = ActiveSupport::TimeZone['Tokyo'] + twz = zone.now + assert_equal '+0900', twz.strftime('%z') + assert_equal '+09:00', twz.strftime('%:z') + assert_equal '+09:00:00', twz.strftime('%::z') + end + def test_formatted_offset_zero zone = ActiveSupport::TimeZone['London'] assert_equal "+00:00", zone.formatted_offset -- cgit v1.2.3