can someone explain in simple terms how to actually use the following <ctime> functions? I have read thru a few C++ reference websites but the explainations given are very vague and complex.
difftime
struct tm
time_t
mktime
ctime
can someone explain in simple terms how to actually use the following <ctime> functions? I have read thru a few C++ reference websites but the explainations given are very vague and complex.
difftime
struct tm
time_t
mktime
ctime
struct tm: is just a structure that can be use like any other structure. The localtime() function takes a time_t object and returns a pointer to a struct tm.
time_t now = time(0); // get current time
struct tm* tm = localtime(&now); // get struct filled out
cout << "Today is "
<< tm->tm_mon+1 // month
<< "/" << tm->tm_mday // day
<< "/" << tm->tm_year + 1900 // year with century
<< " " << tm->tm_hour // hour
<< ":" << tm->tm_min // minute
<< ":" << tm->tm_sec; // second
mktime: does just the opposite of localtime(). It takes a struct tm object and converts it to time_t. In addition it may also normalize the struct tm object. Lets say you want to get a struct tm and time_t for some future date, say one month from now. To do that, you first call localtime() as shown above, add 30 days to the tm_mon structure member, then call mktime() to normalize the structure members. mktime() will take care of insuring the structure contains the correct month, day and year (such as for month and year roll-overs).
difftime: simply returns the difference between two time_t objects. See example program here.
thanks Ancient Dragon for explaining.
however i tried creating a tm structure outside the int main() function and it always doesnt work. It seems to only work in int main(). I can't figure out why.
i declared the day mon yr etc as INT types. The compiler doesnt compile if the below is outside the int main().
Example:
tm mytime;
mytime.tm_mday = day;
mytime.tm_mon = mon;
mytime.tm_year = yr;
mytime.tm_hour = hr;
mytime.tm_min = mm;
mytime.tm_sec = 0;
I believe declarations and definitions are allowed outside main, but assignments are not.
This example compiles fine--
#include <iostream>
#include <ctime>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::stringstream;
ostream& operator << (ostream&, const tm&);
tm mytime; // not quite safe
int main(){
// tm mytime; // safer implementation
mytime.tm_mday = 10;
mytime.tm_mon = 5;
mytime.tm_year = 2008;
mytime.tm_hour = 3;
mytime.tm_min = 8;
mytime.tm_sec = 30;
cout << mytime << endl;
cin.ignore();
cin.get();
return 0;
}
ostream& operator<<(ostream& out, const tm& theTime){
stringstream ss1 (stringstream::in | stringstream::out);
stringstream ss2 (stringstream::in | stringstream::out);
ss1 << 0 << theTime.tm_min;
ss2 << theTime.tm_min;
out << theTime.tm_mon << "/" << theTime.tm_mday << "/" << theTime.tm_year;
out << "\t" << theTime.tm_hour << ":" << ((theTime.tm_min < 10 )
? ss1.str() : ss2.str() ) << ":" << theTime.tm_sec;
return out;
}
Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
Do I need to implement some header for an output implementation? I was getting weird results without using some defensive measures, and I'm tired @_@.
Here's a better version--
#include <iostream>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
ostream& operator << (ostream&, const tm&);
tm mytime; // not quite safe
int main(){
// tm mytime; // safer implementation
mytime.tm_mday = 10;
mytime.tm_mon = 5;
mytime.tm_year = 2008;
mytime.tm_hour = 3;
mytime.tm_min = 8;
mytime.tm_sec = 30;
cout << mytime << endl;
cin.ignore();
cin.get();
return 0;
}
ostream& operator<<(ostream& out, const tm& theTime){
out << theTime.tm_mon << "/" << theTime.tm_mday << "/" << theTime.tm_year;
out << "\t" << theTime.tm_hour << ":" << (theTime.tm_min < 10 ? "0": "") << theTime.tm_min << ":";
out << (theTime.tm_sec < 10 ? "0": "") << theTime.tm_sec;
return out;
}
Here's how -- use setfill() and setw()
#include <iostream>
#include <ctime>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::setw;
using std::setfill;
ostream& operator << (ostream&,const tm&);
tm mytime; // not quite safe
int main(){
// tm mytime; // safer implementation
mytime.tm_mday = 10;
mytime.tm_mon = 5;
mytime.tm_year = 2008;
mytime.tm_hour = 3;
mytime.tm_min = 8;
mytime.tm_sec = 30;
cout << mytime << endl;
cin.ignore();
cin.get();
return 0;
}
ostream& operator<<(ostream& out,const tm& theTime){
out << setfill('0') << setw(2) << theTime.tm_mon << "/"
<< setw(2) << theTime.tm_mday << "/"
<< setw(4) << theTime.tm_year;
out << setfill('0') << setw(2) << "\t"
<< setw(2) << theTime.tm_hour << ":"
<< setw(2) << theTime.tm_min << ":";
out << setfill('0') << setw(2) << theTime.tm_sec;
return out;
}
Thanks! =)
And just for aesthetics O_O --
#include <iostream>
#include <ctime>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::setw;
using std::setfill;
ostream& operator << (ostream&,const tm&);
tm mytime; // not quite safe
int main(){
// tm mytime; // safer implementation
mytime.tm_mday = 10;
mytime.tm_mon = 5;
mytime.tm_year = 2008;
mytime.tm_hour = 3;
mytime.tm_min = 8;
mytime.tm_sec = 30;
cout << mytime << endl;
cin.ignore();
cin.get();
return 0;
}
ostream& operator<<(ostream& out,const tm& theTime){
out << setfill('0') << setw(2) << theTime.tm_mon << "/"
<< setw(2) << theTime.tm_mday << "/"
<< setw(4) << theTime.tm_year;
out << "\t"<< setw(2) << setfill('0')
<< setw(2) << theTime.tm_hour << ":"
<< setw(2) << theTime.tm_min << ":";
out << setfill('0') << setw(2) << theTime.tm_sec;
return out;
}
Hi I post u a code where u can see used all those u mentioned (ctime, time_t, difftime,struct tm, mktime).
Moreover I have to say that Ancient Dragon-s code works both inside and outside the main.
#include <iostream>
#include <ctime>
double diffclock(time_t clock1,time_t clock2) //Using time_t & difftime
{
double diffticks=difftime(clock1,clock2);
double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
return diffms;
}
void ShowTheDate() //Using time_t & struct tm
{
time_t now = time(0); // get current time: sing time_t
struct tm* tm = localtime(&now); // get struct filled out
std::cout << "Today's exact date and time is " //Dragon's code works outside main
<< tm->tm_mon+1 // month
<< "/" << tm->tm_mday // day
<< "/" << tm->tm_year + 1900 // year with century
<< " " << tm->tm_hour // hour
<< ":" << tm->tm_min // minute
<< ":" << tm->tm_sec<<std::endl; // second
}
void FindWhichDayWillItbe (int days, int hours) //using mktime
{
//the user gives the number of days and hours, and this function finds what day
//is it going to be after these days and hours pass
char *wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
time_t t1, t3;
struct tm *t2;
t1 = time(0);
t2 = localtime(&t1);
t2 -> tm_mday += days;
t2 -> tm_hour += hours;
t3 = mktime(t2);
std::cout<<days<<" days and "<<hours<<" hours from now, it will be a "
<<wday[t2 -> tm_wday];
}
int main()
{
//1st Part
time_t startTime,endTime;
double elapsedTime;
startTime = clock();
for (int i = 0; i<1000;++i)
{
std::cout<<"hi 1000 times"<<std::endl;
}
endTime = clock();
elapsedTime = diffclock(endTime,startTime);
std::cout<<"The execution time of the for loop in msecs is = "<<elapsedTime<<std::endl;
//2nd part
ShowTheDate();
//3rd part
int noDays = 0;
int noHours = 0;
std::cout<<"Lets find out what day is it going to be after n days and x hours: "<<std::endl;
std::cout<<"Give number of days: "<<std::endl;
std::cin>>noDays;
std::cout<<"Give number of hours: "<<std::endl;
std::cin>>noHours;
FindWhichDayWillItbe(noDays,noHours);
return 0;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.