fixup! Show times as local time
This commit is contained in:
parent
feede16265
commit
c36ee2494d
1 changed files with 40 additions and 0 deletions
|
|
@ -1,6 +1,11 @@
|
|||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from django import template
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from servala.core.crd.utils import deslugify
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
|
@ -10,3 +15,38 @@ def pprint(value):
|
|||
if isinstance(value, (dict, list)):
|
||||
return json.dumps(value, indent=2)
|
||||
return value
|
||||
|
||||
|
||||
@register.filter
|
||||
def localtime_tag(value, format_str="datetime"):
|
||||
"""
|
||||
Render a datetime value as a <time> element with datetime attribute.
|
||||
JavaScript will convert this to local time on page load.
|
||||
|
||||
Usage: {{ instance.created_at|localtime_tag }}
|
||||
{{ instance.created_at|localtime_tag:"date" }}
|
||||
{{ instance.created_at|localtime_tag:"time" }}
|
||||
"""
|
||||
if value is None:
|
||||
return "-"
|
||||
|
||||
if isinstance(value, str):
|
||||
iso_value = value
|
||||
elif isinstance(value, datetime):
|
||||
iso_value = value.isoformat()
|
||||
else:
|
||||
return str(value)
|
||||
|
||||
if (
|
||||
not iso_value.endswith("Z")
|
||||
and "+" not in iso_value
|
||||
and "-" not in iso_value[10:]
|
||||
):
|
||||
iso_value += "Z"
|
||||
|
||||
return format_html(
|
||||
'<time datetime="{}" data-format="{}" class="local-time">{}</time>',
|
||||
iso_value,
|
||||
format_str,
|
||||
iso_value,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue