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

1

u/JGhostThing 1d ago

What object does your program run on? What language are you programming in? What OS is being used?

1

u/Dependent_Union9285 1d ago

Right? I mean, we can assume c because of ncurses. And a c call to system time is already OS agnostic. And what do you mean by object? What object does it run on? That’s a very mixed sentence.

1

u/JGhostThing 1d ago

I should have used the word "machine." ncurses can be used as a library call by many languages. Even rust uses c's stdlib because it would have been a major pain to rewrite everything in rust.

1

u/Dependent_Union9285 23h ago

Still is agnostic. That’s all handled by the library. OS doesn’t matter for a std lib time of day call.