[前][次][番号順一覧][スレッド一覧]

ruby-changes:55423

From: nobu <ko1@a...>
Date: Sat, 20 Apr 2019 14:00:48 +0900 (JST)
Subject: [ruby-changes:55423] nobu:r67632 (trunk): Add `Time#floor`

nobu	2019-04-20 14:00:43 +0900 (Sat, 20 Apr 2019)

  New Revision: 67632

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67632

  Log:
    Add `Time#floor`
    
    [Feature #15653]
    [Fix GH-2092]
    
    From: manga_osyo <manga.osyo@g...>

  Added files:
    trunk/spec/ruby/core/time/floor_spec.rb
  Modified files:
    trunk/test/ruby/test_time.rb
    trunk/time.c
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 67631)
+++ test/ruby/test_time.rb	(revision 67632)
@@ -970,6 +970,32 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L970
     }
   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]"
 
Index: time.c
===================================================================
--- time.c	(revision 67631)
+++ time.c	(revision 67632)
@@ -4232,6 +4232,75 @@ time_round(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L4232
 }
 
 /*
+ * 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) https://github.com/ruby/ruby/blob/trunk/time.c#L5699
 
     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);
Index: spec/ruby/core/time/floor_spec.rb
===================================================================
--- spec/ruby/core/time/floor_spec.rb	(nonexistent)
+++ spec/ruby/core/time/floor_spec.rb	(revision 67632)
@@ -0,0 +1,37 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/time/floor_spec.rb#L1
+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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]