DateTime offset in Windows : display a date from javascript doesn't take account of daylight.

This commit is contained in:
Julien Wadel 2022-10-19 12:29:25 +02:00
parent 815adbb1f3
commit a69dcc14d3
5 changed files with 30 additions and 17 deletions

View file

@ -26,6 +26,7 @@
#include <QFile>
#include <QImageReader>
#include <QDebug>
#include <QTimeZone>
#include <QUrl>
#include "config.h"
@ -38,6 +39,10 @@
#include "components/settings/SettingsModel.hpp"
#include "app/paths/Paths.hpp"
#ifdef _WIN32
#include <time.h>
#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){

View file

@ -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) {

View file

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

View file

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

View file

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