Mercurial > libervia-backend
changeset 4031:a2d4bd1943ba
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.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Mar 2023 16:57:11 +0200 |
parents | 73936abc6838 |
children | bb211f80c3e6 |
files | sat/tools/common/template.py |
diffstat | 1 files changed, 11 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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