Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #3689, #5223 -- Fixed "1st of January" comparisons for SQLite w…

…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...
commit 32402773f10160a5cf3252a325c98f5e74fb7ceb 1 parent c1f45c3
@malcolmt malcolmt authored
View
6 django/db/models/fields/__init__.py
@@ -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):
View
26 tests/regressiontests/datatypes/models.py
@@ -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)
+[]
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.