Mercurial > libervia-backend
diff libervia/backend/tools/common/date_utils.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | c6d85c31a59f |
children |
line wrap: on
line diff
--- a/libervia/backend/tools/common/date_utils.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/tools/common/date_utils.py Wed Jun 19 18:44:57 2024 +0200 @@ -41,7 +41,7 @@ r"\s*(?P<quantity>\d+)\s*" r"(?P<unit>(second|sec|s|minute|min|month|mo|m|hour|hr|h|day|d|week|w|year|yr|y))s?" r"(?P<ago>\s+ago)?\s*", - re.I + re.I, ) TIME_SYMBOL_MAP = { "s": "second", @@ -72,9 +72,7 @@ dayfirst = False if YEAR_FIRST_RE.match(value) else True try: - dt = default_tzinfo( - parser.parse(value, dayfirst=dayfirst), - default_tz) + dt = default_tzinfo(parser.parse(value, dayfirst=dayfirst), default_tz) except ParserError as e: if value == "now": dt = datetime.datetime.now(tz.tzutc()) @@ -86,7 +84,8 @@ raise e return calendar.timegm(dt.utctimetuple()) -def date_parse_ext(value: str, default_tz: datetime.tzinfo=TZ_UTC) -> float: + +def date_parse_ext(value: str, default_tz: datetime.tzinfo = TZ_UTC) -> float: """Extended date parse which accept relative date @param value: date to parse, in any format supported by parser @@ -103,10 +102,9 @@ return date_parse(value, default_tz=default_tz) if sum(1 for g in ("direction", "in", "ago") if m.group(g)) > 1: - raise ValueError( - _('You can use only one of direction (+ or -), "in" and "ago"')) + raise ValueError(_('You can use only one of direction (+ or -), "in" and "ago"')) - if m.group("direction") == '-' or m.group("ago"): + if m.group("direction") == "-" or m.group("ago"): direction = -1 else: direction = 1 @@ -146,7 +144,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 + tz_info: Union[datetime.tzinfo, str] = TZ_UTC, ) -> str: """Format date according to locale @@ -186,8 +184,9 @@ if auto_limit == 0: now = datetime.datetime.now(tz_info) # we want to use given tz_info, so we don't use date() or today() - today = datetime.datetime(year=now.year, month=now.month, day=now.day, - tzinfo=now.tzinfo) + today = datetime.datetime( + year=now.year, month=now.month, day=now.day, tzinfo=now.tzinfo + ) today = calendar.timegm(today.utctimetuple()) if timestamp < today: fmt = auto_old_fmt @@ -210,8 +209,9 @@ dt = datetime.datetime.fromtimestamp(timestamp, tz_info) return dates.format_date(dt, format=fmt, locale=locale_str) else: - return dates.format_datetime(timestamp, format=fmt, locale=locale_str, - tzinfo=tz_info) + return dates.format_datetime( + timestamp, format=fmt, locale=locale_str, tzinfo=tz_info + ) elif fmt == "iso": if date_only: fmt = "yyyy-MM-dd" @@ -219,8 +219,9 @@ fmt = "yyyy-MM-ddTHH:mm:ss'Z'" return dates.format_datetime(timestamp, format=fmt) else: - return dates.format_datetime(timestamp, format=fmt, locale=locale_str, - tzinfo=tz_info) + return dates.format_datetime( + timestamp, format=fmt, locale=locale_str, tzinfo=tz_info + ) def delta2human(start_ts: Union[float, int], end_ts: Union[float, int]) -> str: @@ -234,8 +235,7 @@ "end timestamp must be bigger or equal to start timestamp !" ) rd = relativedelta( - datetime.datetime.fromtimestamp(end_ts), - datetime.datetime.fromtimestamp(start_ts) + datetime.datetime.fromtimestamp(end_ts), datetime.datetime.fromtimestamp(start_ts) ) text_elems = [] for unit in ("years", "months", "days", "hours", "minutes"):