-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.cpp
129 lines (115 loc) · 4.04 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
#include "Time.h"
// Constructor
Time::Time() {
hour = 0;
minutes = 0;
time_ = "";
}
// Set Time Function
void Time::set_time(string t) {
if (t[1] == ':') { //if number of hours are less than 10
// hour
hour = t[0] - 48; //hours will be equal to ascii of 1st character minus 48
// minutes
minutes = t[2] - 48; //minutes will be calculated from the asciis of 3rd and 4th character of string
minutes *= 10;
minutes += t[3] - 48;
time_ = t; //simply equate the strings
}
else { //else hours will be doubly digit
// hour
hour = t[0] - 48; //hours will be calculated from ascii of 1st two characters of string
hour *= 10;
hour += t[1] - 48;
// minutes
minutes = t[3] - 48; //minutes will be calculated from the asci of 4th and 5th charater of string
minutes *= 10;
minutes += t[4] - 48;
time_ = t; //simply equate the time strings
}
}
// Check Time Function
// Compares the time returns
// 1 if same, 2 if less, 3 if greater
int Time::check_time(const Time& obj) {
if (hour < obj.hour) //if number of hours are smaller means time is lesser
return 2;
else if (hour > obj.hour) //if number of hours is greater means time is greater
return 3;
else if (hour == obj.hour) { //if hours are same than check the minutes
if (minutes == obj.minutes) //if minutes are also same then it means both the times are equal
return 1;
else if (minutes < obj.minutes)
return 2;
else if (minutes > obj.minutes)
return 3;
}
}
// Add Time Function
void Time::add_time(const Time obj) { //function to add time
hour += obj.hour; //add the hours of both objects
minutes += obj.minutes; //add the minutes of both objects
if (minutes >= 60) { //if minutes are greater than 60 increment hours and retain the minutes
minutes -= 60;
hour++;
}
update_time_str(); //function to update the string of time
}
// Sub Time Function
void Time::sub_time(const Time obj) { //function to subtratct time
hour -= obj.hour; //subtracting the hours
minutes -= obj.minutes; //subtracting the minutes
if (minutes < 0) { //if minutes are less than zero decrement in hours and retain the minutes
minutes += 60;
hour--;
}
update_time_str(); //update the string of time
}
// Update Time String Function
void Time::update_time_str() { //function to update the string of time
stringstream hh, mm;
hh << hour;
string temp_h = hh.str(); //converting the hours from integer to string
temp_h.append(":"); //appending : afetr hours
string temp_m;
if (minutes < 10) { //if minutes are less than 10
if (minutes == 0) //if minutes are 0 repalce space of minutes by 00
temp_m = "00";
else {
mm << minutes;
temp_m = mm.str();
temp_m.append("0"); //else they will be in range of 1-9 so left most bit will be 0
}
}
else {
mm << minutes;
temp_m = mm.str(); //else convert the minutes from integre tostrings
}
temp_h.append(temp_m); //now appending minutes in the time
time_ = temp_h;
}
// Add 24 Hours Function
// add 24 hours to the time
void Time::add_24Hours() {
hour += 24;
update_time_str(); //adjusting the time after adding 24 hours
}
// Sub From 24 Hour Function
// subtracts time from 24 hours
void Time::sub_from_24Hour() {
Time temp;
temp.set_time("24:00");
sub_time(temp); //subtracting the time from 24:00
}
// Get Hours Function
int Time::get_hours() {
return hour; //returning no of hours
}
// Get Minutes Function
int Time::get_minutes() {
return minutes; //returning number of minutes
}
// Time To Str Function
string Time::time_to_str() {
return time_; //returning string of time
}