Hi,i am a c++ beginer(*not C++.net*).i am using the visual studio 2003 comand prompt compiler.
I wan't to write program that prints the current date and time.
Can some one tell me the code/statement that would do print the date and time
Thanx
Hi,i am a c++ beginer(*not C++.net*).i am using the visual studio 2003 comand prompt compiler.
I wan't to write program that prints the current date and time.
Can some one tell me the code/statement that would do print the date and time
Thanx
#include
<time.h>int
main(int argc, char* argv[]){
time_t t;
time(&t);
char str[0x100]; if(!ctime_s(str, 0x100, &t)){
printf(
"Date and time: %s\n", str);}
else{
// handle error}
return 0;}
First, note that C++ does not add much for date and time; most everything available for C++ is from the C Library. If you want to use something that is exclusive to the C++ standard, then look in the documentation. When you can't find anything then you will understand why the samples often use C style functions.
Next, just look in Time Management, but don't expect the samples to use C++. There are ample samples in the documentation of the functions and I hope you enjoy converting them to C++. The boost.org web site has many classes that are not yet part of the C++ standard but it is likely that many will soon be standardized.
#include <time.h>
int main()
{
time_t t;
time(&t);
char str[0x100];
if(!ctime_s(str, 0x100, &t))
cout<<"Date and time: %s"<< str<<endl;
return 0;
}
rahulsk19471 wrote:
Hi,i am a c++ beginer(*not C++.net*).i am using the visual studio 2003 comand prompt compiler.
I wan't to write program that prints the current date and time.
Can some one tell me the code/statement that would do print the date and time
Thanx
Here's one Win32 + C++ approach, which uses the string and stream classes from STL, as well as locale specific time formatting (the output will depend on which country your Windows install is setup for). Most of the complexity is wrapped in the getTimeAndDate function, which places the resulting datetime string in the szDateTime parameter passed. See the main function for a demonstration.
#define WIN32_LEAN_AND_MEAN #include <Windows.h> #include <iostream> #include <sstream> using namespace std;bool getTimeAndDate(const SYSTEMTIME& st, string& szDateTime); int main() { SYSTEMTIME st; GetLocalTime(&st); string szDateTime; if(getTimeAndDate(st, szDateTime)) { cout << "Time and date: " << szDateTime << endl; } else { cout << "An error occured while fetching time and date!" << endl; } }bool getTimeAndDate(const SYSTEMTIME& st, string& szDateTime) { ostringstream outputStream; int nDateStringLength = GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, NULL, 0); if(nDateStringLength == 0) { // error condition return false; } char* pszDate = new char[nDateStringLength]; if(GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, pszDate, nDateStringLength) == 0) { // error condition return false; } // Append date to the output stream and free resources outputStream << pszDate; delete[] pszDate; int nTimeStringLength = GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, NULL, 0); if(nTimeStringLength == 0) { // error condition return false; } char* pszTime = new char[nTimeStringLength]; if(GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, pszTime, nTimeStringLength) == 0) { // error condition return false; } // Append time to the output stream and free resources outputStream << " " << pszTime; delete[] pszTime; // Assign the output string szDateTime = outputStream.str();return true; }