the following code works in my local environment macbook:
d_time = datetime.strptime(start_time.strip(), "%b %d %Y %I %M %p %Z")
but when i try to run it on an ec2 instance with ubuntu it returns:
File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data 'Feb 12 2021 02 00 AM PST' does not match format '%b %d %Y %I %M %p %Z'
Both env are using python 3.8.5
Your problem is that %Z
won't parse a time zone abbreviation like PST
(it works just for GMT and UTC afaik). Have a look at dateutil
's parser - here, you can define specific mappings for abbreviations. Ex:
import dateutil
s = 'Feb 12 2021 02 00 AM PST'
tzmap = {'PST': dateutil.tz.gettz('US/Pacific')}
print(dateutil.parser.parse(s, tzinfos=tzmap))
# 2021-02-12 00:00:00-08:00