From 2c715542fd4fae6c2313c55d8c370689ff7dd931 Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Wed, 22 Jun 2016 22:46:22 +0200 Subject: Unify handling of QDateTime time zone information 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 Signed-off-by: Dirk Hohndel --- core/gpslocation.cpp | 17 +++++++++-------- desktop-widgets/simplewidgets.cpp | 8 +++----- mobile-widgets/qmlmanager.cpp | 3 ++- qt-models/diveplannermodel.cpp | 3 ++- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/core/gpslocation.cpp b/core/gpslocation.cpp index 2a097588a..d3f91ebaf 100644 --- a/core/gpslocation.cpp +++ b/core/gpslocation.cpp @@ -475,9 +475,8 @@ void GpsLocation::deleteFixesFromServer() QList keys = m_trackers.keys(); while (!m_deletedTrackers.isEmpty()) { gpsTracker gt = m_deletedTrackers.takeFirst(); - QDateTime dt; + QDateTime dt = QDateTime::fromTime_t(gt.when, Qt::UTC); QUrlQuery data; - dt.setTime_t(gt.when - gettimezoneoffset(gt.when)); data.addQueryItem("login", prefs.userid); data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd")); data.addQueryItem("dive_time", dt.toString("hh:mm")); @@ -521,9 +520,8 @@ void GpsLocation::uploadToServer() QUrl url(GPS_FIX_ADD_URL); Q_FOREACH(qint64 key, m_trackers.keys()) { struct gpsTracker gt = m_trackers.value(key); - QDateTime dt; + QDateTime dt = QDateTime::fromTime_t(gt.when, Qt::UTC); QUrlQuery data; - dt.setTime_t(gt.when - gettimezoneoffset(gt.when)); data.addQueryItem("login", prefs.userid); data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd")); data.addQueryItem("dive_time", dt.toString("hh:mm")); @@ -602,15 +600,18 @@ void GpsLocation::downloadFromServer() qDebug() << downloadedFixes.count() << "GPS fixes downloaded"; for (int i = 0; i < downloadedFixes.count(); i++) { QJsonObject fix = downloadedFixes[i].toObject(); - QString date = fix.value("date").toString(); - QString time = fix.value("time").toString(); + QDate date = QDate::fromString(fix.value("date").toString(), "yyy-M-d"); + QTime time = QTime::fromString(fix.value("time").toString(), "hh:m:s"); 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.setDate(date); + timestamp.setTime(time); 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 cbf31cb98..add4c80de 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -292,7 +292,6 @@ void ShiftImageTimesDialog::buttonClicked(QAbstractButton *button) void ShiftImageTimesDialog::syncCameraClicked() { QPixmap picture; - QDateTime dcDateTime = QDateTime(); QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open image file"), DiveListView::lastUsedImageDir(), @@ -308,7 +307,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)); + QDateTime dcDateTime = QDateTime::fromTime_t(dcImageEpoch, Qt::UTC); ui.dcTime->setDateTime(dcDateTime); connect(ui.dcTime, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(dcDateTimeChanged(const QDateTime &))); } @@ -365,11 +364,10 @@ void ShiftImageTimesDialog::setOffset(time_t offset) void ShiftImageTimesDialog::updateInvalid() { timestamp_t timestamp; - QDateTime time; bool allValid = true; ui.warningLabel->hide(); ui.invalidLabel->hide(); - time.setTime_t(displayed_dive.when - gettimezoneoffset(displayed_dive.when)); + QDateTime time = QDateTime::fromTime_t(displayed_dive.when, Qt::UTC); ui.invalidLabel->setText("Dive:" + time.toString() + "\n"); Q_FOREACH (const QString &fileName, fileNames) { @@ -378,7 +376,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 eae4a4e8f..a8dd24948 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 03cc90e06..425d8bc1a 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -353,6 +353,7 @@ DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTable tempGFLow(100) { memset(&diveplan, 0, sizeof(diveplan)); + startTime.setTimeSpec(Qt::UTC); } DivePlannerPointsModel *DivePlannerPointsModel::instance() @@ -540,7 +541,7 @@ void DivePlannerPointsModel::setStartDate(const QDate &date) void DivePlannerPointsModel::setStartTime(const QTime &t) { startTime.setTime(t); - diveplan.when = startTime.toTime_t(); + diveplan.when = startTime.toTime_t(); displayed_dive.when = diveplan.when; emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1)); } -- cgit v1.2.3-70-g09d2