4 Mar 2008 03:03
Re: Like on DateTime using only date
On 04/03/2008, Lutz Horn <lutz.horn@...> wrote: > Hi Gustavo, > > Am 03.03.2008 um 17:21 schrieb Gustavo Niemeyer: > > >> with at DateTime property I'd like to compare only using the date > >> part. But the like method of DateTime needs a datetime.datetime > >> instance, not a datetime.date instance. How can I formulate the like > >> condition to only select those records whose DateTime propery match a > >> datetime.date instance? > > > > I'm not sure I understand what's the problem you're facing. The > > DateTime indeed needs a datatime object. We may extend it to support > > automatic conversion from date objects, but it should be fairly simple > > to make it work as it is now. > > > > Can you provide some sample code that is failing, and that you're > > having > > trouble to make it work correctly? > > > > I've the following table in MySQL: > > mysql> describe screenings; > +----------+--------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +----------+--------------+------+-----+---------+----------------+ > | id | int(11) | | PRI | NULL | auto_increment | > | when | datetime | YES | | NULL | | > | movie_id | int(11) | YES | | NULL | | > +----------+--------------+------+-----+---------+----------------+ > > Using Strom, the corresponding model class is: > > class Screening(object): > __storm_table__ = "screenings" > id = Int(primary=True) > when = DateTime() > > Now I want to find all screenings for today: > > s_today = [screening for screening in > self.store.find(Screening, > Screening.when.like(datetime.datetime.now()))] > > This obviously returns an empty list since to no screening happens > exactly now. But if I use datetime.date.today() as the parameter for > like(), I get > > <type 'exceptions.TypeError'> at / > Expected datetime, found datetime.date(2008, 3, 3) > > How can I use find() to get all Screening instances for which the date > part for the DateTime property equals datetime.date.today() regardless > of the time part? You could try something like this: today = datetime.date.today() start = datetime.datetime.combine(today, datetime.time(0,0)) end = start + datetime.timedelta(days=1) results = self.store.find( Screening, Screening.when >= start, Screening.when < end) That is, pick all screenings that fall within the desired time interval. James.
RSS Feed