Search code examples
pythondatetimesolrpysolr

How to transform a Solr date back to a python-readable date e.g. 'datetime' and vice versa?


Is there an easy/efficient way of converting the 'Current Solr date' to the 'Desired output' shown below ? I thought of using regex or string methods to clean up the Solr dates but if there is a method to use in Python that converts these dates from Solr it would be great.

Current Solr date:

'2020-01-21T12:23:54.625Z'

Desired output (in datetime module format):


'2020-01-21 12:23:54' 

Solution

  • Here's a quick round-trip from string to datetime object to string again, including a couple of options. Hope this gets you going.

    string → datetime (microseconds kept)

    from datetime import datetime
    
    s = '2020-01-21T12:23:54.625Z'
    
    # to datetime object, including the Z (UTC):
    dt_aware = datetime.fromisoformat(s.replace('Z', '+00:00'))
    print(repr(dt_aware))
    # datetime.datetime(2020, 1, 21, 12, 23, 54, 625000, tzinfo=datetime.timezone.utc)
    
    # to datetime object, ignoring Z:
    dt_naive = datetime.fromisoformat(s.strip('Z'))
    print(repr(dt_naive))
    # datetime.datetime(2020, 1, 21, 12, 23, 54, 625000)
    

    datetime → string (microseconds stripped)

    # to isoformat string, space sep, no UTC offset, no microseconds
    print(dt_aware.replace(microsecond=0, tzinfo=None).isoformat(' '))
    # 2020-01-21 12:23:54
    print(dt_naive.replace(microsecond=0).isoformat(' '))
    # 2020-01-21 12:23:54
    
    # ...or with a Z to specify UTC and a T as date/time separator
    print(dt_aware.replace(microsecond=0).isoformat().replace('+00:00', 'Z'))
    # 2020-01-21T12:23:54Z
    
    # to isoformat string, with Z for UTC, naive / no tzinfo:
    print(dt_naive.replace(microsecond=0).isoformat() + 'Z') # just add Z as a suffix
    # 2020-01-21T12:23:54Z
    

    You might also want to have a look at the timespec kwarg of datetime.isoformat if you need a specific precision, e.g. milliseconds.