Skip to content

Add chess.pgn.Game.set_time_control() - #1216

Open
ybai08 wants to merge 1 commit into
niklasf:masterfrom
ybai08:pgn-set-time-control
Open

ybai08 wants to merge 1 commit into
niklasf:masterfrom
ybai08:pgn-set-time-control

Conversation

@ybai08

@ybai08 ybai08 commented Oct 19, 2026

Copy link
Copy Markdown

Closes #1033.

Adds Game.set_time_control(time_control), the setter counterpart of Game.time_control(). It writes the TimeControl header in the PGN spec format (section 9.6.1), so that parse_time_control() reads back an equal TimeControl.

  • Parts are joined with :. A part is written as moves/seconds, or as plain seconds for sudden death, followed by +increment or +delayd where set. Fractional increments and delays are written with up to 3 decimals, the same way set_clock() writes them (180+0.5, 300+2).
  • A TimeControl without parts is written as - when its type is UNLIMITED, and as ? otherwise.
  • ValueError is raised, and the header is left unchanged, for values that parse_time_control() would reject or cannot express: a sudden-death part before the last part, or a part with both an increment and a delay.

Game.time_control and Game.set_time_control are also added to the Game members listed in docs/pgn.rst.

The new test PgnTestCase.test_set_time_control round-trips headers (40/7200:20/3600:900+30, 300+2, 180+0.5, 5400+30d, 1800, -, ?) and checks the error cases.

Checks run locally:

  • python test.py on Python 3.14 and 3.9: 297 tests OK (22 skipped, engine binaries not installed). The new test fails on master with AttributeError: 'Game' object has no attribute 'set_time_control'.
  • python -m mypy --strict chess: no issues.
  • python -m pyright chess/pgn.py: 0 errors (one existing warning about typing_extensions).
  • python -m doctest README.rst: only the 3 Stockfish examples fail, because no stockfish binary is installed.

Not run: Windows CI and the perft workflow, which this change does not touch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Finalize TimeControl PGN header support

1 participant