Skip to content

Commit 81f9a84

Browse files
authored
Set user's 24h preference from their current OS locale (#29651)
@silverwind Can you update the issue content? Looks like we need to merge issue content and #29651 (comment) as the commit message.
1 parent 25b842d commit 81f9a84

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

web_src/js/components/RepoActionView.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {SvgIcon} from '../svg.js';
33
import ActionRunStatus from './ActionRunStatus.vue';
44
import {createApp} from 'vue';
55
import {toggleElem} from '../utils/dom.js';
6-
import {getCurrentLocale} from '../utils.js';
6+
import {formatDatetime} from '../utils/time.js';
77
import {renderAnsi} from '../render/ansi.js';
88
import {POST, DELETE} from '../modules/fetch.js';
99
@@ -167,7 +167,7 @@ const sfc = {
167167
const logTimeStamp = document.createElement('span');
168168
logTimeStamp.className = 'log-time-stamp';
169169
const date = new Date(parseFloat(line.timestamp * 1000));
170-
const timeStamp = date.toLocaleString(getCurrentLocale(), {timeZoneName: 'short'});
170+
const timeStamp = formatDatetime(date);
171171
logTimeStamp.textContent = timeStamp;
172172
toggleElem(logTimeStamp, this.timeVisible['log-time-stamp']);
173173
// for "Show seconds"

web_src/js/modules/tippy.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import tippy, {followCursor} from 'tippy.js';
22
import {isDocumentFragmentOrElementNode} from '../utils/dom.js';
3+
import {formatDatetime} from '../utils/time.js';
34

45
const visibleInstances = new Set();
56

@@ -93,8 +94,15 @@ function attachTooltip(target, content = null) {
9394
}
9495

9596
function switchTitleToTooltip(target) {
96-
const title = target.getAttribute('title');
97+
let title = target.getAttribute('title');
9798
if (title) {
99+
// apply custom formatting to relative-time's tooltips
100+
if (target.tagName.toLowerCase() === 'relative-time') {
101+
const datetime = target.getAttribute('datetime');
102+
if (datetime) {
103+
title = formatDatetime(new Date(datetime));
104+
}
105+
}
98106
target.setAttribute('data-tooltip-content', title);
99107
target.setAttribute('aria-label', title);
100108
// keep the attribute, in case there are some other "[title]" selectors

web_src/js/utils/time.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dayjs from 'dayjs';
2+
import {getCurrentLocale} from '../utils.js';
23

34
// Returns an array of millisecond-timestamps of start-of-week days (Sundays)
45
export function startDaysBetween(startDate, endDate) {
@@ -44,3 +45,23 @@ export function fillEmptyStartDaysWithZeroes(startDays, data) {
4445

4546
return Object.values(result);
4647
}
48+
49+
let dateFormat;
50+
51+
// format a Date object to document's locale, but with 24h format from user's current locale because this
52+
// option is a personal preference of the user, not something that the document's locale should dictate.
53+
export function formatDatetime(date) {
54+
if (!dateFormat) {
55+
// TODO: replace `hour12` with `Intl.Locale.prototype.getHourCycles` once there is broad browser support
56+
dateFormat = new Intl.DateTimeFormat(getCurrentLocale(), {
57+
day: 'numeric',
58+
month: 'short',
59+
year: 'numeric',
60+
hour: 'numeric',
61+
hour12: !Number.isInteger(Number(new Intl.DateTimeFormat([], {hour: 'numeric'}).format())),
62+
minute: '2-digit',
63+
timeZoneName: 'short',
64+
});
65+
}
66+
return dateFormat.format(date);
67+
}

0 commit comments

Comments
 (0)