Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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)

Change History (3)

comment:1 by Mike Edmunds, 7 years ago

Owner: changed from nobody to Mike Edmunds
Status: newassigned

comment:2 by Mike Edmunds, 7 years ago

Has patch: set

PR submitted, suitable for current dev version (and/or as far back as 1.8).

Last edited 7 years ago by Tim Graham (previous) (diff)

comment:3 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In d1317ed:

Fixed #28739 -- Fixed get_fixed_timezone() for negative timedeltas.

Note: See TracTickets for help on using tickets.
Back to Top