Added events system
This commit is contained in:
42
utils/helpers.py
Normal file
42
utils/helpers.py
Normal file
@@ -0,0 +1,42 @@
|
||||
def clean_path(path):
|
||||
"""
|
||||
Clean a file path by removing leading/trailing slashes and normalizing separators.
|
||||
|
||||
Args:
|
||||
path: Path string to clean
|
||||
|
||||
Returns:
|
||||
str: Cleaned path
|
||||
"""
|
||||
if not path:
|
||||
return ''
|
||||
return path.strip('/\\')
|
||||
|
||||
def timeago(date):
|
||||
"""Convert a datetime to a human-readable time ago string."""
|
||||
if not date:
|
||||
return ''
|
||||
|
||||
# Convert naive datetime to UTC if it's naive
|
||||
if date.tzinfo is None:
|
||||
date = date.replace(tzinfo=timezone.utc)
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
diff = now - date
|
||||
|
||||
if diff.days > 365:
|
||||
years = diff.days // 365
|
||||
return f'{years} year{"s" if years != 1 else ""} ago'
|
||||
elif diff.days > 30:
|
||||
months = diff.days // 30
|
||||
return f'{months} month{"s" if months != 1 else ""} ago'
|
||||
elif diff.days > 0:
|
||||
return f'{diff.days} day{"s" if diff.days != 1 else ""} ago'
|
||||
elif diff.seconds > 3600:
|
||||
hours = diff.seconds // 3600
|
||||
return f'{hours} hour{"s" if hours != 1 else ""} ago'
|
||||
elif diff.seconds > 60:
|
||||
minutes = diff.seconds // 60
|
||||
return f'{minutes} minute{"s" if minutes != 1 else ""} ago'
|
||||
else:
|
||||
return 'just now'
|
||||
Reference in New Issue
Block a user