ruby / ruby/psych

Time objects fail to round-trip with equality

Open
#402 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

I ran the following script:

require 'yaml'

puts Psych::VERSION

def compare(time)
  dumped = YAML.dump(time)
  loaded = YAML.load(YAML.dump(time))
  equal = loaded == time

  pp(
    original: time,
    loaded: loaded,
    equal: equal,
#    nsec: { original: time.nsec, loaded: loaded.nsec, equal: time.nsec == loaded.nsec }
    float: { original: time.to_f, loaded: loaded.to_f, equal: time.to_f == loaded.to_f }
  )
end

compare(Time.new(2002, 4, 5, 6, 7, 8.9992321))
compare(YAML.load YAML.dump Time.new(2002, 4, 5, 6, 7, 8.9992321))

And got the following output:

3.0.2
{:original=>2002-04-05 06:07:08 -0800,
 :loaded=>2002-04-05 06:07:08 -0800,
 :equal=>false,
 :float=>
  {:original=>1018015628.999232, :loaded=>1018015628.9992322, :equal=>false}}
{:original=>2002-04-05 06:07:08 -0800,
 :loaded=>2002-04-05 06:07:08 -0800,
 :equal=>true,
 :float=>
  {:original=>1018015628.9992322, :loaded=>1018015628.9992322, :equal=>true}}

I would expect both Time instances to compare equal to their dumped & loaded counterpart, but the first does not. By inspection, we can see that the Time#to_f of the original & loaded instances are different, which I'd guess is what's breaking Time#==. Curiously, the Time#nsec property is preserved properly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided Ruby script and inspect the YAML.dump/YAML.load round trip for Time objects, comparing Time#==, Time#to_f, and Time#nsec as shown. Done means the original and loaded Time instances compare equal, including the reported fractional-second case.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.