|
9 | 9 | from django.http import QueryDict
|
10 | 10 | from django.test import TestCase, override_settings
|
11 | 11 | from django.utils import six
|
12 |
| -from django.utils.timezone import activate, deactivate, utc |
| 12 | +from django.utils.timezone import activate, deactivate, override, utc |
13 | 13 |
|
14 | 14 | import rest_framework
|
15 | 15 | from rest_framework import compat, serializers
|
@@ -1296,6 +1296,27 @@ def test_current_timezone(self):
|
1296 | 1296 | assert self.field.default_timezone() == utc
|
1297 | 1297 |
|
1298 | 1298 |
|
| 1299 | +@pytest.mark.skipif(pytz is None, reason='pytz not installed') |
| 1300 | +@override_settings(TIME_ZONE='UTC', USE_TZ=True) |
| 1301 | +class TestCustomTimezoneForDateTimeField(TestCase): |
| 1302 | + |
| 1303 | + @classmethod |
| 1304 | + def setup_class(cls): |
| 1305 | + cls.kolkata = pytz.timezone('Asia/Kolkata') |
| 1306 | + cls.date_format = '%d/%m/%Y %H:%M' |
| 1307 | + |
| 1308 | + def test_should_render_date_time_in_default_timezone(self): |
| 1309 | + field = serializers.DateTimeField(default_timezone=self.kolkata, format=self.date_format) |
| 1310 | + dt = datetime.datetime(2018, 2, 8, 14, 15, 16, tzinfo=pytz.utc) |
| 1311 | + |
| 1312 | + with override(self.kolkata): |
| 1313 | + rendered_date = field.to_representation(dt) |
| 1314 | + |
| 1315 | + rendered_date_in_timezone = dt.astimezone(self.kolkata).strftime(self.date_format) |
| 1316 | + |
| 1317 | + assert rendered_date == rendered_date_in_timezone |
| 1318 | + |
| 1319 | + |
1299 | 1320 | class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
|
1300 | 1321 | """
|
1301 | 1322 | Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.
|
|
0 commit comments