Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll take a shot at 'splaining what you're seeing. Here are several comparisons of a Time value, and what happens when converting between different formats. I'm not going to do any equality checks, because you'll be able to see whether a value should match simply by looking at it:</p> <pre><code>require 'time' t_now = Time.now # =&gt; 2013-04-04 20:10:17 -0700 </code></pre> <p>That's the <code>inspect</code> output, which throws away a lot of information and precision. It's good enough for use by mortals.</p> <pre><code>t_now.to_f # =&gt; 1365131417.613106 </code></pre> <p>That's the value the computer sees usually, with microseconds.</p> <pre><code>t_now.to_i # =&gt; 1365131417 </code></pre> <p>That's the same time with microseconds all gone.</p> <pre><code>Time.at(t_now) # =&gt; 2013-04-04 20:10:17 -0700 Time.at(t_now.to_f) # =&gt; 2013-04-04 20:10:17 -0700 Time.at(t_now.to_i) # =&gt; 2013-04-04 20:10:17 -0700 t_now.to_s # =&gt; "2013-04-04 20:10:17 -0700" </code></pre> <p>Normal <code>inspect</code> and <code>to_s</code> output won't show any difference in the precision as long as the integer portion of the value is intact.</p> <pre><code>Time.parse(t_now.to_s) # =&gt; 2013-04-04 20:10:17 -0700 Time.parse(t_now.to_s).to_f # =&gt; 1365131417.0 </code></pre> <p>Parsing loses the resolution, unless you present a value that contains the fractional time and define the parsing format so <code>strptime</code> knows what to do with it. The default parser formats are designed for general-use, not high-precision, so we have to use <code>strftime</code> instead of allowing <code>to_s</code> to have its way with the value, and <code>strptime</code> to know what all those numbers mean:</p> <pre><code>T_FORMAT = '%Y/%m/%d-%H:%M:%S.%N' # =&gt; "%Y/%m/%d-%H:%M:%S.%N" t_now.strftime(T_FORMAT) # =&gt; "2013/04/04-20:10:17.613106000" Time.strptime(t_now.strftime(T_FORMAT), T_FORMAT).to_f # =&gt; 1365131417.613106 </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. 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.
    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