fix: detect negative UTC offsets (-HH:MM) in ISO timestamp parsing#1046
Open
voidborne-d wants to merge 1 commit intosansan0:masterfrom
Open
fix: detect negative UTC offsets (-HH:MM) in ISO timestamp parsing#1046voidborne-d wants to merge 1 commit intosansan0:masterfrom
voidborne-d wants to merge 1 commit intosansan0:masterfrom
Conversation
## 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Three functions in
trendradar/utils/time.py—format_iso_time_friendly,is_within_days, andcalculate_days_old— detected timezone-aware timestamps using:This correctly handles UTC (
+00:00), positive offsets (+08:00,+05:30) andZ-suffix timestamps, but silently misses RFC 3339 negative UTC offsets (-05:00EST,-08:00PST,-03:30Newfoundland, etc.).When a negative-offset timestamp like
2025-01-01T00:00:00-05:00is encountered, the check evaluates toFalseand the code falls through to the barefromisoformat()path, which strips timezone info and callspytz.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 filteredcalculate_days_old: day-count calculation silently wrongFix
Extract
_has_tz_info(iso_time)helper that correctly checks forZ,+HH:MM, and-HH:MM: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 informat_iso_time_friendly,is_within_days,calculate_days_oldtests/__init__.py: new file (test package)tests/test_time_utils.py: 41 new tests covering all three functions and the regressionTests
41 tests, all passing:
TestHasTzInfoTestFormatIsoTimeFriendlyTestIsWithinDaysTestCalculateDaysOld