提交 00de820f 编写于 作者: A Andrew White

Merge pull request #17310 from pabloh/optimize_strftime

Optimize TimeWithZoneTest#strftime
* 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*
......
......@@ -75,8 +75,8 @@ def in_time_zone(new_zone = ::Time.zone)
# Returns a <tt>Time.local()</tt> instance of the simultaneous time in your
# system's <tt>ENV['TZ']</tt> 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 @@ def to_s(format = :default)
end
alias_method :to_formatted_s, :to_s
# Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and
# +formatted_offset+, respectively, before passing to Time#strftime, so
# that zone information is correct
# Replaces <tt>%Z</tt> 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.
......
......@@ -79,6 +79,11 @@ def test_strftime
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册