Handling timezone could be a bit tricky. One option could be to make a map of the interested timezone from https://www.timeanddate.com/time/zones/ and then use dateutils.parser to parse the date.
Something like :
Using UTC Offset:
from dateutil import parser
# Creating a sample map for the timezone abbreviation and the offset
timezone_info = {
"A": 1 * 3600,
"AT": -4 * 3600,
"AWDT": 9 * 3600,
"AWST": 8 * 3600,
"AZOST": 0 * 3600,
"AZOT": -1 * 3600,
"AZST": 5 * 3600,
"AZT": 4 * 3600,
"AoE": -12 * 3600,
"B": 2 * 3600,
"BNT": 8 * 3600,
"BST": 6 * 3600,
"C": 3 * 3600,
"CAST": 8 * 3600,
"CET": 1 * 3600,
"W": -10 * 3600,
"WEST": 1 * 3600,
"WET": 0 * 3600,
"WST": 14 * 3600,
"Z": 0 * 3600,
}
#Date String
date = "Thu Jul 15 12:57:35 AWST 2021"
# parse the timezone info from the date string
tz = date.split(" ")[-2] # Assuming the date format is"%a %b %d %H:%M:%S %Z %Y"
parser.parse(date, tzinfos={tz : timezone_info.get(tz)})
Output:
datetime.datetime(2021, 7, 15, 12, 57, 35, tzinfo=tzoffset('AWST', 28800))
Using IANA Time Zone Names :
(As suggested by @MrFuppes in the comments)
from dateutil import parser
# Creating a sample map for the timezone abbreviation and the offset
timezone_info = {
"AWST": 'Australia/Perth',
"BNT": 'Asia/Brunei',
"CAST": 'Antarctica/Casey',
"CET": 'Europe/Paris'
}
#Date String
date = "Thu Jul 15 12:57:35 AWST 2021"
# parse the timezone info from the date string
tz = date.split(" ")[-2] # Assuming the date format is"%a %b %d %H:%M:%S %Z %Y"
parser.parse(date, tzinfos={tz : timezone_info.get(tz)})
Output:
datetime.datetime(2021, 7, 15, 12, 57, 35, tzinfo=tzstr('Australia/Perth'))