1

I'm having trouble parsing out timezone information from a string that looks like: 8pm PST on sunday So far, using parsedatetime and dateutils allows me to parse the date out but the timezone usually causes issues.

Anyone know of a library that handles this sort of thing? My fallback is to naively parse out the timezones via a regex or a simple "PST" in datestring.

sihrc
  • 2,728
  • 2
  • 22
  • 43

1 Answers1

1

The abbreviations you're using are not unique; you will therefore need to interpret the time zones somehow (e.g. assume United States) and specify what each abbreviation means for your application:

from dateutil import parser

# map time zones to seconds from GMT
zones = {
    'EST': -5 * 3600,
    'PST': -8 * 3600
}

parser.parse('8 PM on Sunday PST', tzinfos=zones)
# datetime.datetime(2016, 4, 24, 20, 0, tzinfo=tzoffset('PST', -28800))

You can install dateutil with pip: pip install python-dateutil.

See this similar question for more information.

Community
  • 1
  • 1
ChrisP
  • 5,812
  • 1
  • 33
  • 36