From: Tanaka Akira Date: 2010-03-11T15:09:45+09:00 Subject: [ruby-dev:40595] Time#round 前にちょっと書きましたが、Time#round を追加するのはどうでしょう。 これは、秒未満の時刻を丸めるメソッドです。 ちょうど [ruby-core:28602] にもありましたが、 Time オブジェクト内に誤差を含む値を入れてしまうことがあります。 まぁ、時刻に誤差があるのは測定のことも考えるとしかたがないと思うのですが、 誤差のある時刻を近似する値を表示することが難しいという問題があります。 strftime の %N は、小数点以下の秒数を表示するものなので、 仕様上、丸めるのは困難で、切り捨てにせざるを得ません。 丸めると、秒への繰り上がりが生じた時にどうするかという問題が生じます。 %N だけでなく、usec や nsec も同様な理由で切り捨てです。 このため、たとえば 0.9999999 秒を表示すると 0秒になって、 1秒を期待している人には、うまく動かないように見えることになります。 ここで、Time#round を使うと、小数点以下を四捨五入した Time オブジェクトを得られます。 % ./ruby -e ' t = Time.utc(1999,12,31, 23,59,59,876543) p t, t.subsec t = t.round p t, t.subsec' 1999-12-31 23:59:59 UTC (876543/1000000) 2000-01-01 00:00:00 UTC 0 また、Integer#round と同様に、省略可能な引数で桁数を指定することができます。 これにより、秒を小数点以下3桁で丸めて表示したければ、 strftime の前に round(3) を呼ぶことで実現できます。 % ./ruby -e ' t = Time.utc(1999,12,31, 23,59,59,876543) p t, t.subsec, t.strftime("%T.%3N") t = t.round(3) p t, t.subsec, t.strftime("%T.%3N") ' 1999-12-31 23:59:59 UTC (876543/1000000) "23:59:59.876" 1999-12-31 23:59:59 UTC (877/1000) "23:59:59.877" どうでしょう? % svn diff --diff-cmd diff -x '-u -p' Index: time.c =================================================================== --- time.c (revision 26871) +++ time.c (working copy) @@ -2997,6 +2997,64 @@ rb_time_succ(VALUE time) #define time_succ rb_time_succ +/* + * call-seq: + * time.round([ndigits]) => new_time + * + * Rounds sub seconds to a given precision in decimal digits (0 digits by default). + * It returns a new time object. + * _ndigits_ should be zero or positive integer. + * + * require 'time' + * + * t = Time.utc(1999,12,31, 23,59,59) + * p((t + 0.4).round.iso8601(3)) #=> "1999-12-31T23:59:59.000Z" + * p((t + 0.49).round.iso8601(3)) #=> "1999-12-31T23:59:59.000Z" + * p((t + 0.5).round.iso8601(3)) #=> "2000-01-01T00:00:00.000Z" + * p((t + 1.4).round.iso8601(3)) #=> "2000-01-01T00:00:00.000Z" + * p((t + 1.49).round.iso8601(3)) #=> "2000-01-01T00:00:00.000Z" + * p((t + 1.5).round.iso8601(3)) #=> "2000-01-01T00:00:01.000Z" + * + * t = Time.utc(1999,12,31, 23,59,59) + * p (t + 0.123456789).round(4).iso8601(6) #=> "1999-12-31T23:59:59.123500Z" + */ + +static VALUE +time_round(int argc, VALUE *argv, VALUE time) +{ + VALUE ndigits, v, a, b, den; + long nd; + struct time_object *tobj; + + rb_scan_args(argc, argv, "01", &ndigits); + + if (NIL_P(ndigits)) + 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 = rb_time_unmagnify(tobj->timexv); + + a = INT2FIX(1); + b = INT2FIX(10); + while (0 < nd) { + if (nd & 1) + a = mul(a, b); + b = mul(b, b); + nd = nd >> 1; + } + den = quo(INT2FIX(1), a); + v = mod(v, den); + if (lt(v, quo(den, INT2FIX(2)))) + return time_add(tobj, v, -1); + else + return time_add(tobj, sub(den, v), 1); +} /* * call-seq: @@ -3918,6 +3976,8 @@ Init_Time(void) rb_define_method(rb_cTime, "-", time_minus, 1); rb_define_method(rb_cTime, "succ", time_succ, 0); + rb_define_method(rb_cTime, "round", time_round, -1); + rb_define_method(rb_cTime, "sec", time_sec, 0); rb_define_method(rb_cTime, "min", time_min, 0); rb_define_method(rb_cTime, "hour", time_hour, 0); Index: test/ruby/test_time.rb =================================================================== --- test/ruby/test_time.rb (revision 26871) +++ test/ruby/test_time.rb (working copy) @@ -576,4 +576,37 @@ class TestTime < Test::Unit::TestCase assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r) assert_kind_of(Rational, Time.utc(1970).to_r) end + + def test_round + t = Time.utc(1999,12,31, 23,59,59) + t2 = (t+0.4).round + assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + t2 = (t+0.49).round + assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + t2 = (t+0.5).round + assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + t2 = (t+1.4).round + assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + t2 = (t+1.49).round + assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + t2 = (t+1.5).round + assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a) + assert_equal(0, t2.subsec) + + t2 = (t+0.123456789).round(4) + assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a) + assert_equal(Rational(1235,10000), t2.subsec) + + off = 0.0 + 100.times {|i| + t2 = (t+off).round(1) + assert_equal(Rational(i % 10, 10), t2.subsec) + off += 0.1 + } + end end -- [田中 哲][たなか あきら][Tanaka Akira]