-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Allow users to select time format for UI rendering #9042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bramkragten
merged 16 commits into
home-assistant:dev
from
spacegaier:time-format-selection
May 20, 2021
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ca830af
Allow users to specify time format for UI rendering
spacegaier dd7bfcd
Linting (mocha test broken due to memoizeOne)
spacegaier 588632c
Fix tests
spacegaier 8ae9e43
Changes from review
spacegaier b307065
Condense useAmPm()
spacegaier d8bc289
Lint
spacegaier a2dfda9
Use `memoizeOne()` for all date and time formatting
spacegaier a16de76
Lint
spacegaier acfbada
Merge branch 'dev' of https://github.com/home-assistant/frontend into…
spacegaier bb99325
Use new logic for date range picker (e.g. logbook and history)
spacegaier 7a2eecf
Merge branch 'dev' into time-format-selection
spacegaier 70ff9c8
Update tsconfig.test.json
bramkragten 54600be
Fix memoize-one double import
spacegaier 3f288cc
Fix tests
bramkragten 4dcffbf
Fix lit imports
spacegaier 2a01fe6
Update src/panels/profile/ha-pick-time-format-row.ts
bramkragten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,32 @@ | ||
| import { format } from "fecha"; | ||
| import { FrontendTranslationData } from "../../data/translation"; | ||
| import memoizeOne from "memoize-one"; | ||
| import { FrontendLocaleData } from "../../data/translation"; | ||
| import { toLocaleDateStringSupportsOptions } from "./check_options_support"; | ||
|
|
||
| const formatDateMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| }) | ||
| ); | ||
|
|
||
| export const formatDate = toLocaleDateStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleDateString(locales.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| }) | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatDateMem(locale).format(dateObj) | ||
| : (dateObj: Date) => format(dateObj, "longDate"); | ||
|
|
||
| const formatDateWeekdayMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| weekday: "long", | ||
| month: "long", | ||
| day: "numeric", | ||
| }) | ||
| ); | ||
|
|
||
| export const formatDateWeekday = toLocaleDateStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleDateString(locales.language, { | ||
| weekday: "long", | ||
| month: "short", | ||
| day: "numeric", | ||
| }) | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatDateWeekdayMem(locale).format(dateObj) | ||
| : (dateObj: Date) => format(dateObj, "dddd, MMM D"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,42 @@ | ||
| import { format } from "fecha"; | ||
| import { FrontendTranslationData } from "../../data/translation"; | ||
| import memoizeOne from "memoize-one"; | ||
| import { FrontendLocaleData } from "../../data/translation"; | ||
| import { toLocaleStringSupportsOptions } from "./check_options_support"; | ||
| import { useAmPm } from "./use_am_pm"; | ||
|
|
||
| const formatDateTimeMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| hour12: useAmPm(locale), | ||
| }) | ||
| ); | ||
|
|
||
| export const formatDateTime = toLocaleStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleString(locales.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| }) | ||
| : (dateObj: Date) => format(dateObj, "MMMM D, YYYY, HH:mm"); | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatDateTimeMem(locale).format(dateObj) | ||
| : (dateObj: Date, locale: FrontendLocaleData) => | ||
| format(dateObj, "MMMM D, YYYY, HH:mm" + useAmPm(locale) ? " A" : ""); | ||
|
|
||
| const formatDateTimeWithSecondsMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| second: "2-digit", | ||
| hour12: useAmPm(locale), | ||
| }) | ||
| ); | ||
|
|
||
| export const formatDateTimeWithSeconds = toLocaleStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleString(locales.language, { | ||
| year: "numeric", | ||
| month: "long", | ||
| day: "numeric", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| second: "2-digit", | ||
| }) | ||
| : (dateObj: Date) => format(dateObj, "MMMM D, YYYY, HH:mm:ss"); | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatDateTimeWithSecondsMem(locale).format(dateObj) | ||
| : (dateObj: Date, locale: FrontendLocaleData) => | ||
| format(dateObj, "MMMM D, YYYY, HH:mm:ss" + useAmPm(locale) ? " A" : ""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,52 @@ | ||
| import { format } from "fecha"; | ||
| import { FrontendTranslationData } from "../../data/translation"; | ||
| import memoizeOne from "memoize-one"; | ||
| import { FrontendLocaleData } from "../../data/translation"; | ||
| import { toLocaleTimeStringSupportsOptions } from "./check_options_support"; | ||
| import { useAmPm } from "./use_am_pm"; | ||
|
|
||
| const formatTimeMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| hour12: useAmPm(locale), | ||
| }) | ||
| ); | ||
|
|
||
| export const formatTime = toLocaleTimeStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleTimeString(locales.language, { | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| }) | ||
| : (dateObj: Date) => format(dateObj, "shortTime"); | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatTimeMem(locale).format(dateObj) | ||
| : (dateObj: Date, locale: FrontendLocaleData) => | ||
| format(dateObj, "shortTime" + useAmPm(locale) ? " A" : ""); | ||
|
|
||
| const formatTimeWithSecondsMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| second: "2-digit", | ||
| hour12: useAmPm(locale), | ||
| }) | ||
| ); | ||
|
|
||
| export const formatTimeWithSeconds = toLocaleTimeStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleTimeString(locales.language, { | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| second: "2-digit", | ||
| }) | ||
| : (dateObj: Date) => format(dateObj, "mediumTime"); | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatTimeWithSecondsMem(locale).format(dateObj) | ||
| : (dateObj: Date, locale: FrontendLocaleData) => | ||
| format(dateObj, "mediumTime" + useAmPm(locale) ? " A" : ""); | ||
|
|
||
| const formatTimeWeekdayMem = memoizeOne( | ||
| (locale: FrontendLocaleData) => | ||
| new Intl.DateTimeFormat(locale.language, { | ||
| weekday: "long", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| hour12: useAmPm(locale), | ||
| }) | ||
| ); | ||
|
|
||
| export const formatTimeWeekday = toLocaleTimeStringSupportsOptions | ||
| ? (dateObj: Date, locales: FrontendTranslationData) => | ||
| dateObj.toLocaleTimeString(locales.language, { | ||
| weekday: "long", | ||
| hour: "numeric", | ||
| minute: "2-digit", | ||
| }) | ||
| : (dateObj: Date) => format(dateObj, "dddd, HH:mm"); | ||
| ? (dateObj: Date, locale: FrontendLocaleData) => | ||
| formatTimeWeekdayMem(locale).format(dateObj) | ||
| : (dateObj: Date, locale: FrontendLocaleData) => | ||
| format(dateObj, "dddd, HH:mm" + useAmPm(locale) ? " A" : ""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { FrontendLocaleData, TimeFormat } from "../../data/translation"; | ||
|
|
||
| export const useAmPm = (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; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.