Forum: Ruby-core Time.at is inaccurate

9361878d459f1709feec780518946ee5?d=identicon&s=25 unknown (Guest)
on 2014-08-15 18:15
(Received via mailing list)
Issue #10135 has been updated by Yui NARUSE.

Related to Bug #10136: printf("%.60f\n", 0.1r) shows
0.100000000000000005551115123125782702118158340454101562500000 added

----------------------------------------
Bug #10135: Time.at is inaccurate
https://bugs.ruby-lang.org/issues/10135#change-48357

* Author: John Smart
* Status: Open
* Priority: High
* Assignee: ruby-core
* Category:
* Target version: current: 2.2.0
* ruby -v: ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
I believe I have found an issue with Time.at.  Many runtimes and DBs use
milliseconds since epoch for recording time.  Currently, the only way to
generate a Time in ruby with milliseconds is:

~~~
Time.at(milliseconds / 1000.0)
~~~

However, this is inaccurate:

~~~
> Time.at(1381089302195 / 1000.0).to_f
=> 1381089302.1949997
> Time.at(1381089302195 / 1000.0).strftime('%3N')
=> "194"
~~~

This doesn't make much sense because you would expect:

~~~
> 1381089302195 / 1000.0
=> 1381089302.195
> (1381089302195 / 1000.0).to_f
=> 1381089302.195
~~~

It seems that somewhere in the MRI, someone is using floating-point math
to represent a Time.

Is there any other way (workaround) to construct Time with millisecond
accuracy?
0e6ac58dab6125c1cd2e7ac645076b6f?d=identicon&s=25 Joel VanderWerf (Guest)
on 2014-08-15 19:38
(Received via mailing list)
> Is there any other way (workaround) to construct Time with millisecond accuracy?

Microsecond accuracy, even:

 >> Time.mktime(1993, 02, 24, 12, 0, 0, 123456).strftime("%6N")
=> "123456"
This topic is locked and can not be replied to.