|
-
October 27th, 2002, 03:45 AM
#1
NEWBIE - System Time
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
-
October 27th, 2002, 04:38 AM
#2
One way;
Code:
#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;
}
-
October 27th, 2002, 11:18 AM
#3
Thanks dumah. the code helped me a lot.
-
October 28th, 2002, 07:17 AM
#4
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.
Code:
#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/de...nReference.asp
--Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|