macOS: Add support for 12/24-hour time display preferences

This commit is contained in:
Jamie Kyle 2023-07-31 09:23:19 -07:00 committed by GitHub
parent 88858af144
commit 1143c0e9ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 208 additions and 14 deletions

View file

@ -0,0 +1,63 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import * as sinon from 'sinon';
import { formatTimestamp } from '../../util/formatTimestamp';
import { HourCyclePreference } from '../../types/I18N';
const min = new Date(2023, 0, 1, 0).getTime();
const max = new Date(2023, 0, 1, 23).getTime();
describe('formatTimestamp', () => {
let sandbox: sinon.SinonSandbox;
let localesStub: sinon.SinonStub;
let hourCycleStub: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
localesStub = sandbox.stub(window, 'getPreferredSystemLocales');
hourCycleStub = sandbox.stub(window, 'getHourCyclePreference');
});
afterEach(() => {
sandbox.restore();
});
function testCase(
locale: string,
preference: HourCyclePreference,
time: number,
expected: string
) {
const timeFmt = new Intl.DateTimeFormat('en', {
timeStyle: 'medium',
}).format(time);
it(`should format with locale: ${locale} (${HourCyclePreference[preference]}) @ ${timeFmt})`, () => {
localesStub.returns([locale]);
hourCycleStub.returns(preference);
assert.equal(formatTimestamp(time, { timeStyle: 'medium' }), expected);
});
}
testCase('en', HourCyclePreference.UnknownPreference, min, '12:00:00 AM');
testCase('en', HourCyclePreference.UnknownPreference, max, '11:00:00 PM');
testCase('en', HourCyclePreference.Prefer12, min, '12:00:00 AM');
testCase('en', HourCyclePreference.Prefer12, max, '11:00:00 PM');
testCase('en', HourCyclePreference.Prefer24, min, '00:00:00');
testCase('en', HourCyclePreference.Prefer24, max, '23:00:00');
testCase('nb', HourCyclePreference.UnknownPreference, min, '00:00:00');
testCase('nb', HourCyclePreference.UnknownPreference, max, '23:00:00');
testCase('nb', HourCyclePreference.Prefer12, min, '12:00:00 a.m.');
testCase('nb', HourCyclePreference.Prefer12, max, '11:00:00 p.m.');
testCase('nb', HourCyclePreference.Prefer24, min, '00:00:00');
testCase('nb', HourCyclePreference.Prefer24, max, '23:00:00');
testCase('ja', HourCyclePreference.UnknownPreference, min, '0:00:00');
testCase('ja', HourCyclePreference.UnknownPreference, max, '23:00:00');
testCase('ja', HourCyclePreference.Prefer12, min, '午前0:00:00');
testCase('ja', HourCyclePreference.Prefer12, max, '午後11:00:00');
testCase('ja', HourCyclePreference.Prefer24, min, '0:00:00');
testCase('ja', HourCyclePreference.Prefer24, max, '23:00:00');
});