parse timestamp as str
This commit is contained in:
parent
c12b061346
commit
14c47695d7
|
|
@ -110,6 +110,8 @@ def date_parser(timestamp: int | str | None) -> str | None:
|
|||
|
||||
if isinstance(timestamp, int):
|
||||
date_obj = datetime.fromtimestamp(timestamp, tz=timezone.utc)
|
||||
elif isinstance(timestamp, str) and timestamp.isdigit():
|
||||
date_obj = datetime.fromtimestamp(int(timestamp), tz=timezone.utc)
|
||||
elif isinstance(timestamp, str):
|
||||
date_obj = datetime.strptime(timestamp, "%Y-%m-%d")
|
||||
date_obj = date_obj.replace(tzinfo=timezone.utc)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,13 @@ def test_date_parser_with_int():
|
|||
assert date_parser(timestamp) == expected_date
|
||||
|
||||
|
||||
def test_date_parser_with_digit():
|
||||
"""unix timestamp"""
|
||||
timestamp = "1621539600"
|
||||
expected_date = "2021-05-20T19:40:00+00:00"
|
||||
assert date_parser(timestamp) == expected_date
|
||||
|
||||
|
||||
def test_date_parser_with_str():
|
||||
"""iso timestamp"""
|
||||
date_str = "2021-05-21"
|
||||
|
|
|
|||
Loading…
Reference in New Issue