mirror of
https://github.com/osmarks/autobotrobot
synced 2025-01-09 15:10:27 +00:00
refactor time unit mappings
This commit is contained in:
parent
2da79c6dfe
commit
41c1636398
55
src/util.py
55
src/util.py
@ -23,16 +23,40 @@ prefixes = {
|
|||||||
}
|
}
|
||||||
number = "(-?[0-9]+(?:\.[0-9]+)?)(" + "|".join(prefixes.keys()) + ")?"
|
number = "(-?[0-9]+(?:\.[0-9]+)?)(" + "|".join(prefixes.keys()) + ")?"
|
||||||
|
|
||||||
# from here: https://github.com/Rapptz/RoboDanny/blob/18b92ae2f53927aedebc25fb5eca02c8f6d7a874/cogs/utils/time.py
|
time_units = (
|
||||||
short_timedelta_regex = re.compile(f"""
|
("galacticyears", "cosmicyears", "gy", "[Cc]y"),
|
||||||
(?:(?P<years>{number})(?:years?|y))? # e.g. 2y
|
("years", "y"),
|
||||||
(?:(?P<months>{number})(?:months?|mo))? # e.g. 2months
|
("beelifespans", "🐝", "bees?"),
|
||||||
(?:(?P<fortnights>{number})(?:fortnights?|fn|f))? # e.g. 10fn
|
("months", "mo"),
|
||||||
(?:(?P<weeks>{number})(?:weeks?|w))? # e.g. 10w
|
("semesters",),
|
||||||
(?:(?P<days>{number})(?:days?|d))? # e.g. 14d
|
("fortnights", "ft?n?"),
|
||||||
(?:(?P<hours>{number})(?:hours?|h))? # e.g. 12h
|
("weeks", "w"),
|
||||||
(?:(?P<minutes>{number})(?:minutes?|m))? # e.g. 10m
|
("days", "d"),
|
||||||
(?:(?P<seconds>{number})(?:seconds?|s))? # e.g. 15s """, re.VERBOSE)
|
("hours", "h"),
|
||||||
|
# Wikipedia tells me this is a traditional Chinese timekeeping unit
|
||||||
|
("ke",),
|
||||||
|
("minutes", "m"),
|
||||||
|
("seconds", "s")
|
||||||
|
)
|
||||||
|
|
||||||
|
tu_mappings = {
|
||||||
|
# dateutil dislikes fractional years, but this is 250My
|
||||||
|
"galacticyears": (7.8892315e15, "seconds"),
|
||||||
|
# apparently the average lifespan of a Western honey bee - I'm not very sure whether this is workers/drones/queens or what so TODO
|
||||||
|
"beelifespans": lambda: (random.randint(122, 152), "days"),
|
||||||
|
"semesters": (18, "weeks"),
|
||||||
|
"fortnights": (2, "weeks"),
|
||||||
|
"ke": (864, "seconds")
|
||||||
|
}
|
||||||
|
|
||||||
|
def rpartfor(u):
|
||||||
|
if u[0][-1] == "s":
|
||||||
|
l = [u[0] + "?"]
|
||||||
|
l.extend(u[1:])
|
||||||
|
else: l = u
|
||||||
|
return f"(?:(?P<{u[0]}>{number})(?:{'|'.join(l)}))?"
|
||||||
|
|
||||||
|
short_timedelta_regex = re.compile("\n".join(map(rpartfor, time_units)), re.VERBOSE)
|
||||||
|
|
||||||
def parse_prefixed(s):
|
def parse_prefixed(s):
|
||||||
match = re.match(number, s)
|
match = re.match(number, s)
|
||||||
@ -46,18 +70,21 @@ def parse_short_timedelta(text):
|
|||||||
match = short_timedelta_regex.fullmatch(text)
|
match = short_timedelta_regex.fullmatch(text)
|
||||||
if match is None or not match.group(0): raise ValueError("parse failed")
|
if match is None or not match.group(0): raise ValueError("parse failed")
|
||||||
data = { k: parse_prefixed(v) if v else 0 for k, v in match.groupdict().items() }
|
data = { k: parse_prefixed(v) if v else 0 for k, v in match.groupdict().items() }
|
||||||
data["weeks"] += data["fortnights"] * 2
|
for tu, mapping in tu_mappings.items():
|
||||||
del data["fortnights"]
|
if callable(mapping): mapping = mapping()
|
||||||
|
qty, resunit = mapping
|
||||||
|
data[resunit] += qty * data[tu]
|
||||||
|
del data[tu]
|
||||||
return datetime.datetime.now(tz=datetime.timezone.utc) + relativedelta(**data)
|
return datetime.datetime.now(tz=datetime.timezone.utc) + relativedelta(**data)
|
||||||
|
|
||||||
cal = parsedatetime.Calendar()
|
cal = parsedatetime.Calendar()
|
||||||
def parse_humantime(text):
|
def parse_humantime(text):
|
||||||
dt_tuple = cal.nlp(text)
|
dt_tuple = cal.nlp(text)
|
||||||
if dt_tuple: return dt_tuple[0][0]
|
if dt_tuple: return dt_tuple[0][0].replace(tzinfo=datetime.timezone.utc)
|
||||||
else: raise ValueError("parse failed")
|
else: raise ValueError("parse failed")
|
||||||
|
|
||||||
def parse_time(text):
|
def parse_time(text):
|
||||||
try: return datetime.datetime.strptime(text, "%d/%m/%Y")
|
try: return datetime.datetime.strptime(text, "%d/%m/%Y").replace(tzinfo=datetime.timezone.utc)
|
||||||
except: pass
|
except: pass
|
||||||
try: return parse_short_timedelta(text)
|
try: return parse_short_timedelta(text)
|
||||||
except: pass
|
except: pass
|
||||||
|
Loading…
Reference in New Issue
Block a user