Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry, brief moment of synapse failure. Here's the real answer.</p> <pre><code>require 'date' Time.at(seconds_since_epoch_integer).to_datetime </code></pre> <p><strong>Brief example</strong> (this takes into account the current system timezone):</p> <pre><code>$ date +%s 1318996912 $ irb ruby-1.9.2-p180 :001 &gt; require 'date' =&gt; true ruby-1.9.2-p180 :002 &gt; Time.at(1318996912).to_datetime =&gt; #&lt;DateTime: 2011-10-18T23:01:52-05:00 (13261609807/5400,-5/24,2299161)&gt; </code></pre> <p><strong>Further update</strong> (for UTC):</p> <pre><code>ruby-1.9.2-p180 :003 &gt; Time.at(1318996912).utc.to_datetime =&gt; #&lt;DateTime: 2011-10-19T04:01:52+00:00 (13261609807/5400,0/1,2299161)&gt; </code></pre> <p><strong>Recent Update</strong>: 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 <code>Time.at(..)</code> outperforms <code>DateTime.strptime(..)</code> (update: added more benchmarks).</p> <pre><code># ~ % ruby -v # =&gt; ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0] irb(main):038:0&gt; Benchmark.measure do irb(main):039:1* ["1318996912", "1318496912"].each do |s| irb(main):040:2* DateTime.strptime(s, '%s') irb(main):041:2&gt; end irb(main):042:1&gt; end =&gt; #&lt;Benchmark ... @real=2.9e-05 ... @total=0.0&gt; irb(main):044:0&gt; Benchmark.measure do irb(main):045:1&gt; [1318996912, 1318496912].each do |i| irb(main):046:2&gt; DateTime.strptime(i.to_s, '%s') irb(main):047:2&gt; end irb(main):048:1&gt; end =&gt; #&lt;Benchmark ... @real=2.0e-05 ... @total=0.0&gt; 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&gt; end irb(main):054:1&gt; end =&gt; #&lt;Benchmark ... @real=1.5e-05 ... @total=0.0&gt; 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&gt; end irb(main):060:1&gt; end =&gt; #&lt;Benchmark ... @real=2.0e-05 ... @total=0.0&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload