Python Forum
Why does datetime.strptime() not work in this situation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does datetime.strptime() not work in this situation
#1
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:

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?
Reply
#2
From the docs:
Quote:strptime() only accepts certain values for %Z:
  • any value in time.tzname for your machine’s locale
  • the hard-coded values UTC and GMT
So someone living in Japan may have JST, UTC, and GMT as valid values, but probably not EST. It will raise ValueError for invalid values.

do print(time.tzname) to check valid values in addition to UTC and GMT
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The issue here is with parsing timezone information using strptime().
Python's strptime() has limited support for timezone parsing with %Z.
Can do it like this.
from datetime import datetime
import pytz

TIMEZONE = pytz.timezone('America/Vancouver')
date_str = '24-12-2025'
when = datetime.strptime(date_str, '%d-%m-%Y')
# Localize the datetime to the timezone
when = TIMEZONE.localize(when)

print(f"Date: {when}")
print(f"Timezone: {when.tzinfo}")
Output:
Date: 2025-12-24 00:00:00-08:00 Timezone: America/Vancouver
Or when dealing with Timezones use Pendulum.
Better API and Features and it's a drop-in replacement for Python's standard datetime.
import pendulum

when = pendulum.parse('2025-12-24', tz='America/Vancouver')
print(when)
Output:
2025-12-24 00:00:00-08:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,240 Jan-02-2025, 02:09 AM
Last Post: lyly19
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,100 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  String formatting (strptime) issues Henrio 2 1,838 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  Extracting year from a string using strptime and datetime builtin Drone4four 3 13,146 Aug-11-2020, 12:02 PM
Last Post: ndc85430
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 11,551 Sep-18-2019, 08:32 AM
Last Post: buran
  Need some help with strange situation SpencerH 6 4,310 Oct-16-2018, 09:20 PM
Last Post: SpencerH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020