tools (common/date_utils): let use a timezone name as str in date_fmt

This commit is contained in:
Goffi 2023-03-30 16:51:09 +02:00
commit 63d62f66f0

View file

@ -138,12 +138,20 @@ def date_parse_ext(value, default_tz=TZ_UTC):
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 @@ def date_fmt(timestamp, fmt="short", date_only=False, auto_limit=7, auto_old_fmt
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":