# HG changeset patch # User Mike Becker # Date 1776361756 -7200 # Node ID 685af47592b5cb3bf795a262e53b5032d055e0b8 # Parent 231a79d93c0c6dc95e637e77948426bfd488cd67 add API for formatting clocks relates to #815 diff -r 231a79d93c0c -r 685af47592b5 src/chess/rules.c --- a/src/chess/rules.c Thu Apr 16 19:29:11 2026 +0200 +++ b/src/chess/rules.c Thu Apr 16 19:49:16 2026 +0200 @@ -36,6 +36,7 @@ #include "king.h" #include +#include #include #include @@ -816,3 +817,14 @@ return used_time >= total_time ? 0 : total_time - used_time; } + +void print_clk(uint16_t time, char *str, bool always_hours) { + unsigned hours = time / 3600; + unsigned minutes = (time % 3600) / 60; + unsigned seconds = time % 60; + if (hours > 0 || always_hours) { + snprintf(str, 9, "%02u:%02u:%02u", hours, minutes, seconds); + } else { + snprintf(str, 6, "%02u:%02u", minutes, seconds); + } +} diff -r 231a79d93c0c -r 685af47592b5 src/chess/rules.h --- a/src/chess/rules.h Thu Apr 16 19:29:11 2026 +0200 +++ b/src/chess/rules.h Thu Apr 16 19:49:16 2026 +0200 @@ -256,5 +256,14 @@ uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, uint8_t color); +/** + * Converts clock time to string. + * + * @param time the time to format + * @param str the target buffer (should be at least 10 chars large) + * @param always_hours if hours should always be printed + */ +void print_clk(uint16_t time, char *str, bool always_hours); + #endif /* RULES_H */ diff -r 231a79d93c0c -r 685af47592b5 src/game.c --- a/src/game.c Thu Apr 16 19:29:11 2026 +0200 +++ b/src/game.c Thu Apr 16 19:49:16 2026 +0200 @@ -35,7 +35,6 @@ #include "chess/pgn.h" #include #include -#include #include #include #include @@ -47,12 +46,12 @@ if (gameinfo->timecontrol) { uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE); uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK); - mvprintw(boardy+4, boardx-1, - "White time: %4" PRIu16 ":%02" PRIu16, - white / 60, white % 60); - mvprintw(boardy+5, boardx-1, - "Black time: %4" PRIu16 ":%02" PRIu16, - black / 60, black % 60); + char clkstr[16]; + bool always_hours = gameinfo->time >= 3600; + print_clk(white, clkstr, always_hours); + mvprintw(boardy+4, boardx-1, "White time: %s", clkstr); + print_clk(black, clkstr, always_hours); + mvprintw(boardy+5, boardx-1, "Black time: %s", clkstr); if (white == 0) { move(inputy, 0);