NDB Query comparing datetime objects skips dates on same day

david1995 Source

I'm currently working on a project in which I have to compare two datetime objects while querying. My GQL query is:

qry = ndb.gql("SELECT * from Task WHERE ANCESTOR IS :1 AND due >= :2 ORDER BY due ASC",
                          ndb.Key(urlsafe=group_key), datetime.now())

The field due is a datetime object. To save it, I got a UTC string as post data and parsed it using

frmt = "%a, %d %b %Y %H:%M:%S %Z"
first_event = datetime.strptime(utc_date_string, frmt)

What I want to do now is getting the first 5 entities where due is older as the moment right now. (Basically the next 5 events that happen). However, my query only returns the next events that are happening from the next day and skips the ones from today.

I have played around with gae-pytz a little, but since I am new to handling dates in programming I wasn't able to solve the problem so far.

EDIT: To observe a little what exactly is happening I created an task entity for three hours later on the same day. I saved the UTC string Sun, 03 Nov 2013 07:10:00 GMT which when converted back to my local time is not correct. EDIT2: Seems like AM/PM conversion on client side is not working.

Then I changed my query to get all entities without comparing the dates and then logged the current datetime and the datetime of my entity:

qry = ndb.gql("SELECT * from Task WHERE ANCESTOR IS :1 ORDER BY due ASC",
                          ndb.Key(urlsafe=group_key))

When logging task.due (logging.error(task.due)) I get:

2013-11-03 07:10:00

When logging datetime.utcnow() (logging.error(datetime.utcnow()) I get:

2013-11-03 16:14:30.736933

So it seems like my task.due field is being saved in 12h format while datetime.utcnow is returning the time in 24h format.

I still haven't found out with what I need to compare, but at least now I know what to research for.

pythongoogle-app-enginedatetimeapp-engine-ndb

Answers

comments powered by Disqus