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 all commits
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)
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 @@ -114,10 +114,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, tz=timezone.utc
) + expiration > datetime.now(tz=timezone.utc):
return True

return False
Expand Down
10 changes: 8 additions & 2 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ 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)


class EncryptionAdapter(Adapter):
Expand Down Expand Up @@ -214,7 +214,13 @@ 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),
)
),
)
),
"checksum"
Expand Down