Add a shared time-format preference

Introduce a locale/12-hour/24-hour preference on the clock service and a shared Intl-based formatter used across the dashboard.
This commit is contained in:
raucao committed 2026-10-27 21:48:54 +04:00
1 parent e314744e77
commit 08a0ae078f
4 files changed
+146 -3

No files matched your search

+24 -1
View File
@@ -1,15 +1,38 @@
import Service from '@ember/service';
import Service, { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { DEFAULT_TIME_FORMAT } from '../utils/time-format';
export default class ClockService extends Service {
@service storage;
@tracked time = new Date();
@tracked timeFormat = DEFAULT_TIME_FORMAT;
intervalId;
ready = null;
constructor() {
super(...arguments);
this.ready = this.load();
this.start();
}
async load() {
try {
const stored = await this.storage.get('preferences', 'time-format');
if (stored) {
this.timeFormat = stored;
}
} catch (err) {
console.error('Error loading the time format:', err);
}
}
async setTimeFormat(format) {
this.timeFormat = format;
await this.storage.set('preferences', 'time-format', format);
}
start() {
this.intervalId = setInterval(() => {
this.time = new Date();
+49
View File
@@ -0,0 +1,49 @@
// Shared time formatting for the clock and calendar widgets. The stored
// preference picks between the system locale's convention, 12-hour, and
// 24-hour; everything goes through Intl so the separators and day periods match
// the locale.
export const DEFAULT_TIME_FORMAT = 'locale';
export const TIME_FORMATS = [
{ value: 'locale', label: 'Match system' },
{ value: '12h', label: '12-hour' },
{ value: '24h', label: '24-hour' },
];
const OPTIONS = {
locale: { hour: 'numeric', minute: '2-digit' },
'12h': { hour: 'numeric', minute: '2-digit', hour12: true },
'24h': { hour: '2-digit', minute: '2-digit', hour12: false },
};
export function timeOptions(format) {
return OPTIONS[format] ?? OPTIONS[DEFAULT_TIME_FORMAT];
}
// The full time string, e.g. `1:05 PM` (12h) or `13:05` (24h).
export function formatTime(date, format = DEFAULT_TIME_FORMAT) {
return new Intl.DateTimeFormat(undefined, timeOptions(format)).format(date);
}
// Splits the time into the clock face and its day period, so the clock can
// render the period smaller. `period` is empty for 24-hour formats.
export function timeParts(date, format = DEFAULT_TIME_FORMAT) {
const parts = new Intl.DateTimeFormat(
undefined,
timeOptions(format)
).formatToParts(date);
let time = '';
let period = '';
for (const part of parts) {
if (part.type === 'dayPeriod') {
period = part.value;
} else {
time += part.value;
}
}
return { time: time.trim(), period };
}
+35 -2
View File
@@ -1,12 +1,45 @@
import { module, test } from 'qunit';
import { setupTest } from 'dashtab/tests/helpers';
import { DEFAULT_TIME_FORMAT } from 'dashtab/utils/time-format';
module('Unit | Service | clock', function (hooks) {
setupTest(hooks);
// TODO: Replace this with your real tests.
hooks.beforeEach(async function () {
const storage = this.owner.lookup('service:storage');
await storage.remove('preferences', 'time-format');
});
test('it exists', function (assert) {
let service = this.owner.lookup('service:clock');
const service = this.owner.lookup('service:clock');
assert.ok(service);
});
test('defaults to the locale format', async function (assert) {
const service = this.owner.lookup('service:clock');
await service.ready;
assert.strictEqual(service.timeFormat, DEFAULT_TIME_FORMAT);
});
test('loads a stored format', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('preferences', 'time-format', '24h');
const service = this.owner.lookup('service:clock');
await service.ready;
assert.strictEqual(service.timeFormat, '24h');
});
test('setTimeFormat updates the state and persists it', async function (assert) {
const service = this.owner.lookup('service:clock');
await service.ready;
await service.setTimeFormat('12h');
const storage = this.owner.lookup('service:storage');
assert.strictEqual(service.timeFormat, '12h');
assert.strictEqual(await storage.get('preferences', 'time-format'), '12h');
});
});
+38
View File
@@ -0,0 +1,38 @@
import { module, test } from 'qunit';
import {
DEFAULT_TIME_FORMAT,
formatTime,
timeParts,
} from 'dashtab/utils/time-format';
const DATE = new Date(2026, 7, 3, 13, 5);
module('Unit | Utility | time-format', function () {
test('defaults to the locale format', function (assert) {
assert.strictEqual(DEFAULT_TIME_FORMAT, 'locale');
assert.ok(formatTime(DATE).length > 0);
assert.strictEqual(
formatTime(DATE, 'bogus'),
formatTime(DATE, DEFAULT_TIME_FORMAT)
);
});
test('24-hour format has no day period', function (assert) {
const { time, period } = timeParts(DATE, '24h');
assert.strictEqual(period, '');
assert.ok(/^13[:.]05$/.test(time), `unexpected time: ${time}`);
assert.strictEqual(formatTime(DATE, '24h'), time);
});
test('12-hour format includes a day period', function (assert) {
const { time, period } = timeParts(DATE, '12h');
assert.ok(period.length > 0);
assert.ok(/^1[:.]05$/.test(time), `unexpected time: ${time}`);
const formatted = formatTime(DATE, '12h');
assert.ok(formatted.includes(time));
assert.ok(formatted.includes(period));
});
});