@thyrith/momentkh 2.5.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1298 -93
- package/dist/momentkh.d.ts +181 -0
- package/dist/momentkh.d.ts.map +1 -0
- package/dist/momentkh.js +1052 -0
- package/momentkh.js +13 -673
- package/momentkh.ts +1337 -0
- package/package.json +24 -2
- package/constant.js +0 -58
- package/example/newYearMoment.js +0 -4
- package/example/toKhDate.js +0 -6
- package/getSoriyatraLerngSak.js +0 -301
- package/index.html +0 -169
- package/locale/km.js +0 -70
package/momentkh.js
CHANGED
|
@@ -1,673 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// const Moment = require('moment');
|
|
15
|
-
function getLocaleConfig() {
|
|
16
|
-
if (typeof require === 'function') {
|
|
17
|
-
return require('./locale/km')
|
|
18
|
-
} else {
|
|
19
|
-
if (!this.momentkhLocales) {
|
|
20
|
-
throw "Please import [MOMENTKH]/locale/km.js to your project"
|
|
21
|
-
} else {
|
|
22
|
-
return this.momentkhLocales['km']
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
let config = getLocaleConfig();
|
|
28
|
-
|
|
29
|
-
let constant, LunarMonths, SolarMonth, MoonStatus, khNewYearMoments;
|
|
30
|
-
if (typeof require === 'function') {
|
|
31
|
-
constant = require('./constant')
|
|
32
|
-
} else {
|
|
33
|
-
if (!this.momentkhConstant) {
|
|
34
|
-
throw "Please import [MOMENTKH]/constant.js to your project"
|
|
35
|
-
} else {
|
|
36
|
-
constant = this.momentkhConstant
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
LunarMonths = constant.LunarMonths
|
|
40
|
-
SolarMonth = constant.SolarMonth
|
|
41
|
-
MoonStatus = constant.MoonStatus
|
|
42
|
-
khNewYearMoments = constant.khNewYearMoments
|
|
43
|
-
|
|
44
|
-
;(function (global, factory) {
|
|
45
|
-
if (typeof exports === 'object' && typeof module !== 'undefined') {
|
|
46
|
-
module.exports = factory
|
|
47
|
-
} else {
|
|
48
|
-
if (typeof define === 'function' && define.amd) {
|
|
49
|
-
define(factory)
|
|
50
|
-
} else {
|
|
51
|
-
global.momentkh = factory
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}(this, (function (Moment) {
|
|
55
|
-
'use strict';
|
|
56
|
-
|
|
57
|
-
Moment.khNewYearMoments = khNewYearMoments
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Bodithey: បូតិថី
|
|
61
|
-
* Bodithey determines if a given beYear is a leap-month year. Given year target year in Buddhist Era
|
|
62
|
-
* @return Number (0-29)
|
|
63
|
-
*/
|
|
64
|
-
function getBodithey(beYear) {
|
|
65
|
-
let ahk = getAharkun(beYear);
|
|
66
|
-
let avml = Math.floor((11 * ahk + 25) / 692);
|
|
67
|
-
let m = avml + ahk + 29;
|
|
68
|
-
return (m % 30);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Avoman: អាវមាន
|
|
73
|
-
* Avoman determines if a given year is a leap-day year. Given a year in Buddhist Era as denoted as adYear
|
|
74
|
-
* @param beYear (0 - 691)
|
|
75
|
-
*/
|
|
76
|
-
function getAvoman(beYear) {
|
|
77
|
-
let ahk = getAharkun(beYear);
|
|
78
|
-
let avm = (11 * ahk + 25) % 692;
|
|
79
|
-
return avm;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Aharkun: អាហារគុណ ឬ ហារគុណ
|
|
84
|
-
* Aharkun is used for Avoman and Bodithey calculation below. Given adYear as a target year in Buddhist Era
|
|
85
|
-
* @param beYear
|
|
86
|
-
* @returns {number}
|
|
87
|
-
*/
|
|
88
|
-
function getAharkun(beYear) {
|
|
89
|
-
let t = beYear * 292207 + 499;
|
|
90
|
-
let ahk = Math.floor(t / 800) + 4;
|
|
91
|
-
return ahk;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Kromathupul
|
|
96
|
-
* @param beYear
|
|
97
|
-
* @returns {number} (1-800)
|
|
98
|
-
*/
|
|
99
|
-
function kromthupul(beYear) {
|
|
100
|
-
let ah = getAharkunMod(beYear);
|
|
101
|
-
let krom = 800 - ah;
|
|
102
|
-
return krom;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* isKhmerSolarLeap
|
|
107
|
-
* @param beYear
|
|
108
|
-
* @returns {number}
|
|
109
|
-
*/
|
|
110
|
-
function isKhmerSolarLeap(beYear) {
|
|
111
|
-
let krom = kromthupul(beYear);
|
|
112
|
-
if (krom <= 207)
|
|
113
|
-
return 1;
|
|
114
|
-
else
|
|
115
|
-
return 0;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* getAkhakunMod
|
|
120
|
-
* @param beYear
|
|
121
|
-
* @returns {number}
|
|
122
|
-
*/
|
|
123
|
-
function getAharkunMod(beYear) {
|
|
124
|
-
let t = beYear * 292207 + 499;
|
|
125
|
-
let ahkmod = t % 800;
|
|
126
|
-
return ahkmod;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* * Regular if year has 30 day
|
|
131
|
-
* * leap month if year has 13 months
|
|
132
|
-
* * leap day if Jesth month of the year has 1 extra day
|
|
133
|
-
* * leap day and month: both of them
|
|
134
|
-
* @param beYear
|
|
135
|
-
* @returns {number} return 0:regular, 1:leap month, 2:leap day, 3:leap day and month
|
|
136
|
-
*/
|
|
137
|
-
function getBoditheyLeap(beYear) {
|
|
138
|
-
let result = 0;
|
|
139
|
-
let avoman = getAvoman(beYear);
|
|
140
|
-
let bodithey = getBodithey(beYear);
|
|
141
|
-
|
|
142
|
-
// check bodithey leap month
|
|
143
|
-
let boditheyLeap = 0;
|
|
144
|
-
if (bodithey >= 25 || bodithey <= 5) {
|
|
145
|
-
boditheyLeap = 1;
|
|
146
|
-
}
|
|
147
|
-
// check for avoman leap-day based on gregorian leap
|
|
148
|
-
let avomanLeap = 0;
|
|
149
|
-
if (isKhmerSolarLeap(beYear)) {
|
|
150
|
-
if (avoman <= 126)
|
|
151
|
-
avomanLeap = 1;
|
|
152
|
-
} else {
|
|
153
|
-
if (avoman <= 137) {
|
|
154
|
-
// check for avoman case 137/0, 137 must be normal year (p.26)
|
|
155
|
-
if (getAvoman(beYear + 1) === 0) {
|
|
156
|
-
avomanLeap = 0;
|
|
157
|
-
} else {
|
|
158
|
-
avomanLeap = 1;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// case of 25/5 consecutively
|
|
164
|
-
// only bodithey 5 can be leap-month, so set bodithey 25 to none
|
|
165
|
-
if (bodithey === 25) {
|
|
166
|
-
let nextBodithey = getBodithey(beYear + 1);
|
|
167
|
-
if (nextBodithey === 5) {
|
|
168
|
-
boditheyLeap = 0;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// case of 24/6 consecutively, 24 must be leap-month
|
|
173
|
-
if (bodithey == 24) {
|
|
174
|
-
let nextBodithey = getBodithey(beYear + 1);
|
|
175
|
-
if (nextBodithey == 6) {
|
|
176
|
-
boditheyLeap = 1;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// format leap result (0:regular, 1:month, 2:day, 3:both)
|
|
181
|
-
if (boditheyLeap === 1 && avomanLeap === 1) {
|
|
182
|
-
result = 3;
|
|
183
|
-
} else if (boditheyLeap === 1) {
|
|
184
|
-
result = 1;
|
|
185
|
-
} else if (avomanLeap === 1) {
|
|
186
|
-
result = 2;
|
|
187
|
-
} else {
|
|
188
|
-
result = 0;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return result;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// return 0:regular, 1:leap month, 2:leap day (no leap month and day together)
|
|
195
|
-
/**
|
|
196
|
-
* bodithey leap can be both leap-day and leap-month but following the khmer calendar rule, they can't be together on the same year, so leap day must be delayed to next year
|
|
197
|
-
* @param beYear
|
|
198
|
-
* @returns {number}
|
|
199
|
-
*/
|
|
200
|
-
function getProtetinLeap(beYear) {
|
|
201
|
-
let b = getBoditheyLeap(beYear);
|
|
202
|
-
if (b === 3) {
|
|
203
|
-
return 1;
|
|
204
|
-
}
|
|
205
|
-
if (b === 2 || b === 1) {
|
|
206
|
-
return b;
|
|
207
|
-
}
|
|
208
|
-
// case of previous year is 3
|
|
209
|
-
if (getBoditheyLeap(beYear - 1) === 3) {
|
|
210
|
-
return 2;
|
|
211
|
-
}
|
|
212
|
-
// normal case
|
|
213
|
-
return 0;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Maximum number of day in Khmer Month
|
|
218
|
-
* @param beMonth
|
|
219
|
-
* @param beYear
|
|
220
|
-
* @returns {number}
|
|
221
|
-
*/
|
|
222
|
-
function getNumberOfDayInKhmerMonth(beMonth, beYear) {
|
|
223
|
-
if (beMonth === LunarMonths['ជេស្ឋ'] && isKhmerLeapDay(beYear)) {
|
|
224
|
-
return 30;
|
|
225
|
-
}
|
|
226
|
-
if (beMonth === LunarMonths['បឋមាសាឍ'] || beMonth === LunarMonths['ទុតិយាសាឍ']) {
|
|
227
|
-
return 30;
|
|
228
|
-
}
|
|
229
|
-
// មិគសិរ : 29 , បុស្ស : 30 , មាឃ : 29 .. 30 .. 29 ..30 .....
|
|
230
|
-
return beMonth % 2 === 0 ? 29 : 30;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Get number of day in Khmer year
|
|
235
|
-
* @param beYear
|
|
236
|
-
* @returns {number}
|
|
237
|
-
*/
|
|
238
|
-
function getNumerOfDayInKhmerYear(beYear) {
|
|
239
|
-
if (isKhmerLeapMonth(beYear)) {
|
|
240
|
-
return 384;
|
|
241
|
-
} else if (isKhmerLeapDay(beYear)) {
|
|
242
|
-
return 355;
|
|
243
|
-
} else {
|
|
244
|
-
return 354;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Get number of day in Gregorian year
|
|
250
|
-
* @param adYear
|
|
251
|
-
* @returns {number}
|
|
252
|
-
*/
|
|
253
|
-
function getNumberOfDayInGregorianYear(adYear) {
|
|
254
|
-
if (isGregorianLeap(adYear)) {
|
|
255
|
-
return 366;
|
|
256
|
-
} else {
|
|
257
|
-
return 365;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* A year with an extra month is called Adhikameas (អធិកមាស). This year has 384 days.
|
|
263
|
-
* @param beYear
|
|
264
|
-
* @returns {boolean}
|
|
265
|
-
*/
|
|
266
|
-
function isKhmerLeapMonth(beYear) {
|
|
267
|
-
return getProtetinLeap(beYear) === 1
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* A year with an extra day is called Chhantrea Thimeas (ចន្ទ្រាធិមាស) or Adhikavereak (អធិកវារៈ). This year has 355 days.
|
|
272
|
-
* @param beYear
|
|
273
|
-
* @returns {boolean}
|
|
274
|
-
*/
|
|
275
|
-
function isKhmerLeapDay(beYear) {
|
|
276
|
-
return getProtetinLeap(beYear) === 2
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Gregorian Leap
|
|
281
|
-
* @param adYear
|
|
282
|
-
* @returns {boolean}
|
|
283
|
-
*/
|
|
284
|
-
function isGregorianLeap(adYear) {
|
|
285
|
-
if (adYear % 4 === 0 && adYear % 100 !== 0 || adYear % 400 === 0) {
|
|
286
|
-
return true;
|
|
287
|
-
} else {
|
|
288
|
-
return false;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* រកថ្ងៃវិសាខបូជា
|
|
294
|
-
* ថ្ងៃដាច់ឆ្នាំពុទ្ធសករាជ
|
|
295
|
-
*/
|
|
296
|
-
function getVisakhaBochea(gregorianYear) {
|
|
297
|
-
var date = Moment('1/1/' + gregorianYear, 'D/M/YYYY')
|
|
298
|
-
for (var i = 0; i < 365; i++) {
|
|
299
|
-
var lunarDate = findLunarDate(date);
|
|
300
|
-
if (lunarDate.month == LunarMonths['ពិសាខ'] && lunarDate.day == 14) {
|
|
301
|
-
return date
|
|
302
|
-
}
|
|
303
|
-
date.add(1, 'day')
|
|
304
|
-
}
|
|
305
|
-
throw 'Cannot find Visakhabochea day. Please report this bug.';
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Buddhist Era
|
|
310
|
-
* ថ្ងៃឆ្លងឆ្នាំ គឺ ១ រោច ខែពិសាខ
|
|
311
|
-
* @ref http://news.sabay.com.kh/article/1039620
|
|
312
|
-
* @summary: ឯកឧត្តម សេង សុមុនី អ្នកនាំពាក្យក្រសួងធម្មការ និងសាសនាឲ្យSabay ដឹងថានៅប្រទេសកម្ពុជាការឆ្លងចូលពុទ្ធសករាជថ្មីគឺកំណត់យកនៅថ្ងៃព្រះពុទ្ធយាងចូលនិព្វាន ពោលគឺនៅថ្ងៃ១រោច ខែពិសាខ។
|
|
313
|
-
* @param moment
|
|
314
|
-
* @returns {*}
|
|
315
|
-
*/
|
|
316
|
-
function getBEYear(moment) {
|
|
317
|
-
if (moment.diff(getVisakhaBochea(moment.year())) > 0) {
|
|
318
|
-
return moment.year() + 544;
|
|
319
|
-
} else {
|
|
320
|
-
return moment.year() + 543;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Due to recursive problem, I need to calculate the BE based on new year's day
|
|
326
|
-
* This won't be displayed on final result, it is used to find number of day in year,
|
|
327
|
-
* It won't affect the result because on ខែចេត្រ និង ខែពិសាខ, number of days is the same every year
|
|
328
|
-
* ពីព្រោះចូលឆ្នាំតែងតែចំខែចេត្រ ឬ ពិសាខ
|
|
329
|
-
* @param moment
|
|
330
|
-
* @returns {*}
|
|
331
|
-
*/
|
|
332
|
-
function getMaybeBEYear(moment) {
|
|
333
|
-
if (moment.month() + 1 <= SolarMonth['មេសា'] + 1) {
|
|
334
|
-
return moment.year() + 543;
|
|
335
|
-
} else {
|
|
336
|
-
return moment.year() + 544;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Moha Sakaraj
|
|
342
|
-
* @param adYear
|
|
343
|
-
* @returns {number}
|
|
344
|
-
*/
|
|
345
|
-
function getMohaSakarajYear(adYear) {
|
|
346
|
-
return adYear - 77;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Jolak Sakaraj
|
|
351
|
-
* @param beYear
|
|
352
|
-
* @returns {number}
|
|
353
|
-
*/
|
|
354
|
-
function getJolakSakarajYear(moment) {
|
|
355
|
-
let gregorianYear = moment.year();
|
|
356
|
-
let newYearMoment = getKhNewYearMoment(gregorianYear);
|
|
357
|
-
if (moment.diff(newYearMoment) < 0) {
|
|
358
|
-
return gregorianYear + 543 - 1182
|
|
359
|
-
} else {
|
|
360
|
-
return gregorianYear + 544 - 1182
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* ១កើត ៤កើត ២រោច ១៤រោច ...
|
|
366
|
-
* @param day 1-30
|
|
367
|
-
* @returns {{count: number, moonStatus: number}}
|
|
368
|
-
*/
|
|
369
|
-
function getKhmerLunarDay(day) {
|
|
370
|
-
return {
|
|
371
|
-
count: (day % 15) + 1,
|
|
372
|
-
moonStatus: day > 14 ? MoonStatus['រោច'] : MoonStatus['កើត']
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Turn be year to animal year
|
|
378
|
-
* @param beYear
|
|
379
|
-
* @returns {number}
|
|
380
|
-
*/
|
|
381
|
-
function getAnimalYear(moment) {
|
|
382
|
-
let gregorianYear = moment.year();
|
|
383
|
-
let newYearMoment = getKhNewYearMoment(gregorianYear);
|
|
384
|
-
if (moment.diff(newYearMoment) < 0) {
|
|
385
|
-
return (gregorianYear + 543 + 4) % 12
|
|
386
|
-
} else {
|
|
387
|
-
return (gregorianYear + 544 + 4) % 12
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Khmer date format handler
|
|
393
|
-
* @param day
|
|
394
|
-
* @param month
|
|
395
|
-
* @param moment
|
|
396
|
-
* @param format
|
|
397
|
-
* @returns {*}
|
|
398
|
-
*/
|
|
399
|
-
function formatKhmerDate({day, month, moment}, format) {
|
|
400
|
-
if (format === null || format === undefined) {
|
|
401
|
-
// Default date format
|
|
402
|
-
let dayOfWeek = moment.day();
|
|
403
|
-
let moonDay = getKhmerLunarDay(day);
|
|
404
|
-
let beYear = getBEYear(moment);
|
|
405
|
-
let animalYear = getAnimalYear(moment);
|
|
406
|
-
let eraYear = getJolakSakarajYear(moment) % 10;
|
|
407
|
-
return config.postformat(`ថ្ងៃ${config.weekdays[dayOfWeek]} ${moonDay.count}${config.moonStatus[moonDay.moonStatus]} ខែ${config.lunarMonths[month]} ឆ្នាំ${config.animalYear[animalYear]} ${config.eraYear[eraYear]} ពុទ្ធសករាជ ${beYear}`);
|
|
408
|
-
} else if (typeof format === 'string') {
|
|
409
|
-
// Follow the format
|
|
410
|
-
let formatRule = {
|
|
411
|
-
'W': function () { // Day of week
|
|
412
|
-
let dayOfWeek = moment.day();
|
|
413
|
-
return config.weekdays[dayOfWeek]
|
|
414
|
-
},
|
|
415
|
-
'w': function () { // Day of week
|
|
416
|
-
let dayOfWeek = moment.day();
|
|
417
|
-
return config.weekdaysShort[dayOfWeek]
|
|
418
|
-
},
|
|
419
|
-
'd': function () { // no determine digit
|
|
420
|
-
let moonDay = getKhmerLunarDay(day);
|
|
421
|
-
return moonDay.count;
|
|
422
|
-
},
|
|
423
|
-
'D': function () { // minimum 2 digits
|
|
424
|
-
let moonDay = getKhmerLunarDay(day);
|
|
425
|
-
return ('' + moonDay.count).length === 1 ? '0' + moonDay.count : moonDay.count;
|
|
426
|
-
},
|
|
427
|
-
'n': function () {
|
|
428
|
-
let moonDay = getKhmerLunarDay(day);
|
|
429
|
-
return config.moonStatusShort[moonDay.moonStatus]
|
|
430
|
-
},
|
|
431
|
-
'N': function () {
|
|
432
|
-
let moonDay = getKhmerLunarDay(day);
|
|
433
|
-
return config.moonStatus[moonDay.moonStatus]
|
|
434
|
-
},
|
|
435
|
-
'o': function () {
|
|
436
|
-
return config.moonDays[day];
|
|
437
|
-
},
|
|
438
|
-
'm': function () {
|
|
439
|
-
return config.lunarMonths[month];
|
|
440
|
-
},
|
|
441
|
-
'M': function () {
|
|
442
|
-
return config.months[moment.month()];
|
|
443
|
-
},
|
|
444
|
-
'a': function () {
|
|
445
|
-
let animalYear = getAnimalYear(moment);
|
|
446
|
-
return config.animalYear[animalYear];
|
|
447
|
-
},
|
|
448
|
-
'e': function () {
|
|
449
|
-
let eraYear = getJolakSakarajYear(moment) % 10;
|
|
450
|
-
return config.eraYear[eraYear];
|
|
451
|
-
},
|
|
452
|
-
'b': function () {
|
|
453
|
-
return getBEYear(moment);
|
|
454
|
-
},
|
|
455
|
-
'c': function () {
|
|
456
|
-
return moment.year();
|
|
457
|
-
},
|
|
458
|
-
'j': function () {
|
|
459
|
-
return getJolakSakarajYear(moment);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
return config.postformat(format.replace(new RegExp(Object.keys(formatRule).join('|'), 'g'), function (matched) {
|
|
464
|
-
return formatRule[matched]();
|
|
465
|
-
}));
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
throw Error(format + ' is not a valid date format.');
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Read Khmer lunar date
|
|
473
|
-
* @param params : String (2) (input and format)
|
|
474
|
-
* @or @param Object {ថ្ងៃ: ..., កើត: ..., ខែ: ..., ពស: ...}
|
|
475
|
-
* @return Moment
|
|
476
|
-
*/
|
|
477
|
-
function readLunarDate(...params) {
|
|
478
|
-
console.log('Now working yet')
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* Next month of the month
|
|
483
|
-
*/
|
|
484
|
-
function nextMonthOf(khmerMonth, BEYear) {
|
|
485
|
-
switch (khmerMonth) {
|
|
486
|
-
case LunarMonths['មិគសិរ']:
|
|
487
|
-
return LunarMonths['បុស្ស'];
|
|
488
|
-
case LunarMonths['បុស្ស']:
|
|
489
|
-
return LunarMonths['មាឃ'];
|
|
490
|
-
case LunarMonths['មាឃ']:
|
|
491
|
-
return LunarMonths['ផល្គុន'];
|
|
492
|
-
case LunarMonths['ផល្គុន']:
|
|
493
|
-
return LunarMonths['ចេត្រ'];
|
|
494
|
-
case LunarMonths['ចេត្រ']:
|
|
495
|
-
return LunarMonths['ពិសាខ'];
|
|
496
|
-
case LunarMonths['ពិសាខ']:
|
|
497
|
-
return LunarMonths['ជេស្ឋ'];
|
|
498
|
-
case LunarMonths['ជេស្ឋ']: {
|
|
499
|
-
if (isKhmerLeapMonth(BEYear)) {
|
|
500
|
-
return LunarMonths['បឋមាសាឍ']
|
|
501
|
-
} else {
|
|
502
|
-
return LunarMonths['អាសាឍ']
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
case LunarMonths['អាសាឍ']:
|
|
506
|
-
return LunarMonths['ស្រាពណ៍'];
|
|
507
|
-
case LunarMonths['ស្រាពណ៍']:
|
|
508
|
-
return LunarMonths['ភទ្របទ'];
|
|
509
|
-
case LunarMonths['ភទ្របទ']:
|
|
510
|
-
return LunarMonths['អស្សុជ'];
|
|
511
|
-
case LunarMonths['អស្សុជ']:
|
|
512
|
-
return LunarMonths['កក្ដិក'];
|
|
513
|
-
case LunarMonths['កក្ដិក']:
|
|
514
|
-
return LunarMonths['មិគសិរ'];
|
|
515
|
-
case LunarMonths['បឋមាសាឍ']:
|
|
516
|
-
return LunarMonths['ទុតិយាសាឍ'];
|
|
517
|
-
case LunarMonths['ទុតិយាសាឍ']:
|
|
518
|
-
return LunarMonths['ស្រាពណ៍'];
|
|
519
|
-
default:
|
|
520
|
-
throw Error('Plugin is facing wrong calculation (Invalid month)');
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* Calculate date from momoentjs to Khmer date
|
|
526
|
-
* @param target : Moment
|
|
527
|
-
* @returns {{day: number, month: *, epochMoved: (*|moment.Moment)}}
|
|
528
|
-
*/
|
|
529
|
-
function findLunarDate(target) {
|
|
530
|
-
/**
|
|
531
|
-
* Epoch Date: January 1, 1900
|
|
532
|
-
*/
|
|
533
|
-
let epochMoment = Moment('1/1/1900', 'D/M/YYYY')
|
|
534
|
-
let khmerMonth = LunarMonths['បុស្ស'];
|
|
535
|
-
let khmerDay = 0; // 0 - 29 ១កើត ... ១៥កើត ១រោច ...១៤រោច (១៥រោច)
|
|
536
|
-
|
|
537
|
-
let differentFromEpoch = target.diff(epochMoment)
|
|
538
|
-
|
|
539
|
-
// Find nearest year epoch
|
|
540
|
-
if (differentFromEpoch > 0) {
|
|
541
|
-
while (Moment.duration(target.diff(epochMoment), 'milliseconds').asDays() > getNumerOfDayInKhmerYear(getMaybeBEYear(epochMoment.clone().add(1, 'year')))) {
|
|
542
|
-
epochMoment.add(getNumerOfDayInKhmerYear(getMaybeBEYear(epochMoment.clone().add(1, 'year'))), 'day')
|
|
543
|
-
}
|
|
544
|
-
} else {
|
|
545
|
-
do {
|
|
546
|
-
epochMoment.subtract(getNumerOfDayInKhmerYear(getMaybeBEYear(epochMoment)), 'day')
|
|
547
|
-
} while (Moment.duration(epochMoment.diff(target), 'milliseconds').asDays() > 0)
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
// Move epoch month
|
|
551
|
-
while (Moment.duration(target.diff(epochMoment), 'milliseconds').asDays() > getNumberOfDayInKhmerMonth(khmerMonth, getMaybeBEYear(epochMoment))) {
|
|
552
|
-
epochMoment.add(getNumberOfDayInKhmerMonth(khmerMonth, getMaybeBEYear(epochMoment)), 'day');
|
|
553
|
-
khmerMonth = nextMonthOf(khmerMonth, getMaybeBEYear(epochMoment))
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
khmerDay += Math.floor(Moment.duration(target.diff(epochMoment), 'milliseconds').asDays());
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* Fix result display 15 រោច ខែ ជេស្ឋ នៅថ្ងៃ ១ កើតខែបឋមាសាធ
|
|
560
|
-
* ករណី ខែជេស្ឋមានតែ ២៩ ថ្ងៃ តែលទ្ធផលបង្ហាញ ១៥រោច ខែជេស្ឋ
|
|
561
|
-
*/
|
|
562
|
-
const totalDaysOfTheMonth = getNumberOfDayInKhmerMonth(khmerMonth, getMaybeBEYear(target))
|
|
563
|
-
if (totalDaysOfTheMonth <= khmerDay) {
|
|
564
|
-
khmerDay = khmerDay % totalDaysOfTheMonth
|
|
565
|
-
khmerMonth = nextMonthOf(khmerMonth, getMaybeBEYear(epochMoment))
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
epochMoment.add(Moment.duration(target.diff(epochMoment), 'milliseconds').asDays(), 'day');
|
|
569
|
-
|
|
570
|
-
return {
|
|
571
|
-
day: khmerDay,
|
|
572
|
-
month: khmerMonth,
|
|
573
|
-
epochMoved: epochMoment
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* Return khmer lunar date
|
|
579
|
-
* @param format String (wanted format)
|
|
580
|
-
* @return String
|
|
581
|
-
* @or @param Array (wanted field) [ថ្ងៃ ថ្ងៃទី កើត/រោច ខែចន្ទគតិ ខែសុរិយគតិ ឆ្នាំសត្វ ឆ្នាំស័ក ពស គស ចស មស សីល]
|
|
582
|
-
* @return Object
|
|
583
|
-
*/
|
|
584
|
-
function toLunarDate(format) {
|
|
585
|
-
|
|
586
|
-
let target = this.clone();
|
|
587
|
-
|
|
588
|
-
let result = findLunarDate(target);
|
|
589
|
-
|
|
590
|
-
return formatKhmerDate({
|
|
591
|
-
day: result.day,
|
|
592
|
-
month: result.month,
|
|
593
|
-
moment: target
|
|
594
|
-
}, format)
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
function getKhNewYearMoment(gregorianYear) {
|
|
598
|
-
if (Moment.khNewYearMoments[gregorianYear] !== undefined) {
|
|
599
|
-
// console.log('cache')
|
|
600
|
-
return Moment(Moment.khNewYearMoments[gregorianYear], 'DD-MM-YYYY H:m')
|
|
601
|
-
} else {
|
|
602
|
-
// console.log('calculate')
|
|
603
|
-
let getSoriyatraLerngSak;
|
|
604
|
-
if (typeof require === 'function') {
|
|
605
|
-
getSoriyatraLerngSak = require('./getSoriyatraLerngSak')
|
|
606
|
-
} else {
|
|
607
|
-
if (window) {
|
|
608
|
-
if (!window.getSoriyatraLerngSak) {
|
|
609
|
-
throw 'Please import [MOMENTKH]/getSoriyatraLerngSak.js to your project'
|
|
610
|
-
} else {
|
|
611
|
-
getSoriyatraLerngSak = window.getSoriyatraLerngSak
|
|
612
|
-
}
|
|
613
|
-
} else {
|
|
614
|
-
throw 'Cannot import getSoriyatraLerngSak. This is might not a nodejs environment or a browser'
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
// ពីគ្រិស្ដសករាជ ទៅ ចុល្លសករាជ
|
|
618
|
-
let jsYear = (gregorianYear + 544) - 1182;
|
|
619
|
-
let info = getSoriyatraLerngSak(jsYear);
|
|
620
|
-
// ចំនួនថ្ងៃចូលឆ្នាំ
|
|
621
|
-
let numberNewYearDay;
|
|
622
|
-
if (info.newYearsDaySotins[0].angsar === 0) { // ថ្ងៃ ខែ ឆ្នាំ ម៉ោង និង នាទី ចូលឆ្នាំ
|
|
623
|
-
// ចូលឆ្នាំ ៤ ថ្ងៃ
|
|
624
|
-
numberNewYearDay = 4;
|
|
625
|
-
// return Moment(`13-04-${gregorianYear} ${info.timeOfNewYear.hour}:${info.timeOfNewYear.minute}`, 'DD-MM-YYYY H:m')
|
|
626
|
-
} else {
|
|
627
|
-
// ចូលឆ្នាំ ៣ ថ្ងៃ
|
|
628
|
-
numberNewYearDay = 3;
|
|
629
|
-
// return Moment(`14-04-${gregorianYear} ${info.timeOfNewYear.hour}:${info.timeOfNewYear.minute}`, 'DD-MM-YYYY H:m')
|
|
630
|
-
}
|
|
631
|
-
let epochLerngSak = Moment(`17-04-${gregorianYear} ${info.timeOfNewYear.hour}:${info.timeOfNewYear.minute}`, 'DD-MM-YYYY H:m')
|
|
632
|
-
let khEpoch = findLunarDate(epochLerngSak)
|
|
633
|
-
let diffFromEpoch = (((khEpoch.month - 4) * 30) + khEpoch.day) -
|
|
634
|
-
(((info.lunarDateLerngSak.month - 4) * 30) + info.lunarDateLerngSak.day)
|
|
635
|
-
let result = epochLerngSak.subtract(diffFromEpoch + numberNewYearDay - 1, 'day')
|
|
636
|
-
// Caching
|
|
637
|
-
Moment.khNewYearMoments[gregorianYear] = result.format('DD-MM-YYYY H:m')
|
|
638
|
-
return result
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
function khDay() {
|
|
643
|
-
let result = findLunarDate(this.clone());
|
|
644
|
-
return result.day;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
function khMonth() {
|
|
648
|
-
let result = findLunarDate(this.clone());
|
|
649
|
-
return result.month;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
function khYear() {
|
|
653
|
-
return getBEYear(this.clone());
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
Moment.readLunarDate =
|
|
657
|
-
Moment.khDate =
|
|
658
|
-
Moment.khdate =
|
|
659
|
-
readLunarDate;
|
|
660
|
-
|
|
661
|
-
Moment.fn.toLunarDate =
|
|
662
|
-
Moment.fn.toKhDate =
|
|
663
|
-
Moment.fn.tokhdate =
|
|
664
|
-
toLunarDate;
|
|
665
|
-
|
|
666
|
-
Moment.getKhNewYearMoment = getKhNewYearMoment;
|
|
667
|
-
|
|
668
|
-
Moment.fn.khDay = khDay;
|
|
669
|
-
Moment.fn.khMonth = khMonth;
|
|
670
|
-
Moment.fn.khYear = khYear;
|
|
671
|
-
|
|
672
|
-
return Moment;
|
|
673
|
-
})));
|
|
1
|
+
// Re-export from compiled TypeScript
|
|
2
|
+
const dist = require('./dist/momentkh');
|
|
3
|
+
|
|
4
|
+
// Export the default (momentkh object) as the main export
|
|
5
|
+
module.exports = dist.default;
|
|
6
|
+
|
|
7
|
+
// Also export named exports for those who want to use them
|
|
8
|
+
module.exports.MoonPhase = dist.MoonPhase;
|
|
9
|
+
module.exports.MonthIndex = dist.MonthIndex;
|
|
10
|
+
module.exports.AnimalYear = dist.AnimalYear;
|
|
11
|
+
module.exports.EraYear = dist.EraYear;
|
|
12
|
+
module.exports.DayOfWeek = dist.DayOfWeek;
|
|
13
|
+
module.exports.momentkh = dist.momentkh;
|