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

Add Time#ceil.

Closes: https://github.com/ruby/ruby/pull/2133
This commit is contained in:
manga_osyo 2019-03-16 14:42:24 +09:00 committed by Nobuyoshi Nakada
parent fe3ff5afb0
commit f5415a95ce
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
3 changed files with 133 additions and 0 deletions

View file

@ -0,0 +1,45 @@
require_relative '../../spec_helper'
ruby_version_is "2.7" do
describe "Time#ceil" do
before do
@time = Time.utc(2010, 3, 30, 5, 43, "25.0123456789".to_r)
end
it "defaults to ceiling to 0 places" do
@time.ceil.should == Time.utc(2010, 3, 30, 5, 43, 26.to_r)
end
it "ceils to 0 decimal places with an explicit argument" do
@time.ceil(0).should == Time.utc(2010, 3, 30, 5, 43, 26.to_r)
end
it "ceils to 2 decimal places with an explicit argument" do
@time.ceil(2).should == Time.utc(2010, 3, 30, 5, 43, "25.02".to_r)
end
it "ceils to 4 decimal places with an explicit argument" do
@time.ceil(4).should == Time.utc(2010, 3, 30, 5, 43, "25.0124".to_r)
end
it "ceils to 7 decimal places with an explicit argument" do
@time.ceil(7).should == Time.utc(2010, 3, 30, 5, 43, "25.0123457".to_r)
end
it "returns an instance of Time, even if #ceil is called on a subclass" do
subclass = Class.new(Time)
instance = subclass.at(0)
instance.class.should equal subclass
instance.ceil.should be_an_instance_of(Time)
end
it "copies own timezone to the returning value" do
@time.zone.should == @time.ceil.zone
with_timezone "JST-9" do
time = Time.at 0, 1
time.zone.should == time.ceil.zone
end
end
end
end

View file

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

62
time.c
View file

@ -4286,6 +4286,67 @@ time_floor(int argc, VALUE *argv, VALUE time)
return time_add(tobj, time, v, -1);
}
/*
* call-seq:
* time.ceil([ndigits]) -> new_time
*
* Ceils 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.0123456789".to_r)
* t.iso8601(10) #=> "2010-03-30T05:43:25.0123456789Z"
* t.ceil.iso8601(10) #=> "2010-03-30T05:43:26.0000000000Z"
* t.ceil(0).iso8601(10) #=> "2010-03-30T05:43:26.0000000000Z"
* t.ceil(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z"
* t.ceil(2).iso8601(10) #=> "2010-03-30T05:43:25.0200000000Z"
* t.ceil(3).iso8601(10) #=> "2010-03-30T05:43:25.0130000000Z"
* t.ceil(4).iso8601(10) #=> "2010-03-30T05:43:25.0124000000Z"
*
* t = Time.utc(1999,12,31, 23,59,59)
* (t + 0.4).ceil.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
* (t + 0.9).ceil.iso8601(3) #=> "2000-01-01T00:00:00.000Z"
* (t + 1.4).ceil.iso8601(3) #=> "2000-01-01T00:00:01.000Z"
* (t + 1.9).ceil.iso8601(3) #=> "2000-01-01T00:00:01.000Z"
*
* t = Time.utc(1999,12,31, 23,59,59)
* (t + 0.123456789).ceil(4).iso8601(6) #=> "1999-12-31T23:59:59.123500Z"
*/
static VALUE
time_ceil(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, subv(den, v), 1);
}
/*
* call-seq:
* time.sec -> integer
@ -5689,6 +5750,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, "ceil", time_ceil, -1);
rb_define_method(rb_cTime, "sec", time_sec, 0);
rb_define_method(rb_cTime, "min", time_min, 0);