如何在 Ruby 中获取当前时间为 13 位整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13148888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current time as 13-digit integer in Ruby?
提问by Tintin81
I have this jQueryfunction that returns the current time as the number of milliseconds since the epoch (Jan 1, 1970):
我有这个jQuery函数返回当前时间作为自纪元(1970 年 1 月 1 日)以来的毫秒数:
time = new Date().getTime();
Is there a way to do the same in Ruby?
有没有办法在 Ruby 中做同样的事情?
Right now, I am using Ruby's Time.now.to_iwhich works great but returns a 10-digitinteger (number of seconds)
现在,我正在使用 Ruby 的Time.now.to_i,它运行良好,但返回一个10 位整数(秒数)
How can I get it to display the number of milliseconds, as in jQuery?
我怎样才能让它显示毫秒数,如jQuery?
回答by steenslag
require 'date'
p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)
回答by Anthony DeSimone
Javascript's gettime()returns the number of milliseconds since epoch.
Javascriptgettime()返回自纪元以来的毫秒数。
Ruby's Time.now.to_iwill give you the number of seconds since epoch. If you change that to Time.now.to_f, you still get seconds but with a fractional component. Just multiply that by 1,000 and you have milliseconds. Then use #to_ito convert it to an integer. And you end up with:
RubyTime.now.to_i会给你自纪元以来的秒数。如果您将其更改为Time.now.to_f,您仍然可以获得秒数,但带有小数部分。只需将其乘以 1,000 即可获得毫秒数。然后使用#to_i将其转换为整数。你最终得到:
(Time.now.to_f * 1000).to_i
回答by Zach Kemp
(Time.now.to_f * 1000).to_ishould do the same thing.
(Time.now.to_f * 1000).to_i应该做同样的事情。
回答by Mike S
Be careful, don't get confused. The fact that Ruby supports the idea of fractional seconds as a float doesn't actually make it a floating point number. I got into trouble with this when I was doing Wireshark timestamp time comparisons in Python... the time calculations in the pcap-ng just weren't working. It was only when I treated the two parts (integral seconds and integral nanoseconds) as both integers was I able to get proper numbers.
小心,不要混淆。Ruby 支持将小数秒作为浮点数的想法这一事实实际上并没有使它成为浮点数。当我在 Python 中进行 Wireshark 时间戳时间比较时,我遇到了这个问题…… pcap-ng 中的时间计算不起作用。只有当我将两个部分(积分秒和积分纳秒)都处理为两个整数时,我才能得到正确的数字。
That's because floating point numbers have Accuracy problems. Indeed, a quick bit of Ruby will show you that to_f does not equal, say, nsec:
那是因为浮点数有精度问题。事实上,一点 Ruby 会告诉你 to_f 不等于,比如说,nsec:
irb(main):019:0> t=Time.now
=> 2015-04-10 16:41:35 -0500
irb(main):020:0> puts "#{t.to_f}; #{t.nsec}"
1428702095.1435847; 143584844
Caveat Programmer. You may be safe to 3 significant digits, but the fact remains: Floating point numbers on computers are approximations. The nanosecond counters on modern computers are integers.
警告程序员。您可能对 3 位有效数字是安全的,但事实仍然存在:计算机上的浮点数是近似值。现代计算机上的纳秒计数器是整数。
回答by David Moles
Using strftime, you can get the number of seconds and append fractional milliseconds (or smaller units, if needed):
使用strftime,您可以获得秒数并附加小数毫秒(或更小的单位,如果需要):
2.2.2 :001 > t = Time.new
=> 2015-06-02 12:16:56 -0700
2.2.2 :002 > t.strftime('%s%3N')
=> "1433272616888"
Note though that this doesn't round, it truncates, as you can see with to_for if you go out to microseconds:
请注意,虽然这不是四舍五入,但它会截断,正如您所看到的,to_f或者如果您以微秒为单位:
2.2.2 :003 > t.to_f
=> 1433272616.888615
2.2.2 :004 > t.usec
=> 888615
and the to_f/ to_isolution has the same problem:
和to_f/to_i溶液有相同的问题:
2.2.2 :009 > (t.to_f * 1000).to_i
=> 1433272616888
so if you really care about millisecond accuracy, a better bet may be to_fwith round:
所以,如果你真的关心毫秒的精度,更好的选择可能是to_f带round:
2.2.2 :010 > (t.to_f * 1000).round
=> 1433272616889
That said, as noted in the docs, "IEEE 754 double is not accurate enough to represent the number of nanoseconds since the Epoch", so if you really reallycare, consider to_rinstead of to_f--
也就是说,如文档中所述,“IEEE 754 double 不够准确,无法表示自 Epoch 以来的纳秒数”,因此,如果您真的很在意,请考虑to_r而不是to_f-
2.2.2 :011 > (t.to_r * 1000).round
=> 1433272616889
-- although if you're only rounding to milliseconds you're probably fine.
- 尽管如果你只四舍五入到毫秒,你可能没问题。
回答by levinalex
回答by chava
The typecast Integer(1e6*Time.now.to_f) returns a Bignum that can hold the milliseconds
类型转换 Integer(1e6*Time.now.to_f) 返回一个可以容纳毫秒的 Bignum

