Thu, 16 Apr 2026 19:49:16 +0200
add API for formatting clocks
relates to #815
| src/chess/rules.c | file | annotate | diff | comparison | revisions | |
| src/chess/rules.h | file | annotate | diff | comparison | revisions | |
| src/game.c | file | annotate | diff | comparison | revisions |
--- 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 <string.h> +#include <stdio.h> #include <stdlib.h> #include <sys/time.h> @@ -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); + } +}
--- 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 */
--- 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 <ncurses.h> #include <string.h> -#include <inttypes.h> #include <sys/select.h> #include <stdio.h> #include <errno.h> @@ -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);