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

Difference of DateTimes with fractional milliseconds calculated incorrectly #5493

Closed
ikaronen-relex opened this issue Dec 3, 2018 · 2 comments
Closed
Milestone

Comments

@ikaronen-relex
Copy link
Contributor

@ikaronen-relex ikaronen-relex commented Dec 3, 2018

The following appears to be a regression in DateTime handling in JRuby 9.2:

[1] pry(main)> JRUBY_VERSION
"1.7.27"
[2] pry(main)> (DateTime.new(2018, 1, 1, 0, 1, 29.000001) - DateTime.new(2018, 1, 1, 0, 0, 30)).to_f * 24 * 60 * 60
59.000001
[1] pry(main)> JRUBY_VERSION
"9.1.15.0"
[2] pry(main)> (DateTime.new(2018, 1, 1, 0, 1, 29.000001) - DateTime.new(2018, 1, 1, 0, 0, 30)).to_f * 24 * 60 * 60
59.000001
[1] pry(main)> JRUBY_VERSION
"9.2.4.1"
[2] pry(main)> (DateTime.new(2018, 1, 1, 0, 1, 29.000001) - DateTime.new(2018, 1, 1, 0, 0, 30)).to_f * 24 * 60 * 60
145.40000008881543

It appears that the DateTime subtraction code in JRuby 9.2 is incorrectly interpreting fractional milliseconds as fractional days. Thus, instead of the expected difference of 59.000001 seconds, the code is instead returning a difference of 59 + (60 * 60 * 24 * 0.001) = 59 + 86.4 = 145.4 seconds (plus some irrelevant floating point roundoff noise).

Ps. We've had issues with DateTime math in JRuby 9.2 before, and pull request #5369 by @massive was intended to address them. However, while commit 1360a4a appears to have made the specs included in that PR pass, it does not seem to have fully fixed the underlying problem(s), as the examples above demonstrate. Until this issue is properly fixed (or we find a reliable work-around), this is blocking JRuby 9.2 adoption for us.

@ikaronen-relex
Copy link
Contributor Author

@ikaronen-relex ikaronen-relex commented Dec 3, 2018

Indeed, looking at the code in https://github.com/jruby/jruby/blob/9.2.4.1/core/src/main/java/org/jruby/ext/date/RubyDate.java#L1210, I'm pretty sure this bug occurs because subMillisDiff() returns a difference measured in (fractional) milliseconds, but op_minus_date() adds it to a difference measured in days. Adding a division by DAY_MS somewhere in there should fix this.

@ikaronen-relex ikaronen-relex changed the title Difference of DateTimes with fractional seconds calculated incorrectly Difference of DateTimes with fractional milliseconds calculated incorrectly Dec 10, 2018
ikaronen-relex added a commit to ikaronen-relex/jruby that referenced this issue Dec 13, 2018
@headius headius added this to the JRuby 9.2.6.0 milestone Dec 13, 2018
@headius
Copy link
Member

@headius headius commented Dec 13, 2018

Thank you for the report and PR!

@kares kares closed this in 9eedcdd Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants