UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

73 lines (72 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const isDateInPeriod = (timeAgoMs, period, referenceDate = /* @__PURE__ */ new Date()) => { const date = new Date(timeAgoMs); const startOfToday = new Date(referenceDate); startOfToday.setHours(0, 0, 0, 0); const startOfTomorrow = new Date(startOfToday); startOfTomorrow.setDate(startOfToday.getDate() + 1); const startOfDayAfterTomorrow = new Date(startOfTomorrow); startOfDayAfterTomorrow.setDate(startOfTomorrow.getDate() + 1); if (period === "tomorrow") { return date >= startOfTomorrow && date < startOfDayAfterTomorrow; } if (period === "afterTomorrow") { return date >= startOfDayAfterTomorrow; } return false; }; const formatTimeAgo = (timeAgoMs, locale, showSeconds = false, referenceDate = /* @__PURE__ */ new Date()) => { const relFormatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto" }); const dayFormatter = new Intl.DateTimeFormat(locale, { hour: "numeric", minute: "numeric", second: showSeconds ? "numeric" : void 0 }); const weekFormatter = new Intl.DateTimeFormat(locale, { weekday: "short", hour: "numeric", minute: "numeric", second: showSeconds ? "numeric" : void 0 }); const fullFormatter = new Intl.DateTimeFormat(locale, { year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "numeric", second: showSeconds ? "numeric" : void 0 }); const date = new Date(timeAgoMs); const secsInDay = date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds(); const secsInWeek = date.getDay() * 86400 + secsInDay; const secsAgo = Math.floor((referenceDate.getTime() - timeAgoMs) / 1e3); const minsAgo = Math.floor(secsAgo / 60); switch (true) { case isDateInPeriod(timeAgoMs, "afterTomorrow", referenceDate): return fullFormatter.format(date); case isDateInPeriod(timeAgoMs, "tomorrow", referenceDate): return `${relFormatter.format(1, "days")}, ${dayFormatter.format(date)}`; case minsAgo < -60: return `${relFormatter.format(0, "days")}, ${dayFormatter.format(date)}`; case minsAgo < -2: return relFormatter.format(-minsAgo, "minutes"); case secsAgo < 0: return `${relFormatter.format(Math.abs(secsAgo), "seconds")}`; case secsAgo < 20: return relFormatter.format(0, "seconds"); case minsAgo < 2: return relFormatter.format(-secsAgo, "seconds"); case minsAgo < 60: return relFormatter.format(-minsAgo, "minutes"); case secsAgo < secsInDay: return `${relFormatter.format(0, "days")}, ${dayFormatter.format(date)}`; case secsAgo < secsInDay + 86400: return `${relFormatter.format(-1, "days")}, ${dayFormatter.format(date)}`; case secsAgo < secsInWeek: return weekFormatter.format(date); default: return fullFormatter.format(date); } }; exports.formatTimeAgo = formatTimeAgo;