From 14c47695d73edccf8702985765cece2d1f23c10c Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 15 Dec 2025 21:16:16 +0700 Subject: [PATCH] parse timestamp as str --- backend/common/src/helper.py | 2 ++ backend/common/tests/test_src/test_helper.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/backend/common/src/helper.py b/backend/common/src/helper.py index dbf238ce..a29da9a2 100644 --- a/backend/common/src/helper.py +++ b/backend/common/src/helper.py @@ -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) diff --git a/backend/common/tests/test_src/test_helper.py b/backend/common/tests/test_src/test_helper.py index 013e774b..73578a76 100644 --- a/backend/common/tests/test_src/test_helper.py +++ b/backend/common/tests/test_src/test_helper.py @@ -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"