=begin
Due to the precision of timestamp that Psych dumps, dumped time loaded
by Psych is not always identical to the original time.
require 'psych'
time = Time.at(Time.now.sec)
p time == (Psych.load Psych.dump time) #=> true
time = Time.now
p time == (Psych.load Psych.dump time) #=> false (in my environment)
time = Time.at(Time.now.sec, Rational(1, 3))
p time == (Psych.load Psych.dump time) #=> false
According to the YAML timestamp specification [http://yaml.org/type/timestamp.html],
the standard format cannot represent time in rational.
Thus, to fully express Ruby's Time object, we have to use a format
other than the standard YAML timestamp.
=end
=begin
I don't believe any change is required here. Psych isn't trying to dump Ruby objects for reloading into Ruby - it's trying to dump Ruby objects so they can be loaded back by ANY language that has a YAML parser. As with most language-independent specs - YAML unfortunately doesn't have as much precision as Ruby does in this area - but for YAML it's more important to be language-independent than full precision over and above the spec.
If one wishes to dump an object and reload it with full fidelity - isn't that the job of Marshall?
As a side note - JSON exhibits the exact same behavior here - again it's trying to be language-independent rather than full precision.
=end
How should this problem be fixed? If a time object when dumped as YAML
does not use the YAML time format, that would be surprising to me. Is
my assumption wrong? I'd like to leave it the way it works now.
If the fractional seconds are representable in the decimal system,
it should be representable in the YAML time format.
For example, Ruby obtains the current time using clock_gettime if available.
clock_gettime returns struct timespec which has nano-second resolution.
It needs 9 digits to preserve the information.
But psych dumps only 6 digits.
This is why Psych.load(Psych.dump(Time.now)) doesn't round trip.
FreeBSD has bintime which is 2**(-64) second resolution.
It is also representable in the decimal system.
(It may need 64 digits, though.)
Ruby generates such time in Socket::AncillaryData#timestamp.
=begin
This issue was solved with changeset r28531.
Tomo, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.