From 5430c3a029590f824419dce5774a5e10efd72df5 Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Thu, 21 Apr 2022 17:17:01 +0000 Subject: [PATCH] applets/digital-clock: Fix date drift dateTimeChanged is called every second. This in turn checks if the date has changed. If so we call setupLabels which updates the clock. The date label used to use main.currentTime which is a value set in the binding of the timeLabel's text property. The order of this being updated vs running this code is undefined. Bug 1: We compare an ever so slightly different date source to the one used in the date text label. Bug 2: We are not necessarily updating the date label with the most updated time. Overall we can't mix declarative and imperative design patterns. This patch commits to being imperative and introduces a function to get the current time so that the date label and time label will always get the most up to date value even if they update from different trigers. BUG: 452554 FIXED-IN: 5.24.5 --- .../package/contents/ui/DigitalClock.qml | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/applets/digital-clock/package/contents/ui/DigitalClock.qml b/applets/digital-clock/package/contents/ui/DigitalClock.qml index 2c33a8803..19b6c0500 100644 --- a/applets/digital-clock/package/contents/ui/DigitalClock.qml +++ b/applets/digital-clock/package/contents/ui/DigitalClock.qml @@ -18,7 +18,6 @@ Item { id: main property string timeFormat - property date currentTime property bool showSeconds: Plasmoid.configuration.showSeconds property bool showLocalTimezone: Plasmoid.configuration.showLocalTimezone @@ -79,7 +78,7 @@ Item { target: Plasmoid.self function onContextualActionsAboutToShow() { ClipboardMenu.secondsIncluded = main.showSeconds; - ClipboardMenu.currentDate = main.currentTime; + ClipboardMenu.currentDate = main.getCurrentTime(); } } @@ -98,6 +97,16 @@ Item { } } + function getCurrentTime() { + // get the time for the given timezone from the dataengine + var now = dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["DateTime"]; + // get current UTC time + var msUTC = now.getTime() + (now.getTimezoneOffset() * 60000); + // add the dataengine TZ offset to it + var currentTime = new Date(msUTC + (dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["Offset"] * 1000)); + return currentTime; + } + states: [ State { name: "horizontalPanel" @@ -497,17 +506,7 @@ Item { } minimumPixelSize: 1 - text: { - // get the time for the given timezone from the dataengine - var now = dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["DateTime"]; - // get current UTC time - var msUTC = now.getTime() + (now.getTimezoneOffset() * 60000); - // add the dataengine TZ offset to it - var currentTime = new Date(msUTC + (dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["Offset"] * 1000)); - - main.currentTime = currentTime; - return Qt.formatTime(currentTime, main.timeFormat); - } + text: Qt.formatTime(main.getCurrentTime(), main.timeFormat) verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter @@ -634,7 +633,7 @@ Item { if (main.showDate) { - dateLabel.text = Qt.formatDate(main.currentTime, main.dateFormat); + dateLabel.text = Qt.formatDate(main.getCurrentTime(), main.dateFormat); } else { // clear it so it doesn't take space in the layout dateLabel.text = ""; @@ -674,7 +673,7 @@ Item { if (main.showDate) { // If the date has changed, force size recalculation, because the day name // or the month name can now be longer/shorter, so we need to adjust applet size - const currentDate = Qt.formatDateTime(dataSource.data["Local"]["DateTime"], "yyyy-MM-dd"); + const currentDate = Qt.formatDateTime(main.getCurrentTime(), "yyyy-MM-dd"); if (main.lastDate !== currentDate) { doCorrections = true; main.lastDate = currentDate