diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index c88b2607cddf5cfc83d2676e9aa805d10afc062c..fe5d75d78aa003025d0265856557694dadbcfb83 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,26 @@ +* 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 diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 49b874b6a6a4f1481193b436122a3aaa05bfd724..097d7be8f24299c72361646b9c1c40af4157c277 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -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 diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 02205ac47d6141de8a1b5a764a0658536d65fbda..3831b6558d53ffdd53a2dff50bf91c6edb0f51ba 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -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