title: Rails AR Date Range Query - Daylight Saving - Overlap
body: <p>I have a Rails 3 application, how would one avoid an overlap happening due to daylight saving?</p>
  
  <p>My problem is that I am having a form that generate reports. Auditing an inconsistency I noticed that a bunch of transactions show up in the week ending in March 11th also show up in the Week starting on March 12th.</p>
  
  <p>The problem boils down to some thing like this...</p>
  
  <pre><code>Time.zone.parse('2018-03-11').to_datetime.end_of_day.utc
   =&gt; Mon, 12 Mar 2018 07:59:59 +0000 
  Time.zone.parse('2018-03-12').to_datetime.beginning_of_day.utc
   =&gt; Mon, 12 Mar 2018 07:00:00 +0000 
  </code></pre>
  
  <p>The 1 hour overlap above seem to be where my problem lies. When checking date ranges (see actual code below) how can I avoid this overlap.</p>
  
  <h3>Actual Code</h3>
  
  <p>Here is the actual code that resemble filtering by date.</p>
  
  <pre><code>  scope :filter_date, lambda { |starts, ends, date, transaction_type = :transaction|
      _scope = scoped
  
  
      starts = Time.zone.parse(starts).to_datetime        if starts.class == String and starts.present?
      ends   = Time.zone.parse(ends).to_datetime.tomorrow if ends.class   == String and ends.present?
  
      begin
        case date
        when 'settled'
          transaction_type == "batch" ? date_field = 'deposited_at' : date_field = 'settled_at'
          _scope = _scope.order('transactions.'+date_field+' DESC')
          _scope = _scope.where("transactions."+date_field+" &gt;= ?", starts) if starts.present?
          _scope = _scope.where("transactions."+date_field+" &lt; ?", ends)   if ends.present?
        else # created, nil, other
          _scope = _scope.order('transactions.created_at DESC')
          _scope = _scope.where("transactions.created_at &gt;= ?", starts) if starts.present?
          _scope = _scope.where("transactions.created_at &lt; ?", ends)   if ends.present?
        end
      end
      _scope
    }
  </code></pre>
  
  <h3>Stack</h3>
  
  <ul>
  <li>Ruby 2.1</li>
  <li>Rails 3.2</li>
  <li>PG</li>
  </ul>
  
  <h3>Question</h3>
  
  <p>How can I overcome this overlap of time where the daylight saving takes effect.</p>