Calculating Time Difference

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 6
Reputation: hui is an unknown quantity at this point 
Solved Threads: 0
hui hui is offline Offline
Newbie Poster

Calculating Time Difference

 
0
  #1
Feb 23rd, 2007
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

  1. #include <iostream.h>
  2. #include <cdate.h>
  3.  
  4. class Date
  5.  
  6. {
  7.  
  8. public:
  9.  
  10. int month;
  11.  
  12. int day;
  13.  
  14. int year;
  15.  
  16. static int dtab[2][13];
  17.  
  18. public:
  19. int getMonth()
  20.  
  21. {
  22. return month;
  23. }
  24.  
  25. int getDay()
  26.  
  27. {
  28. return day;
  29. }
  30.  
  31. int getYear()
  32.  
  33. {
  34. return year;
  35. }
  36.  
  37. Date operator-(Date& d2);
  38.  
  39. Date& operator-()
  40. {
  41. month=-month;
  42.  
  43. day=-day;
  44.  
  45. year=-year;
  46.  
  47. return *this;
  48. }
  49. int compare(Date&);
  50.  
  51. int operator<(Date& d2)
  52.  
  53. {
  54.  
  55. return compare{d2) < 0;
  56.  
  57. }
  58.  
  59. int operator<=(const Date& d2)
  60.  
  61. {
  62.  
  63. return compare(d2) <= 0;
  64.  
  65. }
  66.  
  67. int operator>( Date& d2)
  68.  
  69. {
  70.  
  71. return compare(d2) > 0;
  72.  
  73. }
  74.  
  75. int operator>=( Date& d2)
  76.  
  77. {
  78.  
  79. return compare(d2) >= 0;
  80.  
  81. }
  82.  
  83. int operator==( Date& d2)
  84.  
  85. {
  86.  
  87. return compare(d2) == 0;
  88.  
  89. }
  90.  
  91. int operator!=( Date& d2)
  92.  
  93. {
  94.  
  95. return compare(d2) != 0;
  96.  
  97. }
  98.  
  99. static int isleap(int y)
  100.  
  101. {
  102.  
  103. return y%4 == 0 && y%100 != 0 || y%400 == 0;
  104.  
  105. }
  106.  
  107. };
  108.  
  109. int main()
  110.  
  111. {
  112.  
  113. Date today, d2;
  114.  
  115. cout << "Today's date is "<< today << endl;
  116.  
  117. cout << "Enter another date: ";
  118.  
  119. cin >> d2;
  120.  
  121. cout << "today -d2 = "<< today - d2 << endl;
  122.  
  123. cout << "d2 - today = "<< d2 - today << endl;
  124.  
  125. return 0;
  126.  
  127. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Calculating Time Difference

 
0
  #2
Feb 23rd, 2007
#1: You don't need to double space everything. It's too hard to read.
#2: You need to indent consistently, and 3-4 spaces, not 1 or 2
#3: "it is bringing some bit of a problem" has no meaning. What is the problem. Describe it, don't make us guess.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Calculating Time Difference

 
0
  #3
Feb 23rd, 2007
Originally Posted by hui View Post
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

  1. #include <iostream.h>
  2. #include <cdate.h>
  3.  
  4. class Date
  5.  
  6. {
  7.  
  8. public:
  9.  
  10. int month;
  11.  
  12. int day;
  13.  
  14. int year;
  15.  
  16. static int dtab[2][13];
  17.  
  18. public:
  19. int getMonth()
  20.  
  21. {
  22. return month;
  23. }
  24.  
  25. int getDay()
  26.  
  27. {
  28. return day;
  29. }
  30.  
  31. int getYear()
  32.  
  33. {
  34. return year;
  35. }
  36.  
  37. Date operator-(Date& d2);
  38.  
  39. Date& operator-()
  40. {
  41. month=-month;
  42.  
  43. day=-day;
  44.  
  45. year=-year;
  46.  
  47. return *this;
  48. }
  49. int compare(Date&);
  50.  
  51. int operator<(Date& d2)
  52.  
  53. {
  54.  
  55. return compare{d2) < 0;
  56.  
  57. }
  58.  
  59. int operator<=(const Date& d2)
  60.  
  61. {
  62.  
  63. return compare(d2) <= 0;
  64.  
  65. }
  66.  
  67. int operator>( Date& d2)
  68.  
  69. {
  70.  
  71. return compare(d2) > 0;
  72.  
  73. }
  74.  
  75. int operator>=( Date& d2)
  76.  
  77. {
  78.  
  79. return compare(d2) >= 0;
  80.  
  81. }
  82.  
  83. int operator==( Date& d2)
  84.  
  85. {
  86.  
  87. return compare(d2) == 0;
  88.  
  89. }
  90.  
  91. int operator!=( Date& d2)
  92.  
  93. {
  94.  
  95. return compare(d2) != 0;
  96.  
  97. }
  98.  
  99. static int isleap(int y)
  100.  
  101. {
  102.  
  103. return y%4 == 0 && y%100 != 0 || y%400 == 0;
  104.  
  105. }
  106.  
  107. };
  108.  
  109. int main()
  110.  
  111. {
  112.  
  113. Date today, d2;
  114.  
  115. cout << "Today's date is "<< today << endl;
  116.  
  117. cout << "Enter another date: ";
  118.  
  119. cin >> d2;
  120.  
  121. cout << "today -d2 = "<< today - d2 << endl;
  122.  
  123. cout << "d2 - today = "<< d2 - today << endl;
  124.  
  125. return 0;
  126.  
  127. }
http://www.cprogramming.com/
http://www.cplusplus.com/doc/tutorial/
http://www.codersource.net/codersour...ogramming.html

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 23rd, 2007 at 4:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
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: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Calculating Time Difference

 
0
  #4
Feb 23rd, 2007
difference in days between two dates is very easy to calculate.
1. get time_t for first date. If this is not today's date, then fill out a struct tm with day, month-1, year-1900 -- make all other structure members 0, then pass your struct tm object to mktime(), which will convert it to time_t.
2. get time_t for second date
3. subtract them -- this is difference between two dates in seconds
4. there are 24 * 60 * 60 seconds in one day. So take the result of step 3 above and divide by the number of seconds in one day.
5. finished.

  1. #include <iostream.h>
  2. #include <cdate.h>
1. iostream.h is very old and obsolete. current version does not have .h file extension. If you are using an acient compiler such as Turbo C++ then this is ok because your compiler probably does not support the new file naming convention.

2. cdate.h -- there is no such file. use either date.h or cdate (no file extension). This is how it might appear for modern compilers.

  1. #include <iostream>
  2. #include <cdate>
Last edited by Ancient Dragon; Feb 23rd, 2007 at 6:37 pm.
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  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC