Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Fix DateTime#sec_fraction on ruby 1.9
Browse files Browse the repository at this point in the history
Previously, it had the 1.8 behavior of returning the fraction of
the second as a fraction of the day, even on 1.9.  This increases
compatibility with the 1.9 stdlib.

This also adds a spec for the :sec_fraction entry of Date._parse,
though that is the fraction of the second on both 1.8 and 1.9.
  • Loading branch information
jeremyevans committed Sep 2, 2010
1 parent cb182c7 commit 2fa6fb7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
12 changes: 11 additions & 1 deletion ext/date_ext/datetime.c
Expand Up @@ -1400,17 +1400,27 @@ static VALUE rhrdt_sec(VALUE self) {
/* call-seq:
* sec_fraction() -> Float
*
* Returns a +Float+ representing the fraction of the second as a
* On ruby 1.8, returns a +Float+ representing the fraction of the second as a
* fraction of the day, which will always be in the range [0.0, 1/86400.0).
*
* (DateTime.civil(2009, 1, 2, 12, 13, 14) + (1.5/86400)).sec_fraction
* # => 0.000005787037
*
* On ruby 1.9, returns a +Float+ representing the fraction of the second, which
* will always be in the range [0,1).
*
* (DateTime.civil(2009, 1, 2, 12, 13, 14) + (1.5/86400)).sec_fraction
* # => 0.5
*/
static VALUE rhrdt_sec_fraction(VALUE self) {
rhrdt_t *dt;
Data_Get_Struct(self, rhrdt_t, dt);
RHRDT_FILL_NANOS(dt)
#ifdef RUBY19
return rb_float_new((double)(dt->nanos % RHR_NANOS_PER_SECOND)/RHR_NANOS_PER_SECONDD);
#else
return rb_float_new((double)(dt->nanos % RHR_NANOS_PER_SECOND)/RHR_NANOS_PER_DAYD);
#endif
}

/* call-seq:
Expand Down
17 changes: 13 additions & 4 deletions spec/datetime/accessor_spec.rb
Expand Up @@ -40,10 +40,19 @@
end

describe "DateTime#sec_fraction" do
it "should be the fraction of a second as a fraction of the day" do
DateTime.new(2008, 1, 1).sec_fraction.should == 0.0
DateTime.parse('12:13:15.678900').sec_fraction.should be_close(7.85763888888889e-06, 0.0000000000001)
end
ruby_version_is "" ... "1.9" do
it "should be the fraction of a second as a fraction of the day" do
DateTime.new(2008, 1, 1).sec_fraction.should == 0.0
DateTime.parse('12:13:15.678900').sec_fraction.should be_close(7.85763888888889e-06, 0.0000000000001)
end
end

ruby_version_is "1.9" do
it "should be the fraction of a second" do
DateTime.new(2008, 1, 1).sec_fraction.should == 0.0
DateTime.parse('12:13:15.678900').sec_fraction.should be_close(0.6789, 0.0000000000001)
end
end
end

describe "DateTime#zone" do
Expand Down
11 changes: 10 additions & 1 deletion spec/datetime/parse_spec.rb
@@ -1,7 +1,16 @@
require File.expand_path('../../spec_helper', __FILE__)

describe "DateTime#parse" do
describe "DateTime._parse" do
it "should return a hash of values" do
DateTime._parse("01:02:03").should == {:hour=>1, :min=>2, :sec=>3}
end

it "should have :sec_fraction entry be the fraction of second" do
DateTime._parse('12:13:15.678900')[:sec_fraction].should be_close(0.6789, 0.0000000000001)
end
end

describe "DateTime.parse" do
it "should have defaults and an optional sg value" do
DateTime.parse('2008-10-11').should == DateTime.civil(2008, 10, 11)
DateTime.parse('2008-10-11', true).should == DateTime.civil(2008, 10, 11)
Expand Down

0 comments on commit 2fa6fb7

Please sign in to comment.