diff --git a/src/reminders.py b/src/reminders.py index 72769e0..84b6d9d 100644 --- a/src/reminders.py +++ b/src/reminders.py @@ -28,7 +28,7 @@ def setup(bot): now = datetime.now(tz=timezone.utc) time = util.parse_time(time) except: - await ctx.send(embed=util.error_embed("Invalid time (wrong format/too large/non-integer months or years)")) + await ctx.send(embed=util.error_embed("Invalid time (wrong format/too large months or years)")) return await bot.database.execute("INSERT INTO reminders (remind_timestamp, created_timestamp, reminder, expired, extra) VALUES (?, ?, ?, ?, ?)", (time.timestamp(), now.timestamp(), reminder, 0, util.json_encode(extra_data))) diff --git a/src/util.py b/src/util.py index f980d37..ea174a2 100644 --- a/src/util.py +++ b/src/util.py @@ -12,6 +12,7 @@ import os.path from discord.ext import commands import hashlib import time +import math config = {} @@ -60,12 +61,17 @@ tu_mappings = { "ke": (864, "seconds") } +fractional_tu_mappings = { + "years": (365.25, "days"), # Julian year + "months": (30.4375, "days") # average month length +} + 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)}))?" + return f"(?:(?P<{u[0]}>{number})(?:{'|'.join(l)}))?[\t\n\r ]*" short_timedelta_regex = re.compile("\n".join(map(rpartfor, time_units)), re.VERBOSE) @@ -86,6 +92,12 @@ def parse_short_timedelta(text): qty, resunit = mapping data[resunit] += qty * data[tu] del data[tu] + for tu, (qty, unit) in fractional_tu_mappings.items(): + if tu in data and math.floor(data[tu]) != data[tu]: + whole = math.floor(data[tu]) + fractional = data[tu] - whole + data[tu] = whole + data[unit] += fractional * qty return datetime.datetime.now(tz=datetime.timezone.utc) + relativedelta(**data) cal = parsedatetime.Calendar()