@thyrith/momentkh 3.0.0 → 3.0.1
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 +56 -56
- package/dist/momentkh.d.ts +43 -28
- package/dist/momentkh.d.ts.map +1 -1
- package/dist/momentkh.js +99 -88
- package/momentkh.js +1087 -11
- package/momentkh.ts +102 -98
- package/package.json +3 -2
package/dist/momentkh.js
CHANGED
|
@@ -13,7 +13,13 @@
|
|
|
13
13
|
* @license MIT
|
|
14
14
|
*/
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
16
|
+
exports.constants = exports.DayOfWeek = exports.EraYear = exports.AnimalYear = exports.MonthIndex = exports.MoonPhase = void 0;
|
|
17
|
+
exports.fromGregorian = fromGregorian;
|
|
18
|
+
exports.fromKhmer = fromKhmer;
|
|
19
|
+
exports.getNewYear = getNewYear;
|
|
20
|
+
exports.format = format;
|
|
21
|
+
exports.fromDate = fromDate;
|
|
22
|
+
exports.toDate = toDate;
|
|
17
23
|
// ============================================================================
|
|
18
24
|
// Type Definitions
|
|
19
25
|
// ============================================================================
|
|
@@ -25,20 +31,20 @@ var MoonPhase;
|
|
|
25
31
|
})(MoonPhase || (exports.MoonPhase = MoonPhase = {}));
|
|
26
32
|
var MonthIndex;
|
|
27
33
|
(function (MonthIndex) {
|
|
28
|
-
MonthIndex[MonthIndex["
|
|
29
|
-
MonthIndex[MonthIndex["
|
|
34
|
+
MonthIndex[MonthIndex["Migasir"] = 0] = "Migasir";
|
|
35
|
+
MonthIndex[MonthIndex["Boss"] = 1] = "Boss";
|
|
30
36
|
MonthIndex[MonthIndex["Meak"] = 2] = "Meak";
|
|
31
|
-
MonthIndex[MonthIndex["
|
|
32
|
-
MonthIndex[MonthIndex["
|
|
33
|
-
MonthIndex[MonthIndex["
|
|
37
|
+
MonthIndex[MonthIndex["Phalkun"] = 3] = "Phalkun";
|
|
38
|
+
MonthIndex[MonthIndex["Cheit"] = 4] = "Cheit";
|
|
39
|
+
MonthIndex[MonthIndex["Pisakh"] = 5] = "Pisakh";
|
|
34
40
|
MonthIndex[MonthIndex["Jesth"] = 6] = "Jesth";
|
|
35
|
-
MonthIndex[MonthIndex["
|
|
41
|
+
MonthIndex[MonthIndex["Asadh"] = 7] = "Asadh";
|
|
36
42
|
MonthIndex[MonthIndex["Srap"] = 8] = "Srap";
|
|
37
|
-
MonthIndex[MonthIndex["
|
|
43
|
+
MonthIndex[MonthIndex["Phatrabot"] = 9] = "Phatrabot";
|
|
38
44
|
MonthIndex[MonthIndex["Assoch"] = 10] = "Assoch";
|
|
39
|
-
MonthIndex[MonthIndex["
|
|
40
|
-
MonthIndex[MonthIndex["
|
|
41
|
-
MonthIndex[MonthIndex["
|
|
45
|
+
MonthIndex[MonthIndex["Kadeuk"] = 11] = "Kadeuk";
|
|
46
|
+
MonthIndex[MonthIndex["Pathamasadh"] = 12] = "Pathamasadh";
|
|
47
|
+
MonthIndex[MonthIndex["Tutiyasadh"] = 13] = "Tutiyasadh"; // ទុតិយាសាឍ
|
|
42
48
|
})(MonthIndex || (exports.MonthIndex = MonthIndex = {}));
|
|
43
49
|
var AnimalYear;
|
|
44
50
|
(function (AnimalYear) {
|
|
@@ -57,16 +63,16 @@ var AnimalYear;
|
|
|
57
63
|
})(AnimalYear || (exports.AnimalYear = AnimalYear = {}));
|
|
58
64
|
var EraYear;
|
|
59
65
|
(function (EraYear) {
|
|
60
|
-
EraYear[EraYear["
|
|
61
|
-
EraYear[EraYear["
|
|
62
|
-
EraYear[EraYear["
|
|
63
|
-
EraYear[EraYear["
|
|
64
|
-
EraYear[EraYear["
|
|
65
|
-
EraYear[EraYear["
|
|
66
|
-
EraYear[EraYear["
|
|
67
|
-
EraYear[EraYear["
|
|
68
|
-
EraYear[EraYear["
|
|
69
|
-
EraYear[EraYear["
|
|
66
|
+
EraYear[EraYear["SamridhiSak"] = 0] = "SamridhiSak";
|
|
67
|
+
EraYear[EraYear["AekSak"] = 1] = "AekSak";
|
|
68
|
+
EraYear[EraYear["ToSak"] = 2] = "ToSak";
|
|
69
|
+
EraYear[EraYear["TreiSak"] = 3] = "TreiSak";
|
|
70
|
+
EraYear[EraYear["ChattvaSak"] = 4] = "ChattvaSak";
|
|
71
|
+
EraYear[EraYear["PanchaSak"] = 5] = "PanchaSak";
|
|
72
|
+
EraYear[EraYear["ChhaSak"] = 6] = "ChhaSak";
|
|
73
|
+
EraYear[EraYear["SappaSak"] = 7] = "SappaSak";
|
|
74
|
+
EraYear[EraYear["AtthaSak"] = 8] = "AtthaSak";
|
|
75
|
+
EraYear[EraYear["NappaSak"] = 9] = "NappaSak"; // នព្វស័ក
|
|
70
76
|
})(EraYear || (exports.EraYear = EraYear = {}));
|
|
71
77
|
var DayOfWeek;
|
|
72
78
|
(function (DayOfWeek) {
|
|
@@ -372,7 +378,7 @@ function getNumberOfDaysInKhmerMonth(monthIndex, beYear) {
|
|
|
372
378
|
if (idx === MonthIndex.Jesth && leapType === 2) { // ជេស្ឋ with leap day
|
|
373
379
|
return 30;
|
|
374
380
|
}
|
|
375
|
-
if (idx === MonthIndex.
|
|
381
|
+
if (idx === MonthIndex.Pathamasadh || idx === MonthIndex.Tutiyasadh) { // បឋមាសាឍ, ទុតិយាសាឍ
|
|
376
382
|
return leapType === 1 ? 30 : 0;
|
|
377
383
|
}
|
|
378
384
|
// Alternating pattern: even months = 29 days, odd months = 30 days
|
|
@@ -391,26 +397,26 @@ function nextMonthOf(monthIndex, beYear) {
|
|
|
391
397
|
const leapType = getLeapType(beYear);
|
|
392
398
|
const idx = typeof monthIndex === 'number' ? monthIndex : monthIndex;
|
|
393
399
|
if (idx === MonthIndex.Jesth && leapType === 1) { // ជេស្ឋ in leap month year
|
|
394
|
-
return MonthIndex.
|
|
400
|
+
return MonthIndex.Pathamasadh; // បឋមាសាឍ
|
|
395
401
|
}
|
|
396
|
-
if (idx === MonthIndex.
|
|
397
|
-
return MonthIndex.
|
|
398
|
-
if (idx === MonthIndex.
|
|
399
|
-
return MonthIndex.
|
|
400
|
-
if (idx === MonthIndex.
|
|
402
|
+
if (idx === MonthIndex.Kadeuk)
|
|
403
|
+
return MonthIndex.Migasir; // កត្ដិក -> មិគសិរ
|
|
404
|
+
if (idx === MonthIndex.Pathamasadh)
|
|
405
|
+
return MonthIndex.Tutiyasadh; // បឋមាសាឍ -> ទុតិយាសាឍ
|
|
406
|
+
if (idx === MonthIndex.Tutiyasadh)
|
|
401
407
|
return MonthIndex.Srap; // ទុតិយាសាឍ -> ស្រាពណ៍
|
|
402
408
|
return (idx + 1);
|
|
403
409
|
}
|
|
404
410
|
function previousMonthOf(monthIndex, beYear) {
|
|
405
411
|
const leapType = getLeapType(beYear);
|
|
406
412
|
const idx = typeof monthIndex === 'number' ? monthIndex : monthIndex;
|
|
407
|
-
if (idx === MonthIndex.
|
|
408
|
-
return MonthIndex.
|
|
413
|
+
if (idx === MonthIndex.Migasir)
|
|
414
|
+
return MonthIndex.Kadeuk; // មិគសិរ -> កត្ដិក
|
|
409
415
|
if (idx === MonthIndex.Srap && leapType === 1)
|
|
410
|
-
return MonthIndex.
|
|
411
|
-
if (idx === MonthIndex.
|
|
412
|
-
return MonthIndex.
|
|
413
|
-
if (idx === MonthIndex.
|
|
416
|
+
return MonthIndex.Tutiyasadh; // ស្រាពណ៍ -> ទុតិយាសាឍ (leap)
|
|
417
|
+
if (idx === MonthIndex.Tutiyasadh)
|
|
418
|
+
return MonthIndex.Pathamasadh; // ទុតិយាសាឍ -> បឋមាសាឍ
|
|
419
|
+
if (idx === MonthIndex.Pathamasadh)
|
|
414
420
|
return MonthIndex.Jesth; // បឋមាសាឍ -> ជេស្ឋ
|
|
415
421
|
return (idx - 1);
|
|
416
422
|
}
|
|
@@ -601,7 +607,7 @@ class KhmerDate {
|
|
|
601
607
|
const newDayNum = currentDayNum + remaining;
|
|
602
608
|
const newDay = KhmerDate.fromDayNumber(newDayNum);
|
|
603
609
|
let newBeYear = result.beYear;
|
|
604
|
-
if (result.monthIndex === MonthIndex.
|
|
610
|
+
if (result.monthIndex === MonthIndex.Pisakh) { // ពិសាខ
|
|
605
611
|
if (result.moonPhase === MoonPhase.Waxing && newDay.moonPhase === MoonPhase.Waning) {
|
|
606
612
|
newBeYear++;
|
|
607
613
|
}
|
|
@@ -612,7 +618,7 @@ class KhmerDate {
|
|
|
612
618
|
else {
|
|
613
619
|
remaining -= (daysLeftInMonth + 1);
|
|
614
620
|
const nextMonth = nextMonthOf(result.monthIndex, result.beYear);
|
|
615
|
-
const newBeYear = (result.monthIndex === MonthIndex.
|
|
621
|
+
const newBeYear = (result.monthIndex === MonthIndex.Cheit) ? result.beYear + 1 : result.beYear;
|
|
616
622
|
result = new KhmerDate(1, MoonPhase.Waxing, nextMonth, newBeYear); // Start at 1កើត
|
|
617
623
|
}
|
|
618
624
|
}
|
|
@@ -629,7 +635,7 @@ class KhmerDate {
|
|
|
629
635
|
const newDayNum = currentDayNum - remaining;
|
|
630
636
|
const newDay = KhmerDate.fromDayNumber(newDayNum);
|
|
631
637
|
let newBeYear = result.beYear;
|
|
632
|
-
if (result.monthIndex === MonthIndex.
|
|
638
|
+
if (result.monthIndex === MonthIndex.Pisakh) { // ពិសាខ
|
|
633
639
|
if (result.moonPhase === MoonPhase.Waning && newDay.moonPhase === MoonPhase.Waxing) {
|
|
634
640
|
newBeYear--;
|
|
635
641
|
}
|
|
@@ -640,7 +646,7 @@ class KhmerDate {
|
|
|
640
646
|
else {
|
|
641
647
|
remaining -= (currentDayNum + 1);
|
|
642
648
|
const prevMonth = previousMonthOf(result.monthIndex, result.beYear);
|
|
643
|
-
const newBeYear = (result.monthIndex === MonthIndex.
|
|
649
|
+
const newBeYear = (result.monthIndex === MonthIndex.Pisakh) ? result.beYear - 1 : result.beYear;
|
|
644
650
|
const daysInPrevMonth = getNumberOfDaysInKhmerMonth(prevMonth, newBeYear);
|
|
645
651
|
const newDay = KhmerDate.fromDayNumber(daysInPrevMonth - 1);
|
|
646
652
|
result = new KhmerDate(newDay.day, newDay.moonPhase, prevMonth, newBeYear);
|
|
@@ -665,7 +671,7 @@ function getMaybeBEYear(year, month) {
|
|
|
665
671
|
return year + 544;
|
|
666
672
|
}
|
|
667
673
|
}
|
|
668
|
-
// Cache for
|
|
674
|
+
// Cache for Pisakha Bochea dates by year
|
|
669
675
|
const visakhaBocheaCache = {};
|
|
670
676
|
// Cache for New Year Full Info
|
|
671
677
|
const newYearInfoCache = {};
|
|
@@ -674,7 +680,7 @@ const newYearInfoCache = {};
|
|
|
674
680
|
* BE year increases on ១រោច ខែពិសាខ (1st waning day of Pisakh = dayNumber 15 of month 5)
|
|
675
681
|
* Returns timestamp in milliseconds at midnight of that day
|
|
676
682
|
*/
|
|
677
|
-
function
|
|
683
|
+
function getPisakhaBochea(year, isSearching = false) {
|
|
678
684
|
if (visakhaBocheaCache[year]) {
|
|
679
685
|
return visakhaBocheaCache[year];
|
|
680
686
|
}
|
|
@@ -684,7 +690,7 @@ function getVisakhaBochea(year, isSearching = false) {
|
|
|
684
690
|
for (let searchDay = 1; searchDay <= daysInMonth; searchDay++) {
|
|
685
691
|
// Avoid infinite recursion by using simplified BE year during search
|
|
686
692
|
const result = gregorianToKhmerInternal(year, searchMonth, searchDay, 12, 0, 0, true);
|
|
687
|
-
if (result.khmer.monthIndex === MonthIndex.
|
|
693
|
+
if (result.khmer.monthIndex === MonthIndex.Pisakh && result._khmerDateObj.getDayNumber() === 15) {
|
|
688
694
|
// Found 1រោច Pisakh - return timestamp at midnight (start of BE year change day)
|
|
689
695
|
// BE year changes at 00:00 on this day
|
|
690
696
|
const timestamp = new Date(year, searchMonth - 1, searchDay, 0, 0, 0, 0).getTime();
|
|
@@ -770,7 +776,7 @@ function gregorianToKhmerInternal(year, month, day, hour = 0, minute = 0, second
|
|
|
770
776
|
else {
|
|
771
777
|
// Normal mode: compare against exact BE year transition datetime (1រោច Pisakh at 00:00)
|
|
772
778
|
const inputTimestamp = new Date(year, month - 1, day, hour, minute, second).getTime();
|
|
773
|
-
const beYearTransitionTimestamp =
|
|
779
|
+
const beYearTransitionTimestamp = getPisakhaBochea(year);
|
|
774
780
|
if (inputTimestamp >= beYearTransitionTimestamp) {
|
|
775
781
|
// On or after 1រោច Pisakh (new BE year)
|
|
776
782
|
beYear = year + 544;
|
|
@@ -784,14 +790,14 @@ function gregorianToKhmerInternal(year, month, day, hour = 0, minute = 0, second
|
|
|
784
790
|
let jsYear = beToJs(beYear);
|
|
785
791
|
let animalYearIndex = ((beYear + 4) % 12 + 12) % 12;
|
|
786
792
|
// Adjust Era and Animal Year based on Khmer New Year logic
|
|
787
|
-
// They should change at New Year, not wait for
|
|
793
|
+
// They should change at New Year, not wait for Pisakha Bochea (which changes BE)
|
|
788
794
|
if (!isSearching) {
|
|
789
795
|
const newYearInfo = getNewYearFullInfo(year);
|
|
790
796
|
const inputTimestamp = new Date(year, month - 1, day, hour, minute, second).getTime();
|
|
791
|
-
const visakhaBocheaTimestamp =
|
|
797
|
+
const visakhaBocheaTimestamp = getPisakhaBochea(year);
|
|
792
798
|
// Animal Year changes at Moha Songkran (exact New Year time)
|
|
793
|
-
// Only apply manual increment if we are in the gap between New Year and
|
|
794
|
-
// (After
|
|
799
|
+
// Only apply manual increment if we are in the gap between New Year and Pisakha Bochea
|
|
800
|
+
// (After Pisakha Bochea, the BE year increments, so the formula based on BE automatically gives the new Animal Year)
|
|
795
801
|
if (inputTimestamp >= newYearInfo.newYearMoment.getTime() && inputTimestamp <= visakhaBocheaTimestamp) {
|
|
796
802
|
animalYearIndex = (animalYearIndex + 1) % 12;
|
|
797
803
|
}
|
|
@@ -843,7 +849,7 @@ function khmerToGregorian(day, moonPhase, monthIndex, beYear) {
|
|
|
843
849
|
for (let gDay = 1; gDay <= daysInMonth; gDay++) {
|
|
844
850
|
// For BE year transition day (1រោច Pisakh) and the day before (15កើត Pisakh),
|
|
845
851
|
// check multiple times during the day because BE year can change during this period
|
|
846
|
-
const isAroundBEYearChange = monthIndexNum === MonthIndex.
|
|
852
|
+
const isAroundBEYearChange = monthIndexNum === MonthIndex.Pisakh &&
|
|
847
853
|
((day === 15 && moonPhaseNum === MoonPhase.Waxing) || (day === 1 && moonPhaseNum === MoonPhase.Waning));
|
|
848
854
|
const timesToCheck = isAroundBEYearChange
|
|
849
855
|
? [0, 6, 12, 18, 23] // Check at different hours
|
|
@@ -963,7 +969,7 @@ function getKhmerNewYear(ceYear) {
|
|
|
963
969
|
// ============================================================================
|
|
964
970
|
// Formatting Functions
|
|
965
971
|
// ============================================================================
|
|
966
|
-
function
|
|
972
|
+
function formatKhmer(khmerData, formatString) {
|
|
967
973
|
if (!formatString) {
|
|
968
974
|
// Default format
|
|
969
975
|
const { khmer } = khmerData;
|
|
@@ -1004,49 +1010,54 @@ function gregorianToKhmer(year, month, day, hour = 0, minute = 0, second = 0) {
|
|
|
1004
1010
|
// ============================================================================
|
|
1005
1011
|
// Public API
|
|
1006
1012
|
// ============================================================================
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1013
|
+
// Conversion functions
|
|
1014
|
+
function fromGregorian(year, month, day, hour = 0, minute = 0, second = 0) {
|
|
1015
|
+
return gregorianToKhmer(year, month, day, hour, minute, second);
|
|
1016
|
+
}
|
|
1017
|
+
function fromKhmer(day, moonPhase, monthIndex, beYear) {
|
|
1018
|
+
return khmerToGregorian(day, moonPhase, monthIndex, beYear);
|
|
1019
|
+
}
|
|
1020
|
+
// New Year function
|
|
1021
|
+
function getNewYear(ceYear) {
|
|
1022
|
+
return getKhmerNewYear(ceYear);
|
|
1023
|
+
}
|
|
1024
|
+
// Format function
|
|
1025
|
+
function format(khmerData, formatString) {
|
|
1026
|
+
return formatKhmer(khmerData, formatString);
|
|
1027
|
+
}
|
|
1028
|
+
// Utility for creating date from Date object
|
|
1029
|
+
function fromDate(date) {
|
|
1030
|
+
// Validate Date object
|
|
1031
|
+
validateDateObject(date);
|
|
1032
|
+
return gregorianToKhmer(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
|
|
1033
|
+
}
|
|
1034
|
+
// Convert Khmer to Date object
|
|
1035
|
+
function toDate(day, moonPhase, monthIndex, beYear) {
|
|
1036
|
+
const greg = khmerToGregorian(day, moonPhase, monthIndex, beYear);
|
|
1037
|
+
return new Date(greg.year, greg.month - 1, greg.day);
|
|
1038
|
+
}
|
|
1039
|
+
// Constants export
|
|
1040
|
+
exports.constants = {
|
|
1041
|
+
LunarMonths,
|
|
1042
|
+
LunarMonthNames,
|
|
1043
|
+
SolarMonthNames,
|
|
1044
|
+
AnimalYearNames,
|
|
1045
|
+
EraYearNames,
|
|
1046
|
+
WeekdayNames,
|
|
1047
|
+
MoonStatusNames
|
|
1048
|
+
};
|
|
1049
|
+
// Default export - aggregate all exports for convenience
|
|
1050
|
+
exports.default = {
|
|
1051
|
+
fromGregorian,
|
|
1052
|
+
fromKhmer,
|
|
1053
|
+
getNewYear,
|
|
1054
|
+
format,
|
|
1055
|
+
fromDate,
|
|
1056
|
+
toDate,
|
|
1057
|
+
constants: exports.constants,
|
|
1045
1058
|
MoonPhase,
|
|
1046
1059
|
MonthIndex,
|
|
1047
1060
|
AnimalYear,
|
|
1048
1061
|
EraYear,
|
|
1049
1062
|
DayOfWeek
|
|
1050
1063
|
};
|
|
1051
|
-
// Default export
|
|
1052
|
-
exports.default = exports.momentkh;
|