From 849037b85447aad65fdd33295bf7ae865d8e4ae3 Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Thu, 17 Dec 2020 12:49:22 +0000 Subject: [PATCH 1/4] Duplicate Time#inspect as Time#to_s We need to change the behaviour of `#inspect` without affecting `#to_s`. --- src/main/ruby/truffleruby/core/time.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/ruby/truffleruby/core/time.rb b/src/main/ruby/truffleruby/core/time.rb index 78b687f8f2cb..6077035d9828 100644 --- a/src/main/ruby/truffleruby/core/time.rb +++ b/src/main/ruby/truffleruby/core/time.rb @@ -50,7 +50,15 @@ def inspect end str.force_encoding Encoding::US_ASCII end - alias_method :to_s, :inspect + + def to_s + if gmt? + str = strftime('%Y-%m-%d %H:%M:%S UTC') + else + str = strftime('%Y-%m-%d %H:%M:%S %z') + end + str.force_encoding Encoding::US_ASCII + end def subsec if nsec == 0 From 9539a33ac79dfa105f761b23e8eb1e3a07ee785b Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Thu, 17 Dec 2020 12:55:10 +0000 Subject: [PATCH 2/4] Append time zone separately in Time#inspect This creates the opportunity to append the fractional time first. --- src/main/ruby/truffleruby/core/time.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/ruby/truffleruby/core/time.rb b/src/main/ruby/truffleruby/core/time.rb index 6077035d9828..3b71616e6f90 100644 --- a/src/main/ruby/truffleruby/core/time.rb +++ b/src/main/ruby/truffleruby/core/time.rb @@ -43,11 +43,8 @@ class Time } def inspect - if gmt? - str = strftime('%Y-%m-%d %H:%M:%S UTC') - else - str = strftime('%Y-%m-%d %H:%M:%S %z') - end + str = strftime('%Y-%m-%d %H:%M:%S') + str << (gmt? ? ' UTC' : strftime(' %z')) str.force_encoding Encoding::US_ASCII end From 512cd0a5d636c142aed8513e43306488730908ea Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Thu, 17 Dec 2020 12:57:53 +0000 Subject: [PATCH 3/4] Show nanoseconds in Time#inspect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Although the Ruby specs don’t demonstrate it, we need to strip the trailing zeros. --- spec/tags/core/time/inspect_tags.txt | 1 - src/main/ruby/truffleruby/core/time.rb | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/tags/core/time/inspect_tags.txt b/spec/tags/core/time/inspect_tags.txt index a431a9fba062..3e66224cf157 100644 --- a/spec/tags/core/time/inspect_tags.txt +++ b/spec/tags/core/time/inspect_tags.txt @@ -1,2 +1 @@ -fails:Time#inspect preserves milliseconds fails:Time#inspect formats nanoseconds as a Rational diff --git a/src/main/ruby/truffleruby/core/time.rb b/src/main/ruby/truffleruby/core/time.rb index 3b71616e6f90..08ca497357b9 100644 --- a/src/main/ruby/truffleruby/core/time.rb +++ b/src/main/ruby/truffleruby/core/time.rb @@ -44,6 +44,12 @@ class Time def inspect str = strftime('%Y-%m-%d %H:%M:%S') + + if nsec != 0 + str << sprintf(".%09d", nsec) + str.chop! while str.end_with?('0') + end + str << (gmt? ? ' UTC' : strftime(' %z')) str.force_encoding Encoding::US_ASCII end From 191fe85b3f293d79e63d1b2a3b21c6afbb66ed53 Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Fri, 18 Dec 2020 12:46:00 +0000 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 734c3049963c..b58a660f7007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Compatibility: * Updated `Regexp.last_match` to support `Symbol` and `String` parameter (#2179). * Added support for numbered block parameters (`_1` etc). * Fixed `String#upto` issue with non-ascii strings (#2183). +* Include fractional seconds in `Time#inspect` output (#2194, @tomstuart). Performance: