943,777 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7594
  • C++ RSS
Oct 13th, 2008
0

how to use the ctime function in C++?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Oct 13th, 2008
0

Re: 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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 13th, 2008
0

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

Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 14th, 2008
0

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

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;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Oct 14th, 2008
0

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

I believe declarations and definitions are allowed outside main, but assignments are not.

This example compiles fine--

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 14th, 2008
0

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

Alex: using stringstream as you did is not necessary -- cout already knows how to do it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 14th, 2008
0

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

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--

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 14th, 2008
0

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

Here's how -- use setfill() and setw()
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 14th, 2008
0

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

Thanks! =)

And just for aesthetics O_O --

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 14th, 2008
0

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

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.

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: resize control
Next Thread in C++ Forum Timeline: beginner question on loops.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC