lunar-typescript-optimize
Version:
A TypeScript library for Solar and Chinese Lunar calendar calculations, with optimized build and browser compatibility
106 lines (105 loc) • 3.55 kB
JavaScript
import { Lunar } from "./Lunar.mjs";
import { LunarUtil } from "./LunarUtil.mjs";
import { TaoUtil } from "./TaoUtil.mjs";
import { TaoFestival } from "./TaoFestival.mjs";
import { I18n } from "./I18n.mjs";
export class Tao {
_lunar;
static BIRTH_YEAR = -2697;
constructor(lunar) {
this._lunar = lunar;
}
static fromLunar(lunar) {
return new Tao(lunar);
}
static fromYmdHms(lunarYear, lunarMonth, lunarDay, hour, minute, second) {
return Tao.fromLunar(Lunar.fromYmdHms(lunarYear + Tao.BIRTH_YEAR, lunarMonth, lunarDay, hour, minute, second));
}
static fromYmd(lunarYear, lunarMonth, lunarDay) {
return Tao.fromYmdHms(lunarYear, lunarMonth, lunarDay, 0, 0, 0);
}
getLunar() {
return this._lunar;
}
getYear() {
return this._lunar.getYear() - Tao.BIRTH_YEAR;
}
getMonth() {
return this._lunar.getMonth();
}
getDay() {
return this._lunar.getDay();
}
getYearInChinese() {
const y = this.getYear() + "";
let s = "";
const zero = "0".charCodeAt(0);
for (let i = 0, j = y.length; i < j; i++) {
s += LunarUtil.NUMBER[y.charCodeAt(i) - zero];
}
return s;
}
getMonthInChinese() {
return this._lunar.getMonthInChinese();
}
getDayInChinese() {
return this._lunar.getDayInChinese();
}
getFestivals() {
const l = [];
const fs = TaoUtil.FESTIVAL[this.getMonth() + "-" + this.getDay()];
if (fs) {
fs.forEach((f) => {
l.push(f);
});
}
const jq = this._lunar.getJieQi();
if (I18n.getMessage("jq.dongZhi") === jq) {
l.push(new TaoFestival("\u5143\u59CB\u5929\u5C0A\u5723\u8BDE"));
} else if (I18n.getMessage("jq.xiaZhi") === jq) {
l.push(new TaoFestival("\u7075\u5B9D\u5929\u5C0A\u5723\u8BDE"));
}
return l;
}
_isDayIn(days) {
const md = this.getMonth() + "-" + this.getDay();
for (let i = 0, j = days.length; i < j; i++) {
if (md === days[i]) {
return true;
}
}
return false;
}
isDayMingWu() {
return I18n.getMessage("tg.wu") === this._lunar.getDayGan();
}
isDayTianShe() {
let ret = false;
const mz = this._lunar.getMonthZhi();
const dgz = this._lunar.getDayInGanZhi();
if ([I18n.getMessage("dz.yin"), I18n.getMessage("dz.mao"), I18n.getMessage("dz.chen")].join(",").indexOf(mz) > -1) {
if (I18n.getMessage("jz.wuYin") === dgz) {
ret = true;
}
} else if ([I18n.getMessage("dz.si"), I18n.getMessage("dz.wu"), I18n.getMessage("dz.wei")].join(",").indexOf(mz) > -1) {
if (I18n.getMessage("jz.jiaWu") === dgz) {
ret = true;
}
} else if ([I18n.getMessage("dz.shen"), I18n.getMessage("dz.you"), I18n.getMessage("dz.xu")].join(",").indexOf(mz) > -1) {
if (I18n.getMessage("jz.wuShen") === dgz) {
ret = true;
}
} else if ([I18n.getMessage("dz.hai"), I18n.getMessage("dz.zi"), I18n.getMessage("dz.chou")].join(",").indexOf(mz) > -1) {
if (I18n.getMessage("jz.jiaZi") === dgz) {
ret = true;
}
}
return ret;
}
toString() {
return this.getYearInChinese() + "\u5E74" + this.getMonthInChinese() + "\u6708" + this.getDayInChinese();
}
toFullString() {
return "\u9053\u6B77" + this.getYearInChinese() + "\u5E74\uFF0C\u5929\u904B" + this._lunar.getYearInGanZhi() + "\u5E74\uFF0C" + this._lunar.getMonthInGanZhi() + "\u6708\uFF0C" + this._lunar.getDayInGanZhi() + "\u65E5\u3002" + this.getMonthInChinese() + "\u6708" + this.getDayInChinese() + "\u65E5\uFF0C" + this._lunar.getTimeZhi() + "\u6642\u3002";
}
}