-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f" | ||
_MAXLEN = len("2020-10-09T12:28:14.7710") | ||
|
||
|
||
def parse_as_datetime(timestr: str, *, default: Optional[datetime] = None) -> datetime: | ||
""" | ||
default: if parsing is not possible, it returs default | ||
""" | ||
# datetime_str is typically '2020-10-09T12:28:14.771034099Z' | ||
# - The T separates the date portion from the time-of-day portion | ||
# - The Z on the end means UTC, that is, an offset-from-UTC | ||
# The 099 before the Z is not clear, therefore we will truncate the last part | ||
|
||
try: | ||
timestr = timestr.strip("Z ")[:_MAXLEN] | ||
dt = datetime.strptime(timestr, DATETIME_FORMAT) | ||
return dt | ||
except ValueError as err: | ||
log.debug("Failed to parse %s: %s", timestr, err) | ||
if default is not None: | ||
return default | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
from simcore_service_director.utils import parse_as_datetime | ||
|
||
|
||
# Samples taken from https://docs.docker.com/engine/reference/commandline/service_inspect/ | ||
@pytest.mark.parametrize( | ||
"timestr", | ||
( | ||
"2020-10-09T18:44:02.558012087Z", | ||
"2020-10-09T12:28:14.771034099Z", | ||
"2020-10-09T12:28:14.7710", | ||
"2020-10-09T12:28:14.77 Z", | ||
"2020-10-09T12:28", | ||
), | ||
) | ||
def test_parse_valid_time_strings(timestr): | ||
|
||
dt = parse_as_datetime(timestr) | ||
assert isinstance(dt, datetime) | ||
assert dt.year == 2020 | ||
assert dt.month == 10 | ||
assert dt.day == 9 | ||
|
||
|
||
def test_parse_invalid_timestr(): | ||
now = datetime.utcnow() | ||
invalid_timestr = "asdfasdf" | ||
|
||
# w/ default, it should NOT raise | ||
dt = parse_as_datetime(invalid_timestr, default=now) | ||
assert dt == now | ||
|
||
# w/o default | ||
with pytest.raises(ValueError): | ||
parse_as_datetime(invalid_timestr) |