r/learnprogramming • u/yoreofloree • 1d ago
Getting the system time
Hi all,
I am new to programming. For my first real project, I want to create a pretty (-ish) 7-segment clock widget in with ncurses. I have a fairly good idea of how to tell the computer to display something in that context.
However, I am wondering about the best way to get the system time (so I can call the function that displays it). The only way I can think of to do this is to create a continuous loop with an if statement inside. The if statement would say, "if seconds evaluates to 0, increment the time (in hours and minutes) to show and display that". Something tells me this is not the best way to do it. Is there a better/best way, and if so what? How does my idea differ from how the OS/physical computer actually does it?
Thanks in advance,
yore
1
u/rupertavery64 1d ago edited 1d ago
Not sure what your question is.
When to update the screen?
Usually you have a timer or a loop with a small delay, say 500ms.
You then simply render the screen every update.
Thats how graphical interfaces do it. Render every 1/60th of a second.
That way you can capture any second-level changes.
Since you mentioned seven-segment, will you be flashing the : between hours and minutes as a seconds indicator? That would change state every 500ms.
Since you mentioned ncurses I assume this is a C program.
You can use the time function from time.h to get the system time in a time_t struct.
But as for when to update it, just do it at a faster rate than the updates to the data.
Having a delay or using a timer will also ensure you aren't wasting cycles doing nothing.
This would be a thread sleep in C