diff sat/tools/common/date_utils.py @ 4028:883db2790b11

tools (common/date_utils): let use a timezone name as str in `date_fmt`
author Goffi <goffi@goffi.org>
date Thu, 30 Mar 2023 16:51:09 +0200
parents 92482cc80d0b
children 432f7e422a27
line wrap: on
line diff
--- a/sat/tools/common/date_utils.py	Thu Mar 30 16:47:41 2023 +0200
+++ b/sat/tools/common/date_utils.py	Thu Mar 30 16:51:09 2023 +0200
@@ -138,12 +138,20 @@
     return calendar.timegm(dt.utctimetuple())
 
 
-def date_fmt(timestamp, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short",
-             auto_new_fmt="relative", locale_str=C.DEFAULT_LOCALE, tz_info=TZ_UTC):
-    """format date according to locale
+def date_fmt(
+    timestamp: Union[float, int, str],
+    fmt: str = "short",
+    date_only: bool = False,
+    auto_limit: int = 7,
+    auto_old_fmt: str = "short",
+    auto_new_fmt: str = "relative",
+    locale_str: str = C.DEFAULT_LOCALE,
+    tz_info: Union[datetime.tzinfo, str] = TZ_UTC
+) -> str:
+    """Format date according to locale
 
-    @param timestamp(basestring, float): unix time
-    @param fmt(str): one of:
+    @param timestamp: unix time
+    @param fmt: one of:
         - short: e.g. u'31/12/17'
         - medium: e.g. u'Apr 1, 2007'
         - long: e.g. u'April 1, 2007'
@@ -159,17 +167,19 @@
             old format will be short, and new format will be time only
         or a free value which is passed to babel.dates.format_datetime
         (see http://babel.pocoo.org/en/latest/dates.html?highlight=pattern#pattern-syntax)
-    @param date_only(bool): if True, only display date (not datetime)
-    @param auto_limit (int): limit in days before using auto_old_fmt
+    @param date_only: if True, only display date (not datetime)
+    @param auto_limit: limit in days before using auto_old_fmt
         use 0 to have a limit at last midnight (day change)
-    @param auto_old_fmt(unicode): format to use when date is older than limit
-    @param auto_new_fmt(unicode): format to use when date is equal to or more recent
+    @param auto_old_fmt: format to use when date is older than limit
+    @param auto_new_fmt: format to use when date is equal to or more recent
         than limit
-    @param locale_str(unicode): locale to use (as understood by babel)
-    @param tz_info(datetime.tzinfo): time zone to use
+    @param locale_str: locale to use (as understood by babel)
+    @param tz_info: time zone to use
 
     """
     timestamp = float(timestamp)
+    if isinstance(tz_info, str):
+        tz_info = tz.gettz(tz_info)
     if fmt == "auto_day":
         fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm"
     if fmt == "auto":