Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I subtract 2 date fields?? 1

Status
Not open for further replies.

ssudha17

Programmer
Feb 20, 2001
21
0
0
US
Hi,
In C++ I want to add time (in hours and minutes) to a date field containing the time in hours and minutes along with the date. Also I want to find the difference in time (in minutes) between 2 given dates(with time). How can I do that? Is there any class available or library available in C++ that would ease the job of creating a new one of my own and overriding the operators?
Thanks
Rgds,
Sudha Visit to know more about me
 
You could create your own class with functions GetTime, ConvertTime, DisplayTime and 4 private data members:
[ul]
[li]Days
[li]Hours
[li]Minutes
[li]Total_Minutes
[/ul]
Two constructors, one with no parameters and another to initialize all data members.
You could make a friend function that would overload - operator.
In your main function you have two instances of your class, to sunstract one field from another:
...
diff = Instance1.Total_Minutes - Instance.Total_Minutes;
...

friend int operator - (Your_Class var1, Your_Class var2)
{
return (Total_Minutes - var2.Total_Minutes);
} Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
The problem is that if I add 15 hours to 23:30, it will be a different calculation as the date also changes. how do i keep track of that?
rgds,
Sudha Visit to know more about me
 
1) When you add hours, you have to take modulo 24 and reassign the remainder to the TotalHours
if you add only minutes, take modulo 60 and so on.
2) Declare another variable, change. When you add something you will need to do this :

change = AddedValue div 24;

You check if it equals to one then you add another hour. The same procedure for days and minutes Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
I have created a Time class as suggested by u..Thank you so so much for the post.Cant tell u how much it helped me
This is my file
#ifndef TIME_H
#define TIME_H
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
class Time
{
private:
int hours,minutes;
char *time;
public:
Time()
{
int hours=minutes=0;
}
Time(char *tim)
{
char *min=strpbrk(tim,&quot;:&quot;);
*min++='\0';
minutes=atoi(min);
hours=atoi(tim);
cout<<&quot; the hours passed is&quot;<<hours<<&quot; and minutes passed is &quot;<<minutes<<endl;
}
int operator- (Time t1)
{
if(hours<t1.hours||((hours==t1.hours)&&(minutes<t1.minutes)))
{
cerr<<&quot;the calling time time is lesser than called time&quot;;
return 0;
}
if (minutes<t1.minutes)
{
minutes+=60;

}

// cout<<&quot;value of called minutes is&quot;<<t1.minutes<<endl;
int mindiff=minutes-t1.minutes;
//cout<<&quot;calling hour=&quot;<<hours<<endl;
//cout<<&quot;called hour=&quot;<<t1.hours<<endl;
int hourdiff;
if (minutes<t1.minutes)
hourdiff=hours-t1.hours-1;
else
hourdiff=hours-t1.hours;
//cout<<&quot;difference in hours=&quot;<<hourdiff<<endl;
int diffinmin=((hourdiff*60)+mindiff);
return diffinmin;
}
Time operator+(Time t2)
{
Time sum;
sum.minutes=minutes+t2.minutes;
if(sum.minutes>60)
{
sum.minutes-=60;
sum.hours=sum.hours+1;
}
sum.hours=sum.hours+hours+t2.hours;
if (sum.hours>24)
{
sum.hours=sum.hours-24;
}
return sum;
}
friend ostream& operator<< (ostream&,Time& t3)
{
cout<<&quot;The time is &quot;<<setfill('0')<<setw(2)<<t3.hours<<&quot;:&quot;<<setfill('0')<<setw(2)<<t3.minutes<<endl;
}

};
#endif

I am having a city that has time as one of its private data. So can I have a constructor that takes a Time datatype? If yes, later, how do I access the private data members of Time? Can I have something like the following?
create object of
Time time inside City class
City *c1;
Time t=c1->time ?
ANd if this is the way, how do I retrieve the data in time? Can I use a double arrow?c1->time->hours;
Sorry if I am giving u a lot of data to think over.
Even if u r not able to find time to answer my maybe stupid question, I am very grateful to u for ur help to get me started in thinking in OO terms.
Rgds,
Sudha Visit to know more about me
 
You could make your Time class a friend of City. So that a public function of City could access the private data of the Time.
You could have a int variable, for example, int CityTime;
Then, CityTime = c1->GetTime();
Where c1 is a pointer to your City object, and GetTime() is its function that can retreive value of TotalTime from Time object if it were friend.
To make one class friend of the other in the public section of its declaration you should have:
...
public:
friend class City
...
this will make Time friend of City. Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top