tools (common/template): add 2 new filters: timestamp_to_hour and delta_to_human:

`timestamp_to_hour` give the hour corresponding to a timestamp, it will be used to display
calendar for this time window.
`delta_to_human` convert a delta of 2 timestamp to human readable time using
`tools.common.date_utils` corresponding function.
This commit is contained in:
Goffi 2023-03-30 16:57:11 +02:00
commit f3dd4b8ddc

View file

@ -22,9 +22,10 @@ import os.path
import time
import re
import json
from datetime import datetime
from pathlib import Path
from collections import namedtuple
from typing import Optional, List, Tuple
from typing import Optional, List, Tuple, Union
from xml.sax.saxutils import quoteattr
from babel import support
from babel import Locale
@ -467,6 +468,8 @@ class Renderer(object):
self.env.filters["next_gidx"] = self._next_gidx
self.env.filters["cur_gidx"] = self._cur_gidx
self.env.filters["date_fmt"] = self._date_fmt
self.env.filters["timestamp_to_hour"] = self._timestamp_to_hour
self.env.filters["delta_to_human"] = date_utils.delta2human
self.env.filters["xmlui_class"] = self._xmlui_class
self.env.filters["attr_escape"] = self.attr_escape
self.env.filters["item_filter"] = self._item_filter
@ -773,6 +776,7 @@ class Renderer(object):
) -> str:
if is_undefined(fmt):
fmt = "short"
try:
return date_utils.date_fmt(
timestamp, fmt, date_only, auto_limit, auto_old_fmt,
@ -781,7 +785,12 @@ class Renderer(object):
)
except Exception as e:
log.warning(_("Can't parse date: {msg}").format(msg=e))
return timestamp
return str(timestamp)
def _timestamp_to_hour(self, timestamp: float) -> int:
"""Get hour of day corresponding to a timestamp"""
dt = datetime.fromtimestamp(timestamp)
return dt.hour
def attr_escape(self, text):
"""escape a text to a value usable as an attribute