Skip to content

Commit

Permalink
Merge pull request #1 from sokolpezinok/lukas/test-clock
Browse files Browse the repository at this point in the history
Fix the clock test
  • Loading branch information
lukipuki committed Oct 24, 2023
2 parents 0869521 + a9c6202 commit 0239627
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/yaroc/utils/sim7020.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shlex
import subprocess
import time
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Callable

from ..pb.status_pb2 import Disconnected, Status
Expand Down Expand Up @@ -119,7 +119,7 @@ def mqtt_connect(self):
self._mqtt_id_timestamp = datetime.now()

def set_clock(self, modem_clock: str):
tim = is_time_off(modem_clock, datetime.now())
tim = is_time_off(modem_clock, datetime.now(timezone.utc))
if tim is not None:
subprocess.call(shlex.split(f"sudo -n date -s '{tim.isoformat()}'"))

Expand Down
4 changes: 2 additions & 2 deletions src/yaroc/utils/sys_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ def create_sys_minicallhome() -> MiniCallHome:


def is_time_off(modem_clock: str, now: datetime) -> datetime | None:
# assert now.tzinfo == timezone.utc
try:
tim = (
datetime.strptime(modem_clock[:20], "%y/%m/%d,%H:%M:%S+08")
.replace(tzinfo=timezone.utc)
.astimezone()
)
if tim - now.astimezone() > timedelta(seconds=5):
if tim - now > timedelta(seconds=5):
return tim
return None
except Exception as err:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_clock.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import unittest
from datetime import datetime
from datetime import datetime, timezone

from yaroc.utils.sys_info import is_time_off


class TestMeos(unittest.TestCase):
class TestClock(unittest.TestCase):
def test_competitor_parsing(self):
modem_clock = "23/06/09,12:06:31+08"
now = datetime(2023, 6, 9, 14, 6, 25)
now = datetime(2023, 6, 9, 12, 6, 25, tzinfo=timezone.utc)
self.assertEqual(
is_time_off(modem_clock, now), datetime(2023, 6, 9, 14, 6, 31).astimezone()
is_time_off(modem_clock, now), datetime(2023, 6, 9, 12, 6, 31, tzinfo=timezone.utc)
)
now = datetime(2023, 6, 9, 14, 6, 27)
now = datetime(2023, 6, 9, 12, 6, 27, tzinfo=timezone.utc)
self.assertEqual(is_time_off(modem_clock, now), None)

0 comments on commit 0239627

Please sign in to comment.