1
0
personal-data/personal_data/parse_util.py
Jon Michael Aanes 7eb4b0bb09
All checks were successful
Build container / Package-Python (push) Successful in 25s
Build container / Package-Container (push) Successful in 1m20s
Ruff
2024-05-09 16:59:56 +02:00

47 lines
1.3 KiB
Python

import datetime
DATETIME_UNITS = {
'second': datetime.timedelta(seconds=1),
'seconds': datetime.timedelta(seconds=1),
'minute': datetime.timedelta(minutes=1),
'minutes': datetime.timedelta(minutes=1),
'hour': datetime.timedelta(hours=1),
'hours': datetime.timedelta(hours=1),
'day': datetime.timedelta(days=1),
'days': datetime.timedelta(days=1),
'week': datetime.timedelta(days=7),
'weeks': datetime.timedelta(days=7),
'month': datetime.timedelta(days=30),
'months': datetime.timedelta(days=30),
'year': datetime.timedelta(days=365),
'years': datetime.timedelta(days=365),
}
FORMAT_DATE_HEADER = '%a, %d %b %Y %H:%M:%S GMT'
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
def parse_duration(text: str) -> datetime.timedelta:
(num, unit) = text.split(' ')
num = int(num)
unit = DATETIME_UNITS[unit]
return unit * num
def parse_response_datetime(response) -> datetime.datetime:
return datetime.datetime.strptime(response.headers['Date'], FORMAT_DATE_HEADER)
def parse_time(text: str) -> datetime.datetime:
text = text.replace('\n', ' ')
text = text.strip()
return datetime.datetime.strptime(text, '%d %b %Y %I:%M:%S %p')
def parse_date(text: str) -> datetime.date:
return datetime.datetime.strptime(
text.strip(),
FORMAT_DAY_MONTH_YEAR,
).date()