diff --git a/chess/pgn.py b/chess/pgn.py index 3d4d7e5ff..6a1abea06 100644 --- a/chess/pgn.py +++ b/chess/pgn.py @@ -566,7 +566,8 @@ def set_clock(self, seconds: Optional[float]) -> None: """ clk = "" if seconds is not None: - seconds = max(0, seconds) + # nearest ms, otherwise 59.9996 becomes an invalid :60 + seconds = round(max(0, seconds), 3) hours = int(seconds // 3600) minutes = int(seconds % 3600 // 60) seconds = seconds % 3600 % 60 @@ -595,7 +596,8 @@ def set_emt(self, seconds: Optional[float]) -> None: """ emt = "" if seconds is not None: - seconds = max(0, seconds) + # nearest ms, otherwise 59.9996 becomes an invalid :60 + seconds = round(max(0, seconds), 3) hours = int(seconds // 3600) minutes = int(seconds % 3600 // 60) seconds = seconds % 3600 % 60 diff --git a/test.py b/test.py index a481c2adb..1f2de2ac9 100755 --- a/test.py +++ b/test.py @@ -2970,6 +2970,14 @@ def test_float_emt(self): self.assertEqual(game.comments, ["[%emt 0:00:00]"]) # Clamped self.assertEqual(game.emt(), 0) + game.set_emt(59.9996) + self.assertEqual(game.comments, ["[%emt 0:01:00]"]) + self.assertEqual(game.emt(), 60) + + game.set_emt(3599.9999) + self.assertEqual(game.comments, ["[%emt 1:00:00]"]) + self.assertEqual(game.emt(), 3600) + def test_float_clk(self): game = chess.pgn.Game() game.comments = ["[%clk 0:00:01.234]"] @@ -2983,6 +2991,14 @@ def test_float_clk(self): self.assertEqual(game.comments, ["[%clk 0:00:00]"]) # Clamped self.assertEqual(game.clock(), 0) + game.set_clock(59.9996) + self.assertEqual(game.comments, ["[%clk 0:01:00]"]) + self.assertEqual(game.clock(), 60) + + game.set_clock(3599.9999) + self.assertEqual(game.comments, ["[%clk 1:00:00]"]) + self.assertEqual(game.clock(), 3600) + def test_node_turn(self): game = chess.pgn.Game() self.assertEqual(game.turn(), chess.WHITE)