How to get UTC timestamp in Ruby?

RubyTimestamp

Ruby Problem Overview


How to get UTC timestamp in Ruby?

Ruby Solutions


Solution 1 - Ruby

You could use: Time.now.to_i.

Solution 2 - Ruby

time = Time.now.getutc

Rationale: In my eyes a timestamp is exactly that: A point in time. This can be accurately represented with an object. If you need anything else, a scalar value, e.g. seconds since the Unix epoch, 100-ns intervals since 1601 or maybe a string for display purposes or storing the timestamp in a database, you can readily get that from the object. But that depends very much on your intended use.

Saying that »a true timestamp is the number of seconds since the Unix epoch« is a little missing the point, as that is one way of representing a point in time, but it also needs additional information to even know that you're dealing with a time and not a number. A Time object solves this problem nicely by representing a point in time and also being explicit about what it is.

Solution 3 - Ruby

The default formatting is not very useful, in my opinion. I prefer ISO8601 as it's sortable, relatively compact and widely recognized:

>> require 'time'
=> true
>> Time.now.utc.iso8601
=> "2011-07-28T23:14:04Z"

Solution 4 - Ruby

if you need a human-readable timestamp (like rails migration has) ex. "20190527141340"

Time.now.utc.to_formatted_s(:number)  # using Rails
Time.now.utc.strftime("%Y%m%d%H%M%S") # using Ruby

Solution 5 - Ruby

Usually timestamp has no timezone.

% irb
> Time.now.to_i == Time.now.getutc.to_i
=> true

Solution 6 - Ruby

What good is a timestamp with its granularity given in seconds? I find it much more practical working with Time.now.to_f. Heck, you may even throw a to_s.sub('.','') to get rid of the decimal point, or perform a typecast like this: Integer(1e6*Time.now.to_f).

Solution 7 - Ruby

Time.utc(2010, 05, 17)

Solution 8 - Ruby

time = Time.zone.now()

It will work as

irb> Time.zone.now
=> 2017-12-02 12:06:41 UTC

Solution 9 - Ruby

The proper way is to do a Time.now.getutc.to_i to get the proper timestamp amount as simply displaying the integer need not always be same as the utc timestamp due to time zone differences.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohit JainView Question on Stackoverflow
Solution 1 - RubymanzhikovView Answer on Stackoverflow
Solution 2 - RubyJoeyView Answer on Stackoverflow
Solution 3 - RubyTim SylvesterView Answer on Stackoverflow
Solution 4 - RubyKate KasinskayaView Answer on Stackoverflow
Solution 5 - RubyYuki MatsukuraView Answer on Stackoverflow
Solution 6 - RubychavaView Answer on Stackoverflow
Solution 7 - RubyAugust LilleaasView Answer on Stackoverflow
Solution 8 - RubyAsteriskView Answer on Stackoverflow
Solution 9 - RubySumit BishtView Answer on Stackoverflow