Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?

How do you convert a Unix timestamp (seconds since epoch) to Ruby DateTime?

like image 484
Tronathan Avatar asked Oct 19 '11 03:10

Tronathan


People also ask

How do I convert epoch time to seconds?

Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

How do I convert epoch time to real time?

Convert from epoch to human-readable datemyString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch. =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).

How many seconds have passed since the Unix epoch?

1656892805 seconds elapsed since jan 1 1970. Unix time (also known as POSIX time or UNIX Epoch time) is a system for describing a point in time. It is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus leap seconds.


2 Answers

Sorry, brief moment of synapse failure. Here's the real answer.

require 'date'  Time.at(seconds_since_epoch_integer).to_datetime 

Brief example (this takes into account the current system timezone):

$ date +%s 1318996912  $ irb  ruby-1.9.2-p180 :001 > require 'date'  => true   ruby-1.9.2-p180 :002 > Time.at(1318996912).to_datetime  => #<DateTime: 2011-10-18T23:01:52-05:00 (13261609807/5400,-5/24,2299161)>  

Further update (for UTC):

ruby-1.9.2-p180 :003 > Time.at(1318996912).utc.to_datetime  => #<DateTime: 2011-10-19T04:01:52+00:00 (13261609807/5400,0/1,2299161)> 

Recent Update: I benchmarked the top solutions in this thread while working on a HA service a week or two ago, and was surprised to find that Time.at(..) outperforms DateTime.strptime(..) (update: added more benchmarks).

# ~ % ruby -v #  => ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]  irb(main):038:0> Benchmark.measure do irb(main):039:1*   ["1318996912", "1318496912"].each do |s| irb(main):040:2*     DateTime.strptime(s, '%s') irb(main):041:2>   end irb(main):042:1> end  => #<Benchmark ... @real=2.9e-05 ... @total=0.0>  irb(main):044:0> Benchmark.measure do irb(main):045:1>   [1318996912, 1318496912].each do |i| irb(main):046:2>     DateTime.strptime(i.to_s, '%s') irb(main):047:2>   end irb(main):048:1> end  => #<Benchmark ... @real=2.0e-05 ... @total=0.0>  irb(main):050:0* Benchmark.measure do irb(main):051:1*   ["1318996912", "1318496912"].each do |s| irb(main):052:2*     Time.at(s.to_i).to_datetime irb(main):053:2>   end irb(main):054:1> end  => #<Benchmark ... @real=1.5e-05 ... @total=0.0>  irb(main):056:0* Benchmark.measure do irb(main):057:1*   [1318996912, 1318496912].each do |i| irb(main):058:2*     Time.at(i).to_datetime irb(main):059:2>   end irb(main):060:1> end  => #<Benchmark ... @real=2.0e-05 ... @total=0.0> 
like image 114
Adam Eberlin Avatar answered Sep 24 '22 02:09

Adam Eberlin


DateTime.strptime can handle seconds since epoch. The number must be converted to a string:

require 'date' DateTime.strptime("1318996912",'%s') 
like image 38
steenslag Avatar answered Sep 25 '22 02:09

steenslag