Search code examples
grailsgrails-orm

Filtering by just time in grails


I'm attempting to create a filter on the view that allows users to filter the data by time ranges:

enter image description here

When users select this, it maps to an enum type TimeRange that contains two date objects. These date objects contain just the respective times, called lowerBound and upperBound respectively. For example:

MIDNIGHT_TO_SIX(1, Date.parse("HH:mm:ss", "00:00:00"), Date.parse("HH:mm:ss", "06:00:00")

This makes a Date object. Because I have not specified the date portion, that is set to epoch date. When I use these in a critieria for filtering:

and {
   or {
       eq('startDateTime', range.lowerBound)
       gt('startDateTime', range.lowerBound)
   }

   or {
       eq('startDateTime', range.upperBound)
       lt('startDateTime', range.upperBound)
   }
}

Nothing is being returned! This is clearly because none of the dates in my database are before or equal to epoch time, as per the last or closure.

Is there a way of telling the criteria to only use the time portion during the comparison, or is there a way that I can declare lowerBound and upperBound to not be concerned with the Date?


Solution

  • the straight-forward approach would be to save the number of seconds (or millis) from 00:00:00, so that the lookup is as easy as:

    enum Range { 
      MIDNIGHT_TO_SIX( 1, 0, 6 * 60 * 60 ) //in seconds 
    }
    

    criteria:

    {
      between 'startInSec', range.lower, range.upper 
    }