Files
docupulse/utils/helpers.py
2025-05-29 14:27:15 +02:00

42 lines
1.2 KiB
Python

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'