Skip to content

Commit

Permalink
Replace datetime.utcnow + datetime.utcfromtimestamp (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Sep 14, 2023
1 parent 2546b04 commit f7b554f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
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

0 comments on commit f7b554f

Please sign in to comment.