Python – appengine select based on timestamp

google-app-enginepython

I can't seem to select something based on timestamp. The behavior is a bit weird and the < and = symbols don't seem to mean what I expect them to.

""" A site message """
class Message( db.Model ) :
  # from/to/ a few other fields
  subject = db.StringProperty()
  body = db.Text()

  # this is the field i'm trying to use
  sent = db.DateTimeProperty( auto_now_add=True )

When I write GQL queries like

select * from Message where sent = '2009-09-14 01:00:02.648000'

(there is a message with PRECISELY that time stamp in the datastore)

it gives me nothing back.

If I try

select * from Message where sent < '2009-09-14 01:00:02.648000'

It simply gives me all of them. When I try the > sign, it simply gives me none again.

What's going on here and how do I select based on timestamp?

Best Answer

Do NOT use strings to "stand in" for datetimes! It just won't work...

See the docs: DateTimeProperty corresponds to a datetime.datetime value. import datetime, convert your beloved strings to datetime.datetime instances (e.g. by calling the strptime method thereof), use THOSE instances for comparisons -- and, of course, live happily ever after!-)