The dates returned by imaplib are in the following format:
dates = [
'Mon, 27 May 2019 13:13:02 -0300 (ART)',
'Tue, 28 May 2019 00:28:31 +0800 (CST)',
'Mon, 27 May 2019 18:32:13 +0200',
'Mon, 27 May 2019 18:43:13 +0200',
'Mon, 27 May 2019 19:00:11 +0200',
'27 May 2019 18:54:58 +0100',
'27 May 2019 18:56:02 +0100',
'Mon, 03 Jun 2019 10:19:56 GMT',
'4 Jun 2019 07:46:30 +0100',
'Mon, 03 Jun 2019 18:48:01 +0200',
'5 Jun 2019 10:39:19 +0100'
]
How can I convert these into say, BST datetimes?
Here's what I've tried so far:
def date_parse(date):
try:
return datetime.strptime(date, '%a, %d %b %Y %H:%M:%S %z')
except ValueError:
try:
return datetime.strptime(date[:-6], '%a, %d %b %Y %H:%M:%S %z')
except ValueError:
try:
return datetime.strptime(date[:-6], '%d %b %Y %H:%M:%S')
except ValueError:
return datetime.strptime(date[:-4], '%a, %d %b %Y %H:%M:%S')
for date in dates:
print(date)
parsed_date = date_parse(date)
print(parsed_date, type(parsed_date))
print('')
However I get dates repeated followed by an Traceback (most recent call last):
error.
What is the best way to clean these dates?
Is there a imaplib
/email
function that allows us to return clean dates automatically?