title: Millisecond resolution of DateTime in Ruby
body: <p>I have a string like <code>2012-01-01T01:02:03.456</code> that I am storing in a Postgres database TIMESTAMP using ActiveRecord.</p>
  
  <p>Unfortunately, Ruby seems to chop off the milliseconds:</p>
  
  <pre><code>ruby-1.9.3-rc1 :078 &gt; '2012-12-31T01:01:01.232323+3'.to_datetime
   =&gt; Mon, 31 Dec 2012 01:01:01 +0300 
  </code></pre>
  
  <p>Postgrs supports microsecond resolution.  How can I get my timestamp to be saved accordingly? I need at least millisecond resolution.</p>
  
  <p>(PS Yes I could hack in a milliseconds integer column in postgres; that kind of defeats the whole purpose of ActiveRecord.)</p>
  
  <p><strong>UPDATE:</strong><br>
  The very helpful responses showed that Ruby's <code>DateTime</code> is <em>not</em> chopping off milliseconds; using <code>#to_f</code> shows it.  But, doing:</p>
  
  <pre><code>m.happened_at = '2012-01-01T00:00:00.32323'.to_datetime
  m.save!
  m.reload
  m.happened_at.to_f
  </code></pre>
  
  <p>Does drop the milliseconds.</p>
  
  <p>Now, the interesting thing is that <code>created_at</code> does show milliseconds, both in Rails and Postgres.  But other timestamps fields (like <code>happened_at</code> above) don't.  (Perhaps Rails uses a <code>NOW()</code> function for <code>created_at</code> as opposed to passing in a DateTime).</p>
  
  <p>Which leads to my ultimate question:<br>
  <strong>How can I get ActiveRecord to preserve millisecond resolution on timestamp fields?</strong></p>