-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_time.h
216 lines (199 loc) · 5.08 KB
/
date_time.h
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
//#include<bits/stdc++.h>
#include <iostream>
using namespace std;
class TypeError
{
char msg[100];
public:
TypeError(char s[])
{
strcpy(msg, s);
}
void showError()
{
cout << msg << " Please enter valid type" << endl;
exit(1);
}
};
class Negative
{
char msg[100];
public:
Negative(char s[])
{
strcpy(msg, s);
}
void showError()
{
cout << msg << " Please enter +ve datas!" << endl;
return;
}
};
class Invalid
{
char msg[100];
public:
Invalid(char s[])
{
strcpy(msg, s);
}
void showError()
{
cout << msg << " Please enter valid data!" << endl;
return;
}
};
class Date
{
private:
int year, month, day;
public:
Date()
{
}
Date(int netTime)
{
year = netTime / (24 * 360);
month = (int(netTime) % (24 * 360)) / (30 * 24);
day = ((int(netTime) % (24 * 360)) % (30 * 24)) / 24;
}
void set_date()
{
again:
try
{
cout << "YYYY:MM:DD\n";
cin >> year>>month>>day;
cin.clear();
if (!static_cast<int>(year) || !static_cast<int>(month) || !static_cast<int>(day))
throw TypeError("Type Error.");
if (year < 0 || month < 0 || day < 0)
throw Negative("Negative date entered!!");
if (year < 1000 || month > 12 || day > 32)
throw Invalid("Invalid date entered!!");
}
catch (TypeError t)
{
t.showError();
goto again;
}
catch (Negative n)
{
n.showError();
goto again;
}
catch (Invalid t)
{
t.showError();
goto again;
}
}
int operator-(Date d2)
{
Date d;
int total_days;
total_days = (year * 360 + month * 30 + day) - (d2.year * 360 + d2.month * 30 + d2.day);
d.year = total_days / 360;
d.month = (total_days % 360) / 30;
d.day = (total_days % 360) % 30;
return d.year * 360 * 24 + d.month * 30 * 24 + d.day * 24;
}
void displayDate()
{
char m = ' ', d = ' ';
if (month < 10)
m = '0';
if (day < 10)
d = '0';
cout << "Date: " << year << " / " << m << month << " / " << d << day << endl;
}
void displaynetDate()
{
cout << year << " Years " << month << " Months " << day << " Days" << endl;
}
};
class Time
{
private:
int hour, min, sec;
string format;
public:
Time()
{
}
Time(float netTime)
{
hour = netTime / 3600;
min = (int(netTime) % 3600) / 60;
sec = (int(netTime) % 3600) % 60;
}
void set_time()
{
again:
try
{
cout << "HH:MM:SS\n";
cin >> hour >> min >> sec;
cout << "Enter the Time format AM/PM: ";
if (hour < 0 || min < 0 || sec < 0)
throw Negative("Negative time entered!!");
if (hour > 12 || min > 59 || sec > 59)
throw Invalid("Invalid time entered!!");
cin.clear();
if (!static_cast<int>(hour) || !static_cast<int>(min) || !static_cast<int>(sec))
throw TypeError("Type Error.");
cin >> format;
transform(format.begin(), format.end(), format.begin(), ::toupper);
}
catch (TypeError t)
{
t.showError();
goto again;
}
catch (Negative n)
{
n.showError();
goto again;
}
catch (Invalid t)
{
t.showError();
goto again;
}
}
float operator-(Time t2)
{
Time t;
int total_sec;
total_sec = ((12 + hour) * 3600 + min * 60 + sec) - (t2.hour * 3600 + t2.min * 60 + t2.sec);
t.hour = total_sec / 3600;
t.min = (total_sec % 3600) / 60;
t.sec = (total_sec % 3600) % 60;
if (t2.hour == 12 && t2.format == format)
return 12 + t.hour + t.min / 60.0 + t.sec / 3600.0;
else if (t2.hour > hour && format == t2.format)
return 12 + t.hour + t.min / 60.0 + t.sec / 3600.0;
else
return t.hour + t.min / 60.0 + t.sec / 3600.0;
}
void displayTime()
{
char flag[2];
char h = ' ', m = ' ', s = ' ';
if (format == "PM")
strcpy(flag, "PM");
else
strcpy(flag, "AM");
if (hour < 10)
h = '0';
if (min < 10)
m = '0';
if (sec < 10)
s = '0';
cout << "Time: " << h << hour << ":" << m << min << ":" << s << sec << " " << flag << endl;
}
void displaynetTime()
{
cout << hour << " Hours " << min << " Minutes " << sec << " Seconds" << endl;
}
};