This commit is contained in:
David Heinemeier Hansson
2026-05-20 21:44:08 +02:00
parent 9211b4b9c3
commit 06a312240e
+10 -16
View File
@@ -9,9 +9,10 @@ BarWidget {
property bool alt: false property bool alt: false
// Qt.formatDateTime doesn't recognize `ww` (ISO week number). Pre-substitute readonly property string activeFormat: alt
// it as a Qt-quoted literal before formatting so users can write the natural ? setting("formatAlt", "dd MMMM 'W'ww yyyy")
// `'W'ww yyyy` and get `W21 2026` rather than `Www 2026`. : (bar && bar.vertical ? setting("verticalFormat", "HH\n—\nmm") : setting("format", "dddd HH:mm"))
function isoWeek(date) { function isoWeek(date) {
var d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())) var d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
var day = d.getUTCDay() || 7 var day = d.getUTCDay() || 7
@@ -20,20 +21,13 @@ BarWidget {
return Math.ceil(((d - yearStart) / 86400000 + 1) / 7) return Math.ceil(((d - yearStart) / 86400000 + 1) / 7)
} }
function format(date, fmt) { function isoWeekLiteral(date) {
var f = String(fmt || "") var week = isoWeek(date)
if (f.indexOf("ww") !== -1) { return (week < 10 ? "0" : "") + week
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() { function formatted(date) {
if (alt) return format(clock.date, setting("formatAlt", "dd MMMM 'W'ww yyyy")) return Qt.formatDateTime(date, activeFormat.replace(/ww/g, isoWeekLiteral(date)))
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 implicitWidth: button.implicitWidth
@@ -48,7 +42,7 @@ BarWidget {
id: button id: button
anchors.fill: parent anchors.fill: parent
bar: root.bar bar: root.bar
text: root.label() text: root.formatted(clock.date)
horizontalMargin: 8.75 horizontalMargin: 8.75
verticalPadding: 8.75 verticalPadding: 8.75
onPressed: function(button) { onPressed: function(button) {