mirror of
https://github.com/Crocmagnon/plant-badger.git
synced 2024-09-07 22:57:35 +02:00
Fix DST
This commit is contained in:
parent
7db36cd6b5
commit
03dddba8d5
6 changed files with 83 additions and 2 deletions
|
@ -10,6 +10,7 @@
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/.direnv" />
|
<excludeFolder url="file://$MODULE_DIR$/.direnv" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="Python 3.10 (plant-badge)" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="Python 3.10 (plant-badge)" jdkType="Python SDK" />
|
||||||
|
|
|
@ -5,3 +5,4 @@ pre-commit
|
||||||
requests
|
requests
|
||||||
Pillow
|
Pillow
|
||||||
pyyaml
|
pyyaml
|
||||||
|
pytest
|
||||||
|
|
|
@ -12,6 +12,7 @@ from badger_with_clock import Badger2040
|
||||||
from badger_os import get_battery_level
|
from badger_os import get_battery_level
|
||||||
|
|
||||||
import secrets
|
import secrets
|
||||||
|
from dst import fix_dst
|
||||||
from secrets import HA_BASE_URL, HA_ACCESS_TOKEN
|
from secrets import HA_BASE_URL, HA_ACCESS_TOKEN
|
||||||
|
|
||||||
|
|
||||||
|
@ -251,8 +252,7 @@ def display_header(text):
|
||||||
display.text(text, 3, 4)
|
display.text(text, 3, 4)
|
||||||
|
|
||||||
# Display time
|
# Display time
|
||||||
_, _, _, hour, minute, _, _ = display.rtc.datetime()
|
hour, minute = get_time()
|
||||||
hour = (hour + 1) % 24
|
|
||||||
time = f"{hour:02d}:{minute:02d}"
|
time = f"{hour:02d}:{minute:02d}"
|
||||||
time_offset = display.measure_text(time) + 3
|
time_offset = display.measure_text(time) + 3
|
||||||
display.text(time, WIDTH - time_offset, 4)
|
display.text(time, WIDTH - time_offset, 4)
|
||||||
|
@ -264,4 +264,8 @@ def display_header(text):
|
||||||
display.text(battery, WIDTH - time_offset - battery_offset, 4)
|
display.text(battery, WIDTH - time_offset - battery_offset, 4)
|
||||||
|
|
||||||
|
|
||||||
|
def get_time():
|
||||||
|
return fix_dst(*display.rtc.datetime())
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
22
src/dst.py
Normal file
22
src/dst.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
def fix_dst(year, month, day, hour, minute, second, dow):
|
||||||
|
last_sunday = get_last_sunday_date(day, dow)
|
||||||
|
past_last_sunday = day >= last_sunday
|
||||||
|
if (
|
||||||
|
4 <= month <= 9
|
||||||
|
or (month == 3 and past_last_sunday)
|
||||||
|
or (month == 10 and not past_last_sunday)
|
||||||
|
):
|
||||||
|
delta = 2
|
||||||
|
else:
|
||||||
|
delta = 1
|
||||||
|
hour = (hour + delta) % 24
|
||||||
|
return hour, minute
|
||||||
|
|
||||||
|
|
||||||
|
def get_last_sunday_date(day, dow):
|
||||||
|
until_end_of_month = 31 - day
|
||||||
|
weeks_to_add = until_end_of_month // 7 + 1
|
||||||
|
last_sunday = day + weeks_to_add * 7 - dow - 1
|
||||||
|
if last_sunday > 31:
|
||||||
|
last_sunday -= 7
|
||||||
|
return last_sunday
|
15
tasks.py
15
tasks.py
|
@ -1,4 +1,5 @@
|
||||||
import ast
|
import ast
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -9,6 +10,7 @@ from invoke import task, Context
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).parent.resolve(strict=True)
|
BASE_DIR = Path(__file__).parent.resolve(strict=True)
|
||||||
SRC_DIR = BASE_DIR / "src"
|
SRC_DIR = BASE_DIR / "src"
|
||||||
|
TESTS_DIR = BASE_DIR / "test"
|
||||||
|
|
||||||
MICROPYTHON_DEPENDENCIES = [
|
MICROPYTHON_DEPENDENCIES = [
|
||||||
# "github:miguelgrinberg/microdot/src/microdot.py",
|
# "github:miguelgrinberg/microdot/src/microdot.py",
|
||||||
|
@ -16,6 +18,19 @@ MICROPYTHON_DEPENDENCIES = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@task
|
||||||
|
def test(c: Context) -> None:
|
||||||
|
"""Run tests."""
|
||||||
|
with c.cd(BASE_DIR):
|
||||||
|
path = os.getenv("PYTHONPATH", "")
|
||||||
|
c.run(
|
||||||
|
"pytest",
|
||||||
|
pty=True,
|
||||||
|
echo=True,
|
||||||
|
env={"PYTHONPATH": f"{SRC_DIR}:{path}"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@task(name="list")
|
@task(name="list")
|
||||||
def list_boards(c: Context) -> None:
|
def list_boards(c: Context) -> None:
|
||||||
"""List connected boards with mpremote."""
|
"""List connected boards with mpremote."""
|
||||||
|
|
38
test/test_time.py
Normal file
38
test/test_time.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from dst import fix_dst, get_last_sunday_date
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"year, month, day, hour, minute, second, dow, expected",
|
||||||
|
[
|
||||||
|
(2023, 3, 2, 17, 22, 0, 3, (18, 22)),
|
||||||
|
(2023, 3, 25, 17, 22, 0, 5, (18, 22)),
|
||||||
|
(2023, 3, 26, 17, 22, 0, 6, (19, 22)),
|
||||||
|
(2023, 3, 27, 17, 22, 0, 0, (19, 22)),
|
||||||
|
(2023, 3, 28, 17, 22, 0, 1, (19, 22)),
|
||||||
|
(2023, 10, 2, 17, 22, 0, 0, (19, 22)),
|
||||||
|
(2023, 10, 28, 17, 22, 0, 5, (19, 22)),
|
||||||
|
(2023, 10, 29, 17, 22, 0, 6, (18, 22)),
|
||||||
|
(2023, 10, 30, 17, 22, 0, 0, (18, 22)),
|
||||||
|
(2023, 10, 31, 17, 22, 0, 1, (18, 22)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_fix_dst(year, month, day, hour, minute, second, dow, expected):
|
||||||
|
assert fix_dst(year, month, day, hour, minute, second, dow) == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"day, dow, expected",
|
||||||
|
[
|
||||||
|
(2, 3, 26),
|
||||||
|
(15, 2, 26),
|
||||||
|
(25, 5, 26),
|
||||||
|
(17, 0, 30),
|
||||||
|
(24, 5, 25),
|
||||||
|
(27, 0, 26),
|
||||||
|
(31, 2, 28),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_get_last_sunday_date(day, dow, expected):
|
||||||
|
assert get_last_sunday_date(day, dow) == expected
|
Loading…
Reference in a new issue