I am unable to obtain an instance of a date with the GMT+2 format, that is, from the text string 2022-10-01 00:00:00 GMT+2
I want to obtain a datetime
.
My code is the following:
time_gmt: str = '2022-10-01 00:00:00 GMT+2'
mi_fecha = datetime.datetime.strptime(time_gmt, '%Y-%m-%d %H:%M:%S %Z')
The error I get with this format is: ValueError: unconverted data remains: +2
, because it doesn't know what to do with that "+2" of the GMT. I've looked at https://docs.python.org/3/library/datetime.html for the format to see if it's wrong, but I'm not sure if something else needs to be added to it, or treated in another way.
In the documentation it indicates the following fragment:
In strftime(),
%Z
is replaced by an empty string if tzname() returnsNone
; otherwise%Z
is replaced by the returned value, which must be a string.strptime() only accepts certain values for
%Z
:
- any value in
time.tzname
for your machine's locale- the hard-coded values
UTC
andGMT
So someone living in Japan may have
JST
,UTC
, andGMT
as valid values, but probably notEST
. It will raiseValueError
for invalid values.
From the error that appears, I understand that GMT+2
it is not a valid value, but then, what would be a valid value?
My only goal is from a string similar to this to 2022-10-01 00:00:00 GMT+2
get an object datetime
. If anyone knows of a way to do this, help would be greatly appreciated.
All the best
PS: thanks in advance