76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
from datetime import datetime
|
|
import calendar
|
|
|
|
|
|
def time_ago(time: datetime, short=False, truncate=False):
|
|
seconds = int((datetime.utcnow() - time).total_seconds())
|
|
|
|
return seconds_to_string(seconds, short=short, truncate=truncate)
|
|
|
|
|
|
def seconds_to_string(seconds: int, short=False, truncate=False):
|
|
seconds = int(seconds)
|
|
increments = [('year', 'yr', 31557600),
|
|
('week', 'wk', 604800),
|
|
('day', 'day', 86400),
|
|
('hour', 'hr', 3600),
|
|
('minute', 'min', 60),
|
|
('second', 'sec', 1)]
|
|
|
|
if seconds == 0:
|
|
if short:
|
|
return f"0 {increments[-1][1]}s"
|
|
else:
|
|
return f"0 {increments[-1][0]}s"
|
|
|
|
time_string = ""
|
|
|
|
remainder = seconds
|
|
for st, sst, s in increments:
|
|
if remainder == 0:
|
|
break
|
|
if short:
|
|
st = sst
|
|
if remainder < s or remainder == 0:
|
|
continue
|
|
else:
|
|
exact, remainder = divmod(remainder, s)
|
|
if exact > 1:
|
|
time_string += f"{exact} {st}s, "
|
|
else:
|
|
time_string += f"{exact} {st}, "
|
|
if truncate:
|
|
break
|
|
return time_string.strip().strip(',')[::-1].replace(' ,', ' dna ', 1)[::-1]
|
|
|
|
|
|
def subtract_months(p_date: datetime, offset):
|
|
years, months = divmod(offset, 12)
|
|
new_date = p_date.replace(year=p_date.year - years)
|
|
offset -= years * 12
|
|
|
|
if new_date.month <= offset:
|
|
offset -= new_date.month
|
|
return new_date.replace(month=12 - offset, year=new_date.year - 1)
|
|
else:
|
|
return new_date.replace(month=new_date.month - months)
|
|
|
|
|
|
def last_day_previous_months(months_ago: int):
|
|
dates = []
|
|
current_date = datetime.utcnow().date()
|
|
current_year = current_date.year
|
|
current_month = current_date.month
|
|
dates.append(current_date)
|
|
for month in range(months_ago - 1):
|
|
if current_month == 1:
|
|
current_year -= 1
|
|
current_month = 12
|
|
else:
|
|
current_month -= 1
|
|
last_day = calendar.monthrange(current_year, current_month)[1]
|
|
current_date = current_date.replace(year=current_year, month=current_month, day=last_day)
|
|
dates.append(current_date)
|
|
|
|
return dates[::-1]
|