mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-11-25 11:13:41 +00:00
Format relative timestamps using the locale's own date order (#34484)
Co-authored-by: David Baker <dbkr@users.noreply.github.com>
This commit is contained in:
@@ -19,7 +19,6 @@ import {
|
||||
formatPreciseDuration,
|
||||
formatLocalDateShort,
|
||||
getDaysArray,
|
||||
getMonthsArray,
|
||||
formatFullDateNoDayNoTime,
|
||||
formatTime,
|
||||
formatFullTime,
|
||||
@@ -77,99 +76,6 @@ describe("getDaysArray", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonthsArray", () => {
|
||||
it("should return January-December in long mode", () => {
|
||||
expect(getMonthsArray("long")).toMatchInlineSnapshot(`
|
||||
[
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
"April",
|
||||
"May",
|
||||
"June",
|
||||
"July",
|
||||
"August",
|
||||
"September",
|
||||
"October",
|
||||
"November",
|
||||
"December",
|
||||
]
|
||||
`);
|
||||
});
|
||||
it("should return Jan-Dec in short mode", () => {
|
||||
expect(getMonthsArray("short")).toMatchInlineSnapshot(`
|
||||
[
|
||||
"Jan",
|
||||
"Feb",
|
||||
"Mar",
|
||||
"Apr",
|
||||
"May",
|
||||
"Jun",
|
||||
"Jul",
|
||||
"Aug",
|
||||
"Sep",
|
||||
"Oct",
|
||||
"Nov",
|
||||
"Dec",
|
||||
]
|
||||
`);
|
||||
});
|
||||
it("should return J-D in narrow mode", () => {
|
||||
expect(getMonthsArray("narrow")).toMatchInlineSnapshot(`
|
||||
[
|
||||
"J",
|
||||
"F",
|
||||
"M",
|
||||
"A",
|
||||
"M",
|
||||
"J",
|
||||
"J",
|
||||
"A",
|
||||
"S",
|
||||
"O",
|
||||
"N",
|
||||
"D",
|
||||
]
|
||||
`);
|
||||
});
|
||||
it("should return 1-12 in numeric mode", () => {
|
||||
expect(getMonthsArray("numeric")).toMatchInlineSnapshot(`
|
||||
[
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8",
|
||||
"9",
|
||||
"10",
|
||||
"11",
|
||||
"12",
|
||||
]
|
||||
`);
|
||||
});
|
||||
it("should return 01-12 in 2-digit mode", () => {
|
||||
expect(getMonthsArray("2-digit")).toMatchInlineSnapshot(`
|
||||
[
|
||||
"01",
|
||||
"02",
|
||||
"03",
|
||||
"04",
|
||||
"05",
|
||||
"06",
|
||||
"07",
|
||||
"08",
|
||||
"09",
|
||||
"10",
|
||||
"11",
|
||||
"12",
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatDate", () => {
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers();
|
||||
@@ -304,6 +210,23 @@ describe("formatRelativeTime", () => {
|
||||
const date = new Date(1604142141000);
|
||||
expect(formatRelativeTime(date, true)).toBe("Oct 31, 2020");
|
||||
});
|
||||
|
||||
it("orders the month and day to suit the locale", () => {
|
||||
const date = new Date(2021, 9, 25, 11, 1, 23, 0);
|
||||
expect(formatRelativeTime(date, false, "en-GB")).toBe("25 Oct");
|
||||
expect(formatRelativeTime(date, false, "ja")).toBe("10月25日");
|
||||
});
|
||||
|
||||
it("appends the year in the position the locale expects", () => {
|
||||
const date = new Date(2020, 9, 31, 11, 1, 23, 0);
|
||||
expect(formatRelativeTime(date, false, "en-GB")).toBe("31 Oct 2020");
|
||||
expect(formatRelativeTime(date, false, "ja")).toBe("2020年10月31日");
|
||||
});
|
||||
|
||||
it("passes the locale through to the time format", () => {
|
||||
const date = new Date(2021, 10, 2, 11, 1, 23, 0);
|
||||
expect(formatRelativeTime(date, true, "en-GB")).toBe("11:01 am");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatDuration()", () => {
|
||||
|
||||
+17
-19
@@ -28,15 +28,6 @@ export function getDaysArray(weekday: Intl.DateTimeFormatOptions["weekday"] = "s
|
||||
return [...Array(7).keys()].map((day) => dateTimeFormat.format(sunday + day * DAY_MS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of 12 month names, from January to December, internationalised to the user's language.
|
||||
* @param month - format desired "numeric" | "2-digit" | "long" | "short" | "narrow"
|
||||
*/
|
||||
export function getMonthsArray(month: Intl.DateTimeFormatOptions["month"] = "short"): string[] {
|
||||
const dateTimeFormat = new Intl.DateTimeFormat(getUserLanguage(), { month, timeZone: "UTC" });
|
||||
return [...Array(12).keys()].map((m) => dateTimeFormat.format(Date.UTC(2021, m)));
|
||||
}
|
||||
|
||||
// XXX: Ideally we could just specify `hour12: boolean` but it has issues on Chrome in the `en` locale
|
||||
// https://support.google.com/chrome/thread/29828561?hl=en
|
||||
export function getTwelveHourOptions(showTwelveHour: boolean): Intl.DateTimeFormatOptions {
|
||||
@@ -250,19 +241,26 @@ export function formatFullDateNoDayNoTime(date: Date, locale?: string): string {
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
export function formatRelativeTime(date: Date, showTwelveHour = false): string {
|
||||
/**
|
||||
* Formats a given date as a time if it falls within the current day, and as a short date otherwise.
|
||||
* The year is included only when the date is not within the current year.
|
||||
* @example 16:58, 25 Oct, 31 Oct 2020 in en-GB locale
|
||||
* @param date - date object to format
|
||||
* @param showTwelveHour - whether to use 12-hour rather than 24-hour time. Defaults to `false` (24 hour mode).
|
||||
* @param locale - the locale string to use, in BCP 47 format, defaulting to user's selected application locale
|
||||
*/
|
||||
export function formatRelativeTime(date: Date, showTwelveHour = false, locale?: string): string {
|
||||
const _locale = locale ?? getUserLanguage();
|
||||
const now = new Date();
|
||||
if (withinCurrentDay(date, now)) {
|
||||
return formatTime(date, showTwelveHour);
|
||||
} else {
|
||||
const months = getMonthsArray();
|
||||
let relativeDate = `${months[date.getMonth()]} ${date.getDate()}`;
|
||||
|
||||
if (!withinCurrentYear(date, now)) {
|
||||
relativeDate += `, ${date.getFullYear()}`;
|
||||
}
|
||||
return relativeDate;
|
||||
return formatTime(date, showTwelveHour, _locale);
|
||||
}
|
||||
return new Intl.DateTimeFormat(_locale, {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: withinCurrentYear(date, now) ? undefined : "numeric",
|
||||
timeZone: getUserTimezone(),
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user