-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheth_cal.h
260 lines (149 loc) · 4.87 KB
/
eth_cal.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
eth_cal.h
Purpose: Converts Gregorian Date to Ethiopian Date
@author - Bisrat Yalew
@version 1.0
@date Jan 5, 2018 G.C
*/
#include <iostream>
#include <map>
#include <tuple>
using namespace std;
bool isLeapYear(int year);
int get_month(string month);
int get_year(int year, int month, int day);
int get_day(int year, string month, int day);
//Gregorian's Month Days Length from Sep - Aug
int gregorial_month_days_length[] = { 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31 };
// Ethiopian's Month Days Length from Mes - Pua
int ethiopian_month_days_length[13] = { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5 };
// Month day's difference between gregorians_month[0] and ethiopians_month[0] up-to [12]
int month_days_difference[12] = { 10, 10, 9, 9, 8, 7, 9, 8, 8, 7, 7, 9 };
string gregorian_month[12] = {
"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"
};
string ethiopian_month[13] = {
"Mes", "Tik", "Hid", "Tah", "Tir", "Yek", "Meg", "Miy", "Gin", "Sen", "Ham", "Neh", "Pau"
};
/**
@param { int } year => gregorian year
@return { isLeapYear ? true : false }
@throw { Invalid Year ERROR }
*/
bool isLeapYear(int year) {
if(year % 4 == 0) {
if(year % 100 == 0) {
if(year % 400 == 0) {}
return true;
}
} else {
return false;
}
}
/**
@param { int } year
@param { int } month
@param { int } day
@return year
@throw { Invalid Year ERROR }
*/
int get_year(int year, int month, int day) {
if (!month >= 5 && !month <= 12) {
if (month == 1 && day <= 11) {
cout<<"Year -- "<<endl;
return year - 8;
} else {
return year - 9;
}
} else {
return year - 8;
}
}
/**
@param { string } month => shortened_gregorian_month_name
@return the month number
@throw { Can't Find Month ERROR }
*/
int get_month(string month) {
int monthNumber;
for (int i=0; i<12; ++i) {
if(gregorian_month[i] == month) {
monthNumber = i + 1;
}
}
return monthNumber;
}
/**
@param { string } month - The month name
@return { int } day_to_r - day number
@throw Invalid Month Error
*/
pair<string, int> map_month(string month, int day_to_r) {
typedef map<string, string> MapMonth;
MapMonth gregorian_to_ethiopian;
int monthNumber, day_f;
if(day_to_r < 1) {
for(int i=1; i<=11; ++i) {
gregorian_to_ethiopian[gregorian_month[i]] = ethiopian_month[i-1];
}
monthNumber = get_month(month);
day_f = (gregorial_month_days_length[monthNumber-1]) + day_to_r;
} else if(day_to_r > 0 && day_to_r < 45) {
day_f = day_to_r;
for(int i=1; i<=11; ++i) {
gregorian_to_ethiopian[gregorian_month[i]] = ethiopian_month[i];
}
}
return make_pair(gregorian_to_ethiopian[month], day_f);
}
/**
@param { int } year
@param { string } month => shortened_gregorian_month_name
@param { int } day
@return { int } day - converted day
@throw { Invalid Year, Month, Day => Error }
*/
int get_day(int year, string month, int day) {
int monthIndex;
int day_to_r;
if(isLeapYear(year) == true) {
/**
* Handle Leap Years
*/
monthIndex = get_month(month);
switch(monthIndex) {
case 12:
day_to_r = day - month_days_difference[monthIndex - 1];
default:
day_to_r = day - month_days_difference[monthIndex - 1];
}
} else {
// Handle Non Leap Years
monthIndex = get_month(month);
switch(monthIndex) {
case 12:
day_to_r = day - month_days_difference[monthIndex - 1];
default:
day_to_r = day - month_days_difference[monthIndex - 1];
}
}
return day_to_r;
}
/**
@param { int } year - gregorian_year
@param { string } month - shortened_gregorian_month_name
@param { int } day
@return { year_converted, month_converted, day_converted }
@throw { Invalid Year, Month, Day => Error }
*/
tuple<int, string, int> to_ethiopian_date(int year, string month, int day) {
string month_conv;
int day_conv, year_conv, monthNumber, day_to_ret;
monthNumber = get_month(month);
day_to_ret = get_day(year, month, day);
pair<string, int> mapped_month = map_month(month, day_to_ret);
month_conv = mapped_month.first;
day_conv = mapped_month.second;
year_conv = get_year(year, monthNumber, day);
return make_tuple(year_conv, month_conv, day_conv);
}