4

I'm working on a golang backend run on Google App Engine. I have an Entity called Recruit that has a property

UpdatedAt time.Time `datastore:"updated_at"`

I would like to query Recruits by their updated time. My first instinct is to use a filter.

query = datastore.NewQuery("Recruit").Filter("updated_at <=", updatedAt)

where updatedAt is a string. I've tried the forms 2014-08-28 01:53:19 and 2014-08-28T01:53:19.00618Z (The former is the form the time.Time property takes in the datastore, while the latter is how it propagates in JSON.)

When I call the query with <=, all Recruit objects are returned. When calling with >=, null is returned. I'm quite sure I'm testing with a time in the middle of the data set.

When I test on console.developers.google.com, the console filters support a date and time after/before, but trying to replace <= with after results in an error.

Has anyone managed to filter queries by date or time?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
selljamhere
  • 193
  • 2
  • 7

1 Answers1

4

Try to use updatedAt of type time.Time instead of as a string

Following is a snippet of how I managed to filter queries by time:

My Property:

TimeStamp time.Time

My Query:

timeNow := time.Now()
time1hr := timeNow.Add(-59*time.Minute)
q := datastore.NewQuery("Recruit").Filter("TimeStamp <=", time1hr)

This code would give me all the entities before time1hr.

ntsh
  • 759
  • 5
  • 19
  • 1
    You may also find time.Parse handy in this situation. Here is a good SO answer showing it's usage: http://stackoverflow.com/questions/14106541/go-parsing-date-time-strings-which-are-not-standard-formats – Joe Bergevin Aug 28 '14 at 18:45
  • That worked! I started storing the time as an int, just as a work around, but this is much better. Should have expected that the filter parameter need be of the same type. @JoeBergevin, I did use parse. Worked like a charm. – selljamhere Aug 28 '14 at 19:00