-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new util to Tgram.utils, readable_time
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import datetime | ||
|
||
def readable_time(timestamp): | ||
""" | ||
Converts a timestamp into a human-readable time-ago format. | ||
Parameters: | ||
timestamp (int): Unix timestamp or datetime object. | ||
Returns: | ||
str: Time ago in words (e.g., '5 minutes ago'). | ||
""" | ||
if isinstance(timestamp, int): | ||
timestamp = datetime.datetime.fromtimestamp(timestamp) | ||
delta = datetime.datetime.now() - timestamp | ||
|
||
if delta.days > 365: | ||
years = delta.days // 365 | ||
return f"{years} year(s) ago" | ||
elif delta.days > 30: | ||
months = delta.days // 30 | ||
return f"{months} month(s) ago" | ||
elif delta.days > 0: | ||
return f"{delta.days} day(s) ago" | ||
elif delta.seconds > 3600: | ||
hours = delta.seconds // 3600 | ||
return f"{hours} hour(s) ago" | ||
elif delta.seconds > 60: | ||
minutes = delta.seconds // 60 | ||
return f"{minutes} minute(s) ago" | ||
else: | ||
return "Just now" |