>>> datetime(999,1,1,1).year
999
Interestingly sqlite uses standard strftime
http://www.sqlite.org/lang_datefunc.html:
%Y year: 0000-9999
I thought the answer might be to use strftime but:
>>> datetime(999,1,1,1).strftime('%Y')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=999 is before 1900; the datetime strftime() methods
require year >= 1900
Which is this bug: http://bugs.python.org/issue1777412
This isn't a problem that I can imagine many people have run into, so
sounds like a bug needing a patch. Good luck!
>>> Poll.objects.filter(pub_date__year=1) #We should get the poll p (but we don't)
[]
The SQL queries that are being generated are (for sqlite):{'sql': u'INSERT INTO "polls_poll" ("question", "pub_date") VALUES (Year is 999, 0999-09-09 00:00:00)',
'time': '0.043'},
{'sql': u'SELECT "polls_poll"."id", "polls_poll"."question", "polls_poll"."pub_date" FROM "polls_poll" WHERE "polls_poll"."pub_date" BETWEEN 999-01-01 and 999-12-31 23:59:59.999999 LIMIT 21',
'time': '0.000'}]
FYI Django uses the django.utils.datetime_safe module to work around this problem when necessary.
--
Aymeric.
On 14 avr. 2012, at 21:25, Evan Carmi wrote:>>> Poll.objects.filter(pub_date__year=1) #We should get the poll p (but we don't)
[]
The docs [1] say that the __year lookup "takes a four digit year". I'm not sure what this means exactly for years < 1000!It looks like it's possible to improve either the code or the docs, so it's worth filing a ticket in Trac.