Skip to content

Commit eb9d1d3

Browse files
committed
fix(update): use timezone-aware datetimes to fix offset comparison error
1 parent 7647b68 commit eb9d1d3

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

packages/deepctl-cmd-update/src/deepctl_cmd_update/version_check.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Version checking functionality for deepctl."""
22

3-
from datetime import datetime, timedelta
3+
from datetime import datetime, timedelta, timezone
44

55
import httpx
66
from deepctl_core import Config
@@ -16,7 +16,9 @@ class VersionInfo(BaseModel):
1616
update_available: bool
1717
release_date: datetime | None = None
1818
release_notes_url: str | None = None
19-
check_timestamp: datetime = Field(default_factory=datetime.now)
19+
check_timestamp: datetime = Field(
20+
default_factory=lambda: datetime.now(timezone.utc)
21+
)
2022

2123

2224
# Mapping from config check_frequency values to timedelta
@@ -206,7 +208,10 @@ def should_check(self) -> bool:
206208
if last_check:
207209
try:
208210
last_check_time = datetime.fromisoformat(last_check)
209-
if datetime.now() - last_check_time < cache_duration:
211+
# Ensure aware for comparison — older stored values may be naive
212+
if last_check_time.tzinfo is None:
213+
last_check_time = last_check_time.replace(tzinfo=timezone.utc)
214+
if datetime.now(timezone.utc) - last_check_time < cache_duration:
210215
return False
211216
except ValueError:
212217
# Invalid timestamp, proceed with check
@@ -234,7 +239,9 @@ def _cache_info(self, info: VersionInfo) -> None:
234239
Args:
235240
info: Version info to cache
236241
"""
237-
self.config._set_config_value("update.last_check", datetime.now().isoformat())
242+
self.config._set_config_value(
243+
"update.last_check", datetime.now(timezone.utc).isoformat()
244+
)
238245
self.config._set_config_value(
239246
"update.cached_version_info", info.model_dump(mode="json")
240247
)
@@ -255,7 +262,7 @@ def format_version_message(info: VersionInfo) -> str:
255262

256263
message = f"Update available: {info.current_version}{info.latest_version}"
257264
if info.release_date:
258-
days_old = (datetime.now() - info.release_date).days
265+
days_old = (datetime.now(timezone.utc) - info.release_date).days
259266
if days_old == 0:
260267
message += " (released today)"
261268
elif days_old == 1:

0 commit comments

Comments
 (0)