psych
psych copied to clipboard
Time objects fail to round-trip with equality
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.