Skip to content

Wrong julian day for DateTime with a negative rational offset #1770

@robin850

Description

@robin850

Hello,

There seems to be a problem with the Julian day of a DateTime object when the latter has been initialized specifying a negative offset. With JRuby 1.7.13 and MRI 2.1.2:

>> require 'date'
=> true
>> without_offset = DateTime.civil(1999, 12, 31, 19, 0, 0);
>> with_offset = DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-5, 24))

>> RUBY_ENGINE
=> "jruby"
>> without_offset.jd
=> 2451544
>> with_offset.jd
=> 2451545 # Incremented by 1

>> RUBY_ENGINE
=> "ruby"
>> without_offset.jd
=> 2451544
>> with_offset.jd
=> 2451544

The bug isn't reproducible with any Rational object. It looks like it should be negative and close to 0 (e.g. -5 / 24.0 works but 5 / 24.0 don't). Here's a failing test:

diff --git a/test/mri/date/test_date_new.rb b/test/mri/date/test_date_new.rb
index 0bbbfee..bebafbe 100644
--- a/test/mri/date/test_date_new.rb
+++ b/test/mri/date/test_date_new.rb
@@ -31,6 +31,13 @@ class TestDateNew < Test::Unit::TestCase
     end
   end

+  def test_jd__with_offset
+    without_offset = DateTime.civil(1999, 12, 31, 19, 0, 0)
+    with_offset = DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-5, 24))
+
+    assert_equal without_offset.jd, with_offset.jd
+  end
+
   def test_ordinal
     d = Date.ordinal
     dt = DateTime.ordinal

Have a nice day.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions