Skip to content

Commit

Permalink
Add solr_timestamp_to_datetime method from ppa
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed May 14, 2020
1 parent 0df48f6 commit 4b5acbd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
CHANGELOG
=========

0.6
0.5.3
---

* Support default option adding fields to solr schema
* Add utility method to convert Solr timestamp to python datetime

0.5.2
-----
Expand Down
12 changes: 12 additions & 0 deletions parasolr/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from datetime import datetime

from parasolr.utils import solr_timestamp_to_datetime


def test_solr_timestamp_to_datetime():
# with microseconds
solr_dt = solr_timestamp_to_datetime('2018-07-02T21:08:46.428Z')
assert solr_dt == datetime(2018, 7, 2, 21, 8, 46)
# without
solr_dt = solr_timestamp_to_datetime('2018-07-02T21:08:46Z')
assert solr_dt == datetime(2018, 7, 2, 21, 8, 46)
12 changes: 12 additions & 0 deletions parasolr/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from datetime import datetime


def solr_timestamp_to_datetime(solr_time):
"""Convert Solr timestamp (isoformat that may or may not include
microseconds) to :class:`datetime.datetime`"""
# Solr stores date in isoformat; convert to datetime object
# - microseconds only included when second is not exact; strip out if
# they are present
if '.' in solr_time:
solr_time = '%sZ' % solr_time.split('.')[0]
return datetime.strptime(solr_time, '%Y-%m-%dT%H:%M:%SZ')

0 comments on commit 4b5acbd

Please sign in to comment.