﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18176	SQL queries for filtering datetime objects use incorrect format for years <1000	rrotaru	rrotaru	"Datetime objects with year values <1000 are auto-formatted to 4-digits by adding leading
zeroes (i.e. year 1 becomes 0001). Also, according to the [https://docs.djangoproject.com/en/1.4/ref/models/querysets/#year docs], the !__year lookup takes a
four digit year (i.e., again, 0000-9999). However, when attempting to use the !__year lookup on 
years <1000, the SQL generated lacks the leading zeroes and as a result, no matches are made. 
 
Example with Poll objects:
{{{
>>> poll1 = Poll(question=""This"",pub_date=datetime.datetime(0001,01,01))
>>> poll1.save()
>>> Poll.objects.filter(pub_date__year=0001) #should return our poll1
[]
}}}
Also, certain years <1000, such as 0999, or years ending in a 9 with at least one leading zero
generate ""invalid token"" syntax errors 
{{{
>>> poll2 = Poll(question=""That"",pub_date=datetime.datetime(0999,01,01))
>>> poll2.save()
>>> Poll.objects.filter(pub_date__year=0999) #should return our poll2
  Poll.objects.filter(pub_date__year=0999)
                                        ^
SyntaxError: invalid token
}}}

Note: This bug occurs when using sqlite3, I have not tried replicating it with other databases. \\
More discussion on this bug: https://groups.google.com/forum/?fromgroups#%21topic/django-developers/xPIQKbc4tiQ"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	datetime, SQL, year	flo@…	Accepted	1	0	0	0	0	0
