[robotpy] Add wrapper for timestamp functions, like SetNowImpl (#8889)

`SetNowImpl` is used somewhat often in unit tests. It is a little bit
goofier to wrap because it takes a C function, so a little bit more work
has to be done to get that wrapped in pybind.

Claude helped.
This commit is contained in:
PJ Reiniger
2026-05-15 00:53:26 -04:00
committed by GitHub
parent 68d24bb29e
commit 6ba5734a94
7 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import wpiutil
import time
import pytest
def test_default():
wpi_now = wpiutil.now() * 1e-6
py_now = int(time.time())
# Allow a one second delta. We don't care about it being all that accurate in the
# test, just that we are in the same galaxy
assert py_now == pytest.approx(wpi_now, abs=1)
NOW_TIMESTAMP_S = 0
def custom_now_getter():
global NOW_TIMESTAMP_S
return int(NOW_TIMESTAMP_S * 1e6)
@pytest.fixture
def custom_fixture():
wpiutil.SetNowImpl(custom_now_getter)
yield
wpiutil.SetNowImpl(None)
def test_custom_timestamp(custom_fixture):
global NOW_TIMESTAMP_S
assert 0 == wpiutil.now()
NOW_TIMESTAMP_S = 1.5
assert 1_500_000 == wpiutil.now()
NOW_TIMESTAMP_S = 100
assert 100_000_000 == wpiutil.now()
# Set it back to the standard implementation and expect its roughly milliseconds since 1970
wpiutil.SetNowImpl(None)
wpi_now = wpiutil.now() * 1e-6
py_now = int(time.time())
assert py_now == pytest.approx(wpi_now, abs=1)