Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot which is better? GB or US or doesn't matter at all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter at all here — functionally identical.

date-fns's en-GB locale imports en-US's localize and match verbatim; the only thing it overrides is formatLong (the default date/time ordering). Since this call uses an explicit 'hh:mm a' format string, formatLong is never consulted, and the AM/PM parsing comes straight from the shared en-US match. So enGB and enUS parse 02:00 PM exactly the same way.

If you want the most self-documenting choice, enUS is marginally nicer: date-fns's built-in default locale (when no locale option is passed) is already en-US, so passing enUS explicitly restores the exact pre-IntlStore-override behavior. But either is correct.

Evidence

en-GB.js in the installed date-fns:

import { localize } from "./en-US/_lib/localize.js";
import { match } from "./en-US/_lib/match.js";
import { formatLong } from "./en-GB/_lib/formatLong.js";

AM/PM matching lives in match/localize (shared with en-US); the GB-specific override is formatLong only, which an explicit format string doesn't use.

import throttle from 'lodash/throttle';

import {setCurrentDate} from './actions/CurrentDate';
Expand Down Expand Up @@ -463,8 +464,8 @@ function extractTime12Hour(dateTimeString: string, isFullFormat = false): string
return '';
}
const date = new Date(dateTimeString);
// get12HourTimeObjectFromDate parses this back into TimePicker state, whose period is compared against the English
// CONST.TIME_PERIOD, so the marker has to stay English. That leaves StatusClearAfterPage showing an English AM/PM.
// get12HourTimeObjectFromDate parses this back with the same default locale, so the format and the parse stay
// in step in every language.
// eslint-disable-next-line rulesdir/require-locale-for-localized-date-format -- see above
return format(date, isFullFormat ? 'hh:mm:ss.SSS a' : 'hh:mm a');
}
Expand Down Expand Up @@ -581,7 +582,8 @@ const combineDateAndTime = (updatedTime: string, inputDateTime: string): string
}
Comment thread
mukhrr marked this conversation as resolved.
Resolved
} else if (updatedTime.includes(':')) {
// it's in "hh:mm a" format
const tempTime = parse(updatedTime, 'hh:mm a', new Date());
// The picker always submits English AM/PM markers, which the app's active date-fns locale may not parse.
const tempTime = parse(updatedTime, 'hh:mm a', new Date(), {locale: enGB});
if (isValid(tempTime)) {
parsedTime = tempTime;
}
Expand Down Expand Up @@ -639,8 +641,7 @@ 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(),
period: parsedTime.getHours() >= 12 ? CONST.TIME_PERIOD.PM : CONST.TIME_PERIOD.AM,
};
}

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,26 @@ describe('DateUtils', () => {
expect(DateUtils.getRemainingSecondsInWindow(Date.now() - 31 * 1000, windowMs)).toBe(0);
});
});

describe('time picker helpers with a non-English date-fns locale', () => {
beforeEach(() => IntlStore.load(CONST.LOCALES.DE));

it('combineDateAndTime parses the picker-submitted English AM/PM value', () => {
expect(DateUtils.combineDateAndTime('02:00 PM', '2026-08-04')).toBe('2026-08-04 14:00:00');
expect(DateUtils.combineDateAndTime('08:00 AM', '2026-08-04 00:00:00')).toBe('2026-08-04 08:00:00');
});

it('get12HourTimeObjectFromDate returns the AM/PM period for a localized time string', () => {
const localizedNoon = DateUtils.extractTime12Hour('2026-08-04 12:00:00');
expect(DateUtils.get12HourTimeObjectFromDate(localizedNoon).period).toBe(CONST.TIME_PERIOD.PM);
const localizedMorning = DateUtils.extractTime12Hour('2026-08-04 08:00:00');
expect(DateUtils.get12HourTimeObjectFromDate(localizedMorning)).toEqual({hour: '08', minute: '00', seconds: '00', milliseconds: '000', period: CONST.TIME_PERIOD.AM});
});

it('per diem start/end range built from picker values validates', () => {
const newStart = DateUtils.combineDateAndTime('08:00 AM', '2026-08-04');
const newEnd = DateUtils.combineDateAndTime('02:00 PM', '2026-08-04');
expect(DateUtils.isValidStartEndTimeRange({startTime: newStart, endTime: newEnd})).toBe(true);
});
});
});
Loading