Search code examples
pythondatetimeparsingtimezone-offset

Parse Datetime with +0 timezone


I have the following Datetime string: Dec 03 2020 01: +0 which I want to parse into a datetime object.

dtObj = datetime.strptime("Dec 03 2020 01: +0", '%b %d %Y %I: %z')

Checking the Documentation, this should work but I get the following error:

ValueError: time data 'Dec 03 2020 01: +0' does not match format '%b %d %Y %I: %z'

Any ideas what I have overseen?

Thanks in advance


Solution

  • Any ideas what I have overseen?

    strftime.org claims that %z

    UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).

    this mean that it must contain at least 4 digits after + or - (HHMM part, which is compulsory), taking this is account Dec 03 2020 01: +0 is not compliant with used format string, whilst Dec 03 2020 01: +0000 is

    import datetime
    dtObj = datetime.datetime.strptime("Dec 03 2020 01: +0000", '%b %d %Y %I: %z')
    print(dtObj)
    

    gives output

    2020-12-03 01:00:00+00:00