Opened 9 years ago

Closed 9 years ago

Last modified 5 years ago

#26281 closed Cleanup/optimization (fixed)

Improve utils.formats.date_format() error message when using time formatting with datetime.date

Reported by: Florian Eßer Owned by: Marko Benko
Component: Utilities Version: 1.9
Severity: Normal Keywords:
Cc: f.esser@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When using date_format() on a datetime.date object, I get the following error:

File "<project>/<app>/models.py" in __str__
  104.         return date_format(self.date, format="%A, %d.%m.%Y")

File "<project>/env/lib/python3.4/site-packages/django/utils/formats.py" in date_format
  151.     return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))

File "<project>/env/lib/python3.4/site-packages/django/utils/dateformat.py" in format
  367.     return df.format(format_string)

File "<project>/env/lib/python3.4/site-packages/django/utils/dateformat.py" in format
  37.                 pieces.append(force_text(getattr(self, piece)()))

File "<project>/env/lib/python3.4/site-packages/django/utils/dateformat.py" in A
  66.         if self.data.hour > 11:

Exception Type: AttributeError at /
Exception Value: 'datetime.date' object has no attribute 'hour'

It seems to expect a datetime.datetimeobject, while the docstring for date_format() explicitly mentions datetime.date as a valid input, too: "Formats a datetime.date or datetime.datetime object using a localizable format".

How to reproduce:

models.py:

from django.db import models
from django.utils.formats import date_format

class ArrivalDate(models.Model):
    date = models.DateField()

    def __str__(self):
        return date_format(self.date, format="%A, %d.%m.%Y")

template.html:

the date is {{ arrivaldate_instance }}

settings.py:

LANGUAGE_CODE = 'de-de'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

I'm running Django 1.9.2 on Python 3.4

Change History (8)

comment:1 by Florian Eßer, 9 years ago

Cc: f.esser@… added

comment:2 by Tim Graham, 9 years ago

Component: UncategorizedUtilities
Summary: utils.formats.date_format() does not work with datetime.dateImprove utils.formats.date_format() error message when using time formatting with datetime.date
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

You can't use %A (which corresponds to AM/PM) with a date. I guess you were looking at Python's formatting characters where that corresponds to "Weekday as locale’s full name".

Accepting on the basis that it might be feasible to improve the error message.

comment:3 by Florian Eßer, 9 years ago

I indeed was expecting to use Python's formatting characters in a Python project.

Using PHP's formatting characters instead (format="l, d.m.Y") as documented here gives me the result I was looking for.

Considering the method is intended for use in a Twig-like template environment, the design decision to make it PHP compatible seems reasonable. But mentioning that in the docstring and the error message would help to prevent misunderstandings like mine.

comment:4 by Marko Benko, 9 years ago

Owner: changed from nobody to Marko Benko
Status: newassigned

comment:5 by Tim Graham, 9 years ago

Has patch: set

comment:6 by Tim Graham, 9 years ago

Patch needs improvement: set

Left some comments for improvement.

comment:7 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 45c7acdc:

Fixed #26281 -- Added a helpful error message for an invalid format specifier to dateformat.format().

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 76ec032:

Refs #26281 -- Added a helpful error message for an invalid "r" specifier to dateformat.format().

Note: See TracTickets for help on using tickets.
Back to Top