Skip to content

Commit

Permalink
Use timezone aware objects for RepositoryData.last_fetched (#3570)
Browse files Browse the repository at this point in the history
* Use timezone aware objects for RepositoryData.last_fetched

* Set non UTC system timezone in tests

---------

Co-authored-by: Joakim Sørensen <[email protected]>
  • Loading branch information
emontnemery and ludeeus authored Apr 11, 2024
1 parent 90f1d8a commit 40e3127
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ jobs:
scripts/install/core
scripts/install/frontend
- name: ⏲️ Set time zone
uses: szenius/[email protected]
with:
timezoneLinux: 'Asia/Singapore'

- name: 🏃 Run tests
env:
PYTEST: true
Expand Down Expand Up @@ -69,6 +74,11 @@ jobs:
scripts/install/core_dev
scripts/install/frontend
- name: ⏲️ Set time zone
uses: szenius/[email protected]
with:
timezoneLinux: 'Asia/Singapore'

- name: 🏃 Run tests
env:
PYTEST: true
Expand Down
8 changes: 4 additions & 4 deletions custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from asyncio import sleep
from datetime import datetime
from datetime import datetime, UTC
import os
import pathlib
import shutil
Expand Down Expand Up @@ -196,7 +196,7 @@ def update_data(self, data: dict, action: bool = False) -> None:
continue

if key == "last_fetched" and isinstance(value, float):
setattr(self, key, datetime.fromtimestamp(value))
setattr(self, key, datetime.fromtimestamp(value, UTC))
elif key == "id":
setattr(self, key, str(value))
elif key == "country":
Expand Down Expand Up @@ -501,7 +501,7 @@ async def common_registration(self) -> None:

if self.repository_object:
self.data.last_updated = self.repository_object.attributes.get("pushed_at", 0)
self.data.last_fetched = datetime.utcnow()
self.data.last_fetched = datetime.now(UTC)

@concurrent(concurrenttasks=10, backoff_time=5)
async def common_update(self, ignore_issues=False, force=False, skip_releases=False) -> bool:
Expand Down Expand Up @@ -549,7 +549,7 @@ async def common_update(self, ignore_issues=False, force=False, skip_releases=Fa
self.additional_info = await self.async_get_info_file_contents()

# Set last fetch attribute
self.data.last_fetched = datetime.utcnow()
self.data.last_fetched = datetime.now(UTC)

return True

Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import asyncio
from datetime import datetime
from datetime import datetime, UTC
from typing import Any

from homeassistant.core import callback
Expand Down Expand Up @@ -308,7 +308,7 @@ def async_restore_repository(self, entry: str, repository_data: dict[str, Any]):
repository.data.manifest_name = repository_data.get("manifest_name")

if last_fetched := repository_data.get("last_fetched"):
repository.data.last_fetched = datetime.fromtimestamp(last_fetched)
repository.data.last_fetched = datetime.fromtimestamp(last_fetched, UTC)

repository.repository_manifest = HacsManifest.from_dict(
repository_data.get("manifest") or repository_data.get("repository_manifest") or {}
Expand Down

0 comments on commit 40e3127

Please sign in to comment.