title: Calculating elapsed time and saving to database
body: <p>I'm trying to log in a Ruby app DB Table (MySQL Database), the time and date that an import script started, finished and how long the script took to execute.</p>
  
  <p>The logging of the start time and finish time is easy enough, and I've got that working, however, I can't get it to save the runtime in the table, it just skips the field.</p>
  
  <p>Here's the table structure from the Migration file</p>
  
  <pre><code>create_table :import_alls do |t|
    t.datetime :Import_Start_Time
    t.datetime :Import_Finish_Time
    t.time :Import_Duration
  
  
    t.timestamps
  end
  </code></pre>
  
  <p>Here's my code ...</p>
  
  <pre><code>@script_start_time = Time.now
  [... Script ...]
  @script_finish_time = Time.now
  @script_runtime = @script_finish_time - @script_start_time
  
  import = ImportAll.new
      import.Import_Start_Time = @script_start_time # Saves correctly
      import.Import_Finish_Time = @script_finish_time # Saves Correctly
      import.Import_Duration = @script_runtime # Skips
      [... a few other fields to save ...] # All save correctly
  import.save!
  
  puts @script_runtime # Puts correct number of seconds as a line in the server log
  </code></pre>
  
  <p>Saved Results : </p>
  
  <pre><code>Import_Start_Time = Start Date &amp; Time as expected
  Import_Finish_Time = Finish Date &amp; Time as expected
  Import_Duration = NULL (expected duration expressed as HH:MM:SS)
  </code></pre>
  
  <p>So, </p>
  
  <ol>
  <li><p>is a <code>time</code> field the correct type if field? or should it be <code>datetime</code> ?</p></li>
  <li><p>how can I convert the @script_runtime value into a <code>time</code> field so it can be saved in the table ?</p></li>
  </ol>