# HG changeset patch # User Goffi # Date 1680188231 -7200 # Node ID a2d4bd1943ba14324d98bad72a9295529144a94c # Parent 73936abc68384069ceab95a2be68b9635571945c 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. diff -r 73936abc6838 -r a2d4bd1943ba sat/tools/common/template.py --- a/sat/tools/common/template.py Thu Mar 30 16:53:41 2023 +0200 +++ b/sat/tools/common/template.py Thu Mar 30 16:57:11 2023 +0200 @@ -22,9 +22,10 @@ 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 @@ 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 @@ ) -> 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 @@ ) 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