@carbon/ibm-products
Version:
Carbon for IBM Products
38 lines (36 loc) • 2.07 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/components/NotificationsPanel/utils.js
/**
* Copyright IBM Corp. 2020, 2021
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const timeAgo = ({ previousTime, secondsAgoText, minuteAgoText, minutesAgoText, hoursAgoText, hourAgoText, daysAgoText, yesterdayAtText, monthsAgoText, monthAgoText, yearsAgoText, yearAgoText, nowText }) => {
const msPerMinute = 60 * 1e3;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 24;
const msPerMonth = msPerDay * 30;
const msPerYear = msPerDay * 365;
const elapsed = /* @__PURE__ */ new Date() - previousTime;
switch (true) {
case elapsed < msPerMinute: return Math.round(elapsed / 1e3) > 10 ? secondsAgoText(Math.round(elapsed / 1e3)) : nowText;
case elapsed < msPerHour: return Math.round(elapsed / msPerMinute) > 1 ? minutesAgoText(Math.round(elapsed / msPerMinute)) : minuteAgoText(Math.round(elapsed / msPerMinute));
case elapsed < msPerDay: return Math.round(elapsed / msPerHour) > 1 ? hoursAgoText(Math.round(elapsed / msPerHour)) : hourAgoText(Math.round(elapsed / msPerHour));
case elapsed < msPerMonth: return Math.round(elapsed / msPerDay) > 1 ? daysAgoText(Math.round(elapsed / msPerDay)) : yesterdayAtText(new Date(previousTime).toLocaleTimeString(getBrowserLocales[0]));
case elapsed < msPerYear: return Math.round(elapsed / msPerMonth) > 1 ? monthsAgoText(Math.round(elapsed / msPerMonth)) : monthAgoText(Math.round(elapsed / msPerMonth));
default: return Math.round(elapsed / msPerYear) > 1 ? yearsAgoText(Math.round(elapsed / msPerYear)) : yearAgoText(Math.round(elapsed / msPerYear));
}
};
const getBrowserLocales = () => {
return (navigator.languages === void 0 ? [navigator.language, "en"] : navigator.languages).map((locale) => {
return locale.trim();
});
};
//#endregion
export { timeAgo };