Make clock's alt format include the ISO week number

Set the default formatAlt to "dd MMMM 'W'ww yyyy" (was "dd MMMM yyyy")
in shell-defaults.json, the bundled settings-panel defaults, and the
form placeholder text — clock's own widget fallback already used this
spec but the shipped config overrode it.

Qt.formatDateTime doesn't recognize `ww` and renders it literally
(`20 May Www 2026`), so add an `isoWeek` helper to clock.qml that
pre-substitutes `ww` with a Qt-quoted literal of the ISO 8601 week
number before handing the format off to Qt. Users get `20 May W21 2026`
without anyone needing to know about the substitution.
This commit is contained in:
David Heinemeier Hansson
2026-05-20 21:37:48 +02:00
parent 2b03aec510
commit 3878ad3a57
3 changed files with 27 additions and 6 deletions
+24 -3
View File
@@ -9,10 +9,31 @@ BarWidget {
property bool alt: false
// Qt.formatDateTime doesn't recognize `ww` (ISO week number). Pre-substitute
// it as a Qt-quoted literal before formatting so users can write the natural
// `'W'ww yyyy` and get `W21 2026` rather than `Www 2026`.
function isoWeek(date) {
var d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
var day = d.getUTCDay() || 7
d.setUTCDate(d.getUTCDate() + 4 - day)
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1))
return Math.ceil(((d - yearStart) / 86400000 + 1) / 7)
}
function format(date, fmt) {
var f = String(fmt || "")
if (f.indexOf("ww") !== -1) {
var w = isoWeek(date)
var ww = w < 10 ? "0" + w : String(w)
f = f.replace(/ww/g, "'" + ww + "'")
}
return Qt.formatDateTime(date, f)
}
function label() {
if (alt) return Qt.formatDateTime(clock.date, String(setting("formatAlt", "dd MMMM 'W'ww yyyy")))
if (bar && bar.vertical) return Qt.formatDateTime(clock.date, String(setting("verticalFormat", "HH\n—\nmm")))
return Qt.formatDateTime(clock.date, String(setting("format", "dddd HH:mm")))
if (alt) return format(clock.date, setting("formatAlt", "dd MMMM 'W'ww yyyy"))
if (bar && bar.vertical) return format(clock.date, setting("verticalFormat", "HH\n—\nmm"))
return format(clock.date, setting("format", "dddd HH:mm"))
}
implicitWidth: button.implicitWidth