Keannylen
Keannylen

Reputation: 483

How to convert string including unrecognized timezone to datetime?

I need to parse a string into python datetime, the string received in this format - Thu Jul 15 12:57:35 AWST 2021

I use below code to parse it

datetime.datetime.strptime("Thu Jul 15 12:57:35 AWST 2021", "%a %b %d %H:%M:%S %Z %Y")

It triggers error - "ValueError: time data 'Thu Jul 15 12:57:35 AWST 2021' does not match format '%a %b %d %H:%M:%S %Z %Y'"

The problem is timezone AWST is not recognized by using %Z, according to doc

%Z
In strftime(), %Z is replaced by an empty string if tzname() returns None; otherwise %Z is replaced by the returned value, which must be a string.

strptime() only accepts certain values for %Z:

any value in time.tzname for your machine’s locale

the hard-coded values UTC and GMT

How can I parse this string properly? BTW, I am using python 3.8.2.

Thanks in advance!

Upvotes: 3

Views: 537

Answers (1)

heretolearn
heretolearn

Reputation: 6555

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'))

Upvotes: 3

Related Questions