3

I am trying to read a date string in a particular format.

First let's generate a string of the current time in the required format.

In : format_string = "%Y-%m-%dT%H:%M:%S.%f+%z"
In : now = datetime.now().strftime(format_string)
In : print(now)
Out: '2020-07-19T19:12:09.453475+'

Now, let's try to convert it back to a datetime object.


In : datetime.strptime(now, format_string)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-32937025edd1> in <module>
----> 1 datetime.strptime("2020-07-19T19:12:09.453475+", "%Y-%m-%dT%H:%M:%S.%f+%z")

/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_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,)

/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_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 '2020-07-19T19:12:09.453475+' does not match format '%Y-%m-%dT%H:%M:%S.%f+%z'

The error is about the format, which is exactly the same.

What is going on here?

Hgat
  • 31
  • 3

2 Answers2

1

After you get the datetime object, last %z is not there. Hence, you will have to remove that when converting string back to datetime object

In [29]: format_string = "%Y-%m-%dT%H:%M:%S.%f+%z"

In [30]: now = datetime.now().strftime(format_string)

In [31]: now
Out[31]: '2020-07-20T00:29:48.228980+'

In [32]: format_string = "%Y-%m-%dT%H:%M:%S.%f+"

In [33]: datetime.strptime(now, format_string)
Out[33]: datetime.datetime(2020, 7, 20, 0, 29, 48, 228980)
bigbounty
  • 16,526
  • 5
  • 37
  • 65
1

To dive deeper into bigbounty's answer, Python has some issues with datetime and %z [source]

Python's datetime does not support the military timezone suffixes like 'Z' suffix for UTC. The following simple string replacement does the trick:

Python datetime objects don't have time zone info by default

One option is to remove the %z from your pattern, however you may want to utilize one of the options in the linked answer to include time zone information in order to conform to conform to ISO 8601, e.g. to use arrow:

> str(arrow.utcnow())
'2014-05-17T01:18:47.944126+00:00'
drnugent
  • 1,545
  • 9
  • 22