From: akr@... Date: 2014-08-26T06:02:23+00:00 Subject: [ruby-core:64559] [ruby-trunk - Bug #10135] [Rejected] Time.at is inaccurate Issue #10135 has been updated by Akira Tanaka. Status changed from Open to Rejected John Smart wrote: > It seems that somewhere in the MRI, someone is using floating-point math to represent a Time. You use floating point math in 1381089302195 / 1000.0 which is 1381089302.1949999332427978515625. The Time instance contains the value and strftime('%3N') truncates digits after 3 digits, as documented (with a reason). Floating point math is not used except conversion from the given argument to rational. > Is there any other way (workaround) to construct Time with millisecond accuracy? You can use the rational or the second argument of Time.at as others explained. ---------------------------------------- Bug #10135: Time.at is inaccurate https://bugs.ruby-lang.org/issues/10135#change-48486 * Author: John Smart * Status: Rejected * Priority: Low * Assignee: Akira Tanaka * 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? -- https://bugs.ruby-lang.org/