From f3dd4b8ddc4ec77b450f619aa522e7174c75b972 Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 30 Mar 2023 16:57:11 +0200 Subject: [PATCH] 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. --- sat/tools/common/template.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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