Skip to content

Commit 6d23da4

Browse files
committed
Solve monthly overflow as reported in #399
1 parent f022c48 commit 6d23da4

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from asyncio import CancelledError, Task, create_task, gather, sleep
6+
import calendar
67
from collections.abc import Awaitable, Callable
78
from dataclasses import replace
89
from datetime import UTC, datetime, timedelta
@@ -881,8 +882,13 @@ async def clock_synchronize(self) -> bool:
881882

882883
dt_now = datetime.now(tz=UTC)
883884
days_diff = (response.day_of_week.value - dt_now.weekday()) % 7
885+
last_day_of_month = calendar.monthrange(dt_now.year, dt_now.month)[1]
886+
days_to_end_of_month = last_day_of_month - dt_now.day
887+
corrected_day = dt_now.day + days_diff
888+
if (difference := days_diff - days_to_end_of_month) > 0:
889+
corrected_day = difference
884890
circle_timestamp: datetime = dt_now.replace(
885-
day=dt_now.day + days_diff,
891+
day=corrected_day,
886892
hour=response.time.value.hour,
887893
minute=response.time.value.minute,
888894
second=response.time.value.second,

0 commit comments

Comments
 (0)