-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.cpp
More file actions
143 lines (126 loc) · 2.46 KB
/
Time.cpp
File metadata and controls
143 lines (126 loc) · 2.46 KB
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
#include "Time.hpp"
Time::Time(string get_time)
{
string minute = "", hour = "", type = "";
char dot = ':';
for(int i = 0; i < get_time.size(); i++)
{
if(get_time[i] == dot)
{
for(int j=i+1 ; j<get_time.size()-2 ; j++)
{
minute += get_time[j] ;
}
break;
}
hour+=get_time[i] ;
}
type = type + get_time[get_time.size()-2] + get_time[get_time.size()-1];
//cout << "type ;" << type ;
//cout << "hour ;"<< hour <<"min ;" << minute ;
int sto_h = stoi(hour) ;
int sto_m = stoi(minute) ;
set_hour(sto_h);
set_minute(sto_m);
set_noon(type);
}
void Time::set_hour(int h)
{
if( h > 12 || h < 0)
{
throw invalid_argument("Invalid time(hour) value");
}
else
{
hour = h;
}
}
void Time::set_minute(int m)
{
if( m > 60 || m < 0)
{
throw invalid_argument("Invalid time(minute) value");
}
else
{
minute = m;
}
}
void Time::set_noon(string type)
{
if(type == "am" or type == "AM" or type == "pm" or type == "PM")
{
noon = type;
}
else
{
throw invalid_argument("Invalid time(noon) value");
}
}
void Time::print()
{
cout << hour;
if(minute != 0)
{
cout << ':' << minute << " ";
}
cout << noon << endl;
}
Time Time::operator+=(int min)
{
minute += min;
if(minute >= 60)
{
int count = minute / 60;
minute %= 60;
hour+= count;
if(hour == 12)
{
if(noon == "AM" || noon == "am")
{
noon = "PM";
}
else if(noon == "PM" || noon == "pm")
{
noon = "AM";
}
}
else if(hour > 12 )
{
hour %= 12 ;
}
}
return *this;
}
Time Time::operator+(int min)
{
Time temp;
temp.set_hour(this->get_hour());
temp.set_minute(this->get_minute());
temp.set_noon(this->get_noon());
temp += min;
return temp;
}
istream &operator>>(istream &input, Time &t1)
{
string hour, minute, type;
getline(input, hour, ':');
getline(input, minute, ' ');
getline(input, type);
t1.set_hour(stoi(hour));
t1.set_minute(stoi(minute));
t1.set_noon(type);
return input;
}
int Time::get_hour()
{
return hour;
}
int Time::get_minute()
{
return minute;
}
string Time::get_noon()
{
return noon;
}