ruby strftime给错了时间

Yeh*_*udi 1 ruby time

例如,时间戳是1403667010.574,使用Time.at(1403667010.574).strftime('%Y-%m-%d %H:%M:%S.%L %z'),然后是resuit

"2014-06-25 03:30:10.573 +0000"
Run Code Online (Sandbox Code Playgroud)

结果丢失了1ms.这是红宝石的错误吗?

对我来说,主要的问题是,如果我转换时间字符串Time.parse("2014-06-25 03:30:10.573 +0000").to_f,结果将变为1403667010.573.它不等于原始价值

Ser*_*sev 5

这是浮点数的工作原理.它们大致精确(非绝对).

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"
Run Code Online (Sandbox Code Playgroud)

另外,这就是Time#strftime文档中关于%L标志的内容:

%L - 秒的毫秒(000..999)毫秒下的数字被截断为不产生1000.

因此,该值.573999只是截断(不舍入)为.573