From d630530d3b793b943d43f4fb06ee4583d0dff85b Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 11 Aug 2026 13:16:12 +0000 Subject: [PATCH 1/3] Fix AM/PM button highlight in TimePicker under localized languages Co-authored-by: Abdelrahman Khattab --- src/components/TimePicker/TimePicker.tsx | 2 +- src/styles/utils/index.ts | 13 ++++++-- tests/unit/ButtonStyleUtilsTest.ts | 41 ++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/components/TimePicker/TimePicker.tsx b/src/components/TimePicker/TimePicker.tsx index f6ee710edfea..ba5111f2085a 100644 --- a/src/components/TimePicker/TimePicker.tsx +++ b/src/components/TimePicker/TimePicker.tsx @@ -708,7 +708,7 @@ function TimePicker({defaultValue = '', onSubmit, onInputChange = () => {}, shou ], ); - const {styleForAM, styleForPM} = StyleUtils.getStatusAMandPMButtonStyle(amPmValue); + const {styleForAM, styleForPM} = StyleUtils.getStatusAMandPMButtonStyle(amPmValue, translate('common.am'), translate('common.pm')); const numberPad = useCallback(() => { if (!canUseTouchScreen) { diff --git a/src/styles/utils/index.ts b/src/styles/utils/index.ts index 5a2d86519bfb..e36a69fa1b41 100644 --- a/src/styles/utils/index.ts +++ b/src/styles/utils/index.ts @@ -1705,10 +1705,17 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ /** * Get the style for the AM and PM buttons in the TimePicker + * + * `amPmValue` can be the English CONST.TIME_PERIOD marker (set when the user taps a button) or a localized + * period marker such as the Japanese "午後" (derived from the saved time via date-fns under the active locale). + * We compare against both the English CONST.TIME_PERIOD and the localized `common.am`/`common.pm` labels so the + * correct button stays highlighted regardless of the active language. */ - getStatusAMandPMButtonStyle: (amPmValue: string): {styleForAM: StyleProp; styleForPM: StyleProp} => { - const computedStyleForAM: ViewStyle = amPmValue !== CONST.TIME_PERIOD.AM ? {backgroundColor: theme.componentBG} : {}; - const computedStyleForPM: ViewStyle = amPmValue !== CONST.TIME_PERIOD.PM ? {backgroundColor: theme.componentBG} : {}; + getStatusAMandPMButtonStyle: (amPmValue: string, amLabel: string, pmLabel: string): {styleForAM: StyleProp; styleForPM: StyleProp} => { + const isAM = amPmValue === CONST.TIME_PERIOD.AM || amPmValue === amLabel; + const isPM = amPmValue === CONST.TIME_PERIOD.PM || amPmValue === pmLabel; + const computedStyleForAM: ViewStyle = !isAM ? {backgroundColor: theme.componentBG} : {}; + const computedStyleForPM: ViewStyle = !isPM ? {backgroundColor: theme.componentBG} : {}; return { styleForAM: [styles.timePickerWidth72, computedStyleForAM], diff --git a/tests/unit/ButtonStyleUtilsTest.ts b/tests/unit/ButtonStyleUtilsTest.ts index 8a29cbed0990..7c8b1dc46e66 100644 --- a/tests/unit/ButtonStyleUtilsTest.ts +++ b/tests/unit/ButtonStyleUtilsTest.ts @@ -4,9 +4,10 @@ import type {ThemeColors} from '@src/styles/theme/types'; import createStyleUtils from '@src/styles/utils'; import variables from '@src/styles/variables'; -const mockTheme = {} as ThemeColors; +const mockTheme = {componentBG: '#ffffff'} as ThemeColors; const mockStyles = { + timePickerWidth72: {width: 72}, buttonExtraSmall: {height: 28}, buttonSmall: {height: 32}, buttonMedium: {height: 40}, @@ -23,7 +24,10 @@ const mockStyles = { buttonOpacityDisabled: {opacity: 0.5}, } as unknown as ThemeStyles; -const {getButtonSizeStyle, getButtonPaddingStyle, getButtonStyleWithIcon, getButtonVariantStyles, getReportTableColumnStyles} = createStyleUtils(mockTheme, mockStyles); +const {getButtonSizeStyle, getButtonPaddingStyle, getButtonStyleWithIcon, getButtonVariantStyles, getReportTableColumnStyles, getStatusAMandPMButtonStyle} = createStyleUtils( + mockTheme, + mockStyles, +); describe('getButtonSizeStyle', () => { it.each([ @@ -95,3 +99,36 @@ describe('getReportTableColumnStyles - First approved column width', () => { expect(getReportTableColumnStyles(CONST.SEARCH.TABLE_COLUMNS.FIRST_APPROVED)).toEqual({width: variables.w102}); }); }); + +describe('getStatusAMandPMButtonStyle', () => { + // When a button is highlighted its computed style is an empty object (no background override); when it is not, + // the componentBG background is applied. + const highlighted = [mockStyles.timePickerWidth72, {}]; + const dimmed = [mockStyles.timePickerWidth72, {backgroundColor: mockTheme.componentBG}]; + + it('highlights PM for the English CONST.TIME_PERIOD marker', () => { + const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle(CONST.TIME_PERIOD.PM, 'AM', 'PM'); + expect(styleForPM).toEqual(highlighted); + expect(styleForAM).toEqual(dimmed); + }); + + it('highlights AM for the English CONST.TIME_PERIOD marker', () => { + const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle(CONST.TIME_PERIOD.AM, 'AM', 'PM'); + expect(styleForAM).toEqual(highlighted); + expect(styleForPM).toEqual(dimmed); + }); + + // Regression test for the localized AM/PM bug: the saved time yields a localized period marker (Japanese "午後"), + // which must still highlight the PM button by matching the localized `common.pm` label. + it('highlights PM for a localized period marker (ja "午後")', () => { + const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle('午後', '午前', '午後'); + expect(styleForPM).toEqual(highlighted); + expect(styleForAM).toEqual(dimmed); + }); + + it('highlights AM for a localized period marker (ja "午前")', () => { + const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle('午前', '午前', '午後'); + expect(styleForAM).toEqual(highlighted); + expect(styleForPM).toEqual(dimmed); + }); +}); From d167283a1923db655288bc50dab00c26f5736836 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 11 Aug 2026 22:32:35 +0000 Subject: [PATCH 2/3] Restore internal English AM/PM contract for TimePicker (fixes save under localized locales) Revert the comparison-layer highlight fix and instead normalize the TimePicker period marker back to an internal English CONST.TIME_PERIOD value, pinning the reverse parse in combineDateAndTime to enGB. This fixes both the AM/PM highlight and the more severe save-blocking bug in de/el/zh-hans (English AM/PM could not be parsed under the active date-fns locale, so the time never saved). Co-authored-by: Abdelrahman Khattab --- src/components/TimePicker/TimePicker.tsx | 2 +- src/libs/DateUtils.ts | 16 +++++-- src/styles/utils/index.ts | 13 ++--- tests/unit/ButtonStyleUtilsTest.ts | 41 +--------------- tests/unit/DateUtilsTest.ts | 61 ++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 54 deletions(-) diff --git a/src/components/TimePicker/TimePicker.tsx b/src/components/TimePicker/TimePicker.tsx index ba5111f2085a..f6ee710edfea 100644 --- a/src/components/TimePicker/TimePicker.tsx +++ b/src/components/TimePicker/TimePicker.tsx @@ -708,7 +708,7 @@ function TimePicker({defaultValue = '', onSubmit, onInputChange = () => {}, shou ], ); - const {styleForAM, styleForPM} = StyleUtils.getStatusAMandPMButtonStyle(amPmValue, translate('common.am'), translate('common.pm')); + const {styleForAM, styleForPM} = StyleUtils.getStatusAMandPMButtonStyle(amPmValue); const numberPad = useCallback(() => { if (!canUseTouchScreen) { diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 8fbfce4cf21c..3ac03a559689 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -42,6 +42,7 @@ import { subMinutes, } from 'date-fns'; import {formatInTimeZone, fromZonedTime, toDate, toZonedTime, format as tzFormat} from 'date-fns-tz'; +import {enGB} from 'date-fns/locale/en-GB'; import throttle from 'lodash/throttle'; import {setCurrentDate} from './actions/CurrentDate'; @@ -580,8 +581,11 @@ const combineDateAndTime = (updatedTime: string, inputDateTime: string): string parsedTime = tempTime; } } else if (updatedTime.includes(':')) { - // it's in "hh:mm a" format - const tempTime = parse(updatedTime, 'hh:mm a', new Date()); + // It's in "hh:mm a" format. This branch is only ever fed by TimePicker's internal `${hours}:${minutes} ${amPmValue}` + // string, whose period is always the English CONST.TIME_PERIOD marker, so parse it with the English enGB locale. + // Without pinning the locale, the active date-fns default locale (set via IntlStore) cannot parse the English + // AM/PM under several locales (e.g. de/el/zh-hans), returning an Invalid Date and breaking the time save. + const tempTime = parse(updatedTime, 'hh:mm a', new Date(), {locale: enGB}); if (isValid(tempTime)) { parsedTime = tempTime; } @@ -639,8 +643,12 @@ function get12HourTimeObjectFromDate(dateTime: string, isFullFormat = false): {h minute: format(parsedTime, 'mm'), seconds: isFullFormat ? format(parsedTime, 'ss') : '00', milliseconds: isFullFormat ? format(parsedTime, 'SSS') : '000', - // eslint-disable-next-line rulesdir/require-locale-for-localized-date-format -- this is TimePicker state compared against the English CONST.TIME_PERIOD, not text shown to the user. - period: format(parsedTime, 'a').toUpperCase(), + // The period is TimePicker state, compared against the English CONST.TIME_PERIOD and used to build the submitted + // time string (which combineDateAndTime re-parses with the English enGB locale). It is never shown to the user + // (the AM/PM buttons render their own translated labels), so it must be a locale-independent English marker. + // Deriving it from the 24-hour clock avoids the active date-fns locale returning a localized marker (e.g. de + // "NACHM.", el "Μ.Μ.") that would match neither CONST.TIME_PERIOD nor round-trip through the English parse. + period: parsedTime.getHours() >= 12 ? CONST.TIME_PERIOD.PM : CONST.TIME_PERIOD.AM, }; } diff --git a/src/styles/utils/index.ts b/src/styles/utils/index.ts index e36a69fa1b41..5a2d86519bfb 100644 --- a/src/styles/utils/index.ts +++ b/src/styles/utils/index.ts @@ -1705,17 +1705,10 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ /** * Get the style for the AM and PM buttons in the TimePicker - * - * `amPmValue` can be the English CONST.TIME_PERIOD marker (set when the user taps a button) or a localized - * period marker such as the Japanese "午後" (derived from the saved time via date-fns under the active locale). - * We compare against both the English CONST.TIME_PERIOD and the localized `common.am`/`common.pm` labels so the - * correct button stays highlighted regardless of the active language. */ - getStatusAMandPMButtonStyle: (amPmValue: string, amLabel: string, pmLabel: string): {styleForAM: StyleProp; styleForPM: StyleProp} => { - const isAM = amPmValue === CONST.TIME_PERIOD.AM || amPmValue === amLabel; - const isPM = amPmValue === CONST.TIME_PERIOD.PM || amPmValue === pmLabel; - const computedStyleForAM: ViewStyle = !isAM ? {backgroundColor: theme.componentBG} : {}; - const computedStyleForPM: ViewStyle = !isPM ? {backgroundColor: theme.componentBG} : {}; + getStatusAMandPMButtonStyle: (amPmValue: string): {styleForAM: StyleProp; styleForPM: StyleProp} => { + const computedStyleForAM: ViewStyle = amPmValue !== CONST.TIME_PERIOD.AM ? {backgroundColor: theme.componentBG} : {}; + const computedStyleForPM: ViewStyle = amPmValue !== CONST.TIME_PERIOD.PM ? {backgroundColor: theme.componentBG} : {}; return { styleForAM: [styles.timePickerWidth72, computedStyleForAM], diff --git a/tests/unit/ButtonStyleUtilsTest.ts b/tests/unit/ButtonStyleUtilsTest.ts index 7c8b1dc46e66..8a29cbed0990 100644 --- a/tests/unit/ButtonStyleUtilsTest.ts +++ b/tests/unit/ButtonStyleUtilsTest.ts @@ -4,10 +4,9 @@ import type {ThemeColors} from '@src/styles/theme/types'; import createStyleUtils from '@src/styles/utils'; import variables from '@src/styles/variables'; -const mockTheme = {componentBG: '#ffffff'} as ThemeColors; +const mockTheme = {} as ThemeColors; const mockStyles = { - timePickerWidth72: {width: 72}, buttonExtraSmall: {height: 28}, buttonSmall: {height: 32}, buttonMedium: {height: 40}, @@ -24,10 +23,7 @@ const mockStyles = { buttonOpacityDisabled: {opacity: 0.5}, } as unknown as ThemeStyles; -const {getButtonSizeStyle, getButtonPaddingStyle, getButtonStyleWithIcon, getButtonVariantStyles, getReportTableColumnStyles, getStatusAMandPMButtonStyle} = createStyleUtils( - mockTheme, - mockStyles, -); +const {getButtonSizeStyle, getButtonPaddingStyle, getButtonStyleWithIcon, getButtonVariantStyles, getReportTableColumnStyles} = createStyleUtils(mockTheme, mockStyles); describe('getButtonSizeStyle', () => { it.each([ @@ -99,36 +95,3 @@ describe('getReportTableColumnStyles - First approved column width', () => { expect(getReportTableColumnStyles(CONST.SEARCH.TABLE_COLUMNS.FIRST_APPROVED)).toEqual({width: variables.w102}); }); }); - -describe('getStatusAMandPMButtonStyle', () => { - // When a button is highlighted its computed style is an empty object (no background override); when it is not, - // the componentBG background is applied. - const highlighted = [mockStyles.timePickerWidth72, {}]; - const dimmed = [mockStyles.timePickerWidth72, {backgroundColor: mockTheme.componentBG}]; - - it('highlights PM for the English CONST.TIME_PERIOD marker', () => { - const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle(CONST.TIME_PERIOD.PM, 'AM', 'PM'); - expect(styleForPM).toEqual(highlighted); - expect(styleForAM).toEqual(dimmed); - }); - - it('highlights AM for the English CONST.TIME_PERIOD marker', () => { - const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle(CONST.TIME_PERIOD.AM, 'AM', 'PM'); - expect(styleForAM).toEqual(highlighted); - expect(styleForPM).toEqual(dimmed); - }); - - // Regression test for the localized AM/PM bug: the saved time yields a localized period marker (Japanese "午後"), - // which must still highlight the PM button by matching the localized `common.pm` label. - it('highlights PM for a localized period marker (ja "午後")', () => { - const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle('午後', '午前', '午後'); - expect(styleForPM).toEqual(highlighted); - expect(styleForAM).toEqual(dimmed); - }); - - it('highlights AM for a localized period marker (ja "午前")', () => { - const {styleForAM, styleForPM} = getStatusAMandPMButtonStyle('午前', '午前', '午後'); - expect(styleForAM).toEqual(highlighted); - expect(styleForPM).toEqual(dimmed); - }); -}); diff --git a/tests/unit/DateUtilsTest.ts b/tests/unit/DateUtilsTest.ts index be81192c0676..d1b5b808e0a9 100644 --- a/tests/unit/DateUtilsTest.ts +++ b/tests/unit/DateUtilsTest.ts @@ -724,4 +724,65 @@ describe('DateUtils', () => { expect(DateUtils.getRemainingSecondsInWindow(Date.now() - 31 * 1000, windowMs)).toBe(0); }); }); + + // The TimePicker AM/PM round trip must stay locale-independent: the saved time is displayed localized via + // extractTime12Hour, parsed back into an English CONST.TIME_PERIOD marker by get12HourTimeObjectFromDate, and the + // resulting `hh:mm a` string is re-parsed by combineDateAndTime. Under locales whose date-fns day-period marker or + // translation diverges from English (e.g. de "vorm./nachm.", el "π.μ./μ.μ."), a localized period would break the + // highlight comparison and, once tapped or forced to English, break the reverse parse and the time save entirely. + describe('TimePicker AM/PM round trip under localized locales', () => { + // A PM and an AM saved datetime, in the "yyyy-MM-dd HH:mm:ss" shape stored for a Custom Status "Clear after". + const savedPm = '2026-08-15 23:10:00'; + const savedAm = '2026-08-15 09:05:00'; + + describe.each([CONST.LOCALES.DE, CONST.LOCALES.EL])('locale "%s"', (locale) => { + beforeEach(async () => { + // Awaiting load() guarantees setDefaultOptions({locale}) has run, so DateUtils' locale-less date-fns + // calls (extractTime12Hour display, and the parse inside get12HourTimeObjectFromDate) use this locale. + await IntlStore.load(locale); + await waitForBatchedUpdates(); + }); + + it('get12HourTimeObjectFromDate returns an English PM marker for a localized saved time', () => { + const localizedTime = DateUtils.extractTime12Hour(savedPm); + const {hour, minute, period} = DateUtils.get12HourTimeObjectFromDate(localizedTime); + expect(hour).toBe('11'); + expect(minute).toBe('10'); + expect(period).toBe(CONST.TIME_PERIOD.PM); + }); + + it('get12HourTimeObjectFromDate returns an English AM marker for a localized saved time', () => { + const localizedTime = DateUtils.extractTime12Hour(savedAm); + const {hour, minute, period} = DateUtils.get12HourTimeObjectFromDate(localizedTime); + expect(hour).toBe('09'); + expect(minute).toBe('05'); + expect(period).toBe(CONST.TIME_PERIOD.AM); + }); + + it('combineDateAndTime round-trips the TimePicker string (PM) back to the saved datetime', () => { + const {hour, minute, period} = DateUtils.get12HourTimeObjectFromDate(DateUtils.extractTime12Hour(savedPm)); + // This is exactly the string TimePicker submits: `${hours}:${minutes} ${amPmValue}`. + const timeString = `${hour}:${minute} ${period}`; + expect(DateUtils.combineDateAndTime(timeString, savedPm)).toBe(savedPm); + }); + + it('combineDateAndTime round-trips the TimePicker string (AM) back to the saved datetime', () => { + const {hour, minute, period} = DateUtils.get12HourTimeObjectFromDate(DateUtils.extractTime12Hour(savedAm)); + const timeString = `${hour}:${minute} ${period}`; + expect(DateUtils.combineDateAndTime(timeString, savedAm)).toBe(savedAm); + }); + + it('combineDateAndTime parses an English AM/PM string even when the active locale localizes the marker', () => { + // The button-tap path always sets the English CONST.TIME_PERIOD marker. Before pinning the parse to + // enGB this returned '' under these locales, blocking the save; it must now parse correctly. + expect(DateUtils.combineDateAndTime('11:10 PM', '2026-08-15 00:00:00')).toBe(savedPm); + expect(DateUtils.combineDateAndTime('09:05 AM', '2026-08-15 00:00:00')).toBe(savedAm); + }); + }); + + it('get12HourTimeObjectFromDate maps midnight and noon to the correct English markers', () => { + expect(DateUtils.get12HourTimeObjectFromDate('12:00 AM').period).toBe(CONST.TIME_PERIOD.AM); + expect(DateUtils.get12HourTimeObjectFromDate('12:00 PM').period).toBe(CONST.TIME_PERIOD.PM); + }); + }); }); From 74ab3416d36f12bc5b9c244fdf9a06d78aa20f51 Mon Sep 17 00:00:00 2001 From: "Abdelrahman Khattab (via MelvinBot)" Date: Tue, 11 Aug 2026 22:40:04 +0000 Subject: [PATCH 3/3] Fix: add cspell:ignore for German AM/PM markers in comments Co-authored-by: Abdelrahman Khattab --- src/libs/DateUtils.ts | 1 + tests/unit/DateUtilsTest.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 3ac03a559689..229227a80c3c 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -648,6 +648,7 @@ function get12HourTimeObjectFromDate(dateTime: string, isFullFormat = false): {h // (the AM/PM buttons render their own translated labels), so it must be a locale-independent English marker. // Deriving it from the 24-hour clock avoids the active date-fns locale returning a localized marker (e.g. de // "NACHM.", el "Μ.Μ.") that would match neither CONST.TIME_PERIOD nor round-trip through the English parse. + // cspell:ignore NACHM period: parsedTime.getHours() >= 12 ? CONST.TIME_PERIOD.PM : CONST.TIME_PERIOD.AM, }; } diff --git a/tests/unit/DateUtilsTest.ts b/tests/unit/DateUtilsTest.ts index d1b5b808e0a9..bb912e540176 100644 --- a/tests/unit/DateUtilsTest.ts +++ b/tests/unit/DateUtilsTest.ts @@ -730,6 +730,7 @@ describe('DateUtils', () => { // resulting `hh:mm a` string is re-parsed by combineDateAndTime. Under locales whose date-fns day-period marker or // translation diverges from English (e.g. de "vorm./nachm.", el "π.μ./μ.μ."), a localized period would break the // highlight comparison and, once tapped or forced to English, break the reverse parse and the time save entirely. + // cspell:ignore vorm nachm describe('TimePicker AM/PM round trip under localized locales', () => { // A PM and an AM saved datetime, in the "yyyy-MM-dd HH:mm:ss" shape stored for a Custom Status "Clear after". const savedPm = '2026-08-15 23:10:00';