What is code for getting the time from the computers clock?
What is code for getting the time from the computers clock?
Those who live by the sword get shot by those who don't.
I can C.
Compiler: gcc
Code:#include <iostream> #include <ctime> using namespace std; int main(void) { time_t TheTime; time(&TheTime); cout << ctime(&TheTime) << endl; return 0; }
Is there a way to get JUST the time, like in int or long format?
Brendan
What do you mean by JUST the time? Hours? Minutes? Seconds?
Look into the tm struct, its member variables hold all kinds of different measures of the current time.
Code:#include <ctime> int main(int argc char *argv[]) { using namespace std; time_t currentTime = time(NULL); tm *timeInfo; timeInfo = localtime(¤tTime); cout << timeInfo->tm_hour << endl << // hr (0 - 23) timeInfo->tm_min << endl << // min (0 - 59) timeInfo->tm_sec << endl; // sec (0 - 59) return 0; }
Last edited by Eibro; 09-04-2002 at 07:05 PM.
The seconds works. Thanks! The reason I was wondering was because I wanted to make a random number generator that is seeded based on the current time, which meant I couldn't use the date and all that. This way, it may be a bit more random than it usually is. Thanks again!
Brendan
You could use the Windows API function GetLocalTime() to initialize an instance of the SYSTEMTIME data structure...
Code:#include <windows.h> #include <iostream> int main() { SYSTEMTIME st; GetLocalTime(&st); std::cout << st.wYear << std::endl << st.wMonth << std::endl << st.wDayOfWeek << std::endl << st.wDay << std::endl << st.wHour << std::endl << st.wMinute << std::endl << st.wSecond << std::endl << st.wMilliseconds << std::endl; exit(0); }