Skip to content

Commit

Permalink
make helper for datetimes more robust #85
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Aug 23, 2019
1 parent 6480c53 commit 8c726d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
19 changes: 14 additions & 5 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,12 @@ def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dic

# -- HELPERS ---------------------------------------------------------------
@classmethod
def hlpr_datetimes(cls, in_date: str) -> datetime:
def hlpr_datetimes(cls, in_date: str, try_again: bool = 1) -> datetime:
"""Helper to handle differnts dates formats.
See: https://github.com/isogeo/isogeo-api-py-minsdk/issues/85
:param dict raw_object: metadata dictionary returned by a request.json()
:param bool try_again: iterations on the method
:returns: a correct datetime object
:rtype: datetime
Expand Down Expand Up @@ -951,11 +952,19 @@ def hlpr_datetimes(cls, in_date: str) -> datetime:
in_date.replace(milliseconds.group(1), milliseconds.group(1)[:6])
)
else:
raise TypeError(
"This format of timestamps is not recognized: {} ({}). Try by yourself!".format(
in_date, len(in_date)
if try_again and len(in_date) >= 10:
logger.warning(
"Format of timestamp not recognized: {} ({})."
" Trying again with only the 10 first characters: {}".format(
in_date, len(in_date), in_date[:10]
)
)
)
return cls.hlpr_datetimes(in_date[:10], try_again=0)
else:
logger.error(
"Format of timestamp not recognized: {} ({}). Formatting failed."
)
out_date = None

return out_date

Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,8 @@ def test_helper_datetimes(self):
spec_date = IsogeoUtils.hlpr_datetimes("2014-10-02T00:00:00")
self.assertIsInstance(spec_date, datetime)
self.assertEqual(spec_date.year, 2014)

# specification published date
unrecognized_date = IsogeoUtils.hlpr_datetimes("2014-10-02T00:00:00+00")
self.assertIsInstance(unrecognized_date, datetime)
self.assertEqual(unrecognized_date.year, 2014)

0 comments on commit 8c726d5

Please sign in to comment.