diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2021-05-13 13:52:18 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-01-05 12:51:55 +0100 |
| commit | 65477fd7dae9e2c9f4a241b2eddf25bdb1ceeb0f (patch) | |
| tree | 29838832dea9fd3c57d40a049250cff01fb2b829 /django/utils | |
| parent | 41ca2afd1ce49949d509177987c3b4b7c8ba3fa1 (diff) | |
Added support for datetime.date to DateFormat.r().
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/dateformat.py | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index adf748b21c..103bff7aae 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -265,16 +265,14 @@ class DateFormat(TimeFormat): def r(self): "RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" - if type(self.data) is date: - raise TypeError( - "The format for date objects may not contain time-related " - "format specifiers (found 'r')." - ) - if is_naive(self.data): - dt = make_aware(self.data, timezone=self.timezone) - else: - dt = self.data - return format_datetime_rfc5322(dt) + value = self.data + if not isinstance(value, datetime): + # Assume midnight in default timezone if datetime.date provided. + default_timezone = get_default_timezone() + value = datetime.combine(value, time.min).replace(tzinfo=default_timezone) + elif is_naive(value): + value = make_aware(value, timezone=self.timezone) + return format_datetime_rfc5322(value) def S(self): """ |
