From 9262d6cb883e6a5fc809b512b9ce8fef547d8a7a Mon Sep 17 00:00:00 2001 From: Kosei Kitahara Date: Tue, 17 Oct 2017 23:23:14 +0900 Subject: [PATCH] Support active timezone (#750) * Support active timezone * Add pytz to 3rd-parties --- django_filters/utils.py | 2 +- setup.cfg | 2 +- tests/test_fields.py | 28 +++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/django_filters/utils.py b/django_filters/utils.py index 1dda7b0a3..1531fadb9 100644 --- a/django_filters/utils.py +++ b/django_filters/utils.py @@ -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 diff --git a/setup.cfg b/setup.cfg index 21be78468..4eed51dda 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,5 +9,5 @@ skip=.tox atomic=true multi_line_output=3 known_standard_library=mock -known_third_party=django,rest_framework +known_third_party=django,pytz,rest_framework known_first_party=django_filters diff --git a/tests/test_fields.py b/tests/test_fields.py index 66a6d7fa5..8ede1c947 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -3,9 +3,16 @@ import decimal from datetime import datetime, time, timedelta, tzinfo +import pytz from django import forms from django.test import TestCase, override_settings -from django.utils.timezone import get_default_timezone, make_aware +from django.utils.timezone import ( + activate, + deactivate, + get_current_timezone, + get_default_timezone, + make_aware +) from django_filters.fields import ( BaseCSVField, @@ -169,6 +176,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