# HG changeset patch # User Goffi # Date 1627591861 -7200 # Node ID ab1fe6b256312c802ce77126d7b52824c1970838 # Parent d390ff50af0f166c2c79c712a634937b8bdb8d0f tools (common/date_utils): accept more units (shortcuts) + fix timezone when date with relative delta is used diff -r d390ff50af0f -r ab1fe6b25631 sat/tools/common/date_utils.py --- a/sat/tools/common/date_utils.py Thu Jul 29 22:51:01 2021 +0200 +++ b/sat/tools/common/date_utils.py Thu Jul 29 22:51:01 2021 +0200 @@ -31,8 +31,22 @@ import re RELATIVE_RE = re.compile(r"(?P.*?)(?P[-+]?) *(?P\d+) *" - r"(?P(second|minute|hour|day|week|month|year))s?" + r"(?P(second|sec|s|minute|min|month|mo|m|hour|hr|h|day|d" + r"|week|w|year|yr|y))s?" r"(?P +ago)?", re.I) +TIME_SYMBOL_MAP = { + "s": "second", + "sec": "second", + "m": "minute", + "min": "minute", + "h": "hour", + "hr": "hour", + "d": "day", + "w": "week", + "mo": "month", + "y": "year", + "yr": "year", +} YEAR_FIRST_RE = re.compile(r"\d{4}[^\d]+") TZ_UTC = tz.tzutc() TZ_LOCAL = tz.gettz() @@ -84,11 +98,15 @@ if not date or date == "now": dt = datetime.datetime.now(tz.tzutc()) else: - dt = default_tzinfo(parser.parse(date, dayfirst=True)) + dt = default_tzinfo(parser.parse(date, dayfirst=True), default_tz) quantity = int(m.group("quantity")) - key = m.group("unit").lower() + "s" - delta_kw = {key: direction * quantity} + unit = m.group("unit").lower() + try: + unit = TIME_SYMBOL_MAP[unit] + except KeyError: + pass + delta_kw = {f"{unit}s": direction * quantity} dt = dt + relativedelta(**delta_kw) return calendar.timegm(dt.utctimetuple())