Skip to content

fix: detect negative UTC offsets (-HH:MM) in ISO timestamp parsing#1046

Open
voidborne-d wants to merge 1 commit intosansan0:masterfrom
voidborne-d:fix/negative-utc-offset-not-detected-in-iso-parser
Open

fix: detect negative UTC offsets (-HH:MM) in ISO timestamp parsing#1046
voidborne-d wants to merge 1 commit intosansan0:masterfrom
voidborne-d:fix/negative-utc-offset-not-detected-in-iso-parser

Conversation

@voidborne-d
Copy link
Copy Markdown

Bug

Three functions in trendradar/utils/time.pyformat_iso_time_friendly, is_within_days, and calculate_days_old — detected timezone-aware timestamps using:

if "+" in iso_time or iso_time.endswith("Z"):

This correctly handles UTC (+00:00), positive offsets (+08:00, +05:30) and Z-suffix timestamps, but silently misses RFC 3339 negative UTC offsets (-05:00 EST, -08:00 PST, -03:30 Newfoundland, etc.).

When a negative-offset timestamp like 2025-01-01T00:00:00-05:00 is encountered, the check evaluates to False and the code falls through to the bare fromisoformat() path, which strips timezone info and calls pytz.UTC.localize() — reinterpreting the local time as UTC.

Impact:

  • format_iso_time_friendly: displayed times up to 12 hours wrong for RSS articles from US/European sources (Hacker News, Reddit, NYT, BBC…)
  • is_within_days: freshness filtering incorrect — articles from western-timezone sources could be wrongly kept or wrongly filtered
  • calculate_days_old: day-count calculation silently wrong

Fix

Extract _has_tz_info(iso_time) helper that correctly checks for Z, +HH:MM, and -HH:MM:

def _has_tz_info(iso_time: str) -> bool:
    if iso_time.endswith("Z"):
        return True
    if len(iso_time) >= 6:
        suffix = iso_time[-6:]
        if suffix[0] in ("+", "-") and suffix[3] == ":":
            return True
    return False

Replace the old "+ in" check with _has_tz_info() in all three functions.

Changes

  • trendradar/utils/time.py: add _has_tz_info() helper; apply it in format_iso_time_friendly, is_within_days, calculate_days_old
  • tests/__init__.py: new file (test package)
  • tests/test_time_utils.py: 41 new tests covering all three functions and the regression

Tests

41 tests, all passing:

Class Count Key tests
TestHasTzInfo 13 parametrized detection, regression test
TestFormatIsoTimeFriendly 11 EST/PST/IST/Newfoundland conversions, cross-TZ agreement
TestIsWithinDays 12 freshness filter with negative offsets, boundary values
TestCalculateDaysOld 5 negative offset regression, cross-TZ agreement

## Bug

Three functions in trendradar/utils/time.py — format_iso_time_friendly,
is_within_days, and calculate_days_old — detected timezone-aware timestamps
using the check:

    if "+" in iso_time or iso_time.endswith("Z"):

This correctly handles UTC (+00:00), positive offsets (+08:00 CST, +05:30 IST)
and Z-suffix timestamps, but silently misses RFC 3339 negative UTC offsets
(-05:00 EST, -08:00 PST, -03:30 Newfoundland, etc.).

When a negative-offset timestamp such as '2025-01-01T00:00:00-05:00' is
encountered, the check evaluates to False and the code falls through to the
bare fromisoformat path, which strips the timezone info and then calls
pytz.UTC.localize() — reinterpreting the local time as UTC.

Impact:
- format_iso_time_friendly: displayed times up to 12 hours wrong for
  RSS articles from US/European sources (Hacker News, Reddit, NYT, BBC...)
- is_within_days: freshness filtering incorrect — articles could be
  wrongly kept (time treated as newer than real) or wrongly filtered
  (time treated as older)
- calculate_days_old: day-count calculation silently wrong

## Fix

Extract _has_tz_info(iso_time) helper that checks for Z, +HH:MM, and -HH:MM:

    def _has_tz_info(iso_time: str) -> bool:
        if iso_time.endswith("Z"):
            return True
        if len(iso_time) >= 6:
            suffix = iso_time[-6:]
            if suffix[0] in ("+", "-") and suffix[3] == ":": return True
        return False

Replace the old "+ in" check with _has_tz_info() in all three functions.
Also consolidate the no-tz branch to always use .replace('T',' ').split('.')[0]
so both T-separator and space-separator formats are handled uniformly.

## Tests

Add tests/test_time_utils.py with 41 tests (all pass):
- TestHasTzInfo: 13 tests covering Z, +offset, -offset, no-tz, edge cases
- TestFormatIsoTimeFriendly: 11 tests including cross-TZ agreement
- TestIsWithinDays: 12 tests including boundary and regression cases
- TestCalculateDaysOld: 5 tests including negative offset regression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant