Click to See Complete Forum and Search --> : NEWBIE - System Time


bartman1971
October 27th, 2002, 02:45 AM
This might be easy for most of you, but I am just starting so I need help. I need to get the Current System time and put it into a INT type variable for each of these 3 components (hour, minute, second)

Any help would be greatly appreciated

Thanks

dumah
October 27th, 2002, 03:38 AM
One way;


#include <iostream>
#include <windows.h>

int main(void){

SYSTEMTIME st = {0};

GetSystemTime(&st);

int second = st.wSecond,
hour = st.wHour,
minute = st.wMinute;


std::cout << "Hour:Minute:Second" << std::endl;
std::cout << hour << ":" << minute << ":";
std::cout << second << std::endl;

}

bartman1971
October 27th, 2002, 10:18 AM
Thanks dumah. the code helped me a lot.

PaulWendt
October 28th, 2002, 06:17 AM
If you don't care about portability and you only need to run this
on Windows systems, the code you have seen is fine. If you want
to take the code to other platforms, then you could use the
ANSI approach.

#include <time.h> // for C
#include <ctime> // for C++

char currentTime[256];
time_t timeBuf;
struct tm* pTime;

time(&timeBuf);
pTime = localTime(&timeBuf);
sprintf(currentTime, "%02d/%02d%04d, %02d:%02d:%02d",
pTime->tm_mon+1, pTime->tm_mday,
(1900 + pTime->tm_year), pTime->tm_hour,
pTime->tm_min, pTime->tm_sec);



You can find some information on this at Microsoft's ANSI-C
function page.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/vclrfAlphabeticalFunctionReference.asp

--Paul