From 1897d3a5d240a962dc89d2cab1d36bedb08974f7 Mon Sep 17 00:00:00 2001 From: Pablo Herrero Date: Sat, 18 Oct 2014 04:47:40 -0300 Subject: Optimize TimeWithZoneTest#strftime --- activesupport/CHANGELOG.md | 5 +++++ activesupport/lib/active_support/time_with_zone.rb | 16 ++++++---------- activesupport/test/core_ext/time_with_zone_test.rb | 5 +++++ 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f9df972929..4ad602b066 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* TimeWithZone#strftime now delegates every directive to Time#strftime except for '%Z', + it also now correctly handles escaped '%' characters placed just before time zone related directives. + + *Pablo Herrero* + * Corrected Inflector#underscore handling of multiple successive acroynms. *James Le Cuirot* diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 4a0ed356b1..dbee145196 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -75,8 +75,8 @@ module ActiveSupport # Returns a Time.local() instance of the simultaneous time in your # system's ENV['TZ'] zone. - def localtime - utc.respond_to?(:getlocal) ? utc.getlocal : utc.to_time.getlocal + def localtime(utc_offset = nil) + utc.respond_to?(:getlocal) ? utc.getlocal(utc_offset) : utc.to_time.getlocal(utc_offset) end alias_method :getlocal, :localtime @@ -201,15 +201,11 @@ module ActiveSupport end alias_method :to_formatted_s, :to_s - # Replaces %Z and %z directives with +zone+ and - # +formatted_offset+, respectively, before passing to Time#strftime, so - # that zone information is correct + # Replaces %Z directive with +zone before passing to Time#strftime, + # so that zone information is correct. def strftime(format) - format = format.gsub('%Z', zone) - .gsub('%z', formatted_offset(false)) - .gsub('%:z', formatted_offset(true)) - .gsub('%::z', formatted_offset(true) + ":00") - time.strftime(format) + format = format.gsub(/((?:\A|[^%])(?:%%)*)%Z/, "\\1#{zone}") + getlocal(utc_offset).strftime(format) end # Use the time in UTC for comparisons. diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 3000da8da4..ad4062e5fe 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -79,6 +79,11 @@ class TimeWithZoneTest < ActiveSupport::TestCase assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') end + def test_strftime_with_escaping + assert_equal '%Z %z', @twz.strftime('%%Z %%z') + assert_equal '%EST %-0500', @twz.strftime('%%%Z %%%z') + end + def test_inspect assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect end -- cgit v1.2.3