diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-22 18:02:15 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-05-22 12:31:06 -0700 |
commit | f63485b444bd8469d24c70f64f3097bed239c7a8 (patch) | |
tree | 4960697b8efbe14846c1180160e224d1d903d3b2 | |
parent | 2ba2ea934aac45c6d8b3c6db920776fb2b29cc66 (diff) | |
download | subsurface-f63485b444bd8469d24c70f64f3097bed239c7a8.tar.gz |
cleanup: move timestampToDateTime() to qthelper.cpp
Move this function from maintab.cpp to qthelper.cpp. Since the
functionality was used in numerous places, use the helper function
there as well. This removes a number of inconsistencies. For example,
sometime setTimeSpec(Qt::UTC) was called, even though the
QDateTime object was already created with that time spec.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/gpslocation.cpp | 6 | ||||
-rw-r--r-- | core/qthelper.cpp | 12 | ||||
-rw-r--r-- | core/qthelper.h | 1 | ||||
-rw-r--r-- | core/subsurface-qt/diveobjecthelper.cpp | 6 | ||||
-rw-r--r-- | desktop-widgets/mainwindow.cpp | 2 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.cpp | 14 | ||||
-rw-r--r-- | qt-models/divetripmodel.cpp | 9 |
7 files changed, 22 insertions, 28 deletions
diff --git a/core/gpslocation.cpp b/core/gpslocation.cpp index feea645e1..ebd5b1950 100644 --- a/core/gpslocation.cpp +++ b/core/gpslocation.cpp @@ -133,7 +133,7 @@ QString GpsLocation::currentPosition() if (!hasLocationsSource()) return tr("Unknown GPS location (no GPS source)"); if (m_trackers.count()) { - QDateTime lastFixTime = QDateTime().fromMSecsSinceEpoch((m_trackers.lastKey() - gettimezoneoffset(m_trackers.lastKey())) * 1000); + QDateTime lastFixTime = timestampToDateTime(m_trackers.lastKey() - gettimezoneoffset(m_trackers.lastKey())); QDateTime now = QDateTime::currentDateTime(); int delta = lastFixTime.secsTo(now); qDebug() << "lastFixTime" << lastFixTime.toString() << "now" << now.toString() << "delta" << delta; @@ -173,7 +173,7 @@ void GpsLocation::newPosition(QGeoPositionInfo pos) if (!nr || waitingForPosition || delta > prefs.time_threshold || lastCoord.distanceTo(pos.coordinate()) > prefs.distance_threshold) { QString msg = QStringLiteral("received new position %1 after delta %2 threshold %3 (now %4 last %5)"); - status(qPrintable(msg.arg(pos.coordinate().toString()).arg(delta).arg(prefs.time_threshold).arg(pos.timestamp().toString()).arg(QDateTime().fromMSecsSinceEpoch(lastTime * 1000).toString()))); + status(qPrintable(msg.arg(pos.coordinate().toString()).arg(delta).arg(prefs.time_threshold).arg(pos.timestamp().toString()).arg(timestampToDateTime(lastTime).toString()))); waitingForPosition = false; acquiredPosition(); gpsTracker gt; @@ -182,7 +182,7 @@ void GpsLocation::newPosition(QGeoPositionInfo pos) gt.location = create_location(pos.coordinate().latitude(), pos.coordinate().longitude()); addFixToStorage(gt); gpsTracker gtNew = m_trackers.last(); - qDebug() << "newest fix is now at" << QDateTime().fromMSecsSinceEpoch(gtNew.when - gettimezoneoffset(gtNew.when) * 1000).toString(); + qDebug() << "newest fix is now at" << timestampToDateTime(gtNew.when - gettimezoneoffset(gtNew.when)).toString(); } } diff --git a/core/qthelper.cpp b/core/qthelper.cpp index e1329ca26..561dd9213 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -696,12 +696,19 @@ int gettimezoneoffset(timestamp_t when) if (when == 0) dt1 = QDateTime::currentDateTime(); else - dt1 = QDateTime::fromMSecsSinceEpoch(when * 1000); + dt1 = timestampToDateTime(when); dt2 = dt1.toUTC(); dt1.setTimeSpec(Qt::UTC); return dt2.secsTo(dt1); } +QDateTime timestampToDateTime(timestamp_t when) +{ + // Subsurface always uses "local time" as in "whatever was the local time at the location" + // so all time stamps have no time zone information and are in UTC + return QDateTime::fromMSecsSinceEpoch(1000 * when, Qt::UTC); +} + QString render_seconds_to_string(int seconds) { if (seconds % 60 == 0) @@ -984,8 +991,7 @@ QString get_trip_date_string(timestamp_t when, int nr, bool getday) { struct tm tm; utc_mkdate(when, &tm); - QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000*when,Qt::UTC); - localTime.setTimeSpec(Qt::UTC); + QDateTime localTime = timestampToDateTime(when); QString suffix = " " + gettextFromC::tr("(%n dive(s))", "", nr); if (getday) diff --git a/core/qthelper.h b/core/qthelper.h index 8cd31b29c..b0cab8b15 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -62,6 +62,7 @@ QString getSubsurfaceDataPath(QString folderToFind); QString getPrintingTemplatePathUser(); QString getPrintingTemplatePathBundle(); int gettimezoneoffset(timestamp_t when = 0); +QDateTime timestampToDateTime(timestamp_t when); int parseDurationToSeconds(const QString &text); int parseLengthToMm(const QString &text); int parseTemperatureToMkelvin(const QString &text); diff --git a/core/subsurface-qt/diveobjecthelper.cpp b/core/subsurface-qt/diveobjecthelper.cpp index 7c8651507..45cb73868 100644 --- a/core/subsurface-qt/diveobjecthelper.cpp +++ b/core/subsurface-qt/diveobjecthelper.cpp @@ -328,15 +328,13 @@ DiveObjectHelperGrantlee::DiveObjectHelperGrantlee(const struct dive *d) : QString DiveObjectHelper::date() const { - QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * timestamp, Qt::UTC); - localTime.setTimeSpec(Qt::UTC); + QDateTime localTime = timestampToDateTime(timestamp); return localTime.date().toString(prefs.date_format_short); } QString DiveObjectHelper::time() const { - QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * timestamp, Qt::UTC); - localTime.setTimeSpec(Qt::UTC); + QDateTime localTime = timestampToDateTime(timestamp); return localTime.time().toString(prefs.time_format); } diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index b3f474de8..94c51279f 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -865,7 +865,7 @@ void MainWindow::on_actionReplanDive_triggered() graphics->clearHandlers(); setApplicationState(ApplicationState::PlanDive); divePlannerWidget->setReplanButton(true); - divePlannerWidget->setupStartTime(QDateTime::fromMSecsSinceEpoch(1000 * current_dive->when, Qt::UTC)); + divePlannerWidget->setupStartTime(timestampToDateTime(current_dive->when)); if (current_dive->surface_pressure.mbar) divePlannerWidget->setSurfacePressure(current_dive->surface_pressure.mbar); if (current_dive->salinity) diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index d11581516..81c56a486 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -307,14 +307,6 @@ void MainTab::updateNotes(const struct dive *d) } } -static QDateTime timestampToDateTime(timestamp_t when) -{ - // Subsurface always uses "local time" as in "whatever was the local time at the location" - // so all time stamps have no time zone information and are in UTC - QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * when, Qt::UTC); - localTime.setTimeSpec(Qt::UTC); - return localTime; -} void MainTab::updateDateTime(const struct dive *d) { QDateTime localTime = timestampToDateTime(d->when); @@ -617,8 +609,7 @@ void MainTab::on_dateEdit_editingFinished() { if (ignoreInput || !current_dive) return; - QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(1000*current_dive->when, Qt::UTC); - dateTime.setTimeSpec(Qt::UTC); + QDateTime dateTime = timestampToDateTime(current_dive->when); dateTime.setDate(ui.dateEdit->date()); shiftTime(dateTime); } @@ -627,8 +618,7 @@ void MainTab::on_timeEdit_editingFinished() { if (ignoreInput || !current_dive) return; - QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(1000*current_dive->when, Qt::UTC); - dateTime.setTimeSpec(Qt::UTC); + QDateTime dateTime = timestampToDateTime(current_dive->when); dateTime.setTime(ui.timeEdit->time()); shiftTime(dateTime); } diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index a153b31c7..a48c827dd 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -63,7 +63,7 @@ QString DiveTripModelBase::tripShortDate(const dive_trip *trip) { if (!trip) return QString(); - QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(trip), Qt::UTC); + QDateTime firstTime = timestampToDateTime(trip_date(trip)); QString firstMonth = firstTime.toString("MMM"); return QStringLiteral("%1\n'%2").arg(firstMonth,firstTime.toString("yy")); } @@ -79,10 +79,10 @@ QString DiveTripModelBase::tripTitle(const dive_trip *trip) if (title.isEmpty()) { // so use the date range - QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(trip), Qt::UTC); + QDateTime firstTime = timestampToDateTime(trip_date(trip)); QString firstMonth = firstTime.toString("MMM"); QString firstYear = firstTime.toString("yyyy"); - QDateTime lastTime = QDateTime::fromMSecsSinceEpoch(1000*trip->dives.dives[0]->when, Qt::UTC); + QDateTime lastTime = timestampToDateTime(trip->dives.dives[0]->when); QString lastMonth = lastTime.toString("MMM"); QString lastYear = lastTime.toString("yyyy"); if (lastMonth == firstMonth && lastYear == firstYear) @@ -201,8 +201,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) // variable in the QtQuick list view. That has to be a string because it will try // to do locale-aware sorting. And amazingly this can't be changed. case MobileListModel::DateTimeRole: { - QDateTime localTime = QDateTime::fromMSecsSinceEpoch(1000 * d->when, Qt::UTC); - localTime.setTimeSpec(Qt::UTC); + QDateTime localTime = timestampToDateTime(d->when); return QStringLiteral("%1 %2").arg(localTime.date().toString(prefs.date_format_short), localTime.time().toString(prefs.time_format)); } |