Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure newly created pastes are displayed as "now". #48

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading