2024-02-25 00:38:44 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
DATETIME_UNITS = {
|
2024-03-31 22:55:55 +00:00
|
|
|
'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),
|
2024-02-25 00:38:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 16:25:34 +00:00
|
|
|
FORMAT_DATE_HEADER = '%a, %d %b %Y %H:%M:%S GMT'
|
|
|
|
|
2024-05-09 14:58:09 +00:00
|
|
|
FORMAT_DAY_MONTH_YEAR = '%d %B %Y'
|
|
|
|
|
2024-03-31 22:55:55 +00:00
|
|
|
|
2024-02-25 00:38:44 +00:00
|
|
|
def parse_duration(text: str) -> datetime.timedelta:
|
|
|
|
(num, unit) = text.split(' ')
|
|
|
|
num = int(num)
|
|
|
|
unit = DATETIME_UNITS[unit]
|
|
|
|
return unit * num
|
2024-03-03 16:25:34 +00:00
|
|
|
|
2024-05-09 14:58:09 +00:00
|
|
|
def parse_response_datetime(response) -> datetime.datetime:
|
2024-03-03 16:25:34 +00:00
|
|
|
return datetime.datetime.strptime(response.headers['Date'], FORMAT_DATE_HEADER)
|
2024-05-09 14:58:09 +00:00
|
|
|
|
|
|
|
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()
|