LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting the time in C/C++ in a terminal application (https://www.linuxquestions.org/questions/programming-9/getting-the-time-in-c-c-in-a-terminal-application-149948/)

Error1312 02-24-2004 10:00 AM

getting the time in C/C++ in a terminal application
 
Hello everybody.

I want to make a program that gets the system time in hours, minutes and seconds. Than I want to compare the time with the input from the user.

Could somebody give me an example of how to: - get the system time and print it on the screen
- compare two 'times'


Thanks in advance.

jtshaw 02-24-2004 10:16 AM

The function gettimeofday from sys/time.h will tell you what you need as far as what time the system thinks it is. I recommend reading the man page on it. There will need to be conversions done on the users input to both display it as H:M:S and to compare them.

Error1312 02-24-2004 10:45 AM

Thanks for the answer. I've managed to use the function, but I can't find a way to put it in H:M:S on the screen. Any help would be very appreciated.

jtshaw 02-24-2004 10:58 AM

Actually, now that I think of it, don't use gettimeofday.

Use time and mktime. Read the man pages for these two functions. Time returns the time in seconds since the Epoch. mktime converts it to a struct with all the information you want. Then you just print out all the information individually.


All times are GMT -5. The time now is 06:13 PM.