Skip to content

Commit

Permalink
Fixed #3689, #5223 -- Fixed "1st of January" comparisons for SQLite w…
Browse files Browse the repository at this point in the history
…ithout breaking the other backends.

Based on a patch from raminf and tests from Nebojsa Djordjevic.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7150 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Feb 23, 2008
1 parent c1f45c3 commit 3240277
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ def get_db_prep_lookup(self, lookup_type, value):
value = int(value)
except ValueError:
raise ValueError("The __year lookup type requires an integer argument")
return ['%s-01-01 00:00:00' % value, '%s-12-31 23:59:59.999999' % value]
if settings.DATABASE_ENGINE == 'sqlite3':
first = '%s-01-01'
else:
first = '%s-01-01 00:00:00'
return [first % value, '%s-12-31 23:59:59.999999' % value]
raise TypeError("Field has invalid lookup: %s" % lookup_type)

def has_default(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/regressiontests/datatypes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,30 @@ def __str__(self):
datetime.time(5, 30)
>>> d3.consumed_at
datetime.datetime(2007, 4, 20, 16, 19, 59)
# Year boundary tests (ticket #3689)
>>> d = Donut(name='Date Test 2007', baked_date=datetime.datetime(year=2007, month=12, day=31), consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59))
>>> d.save()
>>> d1 = Donut(name='Date Test 2006', baked_date=datetime.datetime(year=2006, month=1, day=1), consumed_at=datetime.datetime(year=2006, month=1, day=1))
>>> d1.save()
>>> Donut.objects.filter(baked_date__year=2007)
[<Donut: Date Test 2007>]
>>> Donut.objects.filter(baked_date__year=2006)
[<Donut: Date Test 2006>]
>>> Donut.objects.filter(consumed_at__year=2007).order_by('name')
[<Donut: Apple Fritter>, <Donut: Date Test 2007>]
>>> Donut.objects.filter(consumed_at__year=2006)
[<Donut: Date Test 2006>]
>>> Donut.objects.filter(consumed_at__year=2005)
[]
>>> Donut.objects.filter(consumed_at__year=2008)
[]
"""}

0 comments on commit 3240277

Please sign in to comment.