Skip to content

Commit

Permalink
Support active timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Surgo committed Jul 21, 2017
1 parent 7f69e35 commit 1bd52bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def resolve_field(model_field, lookup_expr):

def handle_timezone(value, is_dst=None):
if settings.USE_TZ and timezone.is_naive(value):
return make_aware(value, timezone.get_default_timezone(), is_dst)
return make_aware(value, timezone.get_current_timezone(), is_dst)
elif not settings.USE_TZ and timezone.is_aware(value):
return timezone.make_naive(value, timezone.utc)
return value
Expand Down
22 changes: 21 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from datetime import datetime, time, timedelta, tzinfo
import decimal

import pytz
from django import forms
from django.test import TestCase, override_settings
from django.utils.timezone import make_aware, get_default_timezone
from django.utils.timezone import activate, deactivate, make_aware, get_default_timezone, get_current_timezone

from django_filters.widgets import BaseCSVWidget, CSVWidget, RangeWidget
from django_filters.fields import (
Expand Down Expand Up @@ -163,6 +164,25 @@ def test_datetime_timezone_awareness(self):
self.assertTrue(isinstance(d.tzinfo, tzinfo))
self.assertEqual(d, r)

def test_datetime_timezone_with_current_timezone(self):
z = pytz.timezone('Asia/Tokyo') # Central European Time +01:00

f = IsoDateTimeField()
r = make_aware(self.reference_dt, get_default_timezone())

activate(z)
d = f.strptime(self.reference_str + "+01:00", IsoDateTimeField.ISO_8601)
deactivate()
self.assertTrue(isinstance(d.tzinfo, tzinfo))
self.assertEqual(d, r + r.utcoffset() - d.utcoffset())

activate(z)
d = f.strptime(self.reference_str + "", IsoDateTimeField.ISO_8601)
deactivate()
self.assertTrue(isinstance(d.tzinfo, tzinfo))
self.assertEqual(z.zone, d.tzinfo.zone)
self.assertEqual(d, r + r.utcoffset() - d.utcoffset())

@override_settings(USE_TZ=False)
def test_datetime_timezone_naivety(self):
# parsed datetimes should obey USE_TZ
Expand Down

0 comments on commit 1bd52bb

Please sign in to comment.