-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTime.cpp
218 lines (175 loc) · 5.41 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "Time.h" // class implemented
#include<iomanip>
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Time Default + Overloaded Constructor
Time::Time(int aHour, int aMinute, int aSecond) : mHour(aHour), mMinute(aMinute), mSecond(aSecond) {
if (aHour == -999 && aMinute == -999 && aSecond == -999)
this->SetTime(Time::sGetDefaultTime());
else
this->SetTime(aHour, aMinute, aSecond);
}
// end Time constructor
//============================= OPERATORS ====================================
// Stream Insertion
ostream& operator<<(ostream& os, const Time& t) {
os << setfill('0') << setw(2) << t.GetHour() << ":" << setw(2) << t.GetMinute() << ":" << setw(2) << t.GetSecond();
return os;
}
// end stream insertion
// Stream Extraction
istream& operator >>(istream& is, Time& t) {
int hh, mm, ss;
cout << "Enter Hour: ";
is >> hh;
cout << "Enter Minute: ";
is >> mm;
cout << "Enter Second: ";
is >> ss;
t.SetTime(hh, mm, ss);
return is;
}
// end stream extraction
// Assignment operator to swap time.
Time& Time::operator =(Time& rhs) {
Time t = *this;
this->SetTime(rhs.GetTime());
rhs.SetTime(t.GetTime());
return *this;
}
// Assignment operator
// pre-increment operator
Time& Time::operator ++() {
if (this->GetHour() == 23) {
this->SetHour(0);
}
else {
++mHour;
}
return *this;
}
// pre-increment
// post-increment operator
Time Time::operator ++ (int) {
Time t = *this;
if (this->GetHour() == 23) {
this->SetHour(0);
}
else {
this->mHour++;
}
return t;
}
// end post-increment
//============================= OPERATIONS ===================================
// function that displays time in 24 hours format.
void Time::Display24()const {
cout << setfill('0') << setw(2) << this->GetHour() << ":" << setw(2) << this->GetMinute() << ":" << setw(2) << this->GetSecond() << endl;
}
// end function Display24
// function that displays time in 12 hours format.
void Time::Display12()const {
cout << setfill('0') << setw(2) << ((this->GetHour() == 0 || this->GetHour() == 12) ? 12 : this->GetHour() % 12) << ":" << setw(2) << this->GetMinute() << ":" << setw(2) << this->GetSecond() << " " << ((this->GetHour()<12) ? "AM" : "PM") << endl;
}
// end function Display12
// function that adds 'X' no. of minutes to the Time object
void Time::AddMinute(int x) {
int tMin = this->GetMinute(), tHour = this->GetHour();
tMin += x % 60;
tHour += x / 60;
if (tMin > 59) {
tMin -= 60;
tHour++;
}
this->SetMinute(tMin);
this->SetHour(tHour);
}
// end function AddMinute
//============================= ACESS ===================================
// function that sets hour
void Time::SetHour(int aHour) {
if (aHour < 0 || aHour>23)
this->mHour = 0;
else
this->mHour = aHour;
}
// end function SetHour
// function that sets minute
void Time::SetMinute(int aMinute) {
if (aMinute < 0 || aMinute>59)
this->mMinute = 0;
else
this->mMinute = aMinute;
}
// end function SetMinute
// function that sets second
void Time::SetSecond(int aSecond) {
if (aSecond < 0 || aSecond>59)
this->mSecond = 0;
else
this->mSecond = aSecond;
}
// end function SetSecond
// function that sets the Time
void Time::SetTime(int aHour, int aMinute, int aSecond) {
this->SetHour(aHour);
this->SetMinute(aMinute);
this->SetSecond(aSecond);
}
// end function SetTime
// function that sets the Time
void Time::SetTime(const Time &obj) {
this->SetTime(obj.GetHour(), obj.GetMinute(), obj.GetSecond());
}
// end function SetTime
// static function that sets default time
void Time::sSetDefaultTime(int aHour, int aMinute, int aSecond) {
if (aHour == -999 && aMinute == -999 && aSecond == -999)
msDefaultTime.SetTime(Time::sGetCurrentTime());
else
msDefaultTime.SetTime(aHour, aMinute, aSecond);
}
// end function sSetDefaultTime
// function that gets hour
int Time::GetHour()const {
return this->mHour;
}
// end function GetHour
// function that gets minute
int Time::GetMinute()const {
return this->mMinute;
}
// end function GetMinute
// function that gets second
int Time::GetSecond()const {
return this->mSecond;
}
// end function GetSecond
// function that gets the Time
const Time& Time::GetTime()const {
return *this;
}
// end function GetTime
// static function that gets default date
const Time& Time::sGetDefaultTime() {
return msDefaultTime;
}
// end function sGetDefaultTime
// static function that gets current time
Time Time::sGetCurrentTime() {
time_t currentTime = time(NULL);
struct tm localTime;
time(¤tTime); // Get the current time
localtime_s(&localTime, ¤tTime); // Convert the current time to the local time
int Hour1 = localTime.tm_hour;
int Minute1 = localTime.tm_min;
int Second1 = localTime.tm_sec;
Time t(Hour1, Minute1, Second1);
return t;
}
// end function sGetCurrentTime
/////////////////////////////// PRIVATE ///////////////////////////////////
/*private static member cannot be accessed outside the class except for initialization*/
Time Time::msDefaultTime(Time::sGetCurrentTime().GetHour(), Time::sGetCurrentTime().GetMinute(), Time::sGetCurrentTime().GetSecond()); // intitalize class variable