Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 33459d3

Browse files
authored
Merge pull request #136 from jepler/wwvbdatetime-tk
wwvbtk: Improve --help message & internal improvements
2 parents d5c21c0 + 66d4b32 commit 33459d3

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/wwvb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def _get_dut1_info(cls, year: int, days: int, old_time: WWVBMinute | None = None
729729
return round(get_dut1(d) * 10) * 100, isls(d)
730730

731731

732-
def _bcd_bits(n: int) -> Generator[bool, None, None]:
732+
def _bcd_bits(n: int) -> Generator[bool]:
733733
"""Return the bcd representation of n, starting with the least significant bit"""
734734
while True:
735735
d = n % 10

src/wwvb/wwvbtk.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# SPDX-License-Identifier: GPL-3.0-only
77
from __future__ import annotations
88

9+
import datetime
910
import functools
10-
import time
1111
from tkinter import Canvas, TclError, Tk
1212
from typing import TYPE_CHECKING, Any
1313

@@ -59,31 +59,29 @@ def validate_colors(ctx: Any, param: Any, value: str) -> list[str]: # noqa: ARG
5959
metavar="COLORS",
6060
help="2, 3, 4, or 6 Tk color values",
6161
)
62-
@click.option("--size", default=48)
63-
@click.option("--min-size", default=None)
62+
@click.option("--size", default=48, help="initial size in pixels")
63+
@click.option("--min-size", default=None, type=int, help="minimum size in pixels (default: same as initial size)")
6464
def main(colors: list[str], size: int, min_size: int | None) -> None: # noqa: PLR0915
6565
"""Visualize the WWVB signal in realtime"""
6666
if min_size is None:
6767
min_size = size
6868

69-
def deadline_ms(deadline: float) -> int:
69+
def deadline_ms(deadline: datetime.datetime) -> int:
7070
"""Compute the number of ms until a deadline"""
71-
now = time.time()
72-
return int(max(0, deadline - now) * 1000)
71+
now = datetime.datetime.now(datetime.timezone.utc)
72+
return int(max(0, (deadline - now).total_seconds()) * 1000)
7373

74-
def wwvbtick() -> Generator[tuple[float, wwvb.AmplitudeModulation], None, None]:
74+
def wwvbtick() -> Generator[tuple[datetime.datetime, wwvb.AmplitudeModulation]]:
7575
"""Yield consecutive values of the WWVB amplitude signal, going from minute to minute"""
76-
timestamp = time.time() // 60 * 60
76+
timestamp = datetime.datetime.now(datetime.timezone.utc).replace(second=0, microsecond=0)
7777

7878
while True:
79-
tt = time.gmtime(timestamp)
80-
key = tt.tm_year, tt.tm_yday, tt.tm_hour, tt.tm_min
81-
timecode = wwvb.WWVBMinuteIERS(*key).as_timecode()
79+
timecode = wwvb.WWVBMinuteIERS.from_datetime(timestamp).as_timecode()
8280
for i, code in enumerate(timecode.am):
83-
yield timestamp + i, code
84-
timestamp = timestamp + 60
81+
yield timestamp + datetime.timedelta(seconds=i), code
82+
timestamp = timestamp + datetime.timedelta(seconds=60)
8583

86-
def wwvbsmarttick() -> Generator[tuple[float, wwvb.AmplitudeModulation], None, None]:
84+
def wwvbsmarttick() -> Generator[tuple[datetime.datetime, wwvb.AmplitudeModulation]]:
8785
"""Yield consecutive values of the WWVB amplitude signal
8886
8987
.. but deal with time progressing unexpectedly, such as when the
@@ -94,10 +92,10 @@ def wwvbsmarttick() -> Generator[tuple[float, wwvb.AmplitudeModulation], None, N
9492
"""
9593
while True:
9694
for stamp, code in wwvbtick():
97-
now = time.time()
98-
if stamp < now - 60:
95+
now = datetime.datetime.now(datetime.timezone.utc)
96+
if stamp < now - datetime.timedelta(seconds=60):
9997
break
100-
if stamp < now - 1:
98+
if stamp < now - datetime.timedelta(seconds=1):
10199
continue
102100
yield stamp, code
103101

@@ -137,7 +135,7 @@ def controller_func() -> Generator[int]:
137135
yield deadline_ms(stamp)
138136
led_on(code)
139137
app.update()
140-
yield deadline_ms(stamp + 0.2 + 0.3 * int(code))
138+
yield deadline_ms(stamp + datetime.timedelta(seconds=0.2 + 0.3 * int(code)))
141139
led_off(code)
142140
app.update()
143141

0 commit comments

Comments
 (0)