| | |
how to use the ctime function in C++?
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Oct 13th, 2008 at 7:47 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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;
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--
This example compiles fine--
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Alex Edwards; Oct 14th, 2008 at 7:26 am.
•
•
•
•
Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
Here's a better version--
c++ Syntax (Toggle Plain Text)
#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()
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Oct 14th, 2008 at 8:32 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Thanks! =)
And just for aesthetics O_O --
And just for aesthetics O_O --
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Alex Edwards; Oct 14th, 2008 at 9:52 am.
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.
Moreover I have to say that Ancient Dragon-s code works both inside and outside the main.
c++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Recursion Function Problem (C++)
- error C2447: '{' : missing function header (old-style formal list?) (C++)
- Using the rand() function in C (C)
- Function Back to Main (C++)
- can anyone assiat in getting my function to work? (C)
Other Threads in the C++ Forum
- Previous Thread: resize control
- Next Thread: beginner question on loops.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






