From 24d2a7e53c387166a0301066931016dd114229e3 Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Wed, 22 Jun 2016 22:46:22 +0200 Subject: [PATCH] Unify handling of QDateTime time zone information To: subsurface@subsurface-divelog.org Subsurface uses "local time" which in particular means we never display time zone information to the user. The user (and our file format) only sees times like 5pm or 17:00. A better name than local time (which could mean "local at the dive spot) would be "watch time", the time displayed by the diver's watch when she entered the water. Internally, we store times as time_t, seconds since Jan 1 1970 0:00 UTC. Our convention for conversion between 5pm and time_t as always been to treat 5pm as if it were UTC. Then confusion arose since Qt's QDateTime (which is tied to UI elements like QTimeEdit and similar) is time zone aware and by default assumes the system time zone. So when we set a QDateTime to 5pm and then later convert it to time_t we have to take care about the difference between UTC and the system time zone. This patch unifies our solution to this problem: With it, we set all QDateTime's time zone to UTC. This means we don't have to correct for a time zone anymore when converting to time_t (note, however, the signedness issue: Qt's idea of time_t is broken since it assumes it to be unsigned thus not allowing for dates before 1970. Better use the millisecont variants). We only need to be careful about time zones when using the current time. With this convention, when assigning the current time to a QDateTime, we need to shift for the time zone since its value in UTC should actually be the watch time of the user who is most likely used to the system time zone. Signed-off-by: Robert C. Helling --- core/gpslocation.cpp | 12 ++++++++---- desktop-widgets/simplewidgets.cpp | 10 ++++++---- mobile-widgets/qmlmanager.cpp | 3 ++- qt-models/diveplannermodel.cpp | 2 ++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/core/gpslocation.cpp b/core/gpslocation.cpp index 2a09758..260637b 100644 --- a/core/gpslocation.cpp +++ b/core/gpslocation.cpp @@ -476,8 +476,9 @@ void GpsLocation::deleteFixesFromServer() while (!m_deletedTrackers.isEmpty()) { gpsTracker gt = m_deletedTrackers.takeFirst(); QDateTime dt; + dt.setTimeSpec(Qt::UTC); QUrlQuery data; - dt.setTime_t(gt.when - gettimezoneoffset(gt.when)); + dt.setTime_t(gt.when); data.addQueryItem("login", prefs.userid); data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd")); data.addQueryItem("dive_time", dt.toString("hh:mm")); @@ -522,8 +523,9 @@ void GpsLocation::uploadToServer() Q_FOREACH(qint64 key, m_trackers.keys()) { struct gpsTracker gt = m_trackers.value(key); QDateTime dt; + dt.setTimeSpec(Qt::UTC); QUrlQuery data; - dt.setTime_t(gt.when - gettimezoneoffset(gt.when)); + dt.setTime_t(gt.when); data.addQueryItem("login", prefs.userid); data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd")); data.addQueryItem("dive_time", dt.toString("hh:mm")); @@ -607,10 +609,12 @@ void GpsLocation::downloadFromServer() QString name = fix.value("name").toString(); QString latitude = fix.value("latitude").toString(); QString longitude = fix.value("longitude").toString(); - QDateTime timestamp = QDateTime::fromString(date + " " + time, "yyyy-M-d hh:m:s"); + QDateTime timestamp; + timestamp.setTimeSpec(Qt::UTC); + timestamp = QDateTime::fromString(date + " " + time, "yyyy-M-d hh:m:s"); struct gpsTracker gt; - gt.when = timestamp.toMSecsSinceEpoch() / 1000 + gettimezoneoffset(timestamp.toMSecsSinceEpoch() / 1000); + gt.when = timestamp.toMSecsSinceEpoch() / 1000; gt.latitude.udeg = latitude.toDouble() * 1000000; gt.longitude.udeg = longitude.toDouble() * 1000000; gt.name = name; diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index cbf31cb..ef2371c 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -292,7 +292,8 @@ void ShiftImageTimesDialog::buttonClicked(QAbstractButton *button) void ShiftImageTimesDialog::syncCameraClicked() { QPixmap picture; - QDateTime dcDateTime = QDateTime(); + QDateTime dcDateTime; + dcDateTime.setTimeSpec(Qt::UTC); QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open image file"), DiveListView::lastUsedImageDir(), @@ -308,7 +309,7 @@ void ShiftImageTimesDialog::syncCameraClicked() ui.DCImage->setScene(scene); dcImageEpoch = picture_get_timestamp(fileNames.at(0).toUtf8().data()); - dcDateTime.setTime_t(dcImageEpoch - gettimezoneoffset(displayed_dive.when)); + dcDateTime.setTime_t(dcImageEpoch); ui.dcTime->setDateTime(dcDateTime); connect(ui.dcTime, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(dcDateTimeChanged(const QDateTime &))); } @@ -366,10 +367,11 @@ void ShiftImageTimesDialog::updateInvalid() { timestamp_t timestamp; QDateTime time; + time.setTimeSpec(Qt::UTC); bool allValid = true; ui.warningLabel->hide(); ui.invalidLabel->hide(); - time.setTime_t(displayed_dive.when - gettimezoneoffset(displayed_dive.when)); + time.setTime_t(displayed_dive.when); ui.invalidLabel->setText("Dive:" + time.toString() + "\n"); Q_FOREACH (const QString &fileName, fileNames) { @@ -378,7 +380,7 @@ void ShiftImageTimesDialog::updateInvalid() // We've found invalid image timestamp = picture_get_timestamp(fileName.toUtf8().data()); - time.setTime_t(timestamp + m_amount - gettimezoneoffset(displayed_dive.when)); + time.setTime_t(timestamp + m_amount); ui.invalidLabel->setText(ui.invalidLabel->text() + fileName + " " + time.toString() + "\n"); allValid = false; } diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index eae4a4e..a8dd249 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -578,6 +578,7 @@ bool QMLManager::checkDate(DiveObjectHelper *myDive, struct dive * d, QString da QString oldDate = myDive->date() + " " + myDive->time(); if (date != oldDate) { QDateTime newDate; + newDate.setTimeSpec(Qt::UTC); // what a pain - Qt will not parse dates if the day of the week is incorrect // so if the user changed the date but didn't update the day of the week (most likely behavior, actually), // we need to make sure we don't try to parse that @@ -668,7 +669,7 @@ parsed: // add a hundred years. if (newDate.addYears(100) < QDateTime::currentDateTime().addYears(1)) newDate = newDate.addYears(100); - d->dc.when = d->when = newDate.toMSecsSinceEpoch() / 1000 + gettimezoneoffset(newDate.toMSecsSinceEpoch() / 1000); + d->dc.when = d->when = newDate.toMSecsSinceEpoch() / 1000; return true; } qDebug() << "none of our parsing attempts worked for the date string"; diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index 16a2e40..613b260 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -4,6 +4,7 @@ #include "qt-models/cylindermodel.h" #include "core/planner.h" #include "qt-models/models.h" +#include /* TODO: Port this to CleanerTableModel to remove a bit of boilerplate and * use the signal warningMessage() to communicate errors to the MainWindow. @@ -346,6 +347,7 @@ DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTable tempGFLow(100) { memset(&diveplan, 0, sizeof(diveplan)); + startTime.setTimeSpec(Qt::UTC); } DivePlannerPointsModel *DivePlannerPointsModel::instance() -- 2.7.4 (Apple Git-66)