1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Time#floor

[Feature #15653]
[Fix GH-2092]

From: manga_osyo <manga.osyo@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67632 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2019-04-20 05:00:43 +00:00
parent b914bea88e
commit 1686c0d470
3 changed files with 133 additions and 0 deletions

View file

@ -0,0 +1,37 @@
require_relative '../../spec_helper'
ruby_version_is "2.7" do
describe "Time#floor" do
before do
@time = Time.utc(2010, 3, 30, 5, 43, "25.123456789".to_r)
end
it "defaults to flooring to 0 places" do
@time.floor.should == Time.utc(2010, 3, 30, 5, 43, 25.to_r)
end
it "floors to 0 decimal places with an explicit argument" do
@time.floor(0).should == Time.utc(2010, 3, 30, 5, 43, 25.to_r)
end
it "floors to 7 decimal places with an explicit argument" do
@time.floor(7).should == Time.utc(2010, 3, 30, 5, 43, "25.1234567".to_r)
end
it "returns an instance of Time, even if #floor is called on a subclass" do
subclass = Class.new(Time)
instance = subclass.at(0)
instance.class.should equal subclass
instance.floor.should be_an_instance_of(Time)
end
it "copies own timezone to the returning value" do
@time.zone.should == @time.floor.zone
with_timezone "JST-9" do
time = Time.at 0, 1
time.zone.should == time.floor.zone
end
end
end
end

View file

@ -970,6 +970,32 @@ class TestTime < Test::Unit::TestCase
}
end
def test_floor
t = Time.utc(1999,12,31, 23,59,59)
t2 = (t+0.4).floor
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+0.49).floor
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+0.5).floor
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+1.4).floor
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+1.49).floor
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+1.5).floor
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
assert_equal(0, t2.subsec)
t2 = (t+0.123456789).floor(4)
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
assert_equal(Rational(1234,10000), t2.subsec)
end
def test_getlocal_dont_share_eigenclass
bug5012 = "[ruby-dev:44071]"

70
time.c
View file

@ -4231,6 +4231,75 @@ time_round(int argc, VALUE *argv, VALUE time)
return time_add(tobj, time, subv(den, v), 1);
}
/*
* call-seq:
* time.floor([ndigits]) -> new_time
*
* Floors sub seconds to a given precision in decimal digits (0 digits by default).
* It returns a new Time object.
* +ndigits+ should be zero or a positive integer.
*
* require 'time'
*
* t = Time.utc(2010,3,30, 5,43,"25.123456789".to_r)
* t.iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"
* t.floor.iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
* t.floor(0).iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z"
* t.floor(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z"
* t.floor(2).iso8601(10) #=> "2010-03-30T05:43:25.1200000000Z"
* t.floor(3).iso8601(10) #=> "2010-03-30T05:43:25.1230000000Z"
* t.floor(4).iso8601(10) #=> "2010-03-30T05:43:25.1234000000Z"
* t.floor(5).iso8601(10) #=> "2010-03-30T05:43:25.1234500000Z"
* t.floor(6).iso8601(10) #=> "2010-03-30T05:43:25.1234560000Z"
* t.floor(7).iso8601(10) #=> "2010-03-30T05:43:25.1234567000Z"
* t.floor(8).iso8601(10) #=> "2010-03-30T05:43:25.1234567800Z"
* t.floor(9).iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"
* t.floor(10).iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"
*
* t = Time.utc(1999,12,31, 23,59,59)
* (t + 0.4).floor.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
* (t + 0.49).floor.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
* (t + 0.5).floor.iso8601(3) #=> "1999-12-31T23:59:59.000Z"
* (t + 1.4).floor.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
* (t + 1.49).floor.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
* (t + 1.5).floor.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
*
* t = Time.utc(1999,12,31, 23,59,59)
* (t + 0.123456789).floor(4).iso8601(6) #=> "1999-12-31T23:59:59.123400Z"
*/
static VALUE
time_floor(int argc, VALUE *argv, VALUE time)
{
VALUE ndigits, v, a, b, den;
long nd;
struct time_object *tobj;
if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
ndigits = INT2FIX(0);
else
ndigits = rb_to_int(ndigits);
nd = NUM2LONG(ndigits);
if (nd < 0)
rb_raise(rb_eArgError, "negative ndigits given");
GetTimeval(time, tobj);
v = w2v(rb_time_unmagnify(tobj->timew));
a = INT2FIX(1);
b = INT2FIX(10);
while (0 < nd) {
if (nd & 1)
a = mulv(a, b);
b = mulv(b, b);
nd = nd >> 1;
}
den = quov(INT2FIX(1), a);
v = modv(v, den);
return time_add(tobj, time, v, -1);
}
/*
* call-seq:
* time.sec -> integer
@ -5630,6 +5699,7 @@ Init_Time(void)
rb_define_method(rb_cTime, "succ", time_succ, 0);
rb_define_method(rb_cTime, "round", time_round, -1);
rb_define_method(rb_cTime, "floor", time_floor, -1);
rb_define_method(rb_cTime, "sec", time_sec, 0);
rb_define_method(rb_cTime, "min", time_min, 0);