I'm trying to format a datetime object in Python using either the strftime method or an f-string. I would like to include the time zone offset from UTC with a colon between the hour and minute.
According to the documentation, a format code, %:z, may be available to do exactly what I want. The documentation does warn, however, that this may not be available on all platforms.
I am running Python 3.10 on Windows, and it doesn't seem to work for me. I guess I'm wondering if I just have the syntax mixed up or if indeed this format code isn't available to me. Anyone else have experience with this?
The following statement raises a ValueError: Invalid format string:
print(f"{datetime.now().astimezone():%Y-%m-%d %H:%M:%S%:z}")
Using the %z format code instead of the %:z code does work, however, giving me something close to what I want, namely 2024-10-09 13:17:21-0700 at the time I ran it:
print(f"{datetime.now().astimezone():%Y-%m-%d %H:%M:%S%z}")
strftimeis that it relies too much on the operating system instead of implementing it itself.