diff options
author | Robert C. Helling <helling@atdotde.de> | 2016-06-22 22:46:22 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2016-08-10 15:04:39 -0700 |
commit | 2c715542fd4fae6c2313c55d8c370689ff7dd931 (patch) | |
tree | 773aa73015d13ddc40402aa3f8ee6e5820b03c97 /core/gpslocation.cpp | |
parent | bd40ef7f424604013e1fe1b5feea648ab6304b72 (diff) | |
download | subsurface-2c715542fd4fae6c2313c55d8c370689ff7dd931.tar.gz |
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 <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'core/gpslocation.cpp')
-rw-r--r-- | core/gpslocation.cpp | 17 |
1 files changed, 9 insertions, 8 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<qint64> 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; |