summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanga_osyo <manga.osyo@gmail.com>2019-03-16 14:42:24 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-05-23 22:30:19 +0900
commitf5415a95ce1d393a3fd1d7f657ba85d85171356a (patch)
tree6cbc9cdcba1020be455ed96bf7e90cd28fc7b7e1
parentfe3ff5afb07e171fd950623c69abfbabbb2762a3 (diff)
Add `Time#ceil`.
Closes: https://github.com/ruby/ruby/pull/2133
-rw-r--r--spec/ruby/core/time/ceil_spec.rb45
-rw-r--r--test/ruby/test_time.rb26
-rw-r--r--time.c62
3 files changed, 133 insertions, 0 deletions
diff --git a/spec/ruby/core/time/ceil_spec.rb b/spec/ruby/core/time/ceil_spec.rb
new file mode 100644
index 0000000000..29dcec5d72
--- /dev/null
+++ b/spec/ruby/core/time/ceil_spec.rb
@@ -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
diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb
index cff7f1f0f0..4c0a104976 100644
--- a/test/ruby/test_time.rb
+++ b/test/ruby/test_time.rb
@@ -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]"
diff --git a/time.c b/time.c
index ac9a13d7fd..d9729aee30 100644
--- a/time.c
+++ b/time.c
@@ -4287,6 +4287,67 @@ time_floor(int argc, VALUE *argv, VALUE time)
}
/*
+ * 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);