From fa4ebbc0d695e45b8413677be03a58f9da587c30 Mon Sep 17 00:00:00 2001 From: fderuiter <127706008+fderuiter@users.noreply.github.com> Date: Fri, 27 Feb 2026 18:55:54 +0000 Subject: [PATCH 1/3] feat(utils): robust numeric timestamp parsing in parse_datetime test(utils): add tests for numeric timestamp parsing docs(shield): journal numeric timestamp fix Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/imednet/utils/validators.py | 12 ++++-- tests/unit/test_parse_datetime_robustness.py | 39 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_parse_datetime_robustness.py diff --git a/src/imednet/utils/validators.py b/src/imednet/utils/validators.py index 902c0239..5e1ed35a 100644 --- a/src/imednet/utils/validators.py +++ b/src/imednet/utils/validators.py @@ -1,6 +1,6 @@ from __future__ import annotations -from datetime import datetime +from datetime import datetime, timezone from typing import Any, Callable, Dict, List, TypeVar from imednet.utils.dates import parse_iso_datetime # Centralized date parsing @@ -19,17 +19,23 @@ _SENTINEL_DATETIME = datetime(1969, 4, 20, 16, 20) -def parse_datetime(v: str | datetime) -> datetime: - """Parse an ISO datetime string or return a sentinel value. +def parse_datetime(v: str | int | float | datetime) -> datetime: + """Parse an ISO datetime string, numeric timestamp, or return a sentinel value. The SDK historically returns ``datetime(1969, 4, 20, 16, 20)`` when a timestamp field is empty. This helper mirrors that behaviour for backward compatibility. + + Args: + v: Date string, numeric timestamp (seconds since epoch), or datetime object. + Numeric values are assumed to be UTC timestamps. """ if not v: return _SENTINEL_DATETIME if isinstance(v, str): return parse_iso_datetime(v) + if isinstance(v, (int, float)): + return datetime.fromtimestamp(v, tz=timezone.utc) return v diff --git a/tests/unit/test_parse_datetime_robustness.py b/tests/unit/test_parse_datetime_robustness.py new file mode 100644 index 00000000..5853b4b5 --- /dev/null +++ b/tests/unit/test_parse_datetime_robustness.py @@ -0,0 +1,39 @@ + +from datetime import datetime, timezone +import pytest +from imednet.utils.validators import parse_datetime + +def test_parse_datetime_int_timestamp(): + """Test that parse_datetime correctly handles integer timestamps (seconds).""" + ts = 1609459200 # 2021-01-01 00:00:00 UTC + result = parse_datetime(ts) + assert isinstance(result, datetime) + assert result == datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc) + +def test_parse_datetime_float_timestamp(): + """Test that parse_datetime correctly handles float timestamps (seconds).""" + ts = 1609459200.5 # 2021-01-01 00:00:00.5 UTC + result = parse_datetime(ts) + assert isinstance(result, datetime) + assert result == datetime(2021, 1, 1, 0, 0, 0, 500000, tzinfo=timezone.utc) + +def test_parse_datetime_negative_timestamp(): + """Test that parse_datetime correctly handles negative timestamps (historic dates).""" + ts = -2208988800 # 1900-01-01 00:00:00 UTC + result = parse_datetime(ts) + assert isinstance(result, datetime) + # Note: Depending on platform/implementation, negative timestamps might behave differently, + # but Python's fromtimestamp usually handles them on modern systems. + # We'll check it works generally. + assert result.year == 1900 + assert result.month == 1 + assert result.day == 1 + assert result.tzinfo == timezone.utc + +def test_parse_datetime_zero_timestamp(): + """Test that parse_datetime treats 0 (epoch) as empty/sentinel due to legacy falsy check.""" + ts = 0 + result = parse_datetime(ts) + # Current behavior: 0 is falsy -> Sentinel. + # This documents existing behavior rather than enforcing a change. + assert result == datetime(1969, 4, 20, 16, 20) From 6af11cb292ebf71d82110211711cc9d0621b91bf Mon Sep 17 00:00:00 2001 From: fderuiter <127706008+fderuiter@users.noreply.github.com> Date: Fri, 27 Feb 2026 19:01:41 +0000 Subject: [PATCH 2/3] feat(utils): robust numeric timestamp parsing in parse_datetime test(utils): add tests for numeric timestamp parsing docs(shield): journal numeric timestamp fix Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- tests/unit/test_parse_datetime_robustness.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_parse_datetime_robustness.py b/tests/unit/test_parse_datetime_robustness.py index 5853b4b5..508b665a 100644 --- a/tests/unit/test_parse_datetime_robustness.py +++ b/tests/unit/test_parse_datetime_robustness.py @@ -1,8 +1,10 @@ - from datetime import datetime, timezone + import pytest + from imednet.utils.validators import parse_datetime + def test_parse_datetime_int_timestamp(): """Test that parse_datetime correctly handles integer timestamps (seconds).""" ts = 1609459200 # 2021-01-01 00:00:00 UTC @@ -10,6 +12,7 @@ def test_parse_datetime_int_timestamp(): assert isinstance(result, datetime) assert result == datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc) + def test_parse_datetime_float_timestamp(): """Test that parse_datetime correctly handles float timestamps (seconds).""" ts = 1609459200.5 # 2021-01-01 00:00:00.5 UTC @@ -17,6 +20,7 @@ def test_parse_datetime_float_timestamp(): assert isinstance(result, datetime) assert result == datetime(2021, 1, 1, 0, 0, 0, 500000, tzinfo=timezone.utc) + def test_parse_datetime_negative_timestamp(): """Test that parse_datetime correctly handles negative timestamps (historic dates).""" ts = -2208988800 # 1900-01-01 00:00:00 UTC @@ -30,6 +34,7 @@ def test_parse_datetime_negative_timestamp(): assert result.day == 1 assert result.tzinfo == timezone.utc + def test_parse_datetime_zero_timestamp(): """Test that parse_datetime treats 0 (epoch) as empty/sentinel due to legacy falsy check.""" ts = 0 From 358ee058fcf636478fa68130fa62e0b8a30fc39a Mon Sep 17 00:00:00 2001 From: fderuiter <127706008+fderuiter@users.noreply.github.com> Date: Fri, 27 Feb 2026 19:06:07 +0000 Subject: [PATCH 3/3] feat(utils): robust numeric timestamp parsing in parse_datetime test(utils): add tests for numeric timestamp parsing docs(shield): journal numeric timestamp fix Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- tests/unit/test_parse_datetime_robustness.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/test_parse_datetime_robustness.py b/tests/unit/test_parse_datetime_robustness.py index 508b665a..778b4730 100644 --- a/tests/unit/test_parse_datetime_robustness.py +++ b/tests/unit/test_parse_datetime_robustness.py @@ -1,7 +1,5 @@ from datetime import datetime, timezone -import pytest - from imednet.utils.validators import parse_datetime