Skip to content

Convert a Float when loading the old marshal format - #213

Open
wtn wants to merge 1 commit into
ruby:masterfrom
wtn:fraction
Open

wtn wants to merge 1 commit into
ruby:masterfrom
wtn:fraction

Conversation

@wtn

@wtn wtn commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

In Ruby 1.8 and 1.9.2, the dump of a date has a Float day number after a Float is added to the date:

# Ruby 1.9.2
d = DateTime.new(2000, 1, 1) + 0.25
d.marshal_dump # => [2451544.75, 0, 2299161]

But a DateTime loaded from the array does not work as a Hash key. The loaded DateTime is eql? to a DateTime from DateTime.new for the same time, and the two have different hashes:

d = DateTime.allocate
d.marshal_load([2451544.75, 0, 2299161])
e = DateTime.new(2000, 1, 1, 6)
d.eql?(e)        # => true
d.hash == e.hash # => false
{d => 1}[e]      # => nil
d.sec_fraction   # => 0.0
e.sec_fraction   # => (0/1)

The hashes differ because marshal_load gives the loaded DateTime a Float second fraction, where DateTime.new gives an Integer or a Rational.

Fix

marshal_load now converts a Float day number to a Rational before the second fraction is computed. In the example, d.sec_fraction is (0/1), the two hashes are equal, and {d => 1}[e] returns 1.

In Ruby 1.8 and 1.9.2, Date#+ with a Float stored a Float day number,
and the dump has the Float. old_to_new kept a Float second fraction for
the dump, and the loaded date was eql? to a date with a different hash.
old_to_new now converts a Float day number to a Rational.

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
@wtn
wtn marked this pull request as ready for review November 28, 2026 23:02

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess this is OK, though I find it unlikely that anyone has marshal data from a Ruby version that old with float dates.

Comment on lines +89 to +95

d = Date.allocate
d.marshal_load([0.5, Date::ITALY])
d2 = Date.allocate
d2.marshal_load([Rational(1, 2), Date::ITALY])
assert_equal(d2, d)
assert_equal(d2.hash, d.hash)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Support for fractional dates in Date is already deprecated, so I don't think we need a test for this. If we do add a test for it, we should change the marshal load support so it issues a deprecation warning when attempting to restore a Date with a fractional day.

Suggested change
d = Date.allocate
d.marshal_load([0.5, Date::ITALY])
d2 = Date.allocate
d2.marshal_load([Rational(1, 2), Date::ITALY])
assert_equal(d2, d)
assert_equal(d2.hash, d.hash)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants