Respect user's 12/24 hour preference consistently (#29237)

* Respect user's 12/24 hour preference consistently

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Update test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-02-11 14:16:59 +00:00
committed by GitHub
parent f2fae82e32
commit 6fa8032caa
3 changed files with 11 additions and 4 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ export function getMonthsArray(month: Intl.DateTimeFormatOptions["month"] = "sho
// XXX: Ideally we could just specify `hour12: boolean` but it has issues on Chrome in the `en` locale // 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 // https://support.google.com/chrome/thread/29828561?hl=en
function getTwelveHourOptions(showTwelveHour: boolean): Intl.DateTimeFormatOptions { export function getTwelveHourOptions(showTwelveHour: boolean): Intl.DateTimeFormatOptions {
return { return {
hourCycle: showTwelveHour ? "h12" : "h23", hourCycle: showTwelveHour ? "h12" : "h23",
}; };
+6 -2
View File
@@ -7,6 +7,9 @@ Please see LICENSE files in the repository root for full details.
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { type MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix"; import { type MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
import { getTwelveHourOptions } from "../DateUtils.ts";
import { useSettingValue } from "./useSettings.ts";
/** /**
* Fetch a user's delclared timezone through their profile, and return * Fetch a user's delclared timezone through their profile, and return
* a friendly string of the current time for that user. This will keep * a friendly string of the current time for that user. This will keep
@@ -23,6 +26,7 @@ export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone:
const [updateInterval, setUpdateInterval] = useState<ReturnType<typeof setTimeout>>(); const [updateInterval, setUpdateInterval] = useState<ReturnType<typeof setTimeout>>();
const [friendly, setFriendly] = useState<string>(); const [friendly, setFriendly] = useState<string>();
const [supported, setSupported] = useState<boolean>(); const [supported, setSupported] = useState<boolean>();
const showTwelveHour = useSettingValue("showTwelveHourTimestamps");
useEffect(() => { useEffect(() => {
if (!cli || supported !== undefined) { if (!cli || supported !== undefined) {
@@ -62,8 +66,8 @@ export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone:
const updateTime = (): void => { const updateTime = (): void => {
const currentTime = new Date(); const currentTime = new Date();
const friendly = currentTime.toLocaleString(undefined, { const friendly = currentTime.toLocaleString(undefined, {
...getTwelveHourOptions(showTwelveHour),
timeZone: tz, timeZone: tz,
hour12: true,
hour: "2-digit", hour: "2-digit",
minute: "2-digit", minute: "2-digit",
timeZoneName: "shortOffset", timeZoneName: "shortOffset",
@@ -84,7 +88,7 @@ export const useUserTimezone = (cli: MatrixClient, userId: string): { timezone:
console.error("Could not render current timezone for user", ex); console.error("Could not render current timezone for user", ex);
} }
})(); })();
}, [supported, userId, cli]); }, [supported, userId, cli, showTwelveHour]);
if (!timezone || !friendly) { if (!timezone || !friendly) {
return null; return null;
@@ -241,7 +241,10 @@ describe("<UserInfo />", () => {
_locale, _locale,
opts, opts,
) { ) {
return origDate.call(this, "en-US", opts); return origDate.call(this, "en-US", {
...opts,
hourCycle: "h12",
});
}); });
mockClient.doesServerSupportExtendedProfiles.mockResolvedValue(true); mockClient.doesServerSupportExtendedProfiles.mockResolvedValue(true);
mockClient.getExtendedProfileProperty.mockResolvedValue("Europe/London"); mockClient.getExtendedProfileProperty.mockResolvedValue("Europe/London");