Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow DateTime.jd to handle a single float argument

Previously, home_run ignored the fractional part.  This commit makes
home_run mirror the 1.9.3 behavior of raising an exception if a
floating point argument is first and there is more than a single
argument.
  • Loading branch information...
commit 640d53e45bf7dd189274addf584a30c5e8dc5ac4 1 parent f28c876
@jeremyevans authored
Showing with 21 additions and 1 deletion.
  1. +8 −1 ext/date_ext/datetime.c
  2. +13 −0 spec/datetime/constructor_spec.rb
View
9 ext/date_ext/datetime.c
@@ -743,6 +743,9 @@ static VALUE rhrdt_s_jd(int argc, VALUE *argv, VALUE klass) {
minute = NUM2LONG(argv[2]);
case 2:
hour = NUM2LONG(argv[1]);
+ if (TYPE(argv[0]) == T_FLOAT) {
+ rb_raise(rb_eArgError, "can't use float for first argument if providing multiple arguments");
+ }
case 1:
dt->jd = NUM2LONG(argv[0]);
break;
@@ -754,7 +757,11 @@ static VALUE rhrdt_s_jd(int argc, VALUE *argv, VALUE klass) {
RHR_CHECK_JD(dt)
dt->flags = RHR_HAVE_JD;
rhrdt__set_time(dt, hour, minute, second, offset);
- return rdt;
+ if (TYPE(argv[0]) == T_FLOAT) {
+ return rhrdt__add_days(rdt, NUM2DBL(argv[0]) - NUM2LONG(argv[0]));
+ } else {
+ return rdt;
+ }
}
/* call-seq:
View
13 spec/datetime/constructor_spec.rb
@@ -72,6 +72,19 @@
d.offset.should == 1/24.0
end
+ it ".jd handles a fractional date as first argument" do
+ d = DateTime.jd(2000.5)
+ d.jd.should == 2000
+ d.hour.should == 12
+ d.min.should == 0
+ d.sec.should == 0
+ d.offset.should == 0
+ end
+
+ it ".jd should raise an error if given a float as first argument and any additional arguments" do
+ proc{DateTime.jd(2000.5, 1)}.should raise_error(ArgumentError)
+ end
+
it ".jd should have defaults and an optional sg value" do
DateTime.jd.should == DateTime.jd(0, 0, 0, 0, 0)
DateTime.jd(0).should == DateTime.jd(0, 0, 0, 0, 0)
Please sign in to comment.
Something went wrong with that request. Please try again.