Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace datetime.utcnow + datetime.utcfromtimestamp #1809

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions miio/miioprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import codecs
import logging
import socket
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pprint import pformat as pf
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(

self._discovered = False
# these come from the device, but we initialize them here to make mypy happy
self._device_ts: datetime = datetime.utcnow()
self._device_ts: datetime = datetime.now(tz=timezone.utc).replace(tzinfo=None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @cdce8p! I'm wondering if it's necessary to have replace() calls at all? Alas, I'm not going to have access to any test devices until next month, so I cannot really test this myself.

The CI failures are irrelevant to this PR, so they are not blockers to get this merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't test it myself either, unfortunately.

However, I believe you might be correct here. The call in _valid_cache is covered by the test suite. As for the protocol: AFAICT the conversion happens inside the TimeAdapter. obj.timetuple() strips the timezone anyway and for the decode we might as well leave the tz in place.

self._device_id = b""

def send_handshake(self, *, retry_count=3) -> Message:
Expand Down
9 changes: 4 additions & 5 deletions miio/miot_cloud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module implementing handling of miot schema files."""
import json
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from operator import attrgetter
from pathlib import Path
from typing import Dict, List, Optional
Expand Down Expand Up @@ -110,10 +110,9 @@ def _write_to_cache(self, file: Path, data: Dict):
def _file_from_cache(self, file, cache_hours=6) -> Dict:
def _valid_cache():
expiration = timedelta(hours=cache_hours)
if (
datetime.fromtimestamp(file.stat().st_mtime) + expiration
> datetime.utcnow()
):
if datetime.fromtimestamp(file.stat().st_mtime) + expiration > datetime.now(
tz=timezone.utc
).replace(tzinfo=None):
return True

return False
Expand Down
14 changes: 12 additions & 2 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def _encode(self, obj, context, path):
return calendar.timegm(obj.timetuple())

def _decode(self, obj, context, path):
return datetime.datetime.utcfromtimestamp(obj)
return datetime.datetime.fromtimestamp(obj, tz=datetime.timezone.utc).replace(
tzinfo=None
)


class EncryptionAdapter(Adapter):
Expand Down Expand Up @@ -214,7 +216,15 @@ def _decode(self, obj, context, path) -> Union[Dict, bytes]:
"length" / Rebuild(Int16ub, Utils.get_length),
"unknown" / Default(Int32ub, 0x00000000),
"device_id" / Hex(Bytes(4)),
"ts" / TimeAdapter(Default(Int32ub, datetime.datetime.utcnow())),
"ts"
/ TimeAdapter(
Default(
Int32ub,
datetime.datetime.now(tz=datetime.timezone.utc).replace(
tzinfo=None
),
)
),
)
),
"checksum"
Expand Down