19b256929Sopenharmony_ci/*
29b256929Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49b256929Sopenharmony_ci * you may not use this file except in compliance with the License.
59b256929Sopenharmony_ci * You may obtain a copy of the License at
69b256929Sopenharmony_ci *
79b256929Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89b256929Sopenharmony_ci *
99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129b256929Sopenharmony_ci * See the License for the specific language governing permissions and
139b256929Sopenharmony_ci * limitations under the License.
149b256929Sopenharmony_ci */
159b256929Sopenharmony_ciexport function ConvertLunarCalendar(gregorianCalendarYear, gregorianCalendarMonth, gregorianCalendarDay) {
169b256929Sopenharmony_ci    let lunarIndex1 = 2,
179b256929Sopenharmony_ci        lunarIndex2 = 9,
189b256929Sopenharmony_ci        lunarIndex3 = 10,
199b256929Sopenharmony_ci        lunarIndex4 = 11,
209b256929Sopenharmony_ci        lunarDay1 = 20,
219b256929Sopenharmony_ci        lunarDay2 = 21,
229b256929Sopenharmony_ci        hour = 24,
239b256929Sopenharmony_ci        minutes = 60,
249b256929Sopenharmony_ci        multiple = 1000,
259b256929Sopenharmony_ci        initialLunarTime = 1949
269b256929Sopenharmony_ci
279b256929Sopenharmony_ci    let lunarMonth = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'],
289b256929Sopenharmony_ci        lunarDay = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '初', '廿'],
299b256929Sopenharmony_ci        heavenlyStemsAnd = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'],
309b256929Sopenharmony_ci        earthlyBranches = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
319b256929Sopenharmony_ci
329b256929Sopenharmony_ci    let LUNAR_MON_START_INDEX = 0
339b256929Sopenharmony_ci    let LUNAR_MON_END_INDEX = 11
349b256929Sopenharmony_ci
359b256929Sopenharmony_ci    let lunarCalendar = [
369b256929Sopenharmony_ci        0x0b557,
379b256929Sopenharmony_ci        0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,
389b256929Sopenharmony_ci        0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
399b256929Sopenharmony_ci        0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,
409b256929Sopenharmony_ci        0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
419b256929Sopenharmony_ci        0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
429b256929Sopenharmony_ci        0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
439b256929Sopenharmony_ci        0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
449b256929Sopenharmony_ci        0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
459b256929Sopenharmony_ci        0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
469b256929Sopenharmony_ci        0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,
479b256929Sopenharmony_ci        0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0,
489b256929Sopenharmony_ci        0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4,
499b256929Sopenharmony_ci        0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0,
509b256929Sopenharmony_ci        0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160,
519b256929Sopenharmony_ci        0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252,
529b256929Sopenharmony_ci        0x0d520
539b256929Sopenharmony_ci    ]
549b256929Sopenharmony_ci
559b256929Sopenharmony_ci    function ConvertLunarCalendar(gregorianCalendarYear, gregorianCalendarMonth, gregorianCalendarDay) {
569b256929Sopenharmony_ci        gregorianCalendarMonth -= 1;
579b256929Sopenharmony_ci        let daySpan = (Date.UTC(gregorianCalendarYear, gregorianCalendarMonth, gregorianCalendarDay) - Date.UTC(initialLunarTime, 0, leapFebruarySmallDay)) / (hour * minutes * minutes * multiple) + 1;
589b256929Sopenharmony_ci        let outputLunarYear, outputLunarMonth, outputLunarDay;
599b256929Sopenharmony_ci        for (let j = 0; j < lunarCalendar.length; j++) {
609b256929Sopenharmony_ci            daySpan -= lunarYearDays(lunarCalendar[j]);
619b256929Sopenharmony_ci            if (daySpan <= 0) {
629b256929Sopenharmony_ci                outputLunarYear = initialLunarTime + j;
639b256929Sopenharmony_ci                daySpan += lunarYearDays(lunarCalendar[j]);
649b256929Sopenharmony_ci                break
659b256929Sopenharmony_ci            }
669b256929Sopenharmony_ci        }
679b256929Sopenharmony_ci        let k = 0
689b256929Sopenharmony_ci        for (; k < lunarYearMonths(lunarCalendar[outputLunarYear - initialLunarTime]).length; k++) {
699b256929Sopenharmony_ci            daySpan -= lunarYearMonths(lunarCalendar[outputLunarYear - initialLunarTime])[k];
709b256929Sopenharmony_ci            if (daySpan <= 0) {
719b256929Sopenharmony_ci                if (hasLeapMonth(lunarCalendar[outputLunarYear - initialLunarTime]) > -1 && hasLeapMonth(lunarCalendar[outputLunarYear - initialLunarTime]) <= k) {
729b256929Sopenharmony_ci                    if (hasLeapMonth(lunarCalendar[outputLunarYear - initialLunarTime]) < k) {
739b256929Sopenharmony_ci                        outputLunarMonth = k;
749b256929Sopenharmony_ci                    } else if (hasLeapMonth(lunarCalendar[outputLunarYear - initialLunarTime]) === k) {
759b256929Sopenharmony_ci                        outputLunarMonth = '闰' + k;
769b256929Sopenharmony_ci                    } else {
779b256929Sopenharmony_ci                        outputLunarMonth = k + 1;
789b256929Sopenharmony_ci                    }
799b256929Sopenharmony_ci                } else {
809b256929Sopenharmony_ci                    outputLunarMonth = k + 1;
819b256929Sopenharmony_ci                }
829b256929Sopenharmony_ci                daySpan += lunarYearMonths(lunarCalendar[outputLunarYear - initialLunarTime])[k];
839b256929Sopenharmony_ci                break
849b256929Sopenharmony_ci            }
859b256929Sopenharmony_ci        }
869b256929Sopenharmony_ci        if (outputLunarMonth == undefined) {
879b256929Sopenharmony_ci            outputLunarMonth = (k > LUNAR_MON_END_INDEX) ? LUNAR_MON_END_INDEX + 1 : k;
889b256929Sopenharmony_ci            outputLunarMonth = (k == LUNAR_MON_START_INDEX) ? LUNAR_MON_START_INDEX + 1 : k;
899b256929Sopenharmony_ci        } else {
909b256929Sopenharmony_ci            outputLunarMonth = (outputLunarMonth > LUNAR_MON_END_INDEX) ? LUNAR_MON_END_INDEX + 1 : outputLunarMonth;
919b256929Sopenharmony_ci            outputLunarMonth = (outputLunarMonth == LUNAR_MON_START_INDEX) ? LUNAR_MON_START_INDEX + 1 : outputLunarMonth;
929b256929Sopenharmony_ci        }
939b256929Sopenharmony_ci
949b256929Sopenharmony_ci        outputLunarDay = daySpan;
959b256929Sopenharmony_ci        if (hasLeapMonth(lunarCalendar[outputLunarYear - initialLunarTime]) > -1 && (typeof (outputLunarMonth) === 'string' && outputLunarMonth.indexOf('闰') > -1)) {
969b256929Sopenharmony_ci            let reg = /\d/.exec(outputLunarMonth)
979b256929Sopenharmony_ci            outputLunarMonth = `闰${lunarMonth[Number(reg)- 1]}`
989b256929Sopenharmony_ci        } else {
999b256929Sopenharmony_ci            outputLunarMonth = lunarMonth[outputLunarMonth - 1];
1009b256929Sopenharmony_ci        }
1019b256929Sopenharmony_ci        outputLunarYear = getHeavenlyStemsAnd(outputLunarYear) + getEarthlyBranches(outputLunarYear);
1029b256929Sopenharmony_ci        if (outputLunarDay < lunarIndex4) {
1039b256929Sopenharmony_ci            outputLunarDay = `${lunarDay[lunarIndex3]}${lunarDay[outputLunarDay-1]}`
1049b256929Sopenharmony_ci        } else if (outputLunarDay > lunarIndex3 && outputLunarDay < lunarDay1) {
1059b256929Sopenharmony_ci            outputLunarDay = `${lunarDay[lunarIndex2]}${lunarDay[outputLunarDay-lunarIndex4]}`
1069b256929Sopenharmony_ci        } else if (outputLunarDay === lunarDay1) {
1079b256929Sopenharmony_ci            outputLunarDay = `${lunarDay[1]}${lunarDay[lunarIndex2]}`
1089b256929Sopenharmony_ci        } else if (outputLunarDay > lunarDay1 && outputLunarDay < leapFebruaryBigDay) {
1099b256929Sopenharmony_ci            outputLunarDay = `${lunarDay[lunarIndex4]}${lunarDay[outputLunarDay-lunarDay2]}`
1109b256929Sopenharmony_ci        } else if (outputLunarDay === leapFebruaryBigDay) {
1119b256929Sopenharmony_ci            outputLunarDay = `${lunarDay[lunarIndex1]}${lunarDay[lunarIndex2]}`
1129b256929Sopenharmony_ci        }
1139b256929Sopenharmony_ci        return {
1149b256929Sopenharmony_ci            lunarYear: outputLunarYear,
1159b256929Sopenharmony_ci            lunarMonth: outputLunarMonth,
1169b256929Sopenharmony_ci            lunarDay: outputLunarDay,
1179b256929Sopenharmony_ci        }
1189b256929Sopenharmony_ci    }
1199b256929Sopenharmony_ci
1209b256929Sopenharmony_ci    function hasLeapMonth(outputLunarYear) {
1219b256929Sopenharmony_ci        let lastHexadecimalDigit = 0xf
1229b256929Sopenharmony_ci        if (outputLunarYear & lastHexadecimalDigit) {
1239b256929Sopenharmony_ci            return outputLunarYear & lastHexadecimalDigit
1249b256929Sopenharmony_ci        } else {
1259b256929Sopenharmony_ci            return -1
1269b256929Sopenharmony_ci        }
1279b256929Sopenharmony_ci    }
1289b256929Sopenharmony_ci
1299b256929Sopenharmony_ci    let leapFebruarySmallDay = 29,
1309b256929Sopenharmony_ci        leapFebruaryBigDay = 30
1319b256929Sopenharmony_ci
1329b256929Sopenharmony_ci    function leapMonthDays(outputLunarYear) {
1339b256929Sopenharmony_ci        let hexadecimalFirstDigit = 0xf0000
1349b256929Sopenharmony_ci        if (hasLeapMonth(outputLunarYear) > -1) {
1359b256929Sopenharmony_ci            return (outputLunarYear & hexadecimalFirstDigit) ? leapFebruaryBigDay : leapFebruarySmallDay
1369b256929Sopenharmony_ci        } else {
1379b256929Sopenharmony_ci            return 0
1389b256929Sopenharmony_ci        }
1399b256929Sopenharmony_ci    }
1409b256929Sopenharmony_ci
1419b256929Sopenharmony_ci    let convertToHexDigit = 0x8000,
1429b256929Sopenharmony_ci        convertToHex = 0x8
1439b256929Sopenharmony_ci
1449b256929Sopenharmony_ci    function lunarYearDays(outputLunarYear) {
1459b256929Sopenharmony_ci        let totalDays = 0;
1469b256929Sopenharmony_ci        for (let i = convertToHexDigit; i > convertToHex; i >>= 1) {
1479b256929Sopenharmony_ci            let monthDays = (outputLunarYear & i) ? leapFebruaryBigDay : leapFebruarySmallDay;
1489b256929Sopenharmony_ci            totalDays += monthDays;
1499b256929Sopenharmony_ci        }
1509b256929Sopenharmony_ci        if (hasLeapMonth(outputLunarYear) > -1) {
1519b256929Sopenharmony_ci            totalDays += leapMonthDays(outputLunarYear);
1529b256929Sopenharmony_ci        }
1539b256929Sopenharmony_ci        return totalDays
1549b256929Sopenharmony_ci    }
1559b256929Sopenharmony_ci
1569b256929Sopenharmony_ci    function lunarYearMonths(outputLunarYear) {
1579b256929Sopenharmony_ci        let monthArr = [];
1589b256929Sopenharmony_ci        for (let i = convertToHexDigit; i > convertToHex; i >>= 1) {
1599b256929Sopenharmony_ci            monthArr.push((outputLunarYear & i) ? leapFebruaryBigDay : leapFebruarySmallDay);
1609b256929Sopenharmony_ci        }
1619b256929Sopenharmony_ci        if (hasLeapMonth(outputLunarYear)) {
1629b256929Sopenharmony_ci            monthArr.splice(hasLeapMonth(outputLunarYear), 0, leapMonthDays(outputLunarYear));
1639b256929Sopenharmony_ci        }
1649b256929Sopenharmony_ci        return monthArr
1659b256929Sopenharmony_ci    }
1669b256929Sopenharmony_ci
1679b256929Sopenharmony_ci    let Day3 = 3
1689b256929Sopenharmony_ci
1699b256929Sopenharmony_ci    function getHeavenlyStemsAnd(outputLunarYear) {
1709b256929Sopenharmony_ci        let heavenlyStemsAndKey = (outputLunarYear - Day3) % lunarIndex3;
1719b256929Sopenharmony_ci        if (heavenlyStemsAndKey === 0) heavenlyStemsAndKey = lunarIndex3;
1729b256929Sopenharmony_ci        return heavenlyStemsAnd[heavenlyStemsAndKey - 1]
1739b256929Sopenharmony_ci    }
1749b256929Sopenharmony_ci
1759b256929Sopenharmony_ci    function getEarthlyBranches(outputLunarYear) {
1769b256929Sopenharmony_ci        let monthMultiple = 12
1779b256929Sopenharmony_ci        let EarthlyBranchesKey = (outputLunarYear - Day3) % monthMultiple;
1789b256929Sopenharmony_ci        if (EarthlyBranchesKey === 0) EarthlyBranchesKey = monthMultiple;
1799b256929Sopenharmony_ci        return earthlyBranches[EarthlyBranchesKey - 1]
1809b256929Sopenharmony_ci    }
1819b256929Sopenharmony_ci
1829b256929Sopenharmony_ci    return ConvertLunarCalendar(gregorianCalendarYear, gregorianCalendarMonth, gregorianCalendarDay)
1839b256929Sopenharmony_ci}