From b2e4fef72f0cd6c8e57e2b1eaa00517373367936 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Fri, 22 Nov 2024 11:35:51 +0100 Subject: [PATCH] Remove format date --- src/ha/common/datetime/duration.ts | 19 -- src/ha/common/datetime/format_date.ts | 193 ------------------ src/ha/common/datetime/format_date_time.ts | 114 ----------- src/ha/common/datetime/format_time.ts | 90 -------- .../datetime/milliseconds_to_duration.ts | 25 --- src/ha/common/datetime/use_am_pm.ts | 16 -- src/ha/index.ts | 4 - 7 files changed, 461 deletions(-) delete mode 100644 src/ha/common/datetime/duration.ts delete mode 100644 src/ha/common/datetime/format_date.ts delete mode 100644 src/ha/common/datetime/format_date_time.ts delete mode 100644 src/ha/common/datetime/format_time.ts delete mode 100644 src/ha/common/datetime/milliseconds_to_duration.ts delete mode 100644 src/ha/common/datetime/use_am_pm.ts diff --git a/src/ha/common/datetime/duration.ts b/src/ha/common/datetime/duration.ts deleted file mode 100644 index 36fef1902..000000000 --- a/src/ha/common/datetime/duration.ts +++ /dev/null @@ -1,19 +0,0 @@ -import millisecondsToDuration from "./milliseconds_to_duration"; - -const DAY_IN_MILLISECONDS = 86400000; -const HOUR_IN_MILLISECONDS = 3600000; -const MINUTE_IN_MILLISECONDS = 60000; -const SECOND_IN_MILLISECONDS = 1000; - -export const UNIT_TO_MILLISECOND_CONVERT = { - ms: 1, - s: SECOND_IN_MILLISECONDS, - min: MINUTE_IN_MILLISECONDS, - h: HOUR_IN_MILLISECONDS, - d: DAY_IN_MILLISECONDS, -}; - -export const formatDuration = (duration: string, units: string): string => - millisecondsToDuration( - parseFloat(duration) * UNIT_TO_MILLISECOND_CONVERT[units] - ) || "0"; diff --git a/src/ha/common/datetime/format_date.ts b/src/ha/common/datetime/format_date.ts deleted file mode 100644 index 95ca1a94c..000000000 --- a/src/ha/common/datetime/format_date.ts +++ /dev/null @@ -1,193 +0,0 @@ -import { HassConfig } from "home-assistant-js-websocket"; -import memoizeOne from "memoize-one"; -import { FrontendLocaleData, DateFormat } from "../../data/translation"; - -// Tuesday, August 10 -export const formatDateWeekdayDay = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateWeekdayDayMem(locale, config.time_zone).format(dateObj); - -const formatDateWeekdayDayMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - weekday: "long", - month: "long", - day: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// August 10, 2021 -export const formatDate = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateMem(locale, config.time_zone).format(dateObj); - -const formatDateMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - year: "numeric", - month: "long", - day: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// 10/08/2021 -export const formatDateNumeric = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => { - const formatter = formatDateNumericMem(locale, config.time_zone); - - if ( - locale.date_format === DateFormat.language || - locale.date_format === DateFormat.system - ) { - return formatter.format(dateObj); - } - - const parts = formatter.formatToParts(dateObj); - - const literal = parts.find((value) => value.type === "literal")?.value; - const day = parts.find((value) => value.type === "day")?.value; - const month = parts.find((value) => value.type === "month")?.value; - const year = parts.find((value) => value.type === "year")?.value; - - const lastPart = parts[parts.length - 1]; - let lastLiteral = lastPart?.type === "literal" ? lastPart?.value : ""; - - if (locale.language === "bg" && locale.date_format === DateFormat.YMD) { - lastLiteral = ""; - } - - const formats = { - [DateFormat.DMY]: `${day}${literal}${month}${literal}${year}${lastLiteral}`, - [DateFormat.MDY]: `${month}${literal}${day}${literal}${year}${lastLiteral}`, - [DateFormat.YMD]: `${year}${literal}${month}${literal}${day}${lastLiteral}`, - }; - - return formats[locale.date_format]; -}; - -const formatDateNumericMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => { - const localeString = - locale.date_format === DateFormat.system ? undefined : locale.language; - - if ( - locale.date_format === DateFormat.language || - locale.date_format === DateFormat.system - ) { - return new Intl.DateTimeFormat(localeString, { - year: "numeric", - month: "numeric", - day: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }); - } - - return new Intl.DateTimeFormat(localeString, { - year: "numeric", - month: "numeric", - day: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }); - } -); - -// Aug 10 -export const formatDateShort = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateShortMem(locale, config.time_zone).format(dateObj); - -const formatDateShortMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - day: "numeric", - month: "short", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// August 2021 -export const formatDateMonthYear = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateMonthYearMem(locale, config.time_zone).format(dateObj); - -const formatDateMonthYearMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - month: "long", - year: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// August -export const formatDateMonth = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateMonthMem(locale, config.time_zone).format(dateObj); - -const formatDateMonthMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - month: "long", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// 2021 -export const formatDateYear = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateYearMem(locale, config.time_zone).format(dateObj); - -const formatDateYearMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - year: "numeric", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// Monday -export const formatDateWeekday = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateWeekdayMem(locale, config.time_zone).format(dateObj); - -const formatDateWeekdayMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - weekday: "long", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); - -// Mon -export const formatDateWeekdayShort = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateWeekdayShortMem(locale, config.time_zone).format(dateObj); - -const formatDateWeekdayShortMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat(locale.language, { - weekday: "short", - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); diff --git a/src/ha/common/datetime/format_date_time.ts b/src/ha/common/datetime/format_date_time.ts deleted file mode 100644 index f70c685a1..000000000 --- a/src/ha/common/datetime/format_date_time.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { HassConfig } from "home-assistant-js-websocket"; -import memoizeOne from "memoize-one"; -import { FrontendLocaleData } from "../../data/translation"; -import { formatDateNumeric } from "./format_date"; -import { formatTime } from "./format_time"; -import { useAmPm } from "./use_am_pm"; - -// August 9, 2021, 8:23 AM -export const formatDateTime = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateTimeMem(locale, config.time_zone).format(dateObj); - -const formatDateTimeMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - year: "numeric", - month: "long", - day: "numeric", - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// Aug 9, 2021, 8:23 AM -export const formatShortDateTimeWithYear = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatShortDateTimeWithYearMem(locale, config.time_zone).format(dateObj); - -const formatShortDateTimeWithYearMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - year: "numeric", - month: "short", - day: "numeric", - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// Aug 9, 8:23 AM -export const formatShortDateTime = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatShortDateTimeMem(locale, config.time_zone).format(dateObj); - -const formatShortDateTimeMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - month: "short", - day: "numeric", - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// August 9, 2021, 8:23:15 AM -export const formatDateTimeWithSeconds = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatDateTimeWithSecondsMem(locale, config.time_zone).format(dateObj); - -const formatDateTimeWithSecondsMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - year: "numeric", - month: "long", - day: "numeric", - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - second: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// 9/8/2021, 8:23 AM -export const formatDateTimeNumeric = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => - `${formatDateNumeric(dateObj, locale, config)}, ${formatTime(dateObj, locale, config)}`; diff --git a/src/ha/common/datetime/format_time.ts b/src/ha/common/datetime/format_time.ts deleted file mode 100644 index 3bda190f5..000000000 --- a/src/ha/common/datetime/format_time.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { HassConfig } from "home-assistant-js-websocket"; -import memoizeOne from "memoize-one"; -import { FrontendLocaleData } from "../../data/translation"; -import { useAmPm } from "./use_am_pm"; - -// 9:15 PM || 21:15 -export const formatTime = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatTimeMem(locale, config.time_zone).format(dateObj); - -const formatTimeMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - hour: "numeric", - minute: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// 9:15:24 PM || 21:15:24 -export const formatTimeWithSeconds = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatTimeWithSecondsMem(locale, config.time_zone).format(dateObj); - -const formatTimeWithSecondsMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - second: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// Tuesday 7:00 PM || Tuesday 19:00 -export const formatTimeWeekday = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatTimeWeekdayMem(locale, config.time_zone).format(dateObj); - -const formatTimeWeekdayMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - new Intl.DateTimeFormat( - locale.language === "en" && !useAmPm(locale) - ? "en-u-hc-h23" - : locale.language, - { - weekday: "long", - hour: useAmPm(locale) ? "numeric" : "2-digit", - minute: "2-digit", - hour12: useAmPm(locale), - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - } - ) -); - -// 21:15 -export const formatTime24h = ( - dateObj: Date, - locale: FrontendLocaleData, - config: HassConfig -) => formatTime24hMem(locale, config.time_zone).format(dateObj); - -const formatTime24hMem = memoizeOne( - (locale: FrontendLocaleData, serverTimeZone: string) => - // en-GB to fix Chrome 24:59 to 0:59 https://stackoverflow.com/a/60898146 - new Intl.DateTimeFormat("en-GB", { - hour: "numeric", - minute: "2-digit", - hour12: false, - timeZone: locale.time_zone === "server" ? serverTimeZone : undefined, - }) -); diff --git a/src/ha/common/datetime/milliseconds_to_duration.ts b/src/ha/common/datetime/milliseconds_to_duration.ts deleted file mode 100644 index 384a8a770..000000000 --- a/src/ha/common/datetime/milliseconds_to_duration.ts +++ /dev/null @@ -1,25 +0,0 @@ -const leftPad = (num: number, digits = 2) => { - let paddedNum = "" + num; - for (let i = 1; i < digits; i++) { - paddedNum = parseInt(paddedNum) < 10 ** i ? `0${paddedNum}` : paddedNum; - } - return paddedNum; -}; - -export default function millisecondsToDuration(d: number) { - const h = Math.floor(d / 1000 / 3600); - const m = Math.floor(((d / 1000) % 3600) / 60); - const s = Math.floor(((d / 1000) % 3600) % 60); - const ms = Math.floor(d % 1000); - - if (h > 0) { - return `${h}:${leftPad(m)}:${leftPad(s)}`; - } - if (m > 0) { - return `${m}:${leftPad(s)}`; - } - if (s > 0 || ms > 0) { - return `${s}${ms > 0 ? `.${leftPad(ms, 3)}` : ``}`; - } - return null; -} diff --git a/src/ha/common/datetime/use_am_pm.ts b/src/ha/common/datetime/use_am_pm.ts deleted file mode 100644 index 97bf2911b..000000000 --- a/src/ha/common/datetime/use_am_pm.ts +++ /dev/null @@ -1,16 +0,0 @@ -import memoizeOne from "memoize-one"; -import { FrontendLocaleData, TimeFormat } from "../../data/translation"; - -export const useAmPm = memoizeOne((locale: FrontendLocaleData): boolean => { - if ( - locale.time_format === TimeFormat.language || - locale.time_format === TimeFormat.system - ) { - const testLanguage = - locale.time_format === TimeFormat.language ? locale.language : undefined; - const test = new Date().toLocaleString(testLanguage); - return test.includes("AM") || test.includes("PM"); - } - - return locale.time_format === TimeFormat.am_pm; -}); diff --git a/src/ha/index.ts b/src/ha/index.ts index e8939e4c2..081ea8f24 100644 --- a/src/ha/index.ts +++ b/src/ha/index.ts @@ -1,8 +1,4 @@ export * from "./common/const"; -export * from "./common/datetime/format_date"; -export * from "./common/datetime/format_date_time"; -export * from "./common/datetime/format_time"; -export * from "./common/datetime/use_am_pm"; export * from "./common/dom/fire_event"; export * from "./common/dom/get_main_window"; export * from "./common/entity/compute_domain";