Skip to content

Commit c2bee3c

Browse files
committed
Add failing test for to_representation with explicit default timezone
See discussion here: encode#5435 (comment)
1 parent d1c92c8 commit c2bee3c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tests/test_fields.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,25 @@ def test_current_timezone(self):
12961296
assert self.field.default_timezone() == utc
12971297

12981298

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+
rendered_date = field.to_representation(dt)
1313+
rendered_date_in_timezone = dt.astimezone(self.kolkata).strftime(self.date_format)
1314+
1315+
assert rendered_date == rendered_date_in_timezone
1316+
1317+
12991318
class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
13001319
"""
13011320
Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.

0 commit comments

Comments
 (0)