Hot to retrieve the date and and time of a system??  
Author Message
Bassam72





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

Dear all,

Any one can remind me on how to retrieve the Date, Time or Datetime of a system using stndard C++ and wich header file should I use. I am using win32 C++ with Visual C++ 2005 express edition on windows xp

Regards

Bassam



Visual C++9  
 
 
Mike Danes





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

There is the time function in the time.h header (or ctime):

#include <ctime>

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

time_t t;

time(&t); // get the current time

tm *ptm = localtime(&t); // convert to local time

cout << ptm->tm_year + 1900 << endl; // output the current year

return 0;

}


 
 
ImAnAccident





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

What would I use instead of tm_year to output time and date

[edit]
I got it working, but I can't get it to print them all on the same line :

#include <ctime>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
time_t t;
time(&t); // get the current time
tm *ptm = localtime(&t); // convert to local time
cout << ptm->tm_hour<< endl;
cout << ptm->tm_min<< endl;
cout << ptm->tm_sec<< endl;
return 0;
}

Can you tell me how

[/edit]

 
 
Mike Danes





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

"but I can't get it to print them all on the same line"

Easy, replace endl with something else like the ":" character for example:

cout << ptm->tm_hour<< ":";
cout << ptm->tm_min<< ":";
cout << ptm->tm_sec<< endl;


 
 
Baris ERGUN





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

See if the below lines give you an idea

time_t t;
tm * result;
string timeString;
char timeArray[8+1];
string noSucess = "TIMEERROR";

try
{
t = time(NULL);
result = localtime(&t);
}

catch(...)
{
mxErrorCode->Lock();//
*errorCode = EXCEPTION_ERROR_IN_GETTING_SYSTEM_TIME;
mxErrorCode->UnLock();//

}

if (result)
{
sprintf(timeArray,"%02d_%02d_%02d",result->tm_hour,
result->tm_min,
result->tm_sec);
timeString = timeArray;

return timeString;
}
else
return noSucess;



 
 
ImAnAccident





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

Sorry, I didn't make myself clear. What I meant is that I wanted to have them all on a single 'cout' line. Something like :
cout << ptm->tm_hour,ptm->tm_min,ptm->tm_sec;



 
 
ImAnAccident





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

Ahh, I figured it out. Solution :

#include <ctime>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
time_t t;
time(&t); // get the current time
tm *ptm = localtime(&t); // convert to local time
cout << ptm->tm_hour<< ":" << ptm->tm_min << ":" <<ptm->tm_sec;
return 0;
}

Heh, not so complicated :)

 
 
Bassam72





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

Thanks for reminding me Mike Danes.

Regards

Bassam


 
 
Bassam72





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

Sorry, I didn't make myself clear. What I meant is that I wanted to have them all on a single 'cout' line. Something like :
cout << ptm->tm_hour,ptm->tm_min,ptm->tm_sec;

In fact you can. There is a anice function that you can use it to get what format you want and what values you want to be shown on your console. The function is strftime() and you can see the usage in the following updated code snippet;

#include <ctime>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

const unsigned short timeBufferSize = 12;

char timeBuffer[timeBufferSize];
time_t t;
time(&t); // get the current time
tm *ptm = localtime(&t); // convert to local time

strftime(timeBuffer, timeBufferSize, "%d-%b-%Y", &ptm); //This is to show up "18-Dec-2006"

cout << timeBuffer << endl;

//And this is to show up "Today is Friday, day 25 of April in the year 2003"

strftime(timeBuffer, timeBufferSize, "Today is %A, day %d of %B in the year %Y.\n", &ptm );

cout << timeBuffer << endl;

return 0;
}

Hope this can help

Regards

Bassam


 
 
Simple Samples





PostPosted: Visual C++ Language, Hot to retrieve the date and and time of a system?? Top

For the C Runtime Library, the Run-Time Routines by Category is a very useful reference.