UNPKG

@carbon/utilities

Version:

Utilities and helpers to drive consistency across software products using the Carbon Design System

89 lines (88 loc) 3.28 kB
import { t as __exportAll } from "./chunk-D7D4PA-g.js"; //#region src/dateTimeFormat/relative.ts var relative_exports = /* @__PURE__ */ __exportAll({ format: () => format$1 }); /** * Copyright IBM Corp. 2024 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ function format$1(date, options) { const rtf = new Intl.RelativeTimeFormat(options?.locale, { style: options?.style ?? "long" }); const d = typeof date === "number" ? new Date(date) : date; const seconds = Math.floor((Date.now() - d.getTime()) / 1e3); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); const weeks = Math.floor(days / 7); const months = Math.floor(weeks / 4); const years = Math.floor(days / 365); if (Math.abs(seconds) < 60) return new Intl.RelativeTimeFormat(options?.locale, { numeric: "auto", style: options?.style ?? "long" }).format(0, "seconds"); if (Math.abs(minutes) < 60) return rtf.format(minutes * -1, "minutes"); if (Math.abs(hours) < 24) return rtf.format(hours * -1, "hours"); if (Math.abs(days) < 7) return rtf.format(days * -1, "days"); if (Math.abs(weeks) < 4) return rtf.format(weeks * -1, "weeks"); if (Math.abs(days) < 365) return rtf.format(months * -1, "months"); return rtf.format(years * -1, "years"); } //#endregion //#region src/dateTimeFormat/absolute.ts var absolute_exports = /* @__PURE__ */ __exportAll({ format: () => format, formatDate: () => formatDate, formatRange: () => formatRange, formatTime: () => formatTime }); /** * Copyright IBM Corp. 2024, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ function formatTime(date, options) { return new Intl.DateTimeFormat(options?.locale, { timeStyle: options?.style ?? "short", timeZone: options?.timeZone }).format(date); } function formatDate(date, options) { return new Intl.DateTimeFormat(options?.locale, { dateStyle: options?.style ?? "medium", timeZone: options?.timeZone }).format(date); } function format(date, options) { const timeStyle = options?.timeStyle ?? (options?.style === "tooltip" ? "long" : options?.style) ?? "short"; const dateStyle = options?.dateStyle ?? (options?.style === "tooltip" ? "full" : options?.style) ?? "medium"; return new Intl.DateTimeFormat(options?.locale, { timeStyle, dateStyle, timeZone: options?.timeZone }).format(date); } function formatRange(startDate, endDate, options) { const timeStyle = options?.timeStyle === null ? void 0 : options?.timeStyle ?? options?.style ?? "short"; const dateStyle = options?.dateStyle === null ? void 0 : options?.dateStyle ?? options?.style ?? "medium"; return new Intl.DateTimeFormat(options?.locale, { timeStyle, dateStyle, timeZone: options?.timeZone }).formatRange(startDate, endDate); } //#endregion //#region src/dateTimeFormat/index.ts /** * Copyright IBM Corp. 2024 * * 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 dateTimeFormat = { relative: relative_exports, absolute: absolute_exports }; //#endregion export { dateTimeFormat as t };