how to use the ctime function in C++?
can someone explain in simple terms how to actually use the following 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
number87
Junior Poster in Training
83 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
chunalt787
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 39
Solved Threads: 1
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;
number87
Junior Poster in Training
83 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
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;
}
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341