How to get current time as 13 digit integer in Ruby?

I have this jQuery function that returns the current time as the number of milliseconds since the era (January 1, 1970):

 time = new Date().getTime(); 

Is there a way to do the same in Ruby?

I am currently using Ruby Time.now.to_i , which works fine but returns a 10 digit integer (number of seconds)

How can I make it display the number of milliseconds, as in jQuery ?

+53
ruby
Oct 30 '12 at 23:06
source share
7 answers
 require 'date' p DateTime.now.strftime('%s') # "1384526946" (seconds) p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds) 
+92
Nov 15 '13 at 14:51
source share
— -

Javascript gettime() returns the number of milliseconds since an era.

Ruby Time.now.to_i will give you the number of seconds from an era. If you change this to Time.now.to_f , you still get seconds, but with a fractional component. Just multiply this by 1000 and you have milliseconds. Then use #to_i to convert it to an integer. And you will get:

 (Time.now.to_f * 1000).to_i 
+71
Oct 30 '12 at 23:16
source share

(Time.now.to_f * 1000).to_i should do the same.

+22
Oct 30 '12 at 23:12
source share

Get the Time object of Time.now , a call to #to_i returns the Unix timestamp (in seconds from the era). #to_f gives fractional seconds, which you can use to get milliseconds from an era:

 Time.now.to_f * 1000 
+13
Nov 15 '13 at 13:16
source share

Be careful, do not confuse. The fact that Ruby supports the idea of ​​fractional seconds as a float does not actually make it a floating point number. I had problems with this when I was doing Wireshark time-time comparisons in Python ... the time calculations in pcap-ng just didn't work. Only when I processed two parts (integral seconds and integral nanoseconds), as both integers were able to get the correct numbers.

This is because floating point numbers have precision problems . Indeed, a quick bit of Ruby will show you that to_f is not equal to, say, 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 

Programmer Caveat. You can be safe up to 3 significant digits, but the fact remains: floating point numbers on computers are approximate. Nanosecond counters on modern computers are integers.

+12
Apr 10 '15 at 22:06
source share

Using strftime , you can get the number of seconds and add fractional milliseconds (or, if necessary, smaller units):

 2.2.2 :001 > t = Time.new => 2015-06-02 12:16:56 -0700 2.2.2 :002 > t.strftime('%s%3N') => "1433272616888" 

Note that this is not cool, it is truncated, as you can see with to_f , or if you go out in microseconds:

 2.2.2 :003 > t.to_f => 1433272616.888615 2.2.2 :004 > t.usec => 888615 

and the solution to_f / to_i has the same problem:

 2.2.2 :009 > (t.to_f * 1000).to_i => 1433272616888 

therefore, if you really need precision in milliseconds, it is better to bet to_f with round :

 2.2.2 :010 > (t.to_f * 1000).round => 1433272616889 

However, as noted in the docs , "the dual IEEE 754 is not accurate enough to represent the number of nanoseconds since the era," so if you really pay attention to_r instead of to_f -

 2.2.2 :011 > (t.to_r * 1000).round => 1433272616889 

- although if you only round to the millisecond, you are probably fine.

+7
Jun 02 '15 at 7:28
source share

Sign Integer (1e6 * Time.now.to_f) returns a Bignum that can hold milliseconds

0
Jul 03 '15 at 0:25
source share



All Articles