提交 1e799f23 编写于 作者: A akinomaeni

Add subsec to `ActiveSupport::TimeWithZone#inspect`

Time.at(1498099140).in_time_zone.inspect
=> "Thu, 22 Jun 2017 02:39:00 UTC +00:00"
Time.at(1498099140, 123456780, :nsec).in_time_zone.inspect
=> "Thu, 22 Jun 2017 02:39:00.12345678 UTC +00:00"
Time.at(1498099140 + Rational("1/3")).in_time_zone.inspect
=> "Thu, 22 Jun 2017 02:39:00 1/3 UTC +00:00"
上级 a6711d6e
* Add subsec to `ActiveSupport::TimeWithZone#inspect`.
Before:
Time.at(1498099140).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00 UTC +00:00"
Time.at(1498099140, 123456780, :nsec).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00 UTC +00:00"
Time.at(1498099140 + Rational("1/3")).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00 UTC +00:00"
After:
Time.at(1498099140).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00 UTC +00:00"
Time.at(1498099140, 123456780, :nsec).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00.12345678 UTC +00:00"
Time.at(1498099140 + Rational("1/3")).in_time_zone.inspect
# => "Thu, 22 Jun 2017 02:39:00 1/3 UTC +00:00"
*akinomaeni*
* Calling `ActiveSupport::TaggedLogging#tagged` without a block now returns a tagged logger.
```ruby
......
......@@ -139,7 +139,7 @@ def zone
#
# Time.zone.now.inspect # => "Thu, 04 Dec 2014 11:00:25 EST -05:00"
def inspect
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
"#{time.strftime('%a, %d %b %Y %H:%M:%S')}#{formatted_subsec_with_delimiter} #{zone} #{formatted_offset}"
end
# Returns a string of the object's date and time in the ISO 8601 standard
......@@ -581,5 +581,26 @@ def wrap_with_time_zone(time)
time
end
end
def formatted_subsec
return "" if subsec.zero?
if (1_000_000_000 % subsec.denominator).zero?
format("%09d", subsec * 1_000_000_000).sub(/0+$/, "")
else
subsec.to_s
end
end
def formatted_subsec_with_delimiter
subsec = formatted_subsec
if subsec.empty?
subsec
elsif subsec.include?("/")
" #{subsec}"
else
".#{subsec}"
end
end
end
end
......@@ -115,6 +115,18 @@ def test_strftime_with_escaping
def test_inspect
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
nsec = Time.utc(1986, 12, 12, 6, 23, 00, Rational(1, 1000))
nsec = ActiveSupport::TimeWithZone.new(nsec, @time_zone)
assert_equal "Fri, 12 Dec 1986 01:23:00.000000001 EST -05:00", nsec.inspect
handred_nsec = Time.utc(1986, 12, 12, 6, 23, 00, Rational(100, 1000))
handred_nsec = ActiveSupport::TimeWithZone.new(handred_nsec, @time_zone)
assert_equal "Fri, 12 Dec 1986 01:23:00.0000001 EST -05:00", handred_nsec.inspect
one_third_sec = Time.utc(1986, 12, 12, 6, 23, 00, Rational(1000000, 3))
one_third_sec = ActiveSupport::TimeWithZone.new(one_third_sec, @time_zone)
assert_equal "Fri, 12 Dec 1986 01:23:00 1/3 EST -05:00", one_third_sec.inspect
end
def test_to_s
......@@ -717,7 +729,7 @@ def test_beginning_of_year
def test_end_of_year
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59.999999999 EST -05:00", @twz.end_of_year.inspect
end
def test_beginning_of_month
......@@ -727,7 +739,7 @@ def test_beginning_of_month
def test_end_of_month
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59.999999999 EST -05:00", @twz.end_of_month.inspect
end
def test_beginning_of_day
......@@ -737,7 +749,7 @@ def test_beginning_of_day
def test_end_of_day
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
assert_equal "Fri, 31 Dec 1999 23:59:59.999999999 EST -05:00", @twz.end_of_day.inspect
end
def test_beginning_of_hour
......@@ -751,7 +763,7 @@ def test_end_of_hour
utc = Time.utc(2000, 1, 1, 0, 30)
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
assert_equal "Fri, 31 Dec 1999 19:59:59.999999999 EST -05:00", twz.end_of_hour.inspect
end
def test_beginning_of_minute
......@@ -765,7 +777,7 @@ def test_end_of_minute
utc = Time.utc(2000, 1, 1, 0, 30, 10)
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal "Fri, 31 Dec 1999 19:30:10 EST -05:00", twz.inspect
assert_equal "Fri, 31 Dec 1999 19:30:59 EST -05:00", twz.end_of_minute.inspect
assert_equal "Fri, 31 Dec 1999 19:30:59.999999999 EST -05:00", twz.end_of_minute.inspect
end
def test_since
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册