parse timestamp as str

This commit is contained in:
Simon 2025-12-15 21:16:16 +07:00
parent c12b061346
commit 14c47695d7
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 9 additions and 0 deletions

View File

@ -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)

View File

@ -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"