UNPKG

node-web-mvc

Version:
128 lines (127 loc) 5.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Locale_1 = __importDefault(require("../../locale/Locale")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const dayMillseconds = (24 * 60 * 60 * 1000); const cache = new Map(); class DateTimeTextProvider { static register(locale, info) { const key = locale.toString(); if (!cache.get(key)) { cache.set(key, info); } } static getAndRegister(locale) { const key = locale.toString(); if (!cache.has(key)) { const id = path_1.default.join(__dirname, 'locale', key + '.json'); if (fs_1.default.existsSync(id)) { const content = fs_1.default.readFileSync(id).toString('utf-8'); this.register(key, JSON.parse(content)); } } return cache.get(key); } static getLocaleInfo(locale) { return this.getAndRegister(locale) || this.getAndRegister(Locale_1.default.ENGLISH); } static getMonth(raw, locale, full) { const info = this.getLocaleInfo(locale); const data = full ? info.months : info.monthsShort; return data[raw]; } static findMonth(raw, locale, full) { const info = this.getLocaleInfo(locale); const data = full ? info.months : info.monthsShort; const index = data.indexOf(raw); return index > -1 ? index + 1 : null; } static getWeek(raw, locale, full) { const info = this.getLocaleInfo(locale); const data = full ? info.week : info.weekShort; return data[raw]; } static findWeek(raw, locale, full) { const info = this.getLocaleInfo(locale); const data = full ? info.week : info.weekShort; const index = data.indexOf(raw); return index > -1 ? index : null; } static getPAM(hour, locale) { const index = hour < 12 ? 0 : 1; const info = this.getLocaleInfo(locale); const data = info.pam; return data[index]; } static findPAM(raw, locale) { const info = this.getLocaleInfo(locale); const data = info.pam; const index = data.indexOf(raw); return index > -1 ? index : null; } static startOfWeekOffset(days, dow, weekDef) { // 累计天数 - 最后一周的多余天数 = 第一周(不满7天的天数) const weekStart = this.floorMod(days - dow, 7); // 如不满足一周最小天数,则需要将这些天数减除,所以取负数 let offset = -weekStart; if (weekStart + 1 > weekDef.dminimalDaysInFirstWeeky) { // 如果第一周不满7天,但是大于一周最小天数,则需要算入当年的一周 // 也就是返回需要补全的天数 offset = 7 - weekStart; } return offset; } static computeWeek(offset, days) { return Math.floor((7 + offset + days - 1) / 7); } static dayOffYear(date) { const yearStart = new Date(date.getFullYear() - 1, 11, 31); return (date.getTime() - yearStart.getTime()) / dayMillseconds; } static getWeekOfMonth(date, locale) { const week = new Date(date.getFullYear(), date.getMonth(), 1).getDay(); const dayOffset = 7 - week; const days = date.getDate() - dayOffset; const weeks = Math.ceil(days / 7); return (weeks + 1).toString(); } static getWeekOfYear(date, locale) { const info = this.getLocaleInfo(locale); const weekDef = info.weekDefine; // 按照本地语言计算,当前日期是星期几: 当前星期x和开始星期y的绝对差即等于本地星期lx const dow = this.floorMod(date.getDay() - weekDef.firstDayOfWeek, 7) + 1; // 计算当前日期累计天数 const doy = this.dayOffYear(date); // 计算开始周的差值 (开始日期如果不满足一周最小天数,则值为负数,否则为正数) const offset = this.startOfWeekOffset(doy, dow, weekDef); // 计算累计周数 (常数 +7 与 -1 保证最后天数满一周) let week = this.computeWeek(offset, doy); if (week == 0) { // 如果值为0,则需要按照上一年最后一周算 const previous = new Date(date.getTime() - (doy * dayMillseconds)); return this.getWeekOfYear(previous, locale); } else if (week > 50) { const yearEnd = new Date(Date.UTC(date.getFullYear(), 11, 31)); const days = this.dayOffYear(yearEnd); // 如果日期接近年底,可能该日期会计入下一年的第一周,所以这里要进行兼容 const newYearWeek = this.computeWeek(offset, days + weekDef.dminimalDaysInFirstWeeky); if (week >= newYearWeek) { week = week - newYearWeek + 1; } } return week; } static floorMod(x, y) { let mod = x % y; if ((mod ^ y) < 0 && mod != 0) { mod += y; } return mod; } } exports.default = DateTimeTextProvider;