Skip to content

Commit

Permalink
use timedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Dec 12, 2024
1 parent 4532367 commit 8a9c4cf
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions daras_ai_v2/utils.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 8a9c4cf

Please sign in to comment.