How to use ctime functions(time.h)
#1
How to use ctime functions(time.h)
Posted 22 September 2008 - 01:55 PM
I have a project where I need to get the system time along with the wall clock time in C++. Currently, I have researched time and found out about clock() but isn't sure on how to use it to get the system time. If anyone can give an example I will greatly appreciate it. By the way is this the best way to get the system clock in Linux using C++.
Thanks In Advance
#2
Re: How to use ctime functions(time.h)
Posted 22 September 2008 - 02:06 PM
tnjones, on 22 Sep, 2008 - 02:55 PM, said:
I have a project where I need to get the system time along with the wall clock time in C++. Currently, I have researched time and found out about clock() but isn't sure on how to use it to get the system time. If anyone can give an example I will greatly appreciate it. By the way is this the best way to get the system clock in Linux using C++.
Thanks In Advance
This is how to get System time.....Just scroll down to the C++
http://www.codersour..._date_time.html
Not sure if it will work on linux though, i dont work with linux.
This post has been edited by UG Cyber: 22 September 2008 - 02:08 PM
#3
Re: How to use ctime functions(time.h)
Posted 22 September 2008 - 02:17 PM
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
time_t now = time(0);
cout << ctime(&now);
cin.get();
return EXIT_SUCCESS;
}
Hope this helps
#4
Re: How to use ctime functions(time.h)
Posted 22 September 2008 - 04:47 PM
SYSTEMTIME test;
GetLocalTime(&test); // makes sure its GetLocalTime(&test) and not GetsystemTime(&test)
int day = test.wDay;
int month = test.wMonth;
int dayofweek = test.wDayOfWeek;
int year = test.wYear;
int hour = test.wHour;
int minute = test.wMinute;
int second = test.wSecond;
int realhr = hour;
string am_pm = " am";
if (hour >= 12)
{
realhr = hour - 12;
am_pm=" pm";
}
cout << "today is : "<<month<<"/"<<day<<"/"<<year<<endl;
cout << "the time is: "<<realhr<<":"<<minute<<":"<<second<<am_pm<<endl;
switch(dayofweek){
case 1:
cout << "its monday"<<endl;
break;
case 2:
cout << "its tuesday"<<endl;
break;
case 3:
cout << "its wendsday"<<endl;
break;
case 4:
cout << "its thursday"<<endl;
break;
case 5:
cout << "its fryday"<<endl;
break;
case 6:
cout << "its saturday"<<endl;
break;
case 0:
cout << "its sunday"<<endl;
break;
default:
cout << "error with year"<<endl;
#5
Re: How to use ctime functions(time.h)
Posted 23 September 2008 - 07:23 AM
gabehabe, on 22 Sep, 2008 - 03:17 PM, said:
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
time_t now = time(0);
cout << ctime(&now);
cin.get();
return EXIT_SUCCESS;
}
Hope this helps
Can i ask you : why you place cin.get();
thank you,[color=#660000]
This post has been edited by alowais: 23 September 2008 - 07:26 AM
#6
Re: How to use ctime functions(time.h)
Posted 23 September 2008 - 07:28 AM
alowais, on 23 Sep, 2008 - 10:23 AM, said:
Can i ask you : why you place cin.get();
thank you,
You can find the answer to that here (holding the execution window open):
http://www.dreaminco...h...st&p=238158
edit--ha ha, beat you to it, gabehabe
This post has been edited by OliveOyl3471: 23 September 2008 - 07:29 AM
#9
Re: How to use ctime functions(time.h)
Posted 23 September 2008 - 02:45 PM
gabehabe, on 22 Sep, 2008 - 03:17 PM, said:
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
time_t now = time(0);
cout << ctime(&now);
cin.get();
return EXIT_SUCCESS;
}
Hope this helps
Some work on the above can produce useful results, such as ...
// Another C++ way to get date/time info and format as you like ...
// Demos a table lookup and function 'atoi' ... 'alpha to int'
#include <iostream>
#include <vector>
#include <ctime>
#include <sstream>
using namespace std;
const string monthsTable[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"January", "Febuary", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
// If pass in 'Sep' finds 'n=8' and returns the string '09' ...
string getMonthNumStr( string mStr )
{
// Now do table look up ...
int n=0;
while (n<12)
{
if(monthsTable[n] == mStr )
break;
++n;
}
// ok ... so now we have 'n' ...
return monthsTable[12+n];
}
// If pass in 'Sep' finds 'n=8' and returns the string 'September'
string getLongNameStr( string mStr )
{
// Now do table look up ...
int n=0;
while (n<12)
{
if(monthsTable[n] == mStr )
break;
++n;
}
// ok ... so now we have 'n' ...
return monthsTable[24+n];
}
int main()
{
time_t now = time(0);
ostringstream s1;
s1 << ctime(&now);
string s2 = s1.str();
// Now we have our date/time data in string s2 ... so
// picking apart s2 gives ...
string yearStr = s2.substr(20,4); // '2008'
string monthStr = s2.substr(4,3); // 'Aug'
string dayStr = s2.substr(8,2); // '08'
string timeStr = s2.substr(11,8); // '01:01:01'
// and adding some more formats to be available for output gives ...
string monthNumStr = getMonthNumStr(monthStr); // 'Sep' gives '09'
string mLongNameStr = getLongNameStr(monthStr); // 'Sep' gives 'September'
// first convert to 'c string'; then to 'int' ... to get rid of leading '0'
int dayNum = atoi( dayStr.c_str() );
cout << "Note where year, month, day, etc., "
<< "occur in the following string ..." << endl << endl;
cout << s2 << endl;
cout << "123456789012345678901234" << endl<<endl;
cout << "Now ... picking apart the string to reformat as ..." << endl;
cout << mLongNameStr << " " << dayNum << ", " << yearStr<< " " << timeStr << endl;
cout << "\nOr ..." << endl;
cout << yearStr<< "-"<< monthNumStr << "-"<< dayStr<< " "<<timeStr<<endl;
system("pause");
return 0;
}
#13
Re: How to use ctime functions(time.h)
Posted 23 September 2008 - 02:55 PM
sergio1, on 22 Sep, 2008 - 05:47 PM, said:
SYSTEMTIME test; GetLocalTime(&test); // makes sure its GetLocalTime(&test) and not GetsystemTime(&test) ...
// Another variation of date and time in C++ ...
#include <string>
#include <iostream>
#include <iomanip> // for setw(), setfill()
#include <windows.h> // for SYSTEMTIME
using namespace std;
const string dayOfWeekName[] =
{
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
int main()
{
SYSTEMTIME t;
// makes sure its GetLocalTime(&test) and not GetsystemTime(&test)
GetLocalTime(&t);
int day = t.wDay;
int month = t.wMonth;
int dayOfWeekNum = t.wDayOfWeek;
int year = t.wYear;
int hour = t.wHour;
int minute = t.wMinute;
int second = t.wSecond;
int adjHour = hour;
string ampm = " AM";
if (hour >= 12)
{
adjHour = hour - 12;
ampm=" PM";
}
cout << "Today is " << dayOfWeekName[dayOfWeekNum] << " "
<< setfill('0') // changes default '' until reset...
<< setw(2) << day << "-" // setw(x) needed for each ...
<< setw(2) << month << "-" << year; // year is already 4 digits
cout << " and the local time right now is "
<< setw(2) << adjHour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second << ampm <<". " << endl;
system("pause");
}
#14
Re: How to use ctime functions(time.h)
Posted 27 November 2009 - 02:16 PM
// Demo of C++ way to get date/time info and ...
// format as you like ( using #include <ctime> )
// Demo of table lookup and using stringstream
/*
For BEGINNING COMPUTER PROGRAMMING using HLA, Python, C/C++
goto [url="http://developers-heaven.net/forum/index.php/topic,46.0.html"]http://developers-heaven.net/forum/index.php/topic,46.0.html[/url]
*/
#include <iostream>
#include <ctime>
#include <sstream>
using namespace std;
const string monthsTable[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
const string daysTable[] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
// If pass in 'Fri' ... finds 'n=5' and returns the string 'Friday' ...
string getLongWeekDayStr( string dStr )
{
for( int n=0; n<7; ++n )
if(daysTable[n] == dStr )
return daysTable[7+n];
return "";
}
// If pass in 'Sep' ... finds 'n=8' and returns the string '09' ...
string getMonthNumStr( string mStr )
{
for( int n=0; n<12; ++n )
if(monthsTable[n] == mStr )
return monthsTable[12+n];
return "";
}
// If pass in 'Sep' finds 'n=8' and returns the string 'September'
string getLongMonthStr( string mStr )
{
for( int n=0; n<12; ++n )
if(monthsTable[n] == mStr )
return monthsTable[24+n];
return "";
}
int main()
{
time_t now = time(0);
stringstream ss1;
ss1 << ctime(&now); // "Fri Nov 27 16:22:24 2009"
string weekDayStr, monthStr, mDayNumStr, timeStr, yearNumStr;
// and ... picking apart ss1 gives ...
ss1 >> weekDayStr >> monthStr >> mDayNumStr >> timeStr >> yearNumStr;
// and adding some more formats to be available for output gives ...
// string monthNumStr = getMonthNumStr(monthStr); // 'Sep' gives '09'
// string mLongNameStr = getLongMonthStr(monthStr); // 'Sep' gives 'September
// convert mDayNumStr to an int to get rid of leading zeros
stringstream ss2;
ss2 << mDayNumStr;
int dayNum;
ss2 >> dayNum;
cout << "Note order ... weekDay, month, mDayNum, time, yearNum ... in the\n"
<< "following string: " << ss1.str() << endl;
cout << "\nNow, picking apart the string, we can reformat as ...\n\n"
<< getLongWeekDayStr( weekDayStr ) << ", "
<< getLongMonthStr( monthStr ) << " "
<< dayNum << ", "
<< yearNumStr<< " "
<< timeStr
<< "\n\n or ...\n\n"
<< yearNumStr << "-"
<< getMonthNumStr( monthStr ) << "-"
<< mDayNumStr << " "
<< timeStr<<endl;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
}
and this ...
// Another variation of date and time in C++ ...
#include <string>
#include <iostream>
#include <iomanip> // for setw(), setfill()
#include <windows.h> // for SYSTEMTIME
using namespace std;
const string dayOfWeekName[] =
{
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
const string monthName[] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int main()
{
SYSTEMTIME t;
// makes sure it's GetLocalTime(&t) and not GetsystemTime(&t)
GetLocalTime(&t);
int year = t.wYear;
int month = t.wMonth;
int day = t.wDay;
int dayOfWeek = t.wDayOfWeek;
int hour = t.wHour;
int minute = t.wMinute;
int second = t.wSecond;
int adjHour = hour;
string ampm = " AM";
if (hour >= 12)
{
adjHour = hour - 12;
ampm=" PM";
}
cout << "The local date and time:\n\n"
<< dayOfWeekName[dayOfWeek] << ", "
<< monthName[month] << " "
<< day << ", "
<< year << " "
<< setfill('0') // changes default '' until reset...
<< setw(2) << adjHour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second << ampm
<< "\n\n or ... \n\n"
<< year << "-" // year is already 4 digits
<< setw(2) << month << "-" // setw(x) needed for each ...
<< setw(2) << day << " "
<< setw(2) << hour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second << endl;
cout << "\nPress 'Enter' to continue ... " << flush;
cin.get();
}
This post has been edited by David W: 27 November 2009 - 08:29 PM

Ask A New Question
Reply





MultiQuote





|