comparison sat/tools/common/template.py @ 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 524856bd7b19
comparison
equal deleted inserted replaced
4030:73936abc6838 4031:a2d4bd1943ba
20 20
21 import os.path 21 import os.path
22 import time 22 import time
23 import re 23 import re
24 import json 24 import json
25 from datetime import datetime
25 from pathlib import Path 26 from pathlib import Path
26 from collections import namedtuple 27 from collections import namedtuple
27 from typing import Optional, List, Tuple 28 from typing import Optional, List, Tuple, Union
28 from xml.sax.saxutils import quoteattr 29 from xml.sax.saxutils import quoteattr
29 from babel import support 30 from babel import support
30 from babel import Locale 31 from babel import Locale
31 from babel.core import UnknownLocaleError 32 from babel.core import UnknownLocaleError
32 import pygments 33 import pygments
465 466
466 # custom filters 467 # custom filters
467 self.env.filters["next_gidx"] = self._next_gidx 468 self.env.filters["next_gidx"] = self._next_gidx
468 self.env.filters["cur_gidx"] = self._cur_gidx 469 self.env.filters["cur_gidx"] = self._cur_gidx
469 self.env.filters["date_fmt"] = self._date_fmt 470 self.env.filters["date_fmt"] = self._date_fmt
471 self.env.filters["timestamp_to_hour"] = self._timestamp_to_hour
472 self.env.filters["delta_to_human"] = date_utils.delta2human
470 self.env.filters["xmlui_class"] = self._xmlui_class 473 self.env.filters["xmlui_class"] = self._xmlui_class
471 self.env.filters["attr_escape"] = self.attr_escape 474 self.env.filters["attr_escape"] = self.attr_escape
472 self.env.filters["item_filter"] = self._item_filter 475 self.env.filters["item_filter"] = self._item_filter
473 self.env.filters["adv_format"] = self._adv_format 476 self.env.filters["adv_format"] = self._adv_format
474 self.env.filters["dict_ext"] = self._dict_ext 477 self.env.filters["dict_ext"] = self._dict_ext
771 auto_new_fmt: str = "relative", 774 auto_new_fmt: str = "relative",
772 tz_name: Optional[str] = None 775 tz_name: Optional[str] = None
773 ) -> str: 776 ) -> str:
774 if is_undefined(fmt): 777 if is_undefined(fmt):
775 fmt = "short" 778 fmt = "short"
779
776 try: 780 try:
777 return date_utils.date_fmt( 781 return date_utils.date_fmt(
778 timestamp, fmt, date_only, auto_limit, auto_old_fmt, 782 timestamp, fmt, date_only, auto_limit, auto_old_fmt,
779 auto_new_fmt, locale_str = self._locale_str, 783 auto_new_fmt, locale_str = self._locale_str,
780 tz_info=tz_name or date_utils.TZ_UTC 784 tz_info=tz_name or date_utils.TZ_UTC
781 ) 785 )
782 except Exception as e: 786 except Exception as e:
783 log.warning(_("Can't parse date: {msg}").format(msg=e)) 787 log.warning(_("Can't parse date: {msg}").format(msg=e))
784 return timestamp 788 return str(timestamp)
789
790 def _timestamp_to_hour(self, timestamp: float) -> int:
791 """Get hour of day corresponding to a timestamp"""
792 dt = datetime.fromtimestamp(timestamp)
793 return dt.hour
785 794
786 def attr_escape(self, text): 795 def attr_escape(self, text):
787 """escape a text to a value usable as an attribute 796 """escape a text to a value usable as an attribute
788 797
789 remove spaces, and put in lower case 798 remove spaces, and put in lower case