Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
25 lines
622 B
Python
25 lines
622 B
Python
from datetime import datetime, timedelta
|
|
|
|
EPOCH = datetime(2024, 5, 14, 6, 0, 0)
|
|
|
|
|
|
class VirtualClock:
|
|
"""Simulated time in seconds since EPOCH; only the simulator moves it."""
|
|
|
|
def __init__(self, start=0.0):
|
|
self._now = start
|
|
|
|
def now(self):
|
|
return self._now
|
|
|
|
def advance_to(self, t):
|
|
if t < self._now:
|
|
raise ValueError("clock cannot go backwards: %.3f < %.3f" % (t, self._now))
|
|
self._now = t
|
|
|
|
|
|
def format_ts(t):
|
|
ms = int(round(t * 1000))
|
|
stamp = EPOCH + timedelta(milliseconds=ms)
|
|
return stamp.strftime("%Y-%m-%d %H:%M:%S") + ".%03d" % (ms % 1000)
|