From 8a9c4cf59567fd30a3f5d931286bd6c8548a7273 Mon Sep 17 00:00:00 2001 From: anish-work Date: Thu, 12 Dec 2024 22:15:13 +0530 Subject: [PATCH] use timedelta --- daras_ai_v2/utils.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/daras_ai_v2/utils.py b/daras_ai_v2/utils.py index 2ebc8e553..e728b4d76 100644 --- a/daras_ai_v2/utils.py +++ b/daras_ai_v2/utils.py @@ -1,22 +1,22 @@ -from datetime import datetime +from django.utils import timezone +from datetime import timedelta def get_relative_time(timestamp): - now = datetime.now(timestamp.tzinfo) - diff = now - timestamp - + diff = timezone.now() - timestamp seconds = diff.total_seconds() + if seconds < 2: - return "just now" - if seconds < 60: + return "Just now" + if seconds < timedelta(minutes=1).total_seconds(): return f"{int(seconds)}s ago" - elif seconds < 3600: - return f"{int(seconds/60)}m ago" - elif seconds < 86400: - return f"{int(seconds/3600)}h ago" - elif seconds < 2592000: - return f"{int(seconds/86400)}d ago" - elif seconds < 31536000: - return f"{int(seconds/2592000)}mo ago" + elif seconds < timedelta(hours=1).total_seconds(): + return f"{int(seconds/timedelta(minutes=1).total_seconds())}m ago" + elif seconds < timedelta(days=1).total_seconds(): + return f"{int(seconds/timedelta(hours=1).total_seconds())}h ago" + elif seconds < timedelta(days=30).total_seconds(): + return f"{int(seconds/timedelta(days=1).total_seconds())}d ago" + elif seconds < timedelta(days=365).total_seconds(): + return f"{int(seconds/timedelta(days=30).total_seconds())}mo ago" else: - return f"{int(seconds/31536000)}y ago" + return f"{int(seconds/timedelta(days=365).total_seconds())}y ago"