UNPKG

@cloudcome/utils-core

Version:
240 lines (239 loc) 7.93 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const _const = require("./const.cjs"); const core = require("./core.cjs"); const number = require("./string2.cjs"); const type = require("./type.cjs"); const dateOfStartMap = [ ["s", (d) => d.setMilliseconds(0)], ["m", (d) => d.setSeconds(0)], ["h", (d) => d.setMinutes(0)], ["D", (d) => d.setHours(0)], ["W", (d) => d.setHours(0)], ["M", (d) => d.setDate(1)], ["Y", (d) => d.setMonth(0)] ]; function _dateStart(dateValue, symbol = "D") { const date = core.dateParse(dateValue); for (const [sym, fn] of dateOfStartMap) { fn(date); if (symbol === sym) break; } return date; } function dateStartInSecond(dateValue) { return _dateStart(dateValue, "s"); } function dateStartInMinute(dateValue) { return _dateStart(dateValue, "m"); } function dateStartInHour(dateValue) { return _dateStart(dateValue, "h"); } function dateStartInDay(dateValue) { return _dateStart(dateValue, "D"); } function dateStartInMonth(dateValue) { return _dateStart(dateValue, "M"); } function dateStartInYear(dateValue) { return _dateStart(dateValue, "Y"); } const dateOfEndMap = [ ["s", (d) => d.setMilliseconds(999)], ["m", (d) => d.setSeconds(59)], ["h", (d) => d.setMinutes(59)], ["D", (d) => d.setHours(23)], [ "M", (d) => { const d2 = core.dateParse(d); d2.setMonth(d.getMonth() + 1); d2.setDate(0); d.setDate(d2.getDate()); } ], [ "Y", (d) => { d.setMonth(11); d.setDate(31); } ] ]; function _dateEnd(dateValue, symbol = "D") { const date = core.dateParse(dateValue); for (const [sym, fn] of dateOfEndMap) { fn(date); if (symbol === sym) break; } return date; } function dateEndInSecond(dateValue) { return _dateEnd(dateValue, "s"); } function dateEndInMinute(dateValue) { return _dateEnd(dateValue, "m"); } function dateEndInHour(dateValue) { return _dateEnd(dateValue, "h"); } function dateEndInDay(dateValue) { return _dateEnd(dateValue, "D"); } function dateEndInMonth(dateValue) { return _dateEnd(dateValue, "M"); } function dateEndInYear(dateValue) { return _dateEnd(dateValue, "Y"); } function _dateDays(dateValue, unit) { const d = core.dateParse(dateValue); const ds = unit === "M" ? dateStartInMonth(d) : dateStartInYear(d); const de = unit === "M" ? dateEndInMonth(d) : dateEndInYear(d); return Math.ceil((de.getTime() - ds.getTime()) / _const.DATE_DAY_MS); } function dateDaysInMonth(dateValue) { return _dateDays(dateValue, "M"); } function dateDaysInYear(dateValue) { return _dateDays(dateValue, "Y"); } const defaultDiffTemplates = [ [0, 10, "刚刚"], [1, 60, "{n} 秒前", "{n} 秒后"], [60, 60 * 60, "{n} 分钟前", "{n} 分钟后"], [60 * 60, 60 * 60 * 24, "{n} 小时前", "{n} 小时后"], [0, 60 * 60 * 24 * 2, "昨天", "明天"], [0, 60 * 60 * 24 * 3, "前天", "后天"], [60 * 60 * 24, 60 * 60 * 24 * 30, "{n} 天前", "{n} 天后"], [0, Number.POSITIVE_INFINITY, "YYYY年MM月DD日"] ]; function dateRelative(dateValue, refDateValue, templates) { const now = Date.now(); const refDateValueFinal = type.isArray(refDateValue) ? now : refDateValue || now; const templatesFinal = type.isArray(templates) ? templates : type.isArray(refDateValue) ? refDateValue : defaultDiffTemplates; const d1 = core.dateParse(dateValue); const d2 = core.dateParse(refDateValueFinal); const diff = d1.getTime() - d2.getTime(); const isAgo = diff < 0; const absDiff = Math.abs(diff); let relative = ""; for (const [base, max, agoTemplate, featureTemplate] of templatesFinal) { const unitFinal = base * 1e3; const maxFinal = max * 1e3; if (absDiff < maxFinal) { const template = isAgo ? agoTemplate : featureTemplate || agoTemplate; const length = unitFinal === 0 ? 0 : Math.max(Math.floor(absDiff / unitFinal), 1); relative = unitFinal === 0 ? core.dateFormat(dateValue, template) : number.stringFormat(template, { n: length }); break; } } return relative; } function isLeapYear(year) { if (year % 4 !== 0) return false; if (year % 100 !== 0) return true; if (year % 400 !== 0) return false; return true; } function _isSameDateIn(date1, date2, sameSymbol = "D") { const defines = [ ["Y", (d) => d.getFullYear()], ["M", (d) => d.getMonth()], ["D", (d) => d.getDate()], ["h", (d) => d.getHours()], ["m", (d) => d.getMinutes()], ["s", (d) => d.getSeconds()], ["S", (d) => d.getMilliseconds()] ]; const d1 = core.dateParse(date1); const d2 = core.dateParse(date2); for (const [sym, fn] of defines) { if (fn(d1) !== fn(d2)) { return false; } if (sym === sameSymbol) break; } return true; } function isSameDateInYear(date1, date2) { return _isSameDateIn(date1, date2, "Y"); } function isSameDateInMonth(date1, date2) { return _isSameDateIn(date1, date2, "M"); } function isSameDateInDay(date1, date2) { return _isSameDateIn(date1, date2, "D"); } function isSameDateInHour(date1, date2) { return _isSameDateIn(date1, date2, "h"); } function isSameDateInMinute(date1, date2) { return _isSameDateIn(date1, date2, "m"); } function isSameDateInSecond(date1, date2) { return _isSameDateIn(date1, date2, "s"); } var EWeekStart = /* @__PURE__ */ ((EWeekStart2) => { EWeekStart2[EWeekStart2["sunday"] = 0] = "sunday"; EWeekStart2[EWeekStart2["monday"] = 1] = "monday"; EWeekStart2[EWeekStart2["tuesday"] = 2] = "tuesday"; EWeekStart2[EWeekStart2["wednesday"] = 3] = "wednesday"; EWeekStart2[EWeekStart2["thursday"] = 4] = "thursday"; EWeekStart2[EWeekStart2["friday"] = 5] = "friday"; EWeekStart2[EWeekStart2["saturday"] = 6] = "saturday"; return EWeekStart2; })(EWeekStart || {}); function _dateWeeks(dateValue, type2, weekStart = 0) { const date = core.dateParse(dateValue); const year = date.getFullYear(); const month = date.getMonth(); const firstDate = type2 === "Y" ? new Date(year, 0, 1) : new Date(year, month, 1); const firstWeek = firstDate.getDay(); const days = Math.ceil((date.getTime() - firstDate.getTime()) / _const.DATE_DAY_MS); return Math.ceil((firstWeek + days - weekStart) / 7); } function weeksOfYear(dateValue, weekStart = 0) { return _dateWeeks(dateValue, "Y", weekStart); } function weeksOfMonth(dateValue, weekStart = 0) { return _dateWeeks(dateValue, "M", weekStart); } exports.DATE_DAY_MS = _const.DATE_DAY_MS; exports.DATE_HOUR_MS = _const.DATE_HOUR_MS; exports.DATE_MINUTE_MS = _const.DATE_MINUTE_MS; exports.DATE_MONTH_MS = _const.DATE_MONTH_MS; exports.DATE_SECOND_MS = _const.DATE_SECOND_MS; exports.DATE_YEAR_MS = _const.DATE_YEAR_MS; exports.TzDate = core.TzDate; exports.dateFormat = core.dateFormat; exports.dateParse = core.dateParse; exports.isValidDate = core.isValidDate; exports.EWeekStart = EWeekStart; exports._dateWeeks = _dateWeeks; exports.dateDaysInMonth = dateDaysInMonth; exports.dateDaysInYear = dateDaysInYear; exports.dateEndInDay = dateEndInDay; exports.dateEndInHour = dateEndInHour; exports.dateEndInMinute = dateEndInMinute; exports.dateEndInMonth = dateEndInMonth; exports.dateEndInSecond = dateEndInSecond; exports.dateEndInYear = dateEndInYear; exports.dateRelative = dateRelative; exports.dateStartInDay = dateStartInDay; exports.dateStartInHour = dateStartInHour; exports.dateStartInMinute = dateStartInMinute; exports.dateStartInMonth = dateStartInMonth; exports.dateStartInSecond = dateStartInSecond; exports.dateStartInYear = dateStartInYear; exports.isLeapYear = isLeapYear; exports.isSameDateInDay = isSameDateInDay; exports.isSameDateInHour = isSameDateInHour; exports.isSameDateInMinute = isSameDateInMinute; exports.isSameDateInMonth = isSameDateInMonth; exports.isSameDateInSecond = isSameDateInSecond; exports.isSameDateInYear = isSameDateInYear; exports.weeksOfMonth = weeksOfMonth; exports.weeksOfYear = weeksOfYear; //# sourceMappingURL=date.cjs.map