diff --git a/core/utils.py b/core/utils.py index 5552f2e..5dcde98 100644 --- a/core/utils.py +++ b/core/utils.py @@ -20,6 +20,7 @@ import base64 import binascii +import datetime import json import re import secrets @@ -98,3 +99,37 @@ def validate_discord_token(token: str) -> bool: return False else: return True + + +def natural_time( + td: datetime.timedelta, + /, + *, + source: datetime.datetime | None = None, +) -> str: + now = source or datetime.datetime.now(datetime.UTC) + + then = now - td + future = then > now + + ago = "{delta} from now" if future else "{delta} ago" + + seconds = round(td.total_seconds()) + weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7) + days, seconds = divmod(seconds, 60 * 60 * 24) + hours, seconds = divmod(seconds, 60 * 60) + minutes, seconds = divmod(seconds, 60) + + ret = "" + + if weeks: + ret += f"{weeks} weeks," + if days: + ret += f"{days} days," + if hours: + ret += f"{hours} hours," + if minutes: + ret += f"{minutes} minutes and " + ret += f"{seconds} seconds" + + return ago.format(delta=ret) diff --git a/requirements.txt b/requirements.txt index 0b255d8..e9625dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,7 @@ -git+https://github.com/PythonistaGuild/StarlettePlus.git -uvicorn -asyncpg -asyncpg-stubs -bleach -humanize -python-multipart -pyyaml -aiohttp +starlette_plus @ git+https://github.com/PythonistaGuild/StarlettePlus.git@f21169a +uvicorn==0.29.0 +asyncpg==0.29.0 +asyncpg-stubs==0.29.1 +bleach==6.1.0 +python-multipart==0.0.9 +aiohttp==3.9.5 diff --git a/views/htmx.py b/views/htmx.py index b3a68a0..e752ce9 100644 --- a/views/htmx.py +++ b/views/htmx.py @@ -26,11 +26,10 @@ import asyncpg import bleach -import humanize import starlette_plus from core import CONFIG -from core.utils import validate_paste +from core.utils import natural_time, validate_paste if TYPE_CHECKING: @@ -155,7 +154,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
/{identifier} - Created {humanize.naturaltime(created_delta)}... + Created {natural_time(created_delta)}...
{security_html}