fix: 'Timer' logic

This commit is contained in:
2026-08-11 20:59:21 +08:00
parent 0b359ee4e2
commit bfb45a971e
5 changed files with 59 additions and 59 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "common" name = "common"
description = "Commonly reusable code" description = "Commonly reusable code"
version = "0.1.19" version = "0.1.20"
requires-python = ">=3.13" requires-python = ">=3.13"
authors = [ authors = [
{ name="BreakerBear", email="breakerbear@autistic.men" }, { name="BreakerBear", email="breakerbear@autistic.men" },
+6 -6
View File
@@ -6,17 +6,17 @@ class Timer:
self.clear() self.clear()
def clear(self): def clear(self):
self.checkpoint = None
self.accumulator = 0 self.accumulator = 0
self.checkpoint = 0
def start(self): def start(self):
seconds = floor(time()) self.accumulator = self.delta()
self.checkpoint = seconds self.checkpoint = floor(time())
def pause(self): def pause(self):
assert self.checkpoint is not None, "Uninitialized"
self.accumulator = self.delta() self.accumulator = self.delta()
self.checkpoint = 0
def delta(self): def delta(self):
assert self.checkpoint is not None, "Uninitialized" excess = floor(time()) - rhs if (rhs := self.checkpoint) else 0
return floor(time()) - self.checkpoint + self.accumulator return excess + self.accumulator