def test_to_representation_returns_value_in_iso8601(self): instance = fields.DateTimeField() value = datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC()) self.assertEqual( instance.to_representation(value), '2000-01-01T10:00:00Z' )
def test_enforce_timezone_returns_naive_datetime_with_utc_timezone(self): instance = fields.DateTimeField() value = datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC()) self.assertEqual( instance.enforce_timezone(value), datetime.datetime(2000, 1, 1, 10, 0) )
def enforce_timezone(self, value): """ When `self.timezone` is `None`, always return naive datetimes. When `self.timezone` is not `None`, always return aware datetimes. """ field_timezone = getattr(self, 'timezone', self.default_timezone()) if (field_timezone is not None) and not timezone.is_aware(value): return timezone.make_aware(value, field_timezone) elif (field_timezone is None) and timezone.is_aware(value): return timezone.make_naive(value, timezone.UTC()) return value
def test_enforce_timezone_returns_aware_datetime_with_utc_timezone(self): instance = fields.DateTimeField(default_timezone=timezone.UTC()) self.assertEqual( instance.enforce_timezone(datetime.datetime(2000, 1, 1, 10, 0)), datetime.datetime(2000, 1, 1, 10, 0, tzinfo=timezone.UTC()))