From 0009ebd68f9ecfeac41e63f58954541fce82a98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Tue, 25 Apr 2023 12:34:43 +0200 Subject: [PATCH] fix: device sync timestamp is wrong - prevent QML `int` overflow - use `LocaleUtils.formatRelativeTimestamp()` to perform the timestamp formatting Fixes #10412 --- .../Components/StatusSyncDeviceDelegate.qml | 11 ++++------- ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml | 16 ++++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/ui/StatusQ/src/StatusQ/Components/StatusSyncDeviceDelegate.qml b/ui/StatusQ/src/StatusQ/Components/StatusSyncDeviceDelegate.qml index 3f6646a9e9..31e1848d9e 100644 --- a/ui/StatusQ/src/StatusQ/Components/StatusSyncDeviceDelegate.qml +++ b/ui/StatusQ/src/StatusQ/Components/StatusSyncDeviceDelegate.qml @@ -45,12 +45,10 @@ StatusListItem { if (d.daysFromSync == 1) return qsTr("Last online yesterday") - const date = new Date(d.deviceLastTimestamp) - if (d.daysFromSync <= 6) - return qsTr("Last online [%1]").arg(LocaleUtils.getDayName(date)) + return qsTr("Last online: %1").arg(LocaleUtils.formatRelativeTimestamp(d.deviceLastTimestamp)) - return qsTr("Last online %1").arg(LocaleUtils.formatDate(date)) + return qsTr("Last online: %1").arg(LocaleUtils.formatDate(d.deviceLastTimestamp)) } subTitleBadgeComponent: root.showOnlineBadge ? onlineBadgeComponent : null @@ -77,11 +75,10 @@ StatusListItem { id: d property real now: 0 - readonly property int deviceLastTimestamp: root.timestamp / 1000000 + readonly property real deviceLastTimestamp: root.timestamp / 1000000 readonly property int secondsFromSync: (now - Math.max(0, d.deviceLastTimestamp)) / 1000 readonly property int minutesFromSync: secondsFromSync / 60 - readonly property int hoursFromSync: minutesFromSync / 60 - readonly property int daysFromSync: hoursFromSync / 24 + readonly property int daysFromSync: LocaleUtils.daysBetween(new Date(now), new Date(d.deviceLastTimestamp)) readonly property bool onlineNow: secondsFromSync <= 120 } diff --git a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml index cce7fc99e4..0112cf7d9d 100644 --- a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml +++ b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml @@ -206,13 +206,6 @@ QtObject { } } - // return full days between 2 dates - function daysBetween(firstDate, secondDate) { - firstDate.setHours(0, 0, 0) // discard time - secondDate.setHours(0, 0, 0) - return Math.round(Math.abs((firstDate - secondDate) / d.msInADay)) // Math.round: not all days are 24 hours long! - } - property var nonDigitCharacterRegExpLocale readonly property var nonDigitCharacterRegExp: { @@ -234,6 +227,13 @@ QtObject { return !d.amPmFormatChars.some(ampm => timeFormatString.includes(ampm)) } + // return full days between 2 dates + function daysBetween(firstDate, secondDate) { + firstDate.setHours(0, 0, 0) // discard time + secondDate.setHours(0, 0, 0) + return Math.round(Math.abs((firstDate - secondDate) / d.msInADay)) // Math.round: not all days are 24 hours long! + } + /** Converts the Date to a string containing the date suitable for the specified locale in the specified format. @@ -287,7 +287,7 @@ QtObject { const value = d.readDate(timestamp) const loc = Qt.locale() const formatString = d.fixupTimeFormatString(loc.timeFormat(Locale.ShortFormat)) // format string for the time part - const dayDifference = d.daysBetween(d.readDate(timestamp), now) + const dayDifference = daysBetween(d.readDate(timestamp), now) // within last day, 2 or 7 days if (dayDifference < 1) // today -> "Today 14:23"