How to Get Elapsed Time in Milliseconds in Ruby

How do I get elapsed time in milliseconds in Ruby?

As stated already, you can operate on Time objects as if they were numeric (or floating point) values. These operations result in second resolution which can easily be converted.

For example:

def time_diff_milli(start, finish)
(finish - start) * 1000.0
end

t1 = Time.now
# arbitrary elapsed time
t2 = Time.now

msecs = time_diff_milli t1, t2

You will need to decide whether to truncate that or not.

How to time an operation in milliseconds in Ruby?

You can use ruby's Time class. For example:

t1 = Time.now
# processing...
t2 = Time.now
delta = t2 - t1 # in seconds

Now, delta is a float object and you can get as fine grain a result as the class will provide.

Add 1 millisecond to a Time/DateTime object

Ok i found a solution. With real calculation i looks like.

time_end = ((Date.today + 1).to_time - 1/1001).utc.iso8601(3)
=> "2016-09-28T21:59:59.999Z"

EXAMPLE

Formatting in iso8601(3) is only to show behavior.

irb(main):055:0> Date.today.to_time.iso8601(3)
=> "2016-09-28T00:00:00.000+02:00

Adding a millisecond"

irb(main):058:0> (Date.today.to_time + 1/1000.0).iso8601(3)
=> "2016-09-28T00:00:00.001+02:00"

Subtract a millisecond

!DONT USE, see result with subtracted 2 milliseconds!
irb(main):060:0> (Date.today.to_time - 1/1000.0).iso8601(3)
=> "2016-09-27T23:59:59.998+02:00"

USE
irb(main):061:0> (Date.today.to_time - 1/1001.0).iso8601(3)
=> "2016-09-27T23:59:59.999+02:00"

How to find time difference in milliseconds from two datetime stamp field using ruby

Use to_f

millisec = 1000 * (response_time.to_f - request_time.to_f)

The timestamps must have this format though,

"2016-12-27 18:35:13.833"

Notice the . separator between seconds and milliseconds!

get milliseconds since 1970 from string

Try

require 'date'
DateTime.parse("2016-08-12 06:13:24 UTC").strftime('%Q')

Create TimeWithZone object from integer (unix epoch time with millisecond precision) and with specified zone (string)

Assuming that the timestamp is in milliseconds, then 1586653140000 is

Epoch: 1586653140
GMT: Sunday, April 12, 2020 12:59:00 AM
PDT: Saturday, April 11, 2020 17:59:00 PM -in time zone America/Los Angeles

These are just 3 different ways to refer to a specific point in time around the world.
Sat, 11 Apr 2020 20:59:00 PDT -07:00 and 2020-04-11 20:59:00 -0400 each refer to different points in time and not the same as epoch(1586653140)

Since the Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), it wouldn't make sense to take 1586653140 and only change the time zone without adding the zone's offset because now you are talking about another point in time.

To get the right "translation" from the epoch to any time zone you could just do

Time.zone = "GMT"
Time.zone.at(1586653140)
=> Sun, 12 Apr 2020 00:59:00 GMT +00:00
Time.zone = "America/Los_Angeles"
Time.zone.at(1586653140)
=> Sat, 11 Apr 2020 17:59:00 PDT -07:00

When working with dates in time zones in rails it is important to only use functions that take the set time zone into account:

DON’T USE

  • Time.now
  • Date.today
  • Date.today.to_time
  • Time.parse("2015-07-04 17:05:37")
  • Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")

DO USE

  • Time.current
  • 2.hours.ago
  • Time.zone.today
  • Date.current
  • 1.day.from_now
  • Time.zone.parse("2015-07-04 17:05:37")
  • Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone

Also keep in mind that in a Rails app, we have three different time zones:

  • system time,
  • application time, and
  • database time.

This post by thoughtbot explains things clearly.



Related Topics



Leave a reply



Submit