From a69dcc14d345d63318458b3bdfe1cb50645df474 Mon Sep 17 00:00:00 2001 From: Julien Wadel Date: Wed, 19 Oct 2022 12:29:25 +0200 Subject: [PATCH] DateTime offset in Windows : display a date from javascript doesn't take account of daylight. --- linphone-app/src/utils/Utils.cpp | 25 +++++++++++++++---- linphone-app/src/utils/Utils.hpp | 4 ++- .../ui/modules/Linphone/Chat/Chat.qml | 7 ++---- .../ui/modules/Linphone/Chat/Notice.qml | 3 ++- .../ui/modules/Linphone/History/History.qml | 8 +++--- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/linphone-app/src/utils/Utils.cpp b/linphone-app/src/utils/Utils.cpp index 1f8c28653..1ff0e0351 100644 --- a/linphone-app/src/utils/Utils.cpp +++ b/linphone-app/src/utils/Utils.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "config.h" @@ -38,6 +39,10 @@ #include "components/settings/SettingsModel.hpp" #include "app/paths/Paths.hpp" +#ifdef _WIN32 +#include +#endif + // ============================================================================= namespace { @@ -86,19 +91,29 @@ QDateTime Utils::addMinutes(QDateTime date, const int& min){ return date.addSecs(min*60); } +QDateTime Utils::getOffsettedUTC(const QDateTime& date){ + QDateTime utc = date.toUTC();// Get a date free of any offsets. + auto timezone = date.timeZone(); + utc = utc.addSecs(timezone.offsetFromUtc(date));// add offset from date timezone + utc.setTimeSpec(Qt::UTC);// ensure to have an UTC date + return utc; +} + QString Utils::toDateTimeString(QDateTime date){ if(date.date() == QDate::currentDate()) return toTimeString(date); - else - return date.toString("yyyy/MM/dd hh:mm:ss"); + else{ + return getOffsettedUTC(date).toString("yyyy/MM/dd hh:mm:ss"); + } } -QString Utils::toTimeString(QDateTime date){ - return date.toString("hh:mm:ss"); +QString Utils::toTimeString(QDateTime date, const QString& format){ +// Issue : date.toString() will not print the good time in timezones. Get it from date and add ourself the offset. + return getOffsettedUTC(date).toString(format); } QString Utils::toDateString(QDateTime date){ - return date.toString("yyyy/MM/dd"); + return getOffsettedUTC(date).toString("yyyy/MM/dd"); } QString Utils::getDisplayName(const QString& address){ diff --git a/linphone-app/src/utils/Utils.hpp b/linphone-app/src/utils/Utils.hpp index c59cfa049..dbcdd7bf0 100644 --- a/linphone-app/src/utils/Utils.hpp +++ b/linphone-app/src/utils/Utils.hpp @@ -54,8 +54,9 @@ public: // Qt interfaces Q_INVOKABLE static bool hasCapability(const QString& address, const LinphoneEnums::FriendCapability& capability); Q_INVOKABLE static QDateTime addMinutes(QDateTime date, const int& min); + static QDateTime getOffsettedUTC(const QDateTime& date); Q_INVOKABLE static QString toDateTimeString(QDateTime date); - Q_INVOKABLE static QString toTimeString(QDateTime date); + Q_INVOKABLE static QString toTimeString(QDateTime date, const QString& format = "hh:mm:ss"); Q_INVOKABLE static QString toDateString(QDateTime date); Q_INVOKABLE static QString getDisplayName(const QString& address); Q_INVOKABLE static QString toString(const LinphoneEnums::TunnelMode& mode); @@ -65,6 +66,7 @@ public: Q_INVOKABLE QSize getImageSize(const QString& url); Q_INVOKABLE static QPoint getCursorPosition(); Q_INVOKABLE static QString getFileChecksum(const QString& filePath); + //---------------------------------------------------------------------------------- static inline QString coreStringToAppString (const std::string &str) { diff --git a/linphone-app/ui/modules/Linphone/Chat/Chat.qml b/linphone-app/ui/modules/Linphone/Chat/Chat.qml index a4742c2cb..63adcaf1e 100644 --- a/linphone-app/ui/modules/Linphone/Chat/Chat.qml +++ b/linphone-app/ui/modules/Linphone/Chat/Chat.qml @@ -244,15 +244,12 @@ Rectangle { color: ChatStyle.entry.event.text.color font.pointSize: ChatStyle.entry.time.pointSize - text: $chatEntry.timestamp.toLocaleString( - Qt.locale(App.locale), - 'hh:mm' - ) + text: UtilsCpp.toTimeString($chatEntry.timestamp, 'hh:mm') verticalAlignment: Text.AlignVCenter TooltipArea { - text: $chatEntry.timestamp.toLocaleString(Qt.locale(App.locale)) + text: UtilsCpp.toDateTimeString($chatEntry.timestamp) } visible:!isNotice } diff --git a/linphone-app/ui/modules/Linphone/Chat/Notice.qml b/linphone-app/ui/modules/Linphone/Chat/Notice.qml index d061bb31f..bbe8d7699 100644 --- a/linphone-app/ui/modules/Linphone/Chat/Notice.qml +++ b/linphone-app/ui/modules/Linphone/Chat/Notice.qml @@ -5,6 +5,7 @@ import Common 1.0 import Linphone 1.0 import Linphone.Styles 1.0 import Utils 1.0 +import UtilsCpp 1.0 import LinphoneEnums 1.0 import Units 1.0 import ColorsList 1.0 @@ -115,7 +116,7 @@ RowLayout{ text: $chatEntry.name?_type.arg($chatEntry.name):_type verticalAlignment: Text.AlignVCenter TooltipArea { - text: $chatEntry.timestamp.toLocaleString(Qt.locale(App.locale)) + text: UtilsCpp.toDateTimeString($chatEntry.timestamp) } } Rectangle{ diff --git a/linphone-app/ui/modules/Linphone/History/History.qml b/linphone-app/ui/modules/Linphone/History/History.qml index 031c0b62f..adadda725 100644 --- a/linphone-app/ui/modules/Linphone/History/History.qml +++ b/linphone-app/ui/modules/Linphone/History/History.qml @@ -5,6 +5,7 @@ import QtQuick.Layouts 1.3 import Common 1.0 import Linphone 1.0 import Linphone.Styles 1.0 +import UtilsCpp 1.0 import 'History.js' as Logic import 'qrc:/ui/scripts/Utils/utils.js' as Utils @@ -164,15 +165,12 @@ Rectangle { color: HistoryStyle.entry.time.color font.pointSize: HistoryStyle.entry.time.pointSize - text: $historyEntry.timestamp.toLocaleString( - Qt.locale(App.locale), - 'hh:mm' - ) + text: UtilsCpp.toTimeString($historyEntry.timestamp, 'hh:mm') verticalAlignment: Text.AlignVCenter TooltipArea { - text: $historyEntry.timestamp.toLocaleString(Qt.locale(App.locale)) + text: UtilsCpp.toDateTimeString($historyEntry.timestamp) } }