only do to_pretty_time as relative when requested

This commit is contained in:
jesopo 2020-02-24 10:18:55 +00:00
parent a2c8d7080e
commit 8f9c063114
7 changed files with 52 additions and 24 deletions

View file

@ -194,7 +194,7 @@ class Module(ModuleManager.BaseModule):
time_left = self.bot.cache.until_expiration(cache) time_left = self.bot.cache.until_expiration(cache)
event["stderr"].write("%s: Please wait %s before redeeming" % ( event["stderr"].write("%s: Please wait %s before redeeming" % (
event["user"].nickname, event["user"].nickname,
utils.datetime.format.to_pretty_time(time_left))) utils.datetime.format.to_pretty_until(time_left)))
else: else:
event["stderr"].write( event["stderr"].write(
"%s: You can only redeem coins when you have none" % "%s: You can only redeem coins when you have none" %
@ -492,7 +492,7 @@ class Module(ModuleManager.BaseModule):
def next_lottery(self, event): def next_lottery(self, event):
until = self._until_next_6_hour() until = self._until_next_6_hour()
event["stdout"].write("Next lottery is in: %s" % event["stdout"].write("Next lottery is in: %s" %
utils.datetime.format.to_pretty_time(until)) utils.datetime.format.to_pretty_until(until))
@utils.hook("received.command.lotterywinner") @utils.hook("received.command.lotterywinner")
@utils.kwarg("help", "Show who last won the lottery") @utils.kwarg("help", "Show who last won the lottery")

View file

@ -16,7 +16,7 @@ class Module(ModuleManager.BaseModule):
for i, message in enumerate(message_list): for i, message in enumerate(message_list):
seconds = utils.datetime.seconds_since(message.line.timestamp) seconds = utils.datetime.seconds_since(message.line.timestamp)
messages.append("(%d/%d) %s ago %s" % (i+1, message_count, messages.append("(%d/%d) %s ago %s" % (i+1, message_count,
utils.datetime.format.to_pretty_time(seconds), utils.datetime.format.to_pretty_since(seconds),
message.line.format())) message.line.format()))
event["stdout"].write("%s: found: %s" event["stdout"].write("%s: found: %s"

View file

@ -35,7 +35,7 @@ class Module(ModuleManager.BaseModule):
seen_info = " (%s%s)" % (seen_info["action"], seen_info = " (%s%s)" % (seen_info["action"],
utils.consts.RESET) utils.consts.RESET)
since = utils.datetime.format.to_pretty_time( since = utils.datetime.format.to_pretty_since(
time.time()-seen_seconds, max_units=2) time.time()-seen_seconds, max_units=2)
event["stdout"].write("%s was last seen %s ago%s" % ( event["stdout"].write("%s was last seen %s ago%s" % (
event["args_split"][0], since, seen_info or "")) event["args_split"][0], since, seen_info or ""))

View file

@ -7,7 +7,7 @@ HIDDEN_MODES = set(["s", "p"])
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _uptime(self): def _uptime(self):
return utils.datetime.format.to_pretty_time( return utils.datetime.format.to_pretty_since(
int(time.time()-self.bot.start_time)) int(time.time()-self.bot.start_time))
@utils.hook("received.command.uptime") @utils.hook("received.command.uptime")

View file

@ -3,7 +3,7 @@ from src import utils
def _timestamp(dt): def _timestamp(dt):
seconds_since = time.time()-dt.timestamp() seconds_since = time.time()-dt.timestamp()
timestamp = utils.datetime.format.to_pretty_time( timestamp = utils.datetime.format.to_pretty_since(
seconds_since, max_units=2) seconds_since, max_units=2)
return "%s ago" % timestamp return "%s ago" % timestamp

View file

@ -40,3 +40,7 @@ def timestamp(seconds: float) -> _datetime.datetime:
def seconds_since(dt: _datetime.datetime) -> float: def seconds_since(dt: _datetime.datetime) -> float:
return (utcnow()-dt).total_seconds() return (utcnow()-dt).total_seconds()
class RelativeDirection(enum.Enum):
FORWARD = 1
BACKWARD = 2

View file

@ -48,31 +48,55 @@ def time_unit(seconds: int) -> typing.Tuple[int, str]:
unit = "%ss" % unit # pluralise the unit unit = "%ss" % unit # pluralise the unit
return (since, unit) return (since, unit)
def to_pretty_time(total_seconds: int, minimum_unit: int=UNIT_SECOND, def to_pretty_time(total_seconds: int, max_units: int=UNIT_MINIMUM,
max_units: int=UNIT_MINIMUM) -> str: direction: typing.Optional[RelativeDirection]=None) -> str:
if total_seconds == 0: if total_seconds == 0:
return "0s" return "0s"
now = utcnow() if not direction == None:
later = now+_datetime.timedelta(seconds=total_seconds) now = utcnow()
relative = dateutil.relativedelta.relativedelta(later, now) later = now
mod = _datetime.timedelta(seconds=total_seconds)
if direction == RelativeDirection.FORWARD:
later += mod
else:
later -= mod
relative = dateutil.relativedelta.relativedelta(
*sorted([later, now], reverse=True))
years = relative.years
months = relative.months
weeks, days = divmod(relative.days, 7)
hours = relative.hours
minutes = relative.minutes
seconds = relative.seconds
else:
years, months = 0, 0
weeks, days = divmod(total_seconds, SECONDS_WEEKS)
days, hours = divmod(days, SECONDS_DAYS)
hours, minutes = divmod(hours, SECONDS_HOURS)
minutes, seconds = divmod(minutes, SECONDS_MINUTES)
out: typing.List[str] = [] out: typing.List[str] = []
if relative.years and minimum_unit >= UNIT_YEAR and len(out) < max_units: if years and len(out) < max_units:
out.append("%dy" % relative.years) out.append("%dy" % years)
if relative.months and minimum_unit >= UNIT_MONTH and len(out) < max_units: if months and len(out) < max_units:
out.append("%dmo" % relative.months) out.append("%dmo" % months)
weeks, days = divmod(relative.days, 7) if weeks and len(out) < max_units:
if weeks and minimum_unit >= UNIT_WEEK and len(out) < max_units:
out.append("%dw" % weeks) out.append("%dw" % weeks)
if days and minimum_unit >= UNIT_DAY and len(out) < max_units: if days and len(out) < max_units:
out.append("%dd" % days) out.append("%dd" % days)
if relative.hours and minimum_unit >= UNIT_HOUR and len(out) < max_units: if hours and len(out) < max_units:
out.append("%dh" % relative.hours) out.append("%dh" % hours)
if relative.minutes and minimum_unit >= UNIT_MINUTE and len(out) < max_units: if minutes and len(out) < max_units:
out.append("%dmi" % relative.minutes) out.append("%dmi" % minutes)
if relative.seconds and minimum_unit >= UNIT_SECOND and len(out) < max_units: if seconds and len(out) < max_units:
out.append("%ds" % relative.seconds) out.append("%ds" % seconds)
return " ".join(out) return " ".join(out)
def to_pretty_since(total_seconds: int, max_units: int=UNIT_MINIMUM
) -> str:
return to_pretty_time(total_seconds, max_units, RelativeDirection.BACKWARD)
def to_pretty_until(total_seconds: int, max_units: int=UNIT_MINIMUM
) -> str:
return to_pretty_time(total_seconds, max_units, RelativeDirection.FORWARD)