Added a template filter that is a replacement for Django's builtin 'date' filter, with a more polymorphic signature: It accepts either datetime or date input, and ignores time-specific format entries when handling a date.

- Legacy-Id: 17686
This commit is contained in:
Henrik Levkowetz 2020-04-25 11:29:15 +00:00
parent e0bd77f8a3
commit 59df536958

View file

@ -0,0 +1,29 @@
# Copyright The IETF Trust 2020, All Rights Reserved
# -*- coding: utf-8 -*-
import datetime
from django.template import Library
from django.template.defaultfilters import date
import debug # pyflakes:ignore
register = Library()
elide_timefmt = str.maketrans("aAefgGhHiIOPsTuZ:", " ")
@register.filter()
def dateformat(value, arg=None):
"""
Formats a date or datetime according to the given format.
Ignores the time-related format elements if a date is given.
"""
if value in (None, ''):
return ''
debug.type('value')
if isinstance(value, datetime.datetime):
pass
elif isinstance(value, datetime.date):
debug.say('got date')
arg = arg.translate(elide_timefmt).strip()
return date(value, arg)