#18766 closed Cleanup/optimization (fixed)
Models with datetime fields fail to save when date is below 1900 and USE_TZ is True
Reported by: | Alexey Boriskin | Owned by: | Aymeric Augustin |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | timezone, tz |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have model with datetime field in it. When I save this model in admin site and the filled date is below 1900, I am getting the following traceback:
File "/Users/voidus/Documents/workspace/django-trunk/django/contrib/admin/options.py", line 709, in save_model obj.save() File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/base.py", line 524, in save force_update=force_update, update_fields=update_fields) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/base.py", line 595, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/query.py", line 573, in _update return query.get_compiler(self.db).execute_sql(None) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/sql/compiler.py", line 991, in execute_sql cursor = super(SQLUpdateCompiler, self).execute_sql(result_type) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/sql/compiler.py", line 813, in execute_sql sql, params = self.as_sql() File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/sql/compiler.py", line 956, in as_sql val = field.get_db_prep_save(val, connection=self.connection) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/fields/__init__.py", line 303, in get_db_prep_save prepared=False) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/fields/__init__.py", line 834, in get_db_prep_value value = self.get_prep_value(value) File "/Users/voidus/Documents/workspace/django-trunk/django/db/models/fields/__init__.py", line 820, in get_prep_value if value is not None and settings.USE_TZ and timezone.is_naive(value): File "/Users/voidus/Documents/workspace/django-trunk/django/utils/timezone.py", line 271, in is_naive return value.tzinfo is None or value.tzinfo.utcoffset(value) is None File "/Users/voidus/Documents/workspace/django-trunk/django/utils/timezone.py", line 71, in utcoffset if self._isdst(dt): File "/Users/voidus/Documents/workspace/django-trunk/django/utils/timezone.py", line 89, in _isdst stamp = _time.mktime(tt) ValueError: year out of range
I think that is because utcoffset
and some other functions use _isdst
function, which calls time.mktime
and it have some limitation on the date range it can handle.
time
module can handle dates in the more narrow range (1969-2038) then datetime
module (1-9999).
Change History (20)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Owner: | changed from | to
---|
comment:3 by , 12 years ago
The other corner case is dates after 2038. While simple check for < 1916
will work for dates before 1900, as there were no DST at that time, after 2038 we will probably still have DST. 2038 is not near, but I can imagine a calendar application with events in future.
comment:4 by , 12 years ago
Indeed, this is a limitation of django.utils.tzinfo.LocalTimezone
.
Could you try installing pytz
(as strongly recommeded by the docs)? If that fixes the problem, I'd lean towards a documentation fix rather than extending the huge hack that is LocalTimezone
.
Note that dates after 2038 are handled by returning incorrect data rather than crashing. I deeply dislike that, but I can't change it easily, for backwards-compatibility reasons.
comment:6 by , 12 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Waiting to know if installing pytz resolves the problem...
comment:9 by , 12 years ago
Component: | Uncategorized → Documentation |
---|---|
Resolution: | needsinfo |
Status: | closed → reopened |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
I'm accepting the ticket primarily as a documentation issue. This is related to #17262. I still have to decide how to deal with this.
comment:10 by , 12 years ago
Maybe it would be OK to require pytz installed if user set USE_I18N = True in settings.py?
Or maybe we can even set a hard dependency? I think it's now commonly consented that django should make more use of pip - at least localflavor will be separated, etc. So, maybe pytz would be the first thing to require?
comment:11 by , 12 years ago
USE_TZ = True
is in the default settings files (for a good reason) and we want to keep Django usable without having to learn pip / virtualenv first (also for a good reason). I went to great lengths to ensure that pytz wouldn't be a mandatory.
I think it's more promising to raise a more explicit error in this case, ie. "value outside of supported range, please install pytz".
I
comment:12 by , 12 years ago
Yeah, it may be a good solution. But may this behaviour be considered as a regression? Imagine, you have some values outside of supported range in database, then upgrade django one version upper, set USE_TZ=True, as docs insist. And live being happy until someday you see in logs such message about pytz.
comment:13 by , 12 years ago
Status: | reopened → new |
---|
comment:14 by , 11 years ago
Just to back up void's comment, I have just upgraded a site from Django 1.1 to 1.5. One of the few things that only bit me once it went live (and thanks as ever for the good documentation on upgrades and consistency :) ) was that I have some very old datetimes in the live database (back to the 17th century) that were working fine, that then started to throw "ValueError: year out of range" 500s due to this issue. It does indeed resolve if pytz is installed (or if USE_TZ is set back to False, which I've done for now).
comment:15 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
As wikipedia article says, DST was not used before at least 1916. So, maybe simple check like
will work