bootrails.com is now moving to saaslit.com  See you there!
bootrails moves to  saaslit.com
Post

Milliseconds in Ruby

1. Display time, down to milliseconds in Ruby

1
2
3
current_time = Time.now
current_time.strftime('%H:%M:%S.%L')
# => "10:52:07.119"

If you want more info about strftime, please read this article : https://bootrails.com/blog/ruby-strftime-short-and-long-story/ - milliseconds, however, were not covered at this time :)

2. Get current time system in milliseconds

First possibility is to use .strftime, again :

1
2
3
current_time = Time.now
current_time.strftime('%s%L')
# => "1643017927119"

Please note the result is a String.

The second possibility is the following :

1
2
(current_time.to_f * 1000).to_i
# => 1643017927119

The result is a number.

3. Elapsed time in milliseconds in Ruby

Ruby-on-Rails method

If you are in the Rails environment, the .in_milliseconds method already exists.

1
2
3
4
5
6
7
# Assuming Rails is loaded
time_a = Time.now
sleep(2)
time_b = Time.now
# Just call .in_milliseconds method
(time_b - time_a).in_milliseconds
# => 2016.464

Pure Ruby solution

If you just want the number of milliseconds elapsed since Epoch, type

1
2
3
4
DateTime.now.strftime("%Q")
# => "1643018771523"
DateTime.now.strftime("%Q").to_i
# => 1643018771523

But if you need a difference between two time, you will have to write your own method :

1
2
3
4
5
6
7
8
9
def time_difference_in_milliseconds(start, finish)
   (finish - start) * 1000.0
end
time_a = Time.now
sleep(2)
time_b = Time.now

elapsed_time = time_difference_in_milliseconds(time_a, time_b)
# => 2020.874

If you don’t mind about Monkey Patching, here is another possibility :

1
2
3
4
5
6
7
8
9
10
11
12
class Time
  def to_ms
    (self.to_f * 1000.0).to_i
  end
end

time_a = Time.now
sleep(2)
time_b = Time.now
elapsed_time = time_b.to_ms - time_a.to_ms  
# => 2028

A word of caution

We found an interesting article here about Ruby’s lack of precision with the clock. Whereas we do not very often need a high precision in the web industry, it’s always good to know.

Enjoy !

This post is licensed under CC BY 4.0 by the author.