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>
This commit is contained in:
Berthold Stoeger 2020-05-22 18:02:15 +02:00 committed by Dirk Hohndel
parent 2ba2ea934a
commit f63485b444
7 changed files with 22 additions and 28 deletions

View file

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

View file

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

View file

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

View file

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