Here is a simple example ... but it seems to behave properly:
""" test datetime in db queries """
import sys
sys.path.append("/mnt/data1/web2py")
from gluon import *
import datetime
import random
db = DAL('sqlite://debug_datetime.sqlite',
folder='/home/goncalves/matlab/objrec/target_stacked_lanes',
auto_import=True)
db.define_table('magic',
Field('DT', 'datetime'),
Field('val', 'integer'))
# Dec 12 2010, 12:30:45
init_date = datetime.datetime(2010, 12, 10, 12, 30, 45)
cur_date = init_date
cur_val = 0
for i in range(1000):
db.magic.insert(DT = cur_date, val = cur_val)
# we increment from 30 to 300 seconds at a time
rand_int = random.randint(30, 300)
delta = datetime.timedelta(0, rand_int)
cur_date = cur_date + delta
cur_val = cur_val + rand_int
# make test date 24 hours later
test_date = init_date + datetime.timedelta(1)
results = db( db.magic.DT < test_date).select( orderby=db.magic.DT )
print 'Initial date: ', init_date
print 'Test date: ', test_date
print 'Last query result:', results[-1].DT
print 'Next entry :', db.magic[len(results)+1].dT
When run it produces:
Initial date: 2010-12-10 12:30:45
Test date: 2010-12-11 12:30:45
Last query result: 2010-12-11 12:29:55
Next entry in DB : 2010-12-11 12:33:45
So it behaves perfectly fine. Something else is the problem in my
application! Not sure what, though.
I'm running python 2.7 on my desktop, 2.6.5 on laptop, and have the problem
on both.
Using webp2y version : Version 1.97.1 (2011-06-26 19:25:44)
I am the only person accesssing the DB.
The DB is 62MB.
Could it be that using a field name of 'dateTime' is somehow confusing
web2py?
Thanks,
Luis.