|
-
May 19th, 2005, 05:28 PM
#1
Thread Starter
Admodistrator
Getting the time..
Can anyone tell me how to find out what time it is on the system?
-
May 19th, 2005, 06:24 PM
#2
Re: Getting the time..
Code:
time_t t;
time(&t);
// t now contains the current time
(google for headers and how to use time_t)
-
May 19th, 2005, 06:43 PM
#3
Thread Starter
Admodistrator
Re: Getting the 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..
-
May 19th, 2005, 07:21 PM
#4
Addicted Member
Re: Getting the time..
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
-
May 19th, 2005, 07:49 PM
#5
Thread Starter
Admodistrator
Re: Getting the time..
i dont get what the big deal is, it pauses my code and works fine..i cant understand whats so terrible about it..
-
May 20th, 2005, 12:22 AM
#6
Re: Getting the time..
Assuming you're using windows, you can also do something like this:
Code:
#include <windows.h>
// ...
SYSTEMTIME cur_time;
GetSystemTime(&cur_time); // get UTC time
// ....
GetLocalTime(&cur_time); // get local time
Check out this page for info in the SYSTEMTIME struct.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
May 20th, 2005, 03:49 PM
#7
Thread Starter
Admodistrator
Re: Getting the time..
can you please give me a usage?
like so i can actually get the date and time and such?thx
-
May 20th, 2005, 03:51 PM
#8
Re: Getting the time..
Just click on the link in sunburnt's post, it gives all the information you need.
-
May 20th, 2005, 04:09 PM
#9
Thread Starter
Admodistrator
Re: Getting the time..
i wouldnt ask if i understood it.
-
May 20th, 2005, 04:12 PM
#10
Re: Getting the time..
What don't you understand? It sais that the structure looks like:
Code:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME,
*PSYSTEMTIME;
So after running the code in sunburnt's post this will print the current date for example:
Code:
std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20
-
May 20th, 2005, 04:19 PM
#11
Thread Starter
Admodistrator
Re: Getting the time..
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{
-
May 20th, 2005, 04:20 PM
#12
Re: Getting the time..
You don't need to inlcude the typedef struct SYSTIME in your own code. It is already in windows.h
-
May 20th, 2005, 06:08 PM
#13
Thread Starter
Admodistrator
Re: Getting the time..
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 ";
}
-
May 20th, 2005, 06:09 PM
#14
Re: Getting the time..
You need to get the date first using GetSystemTime(&cur_time); or GetLocalTime.
-
May 20th, 2005, 06:14 PM
#15
Thread Starter
Admodistrator
Re: Getting the time..
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" );
}
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
|