Reputation: 199
For Example, timestamp is 1403667010.574
, use Time.at(1403667010.574).strftime('%Y-%m-%d %H:%M:%S.%L %z')
, then the resuit is
"2014-06-25 03:30:10.573 +0000"
it loss 1ms on the result. Is that a bug of ruby?
For me, the major problem is, if I convert the time string by Time.parse("2014-06-25 03:30:10.573 +0000").to_f
, the result would become 1403667010.573
. It's not equivalent to origin value
Upvotes: 1
Views: 184
Reputation: 230336
This is how floating point numbers work. They are approximately precise (not absolutely).
t = Time.at(1403667010.574)
t.strftime('%Y-%m-%d %H:%M:%S.%L %z') # => "2014-06-25 07:30:10.573 +0400"
# more precision
t.strftime('%Y-%m-%d %H:%M:%S.%6N %z') # => "2014-06-25 07:30:10.573999 +0400"
# even more precision
t.strftime('%Y-%m-%d %H:%M:%S.%10N %z') # => "2014-06-25 07:30:10.5739998817 +0400"
In addition, this is what Time#strftime documentation says about %L
flag:
%L - Millisecond of the second (000..999) The digits under millisecond are truncated to not produce 1000.
So, the value .573999
is simply truncated (not rounded) to .573
Upvotes: 5