UNPKG

@iro/calendar

Version:

lunar is a calendar library for Solar and Chinese Lunar.

142 lines 5.58 kB
import { SolarUtil } from './SolarUtil'; import { Solar } from './Solar'; var SolarWeek = (function () { function SolarWeek(year, month, day, start) { this._year = year; this._month = month; this._day = day; this._start = start; } SolarWeek.fromYmd = function (year, month, day, start) { return new SolarWeek(year, month, day, start); }; SolarWeek.fromDate = function (date, start) { return SolarWeek.fromYmd(date.getFullYear(), date.getMonth() + 1, date.getDate(), start); }; SolarWeek.prototype.getYear = function () { return this._year; }; SolarWeek.prototype.getMonth = function () { return this._month; }; SolarWeek.prototype.getDay = function () { return this._day; }; SolarWeek.prototype.getStart = function () { return this._start; }; SolarWeek.prototype.getIndex = function () { var offset = Solar.fromYmd(this._year, this._month, 1).getWeek() - this._start; if (offset < 0) { offset += 7; } return Math.ceil((this._day + offset) / 7); }; SolarWeek.prototype.getIndexInYear = function () { var offset = Solar.fromYmd(this._year, 1, 1).getWeek() - this._start; if (offset < 0) { offset += 7; } return Math.ceil((SolarUtil.getDaysInYear(this._year, this._month, this._day) + offset) / 7); }; SolarWeek.prototype.next = function (weeks, separateMonth) { var start = this._start; if (0 === weeks) { return SolarWeek.fromYmd(this._year, this._month, this._day, start); } var solar = Solar.fromYmd(this._year, this._month, this._day); if (separateMonth) { var n = weeks; var week = SolarWeek.fromYmd(this._year, this._month, this._day, start); var month = this._month; var plus = n > 0; while (0 !== n) { solar = solar.next(plus ? 7 : -7); week = SolarWeek.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay(), start); var weekMonth = week.getMonth(); if (month !== weekMonth) { var index = week.getIndex(); if (plus) { if (1 === index) { var firstDay = week.getFirstDay(); week = SolarWeek.fromYmd(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start); weekMonth = week.getMonth(); } else { solar = Solar.fromYmd(week.getYear(), week.getMonth(), 1); week = SolarWeek.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay(), start); } } else { var size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start); if (size === index) { var lastDay = week.getFirstDay().next(6); week = SolarWeek.fromYmd(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); weekMonth = week.getMonth(); } else { solar = Solar.fromYmd(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth())); week = SolarWeek.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay(), start); } } month = weekMonth; } n -= plus ? 1 : -1; } return week; } else { solar = solar.next(weeks * 7); return SolarWeek.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay(), start); } }; SolarWeek.prototype.getFirstDay = function () { var solar = Solar.fromYmd(this._year, this._month, this._day); var prev = solar.getWeek() - this._start; if (prev < 0) { prev += 7; } return solar.next(-prev); }; SolarWeek.prototype.getFirstDayInMonth = function () { var index = 0; var days = this.getDays(); for (var i = 0; i < days.length; i++) { if (this._month === days[i].getMonth()) { index = i; break; } } return days[index]; }; SolarWeek.prototype.getDays = function () { var firstDay = this.getFirstDay(); var l = []; l.push(firstDay); for (var i = 1; i < 7; i++) { l.push(firstDay.next(i)); } return l; }; SolarWeek.prototype.getDaysInMonth = function () { var days = this.getDays(); var l = []; for (var i = 0; i < days.length; i++) { var day = days[i]; if (this._month !== day.getMonth()) { continue; } l.push(day); } return l; }; SolarWeek.prototype.toString = function () { return "".concat(this.getYear(), ".").concat(this.getMonth(), ".").concat(this.getIndex()); }; SolarWeek.prototype.toFullString = function () { return "".concat(this.getYear(), "\u5E74").concat(this.getMonth(), "\u6708\u7B2C").concat(this.getIndex(), "\u5468"); }; return SolarWeek; }()); export { SolarWeek }; //# sourceMappingURL=SolarWeek.js.map