Skip to content

Commit

Permalink
use datetime.fromisoformat instead of strptime (#384)
Browse files Browse the repository at this point in the history
* use datetime.fromisoformat instead of strptime to improve performance of et2datetime
  • Loading branch information
johan12345 committed Sep 9, 2020
1 parent 9aa3ade commit 2257a5a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13399,6 +13399,14 @@ def datetime2et(dt: Union[Iterable[datetime], datetime]) -> Union[ndarray, float
return et.value


if hasattr(datetime, 'fromisoformat'):
def fromisoformat(s):
return datetime.fromisoformat(s + '+00:00')
else:
def fromisoformat(s):
return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc)


@spice_error_check
def et2datetime(et: Union[Iterable[float], float]) -> Union[ndarray, datetime]:
"""
Expand All @@ -13411,16 +13419,10 @@ def et2datetime(et: Union[Iterable[float], float]) -> Union[ndarray, datetime]:
:return: Output datetime object in UTC
"""
result = et2utc(et, "ISOC", 6)
isoformat = "%Y-%m-%dT%H:%M:%S.%f"
if stypes.is_iterable(result):
return numpy.array(
[
datetime.strptime(s, isoformat).replace(tzinfo=timezone.utc)
for s in result
]
)
return numpy.array([fromisoformat(s) for s in result])
else:
return datetime.strptime(result, isoformat).replace(tzinfo=timezone.utc)
return fromisoformat(result)


@spice_error_check
Expand Down

0 comments on commit 2257a5a

Please sign in to comment.