From: Dave Burt Date: 2006-05-18T00:01:59+09:00 Subject: Fwd: Bug: Time#-(1e-6) doesn't substract one microsecond. --------------060404030306080704040401 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi, The following minor patch proposal was recently posted on comp.lang.ruby. Cheers, Dave -----Original Message----- Subject: Bug: Time#-(1e-6) doesn't substract one microsecond. From: Dmitry Maksyoma Date: 17 May 2006 15:53 Newsgroups: comp.lang.ruby Hello, Please consider the following test: t = Time.mktime(2006, 5, 1) puts t, "sec: #{t.sec}, usec: #{t.usec}" t2 = t - 1e-6 puts t2, "sec: #{t2.sec}, usec: #{t2.usec}" t2 = t - 2e-6 puts t2, "sec: #{t2.sec}, usec: #{t2.usec}" Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999999 Substracting one microsecond doesn't do anything, substracting 2 microseconds actually substracts one microsecond. The problem is that (time_t)1e-6*1e6 yields 0, so I changed it to (time_t)lround(1e-6*1e6). This way the same Ruby code yields: Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999999 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999998 --- /home/dmaks/time.c 2006-05-16 19:50:35.000000000 +1200 +++ ruby-1.8.2/time.c 2006-05-17 13:29:50.000000000 +1200 @@ -1285,7 +1285,7 @@ if (f != (double)sec_off) rb_raise(rb_eRangeError, "time %s %f out of Time range", sign < 0 ? "-" : "+", v); - usec_off = (time_t)(d*1e6); + usec_off = (time_t)lround(d*1e6); if (sign < 0) { sec = tobj->tv.tv_sec - sec_off; --------------060404030306080704040401 Content-Type: message/rfc822; name="Bug: Time#-(1e-6) doesn't substract one microsecond." Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename*0="Bug: Time#-(1e-6) doesn't substract one microsecond." Path: news-server.bigpond.net.au!ken-transit.news.telstra.net!ken-in.news.telstra.net!news.telstra.net!newsfeed.media.kyoto-u.ac.jp!nntp.gol.com!newsfeed-00.mathworks.com!newscon06.news.prodigy.com!newscon02.news.prodigy.com!prodigy.net!nx02.iad01.newshosting.com!newshosting.com!198.186.190.250.MISMATCH!transit3.readnews.com!news-out.readnews.com!postnews.google.com!j55g2000cwa.googlegroups.com!not-for-mail From: "Dmitry Maksyoma" Newsgroups: comp.lang.ruby Subject: Bug: Time#-(1e-6) doesn't substract one microsecond. Date: 16 May 2006 22:53:57 -0700 Organization: http://groups.google.com Message-ID: <1147845237.858367.88740@j55g2000cwa.googlegroups.com> NNTP-Posting-Host: 202.55.97.147 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1147845242 16944 127.0.0.1 (17 May 2006 05:54:02 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 17 May 2006 05:54:02 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Opera/8.54 (X11; Linux i686; U; en),gzip(gfe),gzip(gfe) X-HTTP-Via: 1.0 serv-2.alb-nz.esphion.com:3128 (squid/2.5.STABLE9) Complaints-To: groups-abuse@google.com Injection-Info: j55g2000cwa.googlegroups.com; posting-host=202.55.97.147; posting-account=zp_Klg0AAABsXVVyRY2w5IJ775FPUGR6 Xref: news-server.bigpond.net.au comp.lang.ruby:111533 Hello, Please consider the following test: t = Time.mktime(2006, 5, 1) puts t, "sec: #{t.sec}, usec: #{t.usec}" t2 = t - 1e-6 puts t2, "sec: #{t2.sec}, usec: #{t2.usec}" t2 = t - 2e-6 puts t2, "sec: #{t2.sec}, usec: #{t2.usec}" Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999999 Substracting one microsecond doesn't do anything, substracting 2 microseconds actually substracts one microsecond. The problem is that (time_t)1e-6*1e6 yields 0, so I changed it to (time_t)lround(1e-6*1e6). This way the same Ruby code yields: Mon May 01 00:00:00 NZST 2006 sec: 0, usec: 0 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999999 Sun Apr 30 23:59:59 NZST 2006 sec: 59, usec: 999998 --- /home/dmaks/time.c 2006-05-16 19:50:35.000000000 +1200 +++ ruby-1.8.2/time.c 2006-05-17 13:29:50.000000000 +1200 @@ -1285,7 +1285,7 @@ if (f != (double)sec_off) rb_raise(rb_eRangeError, "time %s %f out of Time range", sign < 0 ? "-" : "+", v); - usec_off = (time_t)(d*1e6); + usec_off = (time_t)lround(d*1e6); if (sign < 0) { sec = tobj->tv.tv_sec - sec_off; --------------060404030306080704040401--