From d56804217d2eb9d191da705f1a5f1c979ecfebd8 Mon Sep 17 00:00:00 2001 From: RyanOnTheInside <7623207+ryanontheinside@users.noreply.github.com> Date: Sat, 26 Apr 2025 10:28:50 -0400 Subject: [PATCH] timestamp proviider --- src/utils/timing.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/utils/timing.py diff --git a/src/utils/timing.py b/src/utils/timing.py new file mode 100644 index 0000000..3c6bcf9 --- /dev/null +++ b/src/utils/timing.py @@ -0,0 +1,24 @@ +""" +Timing and timestamp utilities for RealTimeNodes. +""" + +import time + +class TimestampProvider: + """ + Provides real-time, monotonic timestamps in milliseconds. + + This ensures timestamps are always increasing while staying aligned with real elapsed time. + Useful for any process that requires monotonically increasing time values. + """ + def __init__(self): + self._start_time = time.time() + self._last_timestamp = 0 + + def next(self) -> int: + """Get the next timestamp in milliseconds, guaranteed to be greater than the previous one.""" + now = int((time.time() - self._start_time) * 1000) + if now <= self._last_timestamp: + now = self._last_timestamp + 1 + self._last_timestamp = now + return now \ No newline at end of file