-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.cpp
96 lines (76 loc) · 1.62 KB
/
Time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Time.h"
#include"UI.h"
//get Day then Hour From Constructor
Time::Time(int d, int h) {
setDay(d);
setHour(h);
}
//.......setters......//
void Time::setHour(int h) {
// handelling exceeding the 24 hours
while (h > 24)
{
h -= 24;
day++;
}
hour = h;
}
void Time::setDay(int d) {
day = d;
}
//......getters......//
int Time::getHour() const { return hour; }
int Time::getDay() const { return day; }
//operators
bool Time::operator<(const Time& t) const {
if (day == t.day) //if the same compare the hour
return (hour < t.hour);
else return (day < t.day); // else compare day
}
// adding 2 times
Time Time::operator+(const Time& t) const
{
Time t1 = *this;
t1.setDay(t.getDay() + day);
t1.setHour(t.getHour() + hour);
return t1;
}
// adding time and hours
Time Time::operator+(const int x) const
{
Time t1 = *this;
t1.setHour(hour + x);
return t1;
}
bool Time::operator==(const Time& t)const {
return (day == t.day && hour == t.hour);
}
bool Time::operator<=(const Time& t) const
{
return (*this < t) || (*this == t);
}
// return number of hours
Time Time::operator-(const Time& t)const {
if (day > t.day) {
return Time(0, (day - t.day) * 24 + hour - t.hour);
}
return Time(0, hour - t.hour);
}
// increase hour by 1 and
//if pass 24 hr increase the day 1
void Time::Update(){
setHour(hour + 1); // in set Hour it handel if it enter new day
}
void Time::Read(ifstream& InFile) {
char buff;
InFile >> day >> buff >> hour;
}
string Time::TimePrint() const
{
return (to_string(day) + ":" + to_string(hour));
}
// convert the time to hours(int)
int Time::ConvertToInt() const
{
return (day*24+hour);
}