#28739 closed Bug (fixed)
get_fixed_timezone incorrect with negative timedelta
Reported by: | Mike Edmunds | Owned by: | Mike Edmunds |
---|---|---|---|
Component: | Uncategorized | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When called with a negative datetime.timedelta, django.utils.timezone.get_fixed_timezone instead creates a timezone with a positive offset 24 hours beyond the negative offset requested:
>>> import datetime >>> from django.utils.timezone import get_fixed_timezone >>> get_fixed_timezone(datetime.timedelta(hours=5)).tzname('') '+0500' # correct >>> get_fixed_timezone(datetime.timedelta(hours=-5)).tzname('') '+1900' # expected: '-0500'
(tzname is just an easy way to see the problem; it accurately reflects the created timezone's offset.)
This has been broken as far back as I've looked; probably everyone uses get_fixed_timezone with integer minutes rather than a timedelta. Patch follows.
Workaround
If you encounter this in an older version of Django that won't be patched, there's an easy workaround in your code (on Python 2.7 or later). Change:
tz = get_fixed_timezone(delta) # where delta is a datetime.timedelta object
to:
tz = get_fixed_timezone(delta.total_seconds() // 60)
PR submitted, suitable for current dev version (and/or as far back as 1.8).