[Bug:trunk] rubyspec: Time#+ rounds micro seconds rather than truncates FAILED

akr e$B$5$se(B
e$B1sF#$G$9!#e(B

Evan P. e$B$He(B Time e$B$N5sF0$K$D$$$FAjCL$7$?>e$Ge(B Time
e$B$K$D$$$F0l1~$Ne(B
e$B3NG’$G$9!#e(B

  • 1.8 e$B$Ne(B Time#+ e$B$Oe(B usec e$B$G;M<N8^F~$7$F$$$^$9$,!“e(B1.9
    e$B$G$O$7$^$;$s!#e(B
    e$BFbItI=8=$,e(B Rational
    [email protected][email protected]$H;W$$$^$9$,!”;EMMJQ99e(B
    e$B$G$9$h$M!#e(B

  • 1.8 e$B$Ne(B Time#usec e$B$de(B nsec
    e$B$,;M<N8^F~$7$J$$$N$O;EMM$G$9$h$M!#e(B
    usec e$B$,e(B 1000000 e$B$H$+$K$J$C$?$i7y$G$9$b$N$M!#e(B

e$B$=$&[email protected][email protected]$C$F$/[email protected]$5$l$P!"0J2<$N%3%%C%H$re(B rubyspec
e$B$K%3%
%C%He(B
e$B$7$h$&$H;W$$$^$9!#e(B

diff --git a/core/time/plus_spec.rb b/core/time/plus_spec.rb
index 1f5c85f…e333430 100644
— a/core/time/plus_spec.rb
+++ b/core/time/plus_spec.rb
@@ -10,21 +10,33 @@ describe “Time#+” do
(Time.at(1.1) + 0.9).should == Time.at(0.9) + 1.1
end

  • it “rounds micro seconds rather than truncates” do
  • The use of 8.9999999 is intentional. This is because

  • Time treats the fractional part as the number of micro seconds.

  • Thusly it multiplies the result by 1_000_000 to go from

  • seconds to microseconds. That conversion should be rounded

  • properly. In this case, it’s rounded up to 1,000,000, and thus

  • contributes a full extra second to the Time object.

  • t = Time.at(0) + 8.9999999
  • t.should == Time.at(9)
  • t.usec.should == 0
  • ruby_version_is “” … “1.9” do
  • it “rounds micro seconds rather than truncates” do
  •  # The use of 8.9999999 is intentional. This is because
    
  •  # Time treats the fractional part as the number of micro seconds.
    
  •  # Thusly it multiplies the result by 1_000_000 to go from
    
  •  # seconds to microseconds. That conversion should be rounded
    
  •  # properly. In this case, it's rounded up to 1,000,000, and thus
    
  •  # contributes a full extra second to the Time object.
    
  •  t = Time.at(0) + 8.9999999
    
  •  t.should == Time.at(9)
    
  •  t.usec.should == 0
    
  •  # Check the non-edge case works properly, that the fractional 
    

part

  •  # contributes to #usecs
    
  •  t2 = Time.at(0) + 8.9
    
  •  t2.usec.should == 900000
    
  • end
  • end
  • Check the non-edge case works properly, that the fractional part

  • contributes to #usecs

  • t2 = Time.at(0) + 8.9
  • t2.usec.should == 900000
  • ruby_version_is “1.9” do
  • it “does NOT round” do
  •  t = Time.at(0) + Rational(8_999_999_999_999_999, 
    

1_000_000_000_000_000)

  •  t.should_not == Time.at(9)
    
  •  t.usec.should == 999_999
    
  •  t.nsec.should == 999_999_999
    
  •  t.subsec.should == Rational(999_999_999_999_999, 
    

1_000_000_000_000_000)

  • end
    end

ruby_version_is “” … “1.9” do

2010e$BG/e(B2e$B7ne(B11e$BF|e(B2:58 Yusuke ENDOH [email protected]:

  • 1.8 e$B$Ne(B Time#+ e$B$Oe(B usec e$B$G;M<N8^F~$7$F$$$^$9$,!“e(B1.9 e$B$G$O$7$^$;$s!#e(B
    e$BFbItI=8=$,e(B Rational [email protected][email protected]$H;W$$$^$9$,!”;EMMJQ99e(B
    e$B$G$9$h$M!#e(B

1.9.2 e$B$G$O$=$&$G$9!#e(B
1.9.1 e$B$NFbItI=8=$Oe(B nsec e$BC10L$J$N$G!"e(Bnsec
e$B$G;M<N8^F~$J$s$G$O$J$$$+$H;W$$$^$9!#e(B

  • 1.8 e$B$Ne(B Time#usec e$B$de(B nsec e$B$,;M<N8^F~$7$J$$$N$O;EMM$G$9$h$M!#e(B
    usec e$B$,e(B 1000000 e$B$H$+$K$J$C$?$i7y$G$9$b$N$M!#e(B

1.8 e$B$Ke(B nsec
e$B$O$"$j$^$;$s$,!"$^$!$=$&[email protected]$J$$$G$9$+$M!#e(B

e$B7+$j>e$,[email protected]$,$"$k>l9g$O$I$&$+$J$!!#e(B

e$B1sF#$G$9!#e(B

2010e$BG/e(B2e$B7ne(B11e$BF|e(B21:36 Tanaka A. [email protected]:

usec e$B$,e(B 1000000 e$B$H$+$K$J$C$?$i7y$G$9$b$N$M!#e(B

1.8 e$B$Ke(B nsec e$B$O$"$j$^$;$s$,!"$^$!$=$&[email protected]$J$$$G$9$+$M!#e(B

e$B$"$j$,$H$&$4$6$$$^$9!#%3%_%C%H$5$;$FD:$-$^$7$?!#e(B

e$B$A$J$$Ke(B rubyspec e$B$Oe(B 1.9.1
e$B$N;EMM$rL5;k$9$kJ}?K$i$7$$$G$9e(B (1.9.1 e$B$He(B
1.9.2 e$B$N4V$G;EMMJQ99$,$"[email protected]$+$ie(B) e$B!#$J$N$G!“e(B1.9
e$B$H=q$$$F$”$l$Pe(B
1.9.2 e$B0J9
$r;X$9$H$$$&$3$H$G$9!#e(B

e$B7+$j>e$,[email protected]$,$"$k>l9g$O$I$&$+$J$!!#e(B

e$B8D?ME*[email protected]$j<N$F$,$o$+$j$d$9$$$N$G$=$l$G$$$$$H;W$$$^$9$,!"JQ$($ke(B
e$B$J$iJQ$($F$b$i$C$F$b9=$o$J$$$G$9!#e(Brubyspec e$BB&[email protected]$7$^$9!#e(B

個人的には切り捨てがわかりやすいのでそれでいいと思いますが、変える
なら変えてもらっても構わないです。rubyspec 側を修正します。

仕掛けがわかりやすいからといって結果がわかりやすいかというと
微妙なのではないかと。

切り捨てで処理するのは崖っぷちを歩くようなもので、
ひとつ間違えると意図せざる結果になりがちです。

とりあえず Time#round(ndigits) とかどうかなぁ。

Timeの元データは何があるんでしょう?一番苦情がくるのは oldtime > newtime または oldtime > now
になってしまうケースで、oldtimeが精度落とすときに切り上げされていてnowが高精度だったりすると
起こせそうな気がします。

個人的には切り捨てに一票で、理由は「え、思ってたよりちょっと古い時刻だった?そりゃ測定してるうちにも時間は
経過するからねHAHAHA」でほとんどのケースは済んでしまうコーナーケースの少なさに利点があるかと。

2010e$BG/e(B2e$B7ne(B11e$BF|e(B21:58 Yusuke ENDOH [email protected]:

e$B8D?ME*[email protected]$j<N$F$,$o$+$j$d$9$$$N$G$=$l$G$$$$$H;W$$$^$9$,!"JQ$($ke(B
e$B$J$iJQ$($F$b$i$C$F$b9=$o$J$$$G$9!#e(Brubyspec e$BB&[email protected]$7$^$9!#e(B

e$B;E3]$1$,$o$+$j$d$9$$$+$i$H$$$C$F7k2L$,$o$+$j$d$9$$$+$H$$$&$He(B
e$BHyL/$J$N$G$O$J$$$+$H!#e(B

[email protected]$j<N$F$G=hM}$9$k$N$O33$C$W$A$rJb$/$h$&$J$b$N$G!"e(B
e$B$R$H$D4V0c$($k$H0U?^$;$6$k7k2L$K$J$j$,$A$G$9!#e(B

e$B$H$j$"$($:e(B Time#round(ndigits) e$B$H$+$I$&$+$J$!!#e(B

2010e$BG/e(B2e$B7ne(B11e$BF|e(B23:01 KOSAKI Motohiro
[email protected]:

Timee$B$N85%G!<%?$O2?$,$"$k$s$G$7$g$&!)0lHV6l>p$,$/$k$N$Oe(B oldtime > newtime e$B$^$?$Oe(B oldtime > now
e$B$K$J$C$F$7$^$&%1!<%9$G!"e(Boldtimee$B$,@:[email protected]$j>e$2$5$l$F$$$Fe(Bnowe$B$,[email protected]:[email protected]$C$?$j$9$k$He(B
e$B5/$3$;$=$&$J5$$,$7$^$9!#e(B

e$B$9$0$K;W$$=P$;$k$N$Oe(B

  • e$B8=:_;~9oe(B
    gettimeofday e$B$N%^%$%/%mICe(B
    clock_gettime e$B$N%J%NICe(B
  • e$B%U%!%$%k$N%?%$%`%9%?%s%We(B
    st_mtime e$B$J$I$NICe(B
    st_mtim e$B$J$I$N%J%NICe(B
  • e$B%Q%1%C%H$N%?%$%`%9%?%s%We(B
    SCM_TIMESTAMP e$B$N%^%$%/%mICe(B
    SCM_TIMESTAMPNS e$B$N%J%NICe(B
    SCM_BINTIME e$B$Ne(B 2**(-64)e$BICe(B
  • Ruby e$BFbe(B
    DateTime e$B$NM-M}?te(B
  • e$B3F<o%U%)!<%^%C%He(B
    ISO 8601 e$B$Oe(B10e$B?J?t$G>[email protected]<G$0U7eICe(B
    e$B$/$i$$$+$J$!!#e(B

[email protected]$Ke(B Time e$B$KJQ49$G$-$^$9!#e(B
e$B$^$!!"e(BTime e$B$NCf?H$Oe(B (e$B35G0E*$K$Oe(B) e$BM-M}?t$J$s$G!#e(B

e$B8D?ME*[email protected]$j<N$F$K0lI<$G!“M}M3$O!V$(!”;W$C$F$?$h$j$A$g$C$H8E$$;[email protected]$C$?!)$=$j$cB,Dj$7$F$k$&$A$K$b;~4V$Oe(B
e$B7P2a$9$k$+$i$Me(BHAHAHAe$B!W$G$[$H$s$I$N%1!<%9$O:Q$s$G$7$^$&%3!<%J!<%1!<%9$N>/[email protected]$,$"$k$+$H!#e(B

e$B$J$k$[$I!#e(B

e$B$^$!!"$=$N$&$Ae(B

% ./ruby -ve ’
t = Time.new(1989,3,3)
p t, t + 0.3 + 0.7’
ruby 1.9.2dev (2010-02-11 trunk 26640) [i686-linux]
1989-03-03 00:00:00 +0900
1989-03-03 00:00:00 +0900

e$B$,[email protected][email protected]$$=P$9$R$H$,=P$F$/$k$s$8$c$J$$$+$J$!!"$H$$$&$/$i$$$NOC$J$N$G!“e(B
e$B8=<BE*[email protected]$j<[email protected]$,B?$$$J$i%G%U%)%k%H$=$C$A$H$$$&$N$O$”$j$&$ke(B
e$BA*Br;h$+$J$!!#e(B

e$B$=$l$O4]$a$?$$$H$-$K;H$($k%a%=%C%I$,$"$k$3$H$H$OL7=b$7$^$;$s$,!#e(B

e$B%A%1%C%He(B #2733 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)

e$B%9%F!<%?%9e(B Assignede$B$+$ie(BRejectede$B$KJQ99e(B

e$B1sF#$G$9!#e(B

e$B2r7h:Q$_$J$N$Ge(B close e$B$7$^$9!#e(B


Yusuke E. [email protected]

http://redmine.ruby-lang.org/issues/show/2733

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs