I'm new to programming and working on my first real project: a roughly seven-segment digital clock displayed with ncurses. I understand how to draw the digits, but I'm unsure how to obtain the current system time and decide when to refresh the display. Should I continuously check whether the seconds have reached zero and then update the hours and minutes, or is there a better approach? I'd also like to understand how this differs from the way an operating system or physical computer keeps track of time.
3 Answers
You can keep a loop for the ncurses interface, but don’t make it a tight loop that constantly consumes the CPU. Read the current time, draw the screen, then sleep briefly with something like nanosleep() or usleep(). Updating every 250–500 milliseconds is usually plenty for a clock, and you can redraw only when the displayed values change.
Another option is to calculate how long remains until the next minute and sleep until roughly then. However, checking the time periodically is simpler and handles manual clock changes or time synchronization more reliably. Compare the newly read hour and minute with the values you last displayed, and refresh the ncurses screen when they differ. The operating system maintains the underlying clock; your program should just query it instead of maintaining its own running total.
In C, use the standard time functions from

Be aware that a sleep or timer usually means “wait at least this long,” not “wake up at exactly this instant.” For a normal clock widget that small delay is harmless, but timing-sensitive programs need to account for scheduling delays.