From 4d394c31f998276e96e0e192714f4ac38341ea55 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Tue, 5 Nov 2024 20:54:38 +0100 Subject: [PATCH] fix the timezones used for the user info box and add comments (#5162) --- cockatrice/src/server/user/user_info_box.cpp | 10 +++++++--- servatrice/src/servatrice_database_interface.cpp | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cockatrice/src/server/user/user_info_box.cpp b/cockatrice/src/server/user/user_info_box.cpp index b291f62ff..364f38afd 100644 --- a/cockatrice/src/server/user/user_info_box.cpp +++ b/cockatrice/src/server/user/user_info_box.cpp @@ -138,17 +138,21 @@ void UserInfoBox::updateInfo(const ServerInfo_User &user) QString UserInfoBox::getAgeString(int ageSeconds) { QString accountAgeString = tr("Unknown"); - if (ageSeconds == 0) + if (ageSeconds <= 0) return accountAgeString; - auto date = QDateTime::fromSecsSinceEpoch(QDateTime::currentSecsSinceEpoch() - ageSeconds).date(); + // secsSinceEpoch is in utc + auto secsSinceEpoch = QDateTime::currentSecsSinceEpoch() - ageSeconds; + // the date is in local time, fromSecsSinceEpoch expects a timestamp from utc and converts it to local time + auto date = QDateTime::fromSecsSinceEpoch(secsSinceEpoch).date(); if (!date.isValid()) return accountAgeString; - QString dateString = QLocale().toString(date, QLocale::ShortFormat); + // now can be local time as the date is also local time auto now = QDate::currentDate(); auto daysAndYears = getDaysAndYearsBetween(date, now); + QString dateString = QLocale().toString(date, QLocale::ShortFormat); QString yearString; if (daysAndYears.second > 0) { yearString = tr("%n Year(s), ", "amount of years (only shown if more than 0)", daysAndYears.second); diff --git a/servatrice/src/servatrice_database_interface.cpp b/servatrice/src/servatrice_database_interface.cpp index ab90eb97e..38d8128c9 100644 --- a/servatrice/src/servatrice_database_interface.cpp +++ b/servatrice/src/servatrice_database_interface.cpp @@ -613,7 +613,8 @@ ServerInfo_User Servatrice_DatabaseInterface::evalUserQueryResult(const QSqlQuer const QDateTime regDate = query->value(7).toDateTime(); if (!regDate.toString(Qt::ISODate).isEmpty()) { - qint64 accountAgeInSeconds = regDate.secsTo(QDateTime::currentDateTime()); + // the registration date is in utc + qint64 accountAgeInSeconds = regDate.secsTo(QDateTime::currentDateTimeUtc()); result.set_accountage_secs(accountAgeInSeconds); }