diff --git a/sat/tools/common/template.py b/sat/tools/common/template.py index 632e3ee0..d7a1c15b 100644 --- a/sat/tools/common/template.py +++ b/sat/tools/common/template.py @@ -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