summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-20 05:00:43 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-20 05:00:43 +0000
commit1686c0d470d56dcd7348351f0d1979d1ca1b4133 (patch)
treecf621b73303f4372802805f66a3a408b096fc8e5
parentb914bea88ea0371f0b37781a2d0db03759bd5a7e (diff)
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
-rw-r--r--spec/ruby/core/time/floor_spec.rb37
-rw-r--r--test/ruby/test_time.rb26
-rw-r--r--time.c70
3 files changed, 133 insertions, 0 deletions
diff --git a/spec/ruby/core/time/floor_spec.rb b/spec/ruby/core/time/floor_spec.rb
new file mode 100644
index 0000000000..763ed1ba51
--- /dev/null
+++ b/spec/ruby/core/time/floor_spec.rb
@@ -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
diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb
index 2bd4bc8455..cff7f1f0f0 100644
--- a/test/ruby/test_time.rb
+++ b/test/ruby/test_time.rb
@@ -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]"
diff --git a/time.c b/time.c
index aaec4c102a..c6cc0f6755 100644
--- a/time.c
+++ b/time.c
@@ -4232,6 +4232,75 @@ time_round(int argc, VALUE *argv, VALUE time)
}
/*
+ * 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);