Skip to content

Commit

Permalink
Make sure newly created pastes are displayed as "now". (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy authored May 23, 2024
1 parent 9a02d2b commit d1f2a2a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ def validate_discord_token(token: str) -> bool:
else:
return True


def pluralize(count: int, singular: str) -> str:
return singular if count == 1 else singular + "s"


def natural_time(
td: datetime.timedelta,
/,
Expand All @@ -114,8 +116,13 @@ def natural_time(
then = now - td
future = then > now

ago = "{delta} from now" if future else "{delta} ago"
seconds = round(td.total_seconds())

if seconds < 60 and not future:
return "now"

ago = "{delta} from now" if future else "{delta} ago"

years, seconds = divmod(seconds, 60 * 60 * 24 * 365)
months, seconds = divmod(seconds, 60 * 60 * 24 * 30)
weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7)
Expand All @@ -142,14 +149,7 @@ def natural_time(
if ret:
ret += ", "
ret += f"{hours} {pluralize(hours, 'hour')}"
if (
minutes
and not years
and not months
and not weeks
and not days
and not hours
):
if minutes and not years and not months and not weeks and not days and not hours:
if ret:
ret += ", "
ret += f"{minutes} {pluralize(minutes, 'minute')}"
Expand Down

0 comments on commit d1f2a2a

Please sign in to comment.