I am relatively new to Python, and I am encountering a very strange issue. This is on python3.8. This really seems like a bug in python to me, but I am not skilled enough to make that determination for myself. The code as follows demonstrates the error:
Unfortunately, I need this datetime to be established with a timezone. If I don't I get errors later on in my code complaining "TypeError: can't compare offset-naive and offset-aware datetimes"
Is this a bug, or am I doing something wrong? Is there another way to set the timezone that I should be using?
from datetime import datetime as datetime import pytz TIMEZONE = pytz.timezone('America/Vancouver') TZ=datetime.now(TIMEZONE).strftime("%Z") combined_date = '24-12-2025 '+TZ print(f"try = {combined_date}") when = datetime.strptime(combined_date,'%d-%m-%Y %Z')What happens is as follows:
Error:try = 24-12-2025 PDT
Traceback (most recent call last):
File "a.py", line 10, in <module>
when = datetime.strptime(combined_date,'%d-%m-%Y %Z')
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 '24-12-2025 PDT' does not match format '%d-%m-%Y %Z'
What is happening here? I never specified %r. Why is it complaining?Unfortunately, I need this datetime to be established with a timezone. If I don't I get errors later on in my code complaining "TypeError: can't compare offset-naive and offset-aware datetimes"
Is this a bug, or am I doing something wrong? Is there another way to set the timezone that I should be using?