Python Forum
Datetime format issue with z
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Datetime format issue with z
#1
I'm trying to parse a string as a datetime object and running into issues parsing the timezone offset part.

This is my code:

from datetime import datetime
upd_time_str = 'as on : 07 April 2020, 09:00 GMT+5:30'
upd_time_str = upd_time_str[upd_time_str.find(':')+2:]
print(upd_time_str)
upd_time = datetime.strptime(upd_time_str,'%d %B %Y, %H:%M GMT%z')
print(upd_time)
And here's the output:

07 April 2020, 09:00 GMT+5:30
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 
      3 upd_time_str = upd_time_str[upd_time_str.find(':')+2:]
      4 print(upd_time_str)
----> 5 upd_time = datetime.strptime(upd_time_str,'%d %B %Y, %H:%M GMT%z')
      6 print(upd_time)

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    575     """Return a class cls instance based on the input string and the
    576     format string."""
--> 577     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    578     tzname, gmtoff = tt[-2:]
    579     args = tt[:6] + (fraction,)

~\AppData\Local\Programs\Python\Python37\lib\_strptime.py in _strptime(data_string, format)
    357     if not found:
    358         raise ValueError("time data %r does not match format %r" %
--> 359                          (data_string, format))
    360     if len(data_string) != found.end():
    361         raise ValueError("unconverted data remains: %s" %

ValueError: time data '07 April 2020, 09:00 GMT+5:30' does not match format '%d %B %Y, %H:%M GMT%z'
EDIT: Interestingly, if I remove the part 'GMT+5:30' (and also remove the relevant format specifier), the code works perfectly fine, so it's the offset portion that seems to be messing things up. FYI I'm on Python 3.7.6 so the ':' in the offset should be parsed properly.
Reply
#2
Perhaps this will help: https://pymotw.com/3/datetime/
Reply
#3
Thanks, I had a look at that post. The issue I am facing is that %z doesn't seem to be recognised. The syntax of the command is ok since removing the "GMT+..." part allows the code to run

Got it! Python is expecting the timezone offset in HH:MM. The string has it as h:MM so the missing 0 padding is throwing it off. Added the 0 and problem solved
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,677 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  How to get datetime from numeric format field klllmmm 3 2,009 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,242 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Issue with 'if' and datetime blakefindlay 6 2,834 Feb-08-2021, 02:52 PM
Last Post: deanhystad
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,225 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,630 Sep-18-2019, 08:32 AM
Last Post: buran
  Date format issue the_dude 11 7,551 Jun-07-2019, 06:27 AM
Last Post: snippsat
  Change format of datetime Jonas85 6 3,555 Feb-05-2019, 03:47 PM
Last Post: Jonas85

Forum Jump:

User Panel Messages

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