UNPKG

@gez/date-time-kit

Version:

133 lines (132 loc) 3.9 kB
import { getCurrentTzOffset } from "../../utils/index.mjs"; import { weekKey } from "../calendar/index.mjs"; export const limitKeys = [ "all", "today", "yesterday", "week", "lastWeek", "last7Days", "month", "last30Days", "last90Days", "last180Days", "last6Month", "year" ]; export const quickKeys = [...limitKeys, "custom"]; const genDateWithHours = (isStart, fn = (_t) => { }, t = /* @__PURE__ */ new Date()) => { if (isStart) t.setHours(0, 0, 0, 0); else t.setHours(23, 59, 59, 999); fn(t); return t; }; const genStartDate = (fn, t) => genDateWithHours(true, fn, t); const genEndDate = (fn, t) => genDateWithHours(false, fn, t); export const genPeriodTimes = ({ start, end, initTime = /* @__PURE__ */ new Date(), weekStartAt = "sun" } = {}) => { const weekOffset = weekKey.indexOf(weekStartAt); return { start: genStartDate((t) => start == null ? void 0 : start(t, weekOffset), new Date(initTime)), end: genEndDate((t) => end == null ? void 0 : end(t, weekOffset), new Date(initTime)) }; }; const noop = () => { }; const presetPeriods = { all: () => null, today: (ops) => genPeriodTimes({ ...ops, start: noop, end: noop }), yesterday: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(t.getDate() - 1), end: (t) => t.setDate(t.getDate() - 1) }), week: (ops) => genPeriodTimes({ ...ops, start: (t, weekOffset) => t.setDate(t.getDate() - t.getDay() + weekOffset), end: (t, weekOffset) => t.setDate(t.getDate() - t.getDay() + weekOffset + 6) }), lastWeek: (ops) => genPeriodTimes({ ...ops, start: (t, weekOffset) => t.setDate(t.getDate() - t.getDay() + weekOffset - 7), end: (t, weekOffset) => t.setDate(t.getDate() - t.getDay() + weekOffset - 1) }), last7Days: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(t.getDate() - 6), end: noop }), month: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(1), end: (t) => t.setMonth(t.getMonth() + 1, 0) }), last30Days: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(t.getDate() - 29), end: noop }), last90Days: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(t.getDate() - 89), end: noop }), last180Days: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setDate(t.getDate() - 179), end: noop }), last6Month: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setMonth(t.getMonth() - 5, 1), end: (t) => t.setMonth(t.getMonth() + 1, 0) }), year: (ops) => genPeriodTimes({ ...ops, start: (t) => t.setMonth(0, 1), end: (t) => t.setFullYear(t.getFullYear() + 1, 0, 0) }) }; export const quickGenPeriodTimes = ({ periods = limitKeys, ...options } = {}) => { periods = [...new Set(periods)].filter((k) => k in presetPeriods); return Object.fromEntries( periods.map((k) => [k, presetPeriods[k](options)]) ); }; export const quickGenPeriodTime = (period, options = {}) => presetPeriods[period](options); export const quickGenPeriodTimeInfo = (type, options = {}, tzOffset = getCurrentTzOffset()) => { const t = quickGenPeriodTime(type, options); return !t ? { type, tzOffset } : { type, ...t, tzOffset }; }; export const localeInfo2UTCInfo = (info) => { info = { ...info }; if (info.type === "all") return info; const { tzOffset, start, end } = info; info.start = new Date( +start - (getCurrentTzOffset() - tzOffset) * 60 * 1e3 ); info.end = new Date(+end - (getCurrentTzOffset() - tzOffset) * 60 * 1e3); return info; }; export const UTCInfo2LocaleInfo = (info) => { info = { ...info }; if (info.type === "all") return info; const { tzOffset, start, end } = info; info.start = new Date( +start + (getCurrentTzOffset() - tzOffset) * 60 * 1e3 ); info.end = new Date(+end + (getCurrentTzOffset() - tzOffset) * 60 * 1e3); return info; };