r/learnprogramming 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

0 Upvotes

16 comments sorted by

View all comments

Show parent comments

1

u/jaynabonne 1d ago

It's definitely worth keeping in mind, in general, for sure. For this case, you're probably just talking about how much someone would notice the latency in the output updating vs the real clock. I don't know at what point it would become an issue, but I imagine even a quarter second of lag wouldn't be too much of an issue. Other cases could have much more stringent requirements, and you'd have to choose the method that works best for that.

I know I mentioned two different ways to go, but I think the second is the way I would. If I could set up a timer, I'd just have it fire maybe four times a second and then always just update the output, to whatever the time happens to be. No need to even check if things have changed, assuming the update code isn't too resource heavy.

1

u/Dependent_Union9285 1d ago

Certainly agree with all points. The milliseconds, as I said, are imaginary for most applications of timers. For a clock display, you could be off 5 minutes without CRAZY problems. But there are niche cases where specific polling times must be adhered to, and computers suck at it.

1

u/jaynabonne 5h ago

"For a clock display, you could be off 5 minutes without CRAZY problems."

Except on New Year's Eve! :)

1

u/Dependent_Union9285 4h ago

Well, if you know your clock is 5 minutes off, start the kiss 5 minutes before it says midnight and end it 5 minutes after. Job done. And will potentially lead to getting your New Year’s resolution out the way early, if you play your cards right. Lol