Skip to content

Commit

Permalink
add reset and t0 method to the clock object
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheltienne committed Dec 2, 2024
1 parent 6338c9e commit 70f4bd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion stimuli/time/_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def get_time(self) -> float:
"""
return self.get_time_ns() / 1e9

@abstractmethod
def reset(self) -> None:
"""Reset the clock to zero."""


class Clock(BaseClock):
"""Clock which keeps track of time in nanoseconds.
Expand All @@ -77,8 +81,20 @@ def __init__(self) -> None:
self._function = time.perf_counter_ns
else:
self._function = time.monotonic_ns
self._t0 = self._function()
self.reset()

@copy_doc(BaseClock.get_time_ns)
def get_time_ns(self) -> int:
return self._function() - self._t0

def reset(self) -> None:
"""Reset the clock to zero."""
self._t0 = self._function()

@property
def t0(self) -> int:
"""The time of instantiation of the clock.
:type: :class:`int`
"""
return self._t0
9 changes: 9 additions & 0 deletions stimuli/time/tests/test_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ def test_clock():
t1 = clock.get_time_ns()
t2 = clock.get_time_ns()
assert t1 < t2


def test_reset():
"""Test the clock reset method."""
clock = Clock()
t0 = clock.t0
assert isinstance(t0, int)
clock.reset()
assert t0 < clock.t0

0 comments on commit 70f4bd6

Please sign in to comment.