Can anyone tell me how to find out what time it is on the system?
Printable View
Can anyone tell me how to find out what time it is on the system?
(google for headers and how to use time_t)Code:time_t t;
time(&t);
// t now contains the current time
k thx...
// function example
#include <iostream>
#include <TIME.H>
using namespace std;
int main ()
{
time_t t;
time(&t);
cout << t;
system ( "pause" );
}
returns some crazy ass numbers..
You should REALLY stop using namespaces and system() calls.... That having been said, you need to convert time_t to a FILETIME or SYSTEMTIME struct before you can get the actual time from it.
http://msdn.microsoft.com/library/de..._file_time.asp
i dont get what the big deal is, it pauses my code and works fine..i cant understand whats so terrible about it..
Assuming you're using windows, you can also do something like this:
Check out this page for info in the SYSTEMTIME struct.Code:#include <windows.h>
// ...
SYSTEMTIME cur_time;
GetSystemTime(&cur_time); // get UTC time
// ....
GetLocalTime(&cur_time); // get local time
can you please give me a usage?
like so i can actually get the date and time and such?thx
Just click on the link in sunburnt's post, it gives all the information you need.
i wouldnt ask if i understood it.
What don't you understand? It sais that the structure looks like:
So after running the code in sunburnt's post this will print the current date for example:Code:typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME,
*PSYSTEMTIME;
Code:std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20
thankyou. all i need is code, and i can mostly figure things out..except now ive never seen this error before, so im gonna post it
VB Code:
#include <iostream> #include <windows.h> using namespace std; int main () { // ... SYSTEMTIME cur_time; std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20 "; system ( "pause" ); } typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME;
C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(16) : error C2011: '_SYSTEMTIME' : 'struct' type redefinition
over typedef struct_SYSTEMTIME{
You don't need to inlcude the typedef struct SYSTIME in your own code. It is already in windows.h
wow, kthx, that makes alot of sense now!
but my result does not.
im getting 52428-52428-52428
instead of 2005-5-20
VB Code:
#include <iostream> #include <windows.h> using namespace std; int main () { // ... SYSTEMTIME cur_time; std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20 "; }
You need to get the date first using GetSystemTime(&cur_time); or GetLocalTime.
i love you.
edit**
for reference
Code:// function example
#include <iostream>
#include <windows.h>
using namespace std;
int main ()
{
// ...
SYSTEMTIME cur_time;
GetSystemTime(&cur_time);
std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20 ";
system( "pause" );
}