node-web-mvc
Version:
node spring mvc
140 lines (139 loc) • 6.07 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DateTimeParseException_1 = __importDefault(require("../../errors/DateTimeParseException"));
const InvalidDateTimeFormatException_1 = __importDefault(require("../../errors/InvalidDateTimeFormatException"));
const Locale_1 = __importDefault(require("../../locale/Locale"));
const formatters_1 = __importDefault(require("./formatters"));
const parsers_1 = __importStar(require("./parsers"));
const patternRegexp = /(y+|M+|d+|h+|H+|m+|s+|S+|E+|L+|q|a|Z|w|W)/g;
const alias = {
'L': 'M',
};
class DateConverter {
constructor(pattern, locale) {
this.pattern = pattern;
this.locale = locale || Locale_1.default.ENGLISH;
}
static of(pattern, locale) {
return new DateConverter(pattern, locale);
}
getExpression(expressions, index) {
const exp = expressions[index];
const key = alias[exp === null || exp === void 0 ? void 0 : exp[0]] || (exp === null || exp === void 0 ? void 0 : exp[0]);
return {
exp: exp || '',
key,
handler: parsers_1.default[key],
};
}
/**
* 根据指定格式将字符串解析成日期对象
* @param raw 日期字符串
* @param pattern 日期格式
* @returns
*/
parse(raw) {
var _a;
if (!raw)
return null;
const record = {};
const expressions = this.pattern.replace(patternRegexp, (match) => `\n${match}\n`).split('\n').filter((m) => m !== '').map((m) => m.replace(/'/g, ''));
let readValue = '';
let expIndex = 0;
let current = this.getExpression(expressions, expIndex);
let next = this.getExpression(expressions, expIndex + 1);
for (let i = 0, k = raw.length; i < k; i++) {
readValue += raw[i];
const exp = current.exp;
const key = current.key;
const splitChar = next.exp[0];
const isSatisfy = next.handler ? true : splitChar == raw[i + 1];
const v = (_a = current.handler) === null || _a === void 0 ? void 0 : _a.call(current, readValue, exp, this.locale);
if ((v && isSatisfy) || readValue === exp) {
v && (record[key] = v);
readValue = '';
expIndex++;
current = this.getExpression(expressions, expIndex);
next = this.getExpression(expressions, expIndex + 1);
}
}
if (expIndex < (expressions.length - 1)) {
throw new DateTimeParseException_1.default(raw, this.pattern, expressions[expIndex]);
}
const startYear = 1970;
const year = parseInt(record.y || startYear.toString());
const month = (parseInt(record.M || '1') - 1);
const day = parseInt(record.d || '1');
const hours = parseInt(record.h || record.H || '0');
const minutes = parseInt(record.m || '0');
const seconds = parseInt(record.s || '0');
const millseconds = parseInt(record.S || '0');
const zone = parseInt(record.Z || '0');
const pam = parseInt(record.a || '-1');
let date = new Date(year, month, day, hours, minutes, seconds, millseconds);
const offset = zone ? date.getTimezoneOffset() - zone : 0;
if (pam == 1) {
// 根据上下午 推敲小时
date.setHours(date.getHours() + 12);
}
if (offset !== 0) {
// 补充时差计算
const time = date.getTime() - offset * 60000;
date = new Date(time);
}
if (!record.d && record.E) {
// 在没有日期场景下,根据星期来设置日期
const week = (0, parsers_1.getNormalDay)(date);
const offset = parseInt(record.E) - week;
const isStartYear = year == startYear && month == 0;
const offsetDay = isStartYear ? 7 - week + parseInt(record.E) : offset;
date = new Date(date.getTime() + offsetDay * 24 * 60 * 60 * 1000);
}
if (/Invalid/i.test(date.toString())) {
throw new InvalidDateTimeFormatException_1.default(raw, this.pattern);
}
return date;
}
;
/**
* 将日期对象格式化成指定格式的字符串
* @param date 日期对象
* @param pattern 日期格式
* @returns
*/
format(date) {
var _a, _b;
return (_b = (_a = this.pattern.replace(patternRegexp, (match) => {
const key = alias[match === null || match === void 0 ? void 0 : match[0]] || (match === null || match === void 0 ? void 0 : match[0]);
const handler = formatters_1.default[key];
return handler ? handler(date, match, this.locale) : match;
})).replace) === null || _b === void 0 ? void 0 : _b.call(_a, /'/g, '');
}
}
exports.default = DateConverter;