title: filtering time range in datetime field in sqlalchemy
body: <p>I got a weird problem when I was doing the time range filtering in sqlalchemy.
  There is a datetime column in Event table called startDate. I was writing the query to return all the events that have the starting time between 6pm and 11:59pm (doesn't matter which date). </p>
  
  <p>I have the following code,</p>
  
  <pre><code>from sqlalchemy import and_, Date, Time, cast
  import datetime
  startingTime = datetime.time(18, 0, 0)
  endingTime = datetime.time(23, 59, 59)
  </code></pre>
  
  <p>If I run,</p>
  
  <pre><code>results = session.query(Event).filter(and_(cast(Event.startDate, Time)&gt;=startingTime, cast(Event.startDate, Time)&lt;=endingTime).all()
  </code></pre>
  
  <p>I got no result (for sure there are events in this range).</p>
  
  <p>But if I run,</p>
  
  <pre><code>results = session.query(Event).filter(cast(Event.startDate, Time)&gt;=endingTime).all()
  </code></pre>
  
  <p>It returns all rows in the table.</p>
  
  <p>If I filter by dates, </p>
  
  <pre><code>results = session.query(Event).filter(and_(cast(Event.startDate, Date)&gt;=one_date, cast(Event.startDate, Date)&lt;=another_date).all()
  </code></pre>
  
  <p>results are correct!</p>
  
  <p>Let's say the startDate of Event_A is 2013-11-27T19:30:00, and</p>
  
  <pre><code>result = session.query(cast(Event.startDate, Time)).filter(Event.eventName=='Event_A').one()[0]
  result &gt;= startingTime ----&gt; returns True
  result &lt;= endingTime ----&gt; returns True
  </code></pre>
  
  <p>Any help would be appreciated.</p>