diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-03-11 13:39:12 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-03-11 13:39:12 +0000 |
commit | 7c269e72efa9e37150831e6d5d37f4f6f5622d20 (patch) | |
tree | 07d1c93064249f938509784910db2f1c8064f75f | |
parent | c2f7ed2f243840ff5e3b0e04e06efcf8a3a0cb6a (diff) |
* time.c (time_round): new method Time#round. [ruby-dev:40595]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | test/ruby/test_time.rb | 33 | ||||
-rw-r--r-- | time.c | 60 |
3 files changed, 97 insertions, 0 deletions
@@ -1,3 +1,7 @@ +Thu Mar 11 22:35:27 2010 Tanaka Akira <akr@fsij.org> + + * time.c (time_round): new method Time#round. [ruby-dev:40595] + Thu Mar 11 17:31:59 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> * transcode.c (str_transcode0): confirm the code range. diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index c583b4e873..6ccac25553 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -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 @@ -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); |