fix: device sync timestamp is wrong

- prevent QML `int` overflow
- use `LocaleUtils.formatRelativeTimestamp()` to perform the timestamp
formatting

Fixes #10412
This commit is contained in:
Lukáš Tinkl 2023-04-25 12:34:43 +02:00 committed by Lukáš Tinkl
parent e170af5115
commit 0009ebd68f
2 changed files with 12 additions and 15 deletions

View File

@ -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
}

View File

@ -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"