Pavel "LEdoian" Turinsky
4 years ago
3 changed files with 43 additions and 3 deletions
@ -0,0 +1,27 @@ |
|||||
|
from django import template |
||||
|
from datetime import datetime, timedelta |
||||
|
from pytz import timezone |
||||
|
from mamweb.settings import TIME_ZONE |
||||
|
import logging |
||||
|
register = template.Library() |
||||
|
|
||||
|
logger = logging.getLogger(__name__) |
||||
|
|
||||
|
@register.filter(name='kratke_datum', expects_localtime=True) |
||||
|
def kratke_datum(dt): |
||||
|
# None dává None, ne-datum dává False, aby se daly použít filtry typu "default". |
||||
|
if dt is None: |
||||
|
return None |
||||
|
if not isinstance(dt, datetime): |
||||
|
logger.warning(f"Špatné volání filtru {__name__}: {dt}") |
||||
|
return False |
||||
|
naive_now = datetime.now() |
||||
|
tz = timezone(TIME_ZONE) |
||||
|
now = tz.localize(naive_now) |
||||
|
delta = dt - now |
||||
|
if delta <= timedelta(days=1): |
||||
|
return dt.strftime("%k:%M") |
||||
|
if delta <= timedelta(years=1): |
||||
|
return dt.strftime("%d. %m.") |
||||
|
return dt.strftime("%d. %m. %Y") |
||||
|
|
Loading…
Reference in new issue