how to use the ctime function in C++?

Reply

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

how to use the ctime function in C++?

 
0
  #1
Oct 13th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to use the ctime function in C++?

 
1
  #2
Oct 13th, 2008
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.
  1. time_t now = time(0); // get current time
  2. struct tm* tm = localtime(&now); // get struct filled out
  3.  
  4. cout << "Today is "
  5. << tm->tm_mon+1 // month
  6. << "/" << tm->tm_mday // day
  7. << "/" << tm->tm_year + 1900 // year with century
  8. << " " << tm->tm_hour // hour
  9. << ":" << tm->tm_min // minute
  10. << ":" << 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: how to use the ctime function in C++?

 
0
  #3
Oct 13th, 2008
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: how to use the ctime function in C++?

 
0
  #4
Oct 14th, 2008
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;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: how to use the ctime function in C++?

 
0
  #5
Oct 14th, 2008
I believe declarations and definitions are allowed outside main, but assignments are not.

This example compiles fine--

  1. #include <iostream>
  2. #include <ctime>
  3. #include <sstream>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::ostream;
  9. using std::stringstream;
  10.  
  11. ostream& operator << (ostream&, const tm&);
  12.  
  13. tm mytime; // not quite safe
  14.  
  15. int main(){
  16.  
  17.  
  18. // tm mytime; // safer implementation
  19. mytime.tm_mday = 10;
  20. mytime.tm_mon = 5;
  21. mytime.tm_year = 2008;
  22. mytime.tm_hour = 3;
  23. mytime.tm_min = 8;
  24. mytime.tm_sec = 30;
  25.  
  26. cout << mytime << endl;
  27. cin.ignore();
  28. cin.get();
  29.  
  30. return 0;
  31. }
  32.  
  33. ostream& operator<<(ostream& out, const tm& theTime){
  34. stringstream ss1 (stringstream::in | stringstream::out);
  35. stringstream ss2 (stringstream::in | stringstream::out);
  36. ss1 << 0 << theTime.tm_min;
  37. ss2 << theTime.tm_min;
  38. out << theTime.tm_mon << "/" << theTime.tm_mday << "/" << theTime.tm_year;
  39. out << "\t" << theTime.tm_hour << ":" << ((theTime.tm_min < 10 )
  40. ? ss1.str() : ss2.str() ) << ":" << theTime.tm_sec;
  41. return out;
  42. }
Last edited by Alex Edwards; Oct 14th, 2008 at 7:26 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to use the ctime function in C++?

 
0
  #6
Oct 14th, 2008
Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: how to use the ctime function in C++?

 
0
  #7
Oct 14th, 2008
Originally Posted by Ancient Dragon View Post
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--

  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7. using std::ostream;
  8.  
  9. ostream& operator << (ostream&, const tm&);
  10.  
  11. tm mytime; // not quite safe
  12.  
  13. int main(){
  14.  
  15.  
  16. // tm mytime; // safer implementation
  17. mytime.tm_mday = 10;
  18. mytime.tm_mon = 5;
  19. mytime.tm_year = 2008;
  20. mytime.tm_hour = 3;
  21. mytime.tm_min = 8;
  22. mytime.tm_sec = 30;
  23.  
  24. cout << mytime << endl;
  25. cin.ignore();
  26. cin.get();
  27.  
  28. return 0;
  29. }
  30.  
  31. ostream& operator<<(ostream& out, const tm& theTime){
  32. out << theTime.tm_mon << "/" << theTime.tm_mday << "/" << theTime.tm_year;
  33. out << "\t" << theTime.tm_hour << ":" << (theTime.tm_min < 10 ? "0": "") << theTime.tm_min << ":";
  34. out << (theTime.tm_sec < 10 ? "0": "") << theTime.tm_sec;
  35. return out;
  36. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to use the ctime function in C++?

 
1
  #8
Oct 14th, 2008
Here's how -- use setfill() and setw()
  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::ostream;
  9. using std::setw;
  10. using std::setfill;
  11.  
  12. ostream& operator << (ostream&,const tm&);
  13.  
  14. tm mytime; // not quite safe
  15.  
  16. int main(){
  17.  
  18.  
  19. // tm mytime; // safer implementation
  20. mytime.tm_mday = 10;
  21. mytime.tm_mon = 5;
  22. mytime.tm_year = 2008;
  23. mytime.tm_hour = 3;
  24. mytime.tm_min = 8;
  25. mytime.tm_sec = 30;
  26.  
  27. cout << mytime << endl;
  28. cin.ignore();
  29. cin.get();
  30.  
  31. return 0;
  32. }
  33.  
  34. ostream& operator<<(ostream& out,const tm& theTime){
  35. out << setfill('0') << setw(2) << theTime.tm_mon << "/"
  36. << setw(2) << theTime.tm_mday << "/"
  37. << setw(4) << theTime.tm_year;
  38. out << setfill('0') << setw(2) << "\t"
  39. << setw(2) << theTime.tm_hour << ":"
  40. << setw(2) << theTime.tm_min << ":";
  41. out << setfill('0') << setw(2) << theTime.tm_sec;
  42. return out;
  43. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: how to use the ctime function in C++?

 
0
  #9
Oct 14th, 2008
Thanks! =)

And just for aesthetics O_O --

  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::ostream;
  9. using std::setw;
  10. using std::setfill;
  11.  
  12. ostream& operator << (ostream&,const tm&);
  13.  
  14. tm mytime; // not quite safe
  15.  
  16. int main(){
  17.  
  18.  
  19. // tm mytime; // safer implementation
  20. mytime.tm_mday = 10;
  21. mytime.tm_mon = 5;
  22. mytime.tm_year = 2008;
  23. mytime.tm_hour = 3;
  24. mytime.tm_min = 8;
  25. mytime.tm_sec = 30;
  26.  
  27. cout << mytime << endl;
  28. cin.ignore();
  29. cin.get();
  30.  
  31. return 0;
  32. }
  33.  
  34. ostream& operator<<(ostream& out,const tm& theTime){
  35. out << setfill('0') << setw(2) << theTime.tm_mon << "/"
  36. << setw(2) << theTime.tm_mday << "/"
  37. << setw(4) << theTime.tm_year;
  38. out << "\t"<< setw(2) << setfill('0')
  39. << setw(2) << theTime.tm_hour << ":"
  40. << setw(2) << theTime.tm_min << ":";
  41. out << setfill('0') << setw(2) << theTime.tm_sec;
  42. return out;
  43. }
Last edited by Alex Edwards; Oct 14th, 2008 at 9:52 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: how to use the ctime function in C++?

 
0
  #10
Oct 14th, 2008
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.

  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. double diffclock(time_t clock1,time_t clock2) //Using time_t & difftime
  5. {
  6. double diffticks=difftime(clock1,clock2);
  7. double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
  8. return diffms;
  9. }
  10.  
  11. void ShowTheDate() //Using time_t & struct tm
  12. {
  13. time_t now = time(0); // get current time: sing time_t
  14. struct tm* tm = localtime(&now); // get struct filled out
  15. std::cout << "Today's exact date and time is " //Dragon's code works outside main
  16. << tm->tm_mon+1 // month
  17. << "/" << tm->tm_mday // day
  18. << "/" << tm->tm_year + 1900 // year with century
  19. << " " << tm->tm_hour // hour
  20. << ":" << tm->tm_min // minute
  21. << ":" << tm->tm_sec<<std::endl; // second
  22. }
  23. void FindWhichDayWillItbe (int days, int hours) //using mktime
  24. {
  25. //the user gives the number of days and hours, and this function finds what day
  26. //is it going to be after these days and hours pass
  27. char *wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
  28. "Thursday", "Friday", "Saturday" };
  29. time_t t1, t3;
  30. struct tm *t2;
  31. t1 = time(0);
  32. t2 = localtime(&t1);
  33. t2 -> tm_mday += days;
  34. t2 -> tm_hour += hours;
  35. t3 = mktime(t2);
  36. std::cout<<days<<" days and "<<hours<<" hours from now, it will be a "
  37. <<wday[t2 -> tm_wday];
  38. }
  39.  
  40. int main()
  41. {
  42. //1st Part
  43. time_t startTime,endTime;
  44. double elapsedTime;
  45. startTime = clock();
  46. for (int i = 0; i<1000;++i)
  47. {
  48. std::cout<<"hi 1000 times"<<std::endl;
  49. }
  50. endTime = clock();
  51. elapsedTime = diffclock(endTime,startTime);
  52. std::cout<<"The execution time of the for loop in msecs is = "<<elapsedTime<<std::endl;
  53. //2nd part
  54. ShowTheDate();
  55. //3rd part
  56. int noDays = 0;
  57. int noHours = 0;
  58. std::cout<<"Lets find out what day is it going to be after n days and x hours: "<<std::endl;
  59. std::cout<<"Give number of days: "<<std::endl;
  60. std::cin>>noDays;
  61. std::cout<<"Give number of hours: "<<std::endl;
  62. std::cin>>noHours;
  63. FindWhichDayWillItbe(noDays,noHours);
  64. return 0;
  65. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC