Python datetime strptime does not match format
This is the code I’m going to run
from datetime import datetime
date="08/30/2017 10:02 pm (PDT)"
dt = datetime.strptime(date, '%m/%d/%Y %I:%M %p (%Z)')
The date is a string with the value 08/30/2017 10:02 PM (PDT).
I look fine, but python gives me this error :
time data '08/30/2017 10:02 PM (PDT)' does not match format '%m/%d/%Y %I:%M %p (%Z)'
The code runs on a remote machine with Python 2.7 installed. However, if I manually type these codes into the local python terminal. It works perfectly.
Is there anything that can be changed?
(I tried to change the date between unicode/str, no difference)
Solution
Check the value of the TZ
environment variable. time.strptime
uses the TZ
variable to disambiguate time zone abbreviations, otherwise they would not be unique. I can reproduce the matching error using TZ=Europe/Berlin
, but it resolves successfully using TZ=America/Tijuana
.
Another source of discrepancies that can occur if another machine has TZ data that uses only numeric time zones, which is a recent change (2017) in some time zones (this also means that the machine on which it works will have an outdated timezone database).