Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing fractional seconds in Time#inspect #2194

Merged
merged 4 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/tags/core/time/inspect_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails:Time#inspect preserves milliseconds
fails:Time#inspect formats nanoseconds as a Rational
13 changes: 12 additions & 1 deletion src/main/ruby/truffleruby/core/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ 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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems better to refactor to_s to also use the str << (gmt? ? ' UTC' : strftime(' %z')) approach to make comparison easier, I'll do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I wasn’t sure of the performance impact of mutating the string so I erred on the side of leaving it alone, but I agree it’s better to make it look the same if there’s no downside.

end
alias_method :to_s, :inspect

def subsec
if nsec == 0
Expand Down