-
Notifications
You must be signed in to change notification settings - Fork 1
/
ken.js
262 lines (240 loc) · 8.77 KB
/
ken.js
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
261
262
/*
* Ken.js
* A Gregorian calendar to Ethiopic calendar converter and vice versa
*
*/
var ken = function(rawDate){
//http://www.geez.org/Calendars/
var getJDNFromEthiopic = function(date){
var ethiopicOffset = 1723856;
var year= date[0],month=date[1],day=date[2];
var JDN = (ethiopicOffset + 365 ) + (365 * (year -1)) + (30 * month) + (day -31) + (Math.floor(year/4) );
return JDN;
};
//http://www.geez.org/Calendars/
var getEthiopicFromJDN = function(jdn){
var ethiopicOffset = 1723856;
var r = ((jdn - ethiopicOffset) % 1461);
var n = ( r % 365 ) + 365 * Math.floor(r/1460) ;
var year = ( 4 * Math.floor((jdn - ethiopicOffset)/1461) ) + Math.floor(r/365) - Math.floor(r/1460);
var month = Math.floor( n/ 30 ) + 1 ;
var day = ( n % 30 ) + 1 ;
return [year,month,day];
};
//http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/jdn-explanation.html
var getJDNFromGregorian =function(date){
var year=date[0];
var month=date[1];
var day=date[2];
var a = Math.floor((14-month)/12);
var y = Math.floor(year+4800-a);
var m = month+12*a-3;
var JDN = day + Math.floor((153*m+2)/5)+(365*y)+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400)-32045;
return JDN;
};
//http://www.csgnetwork.com/juliangregcalconv.html
var getGregorianFromJDN =function(jdn){
var julDate = jdn ;
var z = Math.floor(julDate);
var f = julDate - z;
if (z < 2299161) {
var A = z;
}
else {
var omega = Math.floor((z-1867216.25)/36524.25);
var A = z + 1 + omega - Math.floor(omega/4);
}
var B = A + 1524;
var C = Math.floor((B-122.1)/365.25);
var D = Math.floor(365.25*C);
var Epsilon = Math.floor((B-D)/30.6001);
var dayGreg = B - D - Math.floor(30.6001*Epsilon) + f;
if (Epsilon < 14) {
monthGreg = Epsilon - 1;
}
else {
monthGreg = Epsilon - 13;
}
if (monthGreg > 2) {
yearGreg = C - 4716;
}
else {
yearGreg = C - 4715;
}
return [yearGreg,monthGreg,dayGreg];
};
//function to convert Ethipian calendar to Gregorian calendar
var convertToGreg=function(date){
var jdn = getJDNFromEthiopic([
date.parsed.getFullYear(),
date.parsed.getMonth()+1,
date.parsed.getDate()
]);
var date = getGregorianFromJDN(jdn);
var isLeapYear=function(year){
if((year%4==0 && year%100!=0)|| year%400==0){
return true
}
else
return false;
};
return {
parsed:date.parsed,
date:date,
isLeapYear:function(){
return isLeapYear(this.date[0]);
},
getDate:function(){
return parseInt(this.date[2]);
},
getMonth:function(){
return parseInt(this.date[1]);
},
getFullYear:function(){
return parseInt(this.date[0]);
},
toString:function(){
return this.date[0]+"-"+((this.date[1]/10)>=1?this.date[1]:"0"+this.date[1])+"-"+((this.date[2]/10)>=1?this.date[2]:"0"+this.date[2]);
},
getDateInstance:function(){
return new Date(this.date.toString())
},
getMonthDate:function(){
var tempInstance = this.getDateInstance();
tempInstance.setMonth(tempInstance.getMonth() +1);
tempInstance.setDate(0);
return tempInstance.getDate();
}
};
};
//function to convert Gregorian calendar to Ethiopian calendar
var convertToEt=function(date){
var jdn = getJDNFromGregorian([
date.parsed.getFullYear(),
date.parsed.getMonth()+1,
date.parsed.getDate()
]);
var date = getEthiopicFromJDN(jdn);
//calculates leapyear for eth calendar
var isLeapYear=function(year){
year = year + 1;
if((year%4==0 && year%100!=0)|| year%400==0){
return true
}
else
return false;
};
return {
parsed:date.parsed,
date:date,
isLeapYear:function(){
return isLeapYear(this.date[0]);
},
getDate:function(){
return parseInt(this.date[2]);
},
getMonth:function(){
return parseInt(this.date[1]);
},
getFullYear:function(){
return parseInt(this.date[0]);
},
toString:function(){
return this.date[0]+"-"+((this.date[1]/10)>=1?this.date[1]:"0"+this.date[1])+"-"+((this.date[2]/10)>=1?this.date[2]:"0"+this.date[2]);
},
getMonthDate:function(){
if(this.date[1] == 13){
if(this.isLeapYear()){
return 6;
}else{
return 5;
}
}else{
return 30;
}
}
};
};
function toEt(){
return convertToEt(this);
};
function toGreg(){
return convertToGreg(this);
};
function getEt(){
return convertToEt(generateDateObjcet(parseFromString(convertToGreg(this).toString())));
};
function getGreg(){
return convertToGreg(generateDateObjcet(parseFromString(convertToEt(this).toString())));
};
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
function IllegalArgumentException(eMessage) {
this.name = "IllegalArgumentException";
this.message = eMessage || 'Invalid date supplied, try YYYY-MM-DD format';
this.stack = (new Error()).stack;
}
IllegalArgumentException.prototype = Object.create(Error.prototype);
IllegalArgumentException.prototype.constructor = IllegalArgumentException;
function generateDateObjcet(date){
return {
parsed:{
date:date,
getDate:function(){
return parseInt(this.date[2]);
},
getMonth:function(){
return parseInt(this.date[1]);
},
getFullYear:function(){
return parseInt(this.date[0]);
}
},
toGC:toGreg,
toEC:toEt,
getGC:getGreg,
getEC:getEt
}
}
//http://stackoverflow.com/a/10638617/2147627
function parseFromString(date){
var y = date.substr(0,4),
m = date.substr(5,2) - 1,
d = date.substr(8,2);
if( (!isNaN(y) && !isNaN(m) && !isNaN(d))){
return [y,m,d];
}else{
throw new IllegalArgumentException();
}
}
function parseFromDate(date){
var y = date.getFullYear(),
m = date.getMonth(),
d = date.getDate();
if( (y && m && d) != null){
return [y,m,d];
}else{
throw new IllegalArgumentException();
}
}
function parseFromKen(kenDate){
if(kenDate.date){
return kenDate.date;
}
throw new IllegalArgumentException();
}
if(typeof rawDate == "object"){
if(rawDate instanceof Date)
return generateDateObjcet(parseFromDate(rawDate));
else
return generateDateObjcet(parseFromKen(rawDate));
}else if(typeof rawDate == "string"){
return generateDateObjcet(parseFromString(rawDate));
}else if(typeof rawDate == "undefined"){
return generateDateObjcet(parseFromDate(new Date()));
}
throw new IllegalArgumentException("invalid parameter");
}
//the if statment is to make it compatible with the browser and node.js environment.
if (typeof exports !== 'undefined') {
exports.ken = ken
}