cleanup: create common QDateTime -> timestamp conversion function

In analogy to the timestamp -> QDateTime conversion, create a
common function.
1) For symmetry with the opposite conversion.
2) To remove numerous inconsistencies.
3) To remove use of the deprecated QDateTime::toTime_t() function.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-05-22 18:53:25 +02:00 committed by Dirk Hohndel
parent e33e420ef3
commit 8f80129bac
9 changed files with 19 additions and 13 deletions

View file

@ -169,7 +169,7 @@ void GpsLocation::newPosition(QGeoPositionInfo pos)
// if we are waiting for a position update or // if we are waiting for a position update or
// if we have no record stored or if at least the configured minimum // if we have no record stored or if at least the configured minimum
// time has passed or we moved at least the configured minimum distance // time has passed or we moved at least the configured minimum distance
int64_t delta = (int64_t)pos.timestamp().toTime_t() + gettimezoneoffset() - lastTime; int64_t delta = dateTimeToTimestamp(pos.timestamp()) + gettimezoneoffset() - lastTime;
if (!nr || waitingForPosition || delta > prefs.time_threshold || if (!nr || waitingForPosition || delta > prefs.time_threshold ||
lastCoord.distanceTo(pos.coordinate()) > prefs.distance_threshold) { lastCoord.distanceTo(pos.coordinate()) > prefs.distance_threshold) {
QString msg = QStringLiteral("received new position %1 after delta %2 threshold %3 (now %4 last %5)"); QString msg = QStringLiteral("received new position %1 after delta %2 threshold %3 (now %4 last %5)");
@ -177,7 +177,7 @@ void GpsLocation::newPosition(QGeoPositionInfo pos)
waitingForPosition = false; waitingForPosition = false;
acquiredPosition(); acquiredPosition();
gpsTracker gt; gpsTracker gt;
gt.when = pos.timestamp().toTime_t(); gt.when = dateTimeToTimestamp(pos.timestamp());
gt.when += gettimezoneoffset(gt.when); gt.when += gettimezoneoffset(gt.when);
gt.location = create_location(pos.coordinate().latitude(), pos.coordinate().longitude()); gt.location = create_location(pos.coordinate().latitude(), pos.coordinate().longitude());
addFixToStorage(gt); addFixToStorage(gt);

View file

@ -271,7 +271,7 @@ static bool parseDate(const QString &s_in, timestamp_t &timestamp)
if (datetime.isValid()) { if (datetime.isValid()) {
// Not knowing any better, we suppose that time is give in UTC // Not knowing any better, we suppose that time is give in UTC
datetime.setTimeSpec(Qt::UTC); datetime.setTimeSpec(Qt::UTC);
timestamp = datetime.toMSecsSinceEpoch() / 1000; timestamp = dateTimeToTimestamp(datetime);
return true; return true;
} }
@ -312,7 +312,7 @@ static bool parseDate(const QString &s_in, timestamp_t &timestamp)
// Not knowing any better, we suppose that time is give in UTC // Not knowing any better, we suppose that time is give in UTC
datetime = QDateTime(date, time, Qt::UTC); datetime = QDateTime(date, time, Qt::UTC);
if (datetime.isValid()) { if (datetime.isValid()) {
timestamp = datetime.toMSecsSinceEpoch() / 1000; timestamp = dateTimeToTimestamp(datetime);
return true; return true;
} }
@ -542,9 +542,9 @@ extern "C" mediatype_t get_metadata(const char *filename_in, metadata *data)
// have a standard way of storing this datum), use the file creation date of the file. // have a standard way of storing this datum), use the file creation date of the file.
if (data->timestamp == 0) if (data->timestamp == 0)
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
data->timestamp = QFileInfo(filename).birthTime().toMSecsSinceEpoch() / 1000; data->timestamp = dateTimeToTimestamp(QFileInfo(filename).birthTime());
#else #else
data->timestamp = QFileInfo(filename).created().toMSecsSinceEpoch() / 1000; data->timestamp = dateTimeToTimestamp(QFileInfo(filename).created());
#endif #endif
return res; return res;
} }

View file

@ -709,6 +709,11 @@ QDateTime timestampToDateTime(timestamp_t when)
return QDateTime::fromMSecsSinceEpoch(1000 * when, Qt::UTC); return QDateTime::fromMSecsSinceEpoch(1000 * when, Qt::UTC);
} }
timestamp_t dateTimeToTimestamp(const QDateTime &t)
{
return t.toSecsSinceEpoch();
}
QString render_seconds_to_string(int seconds) QString render_seconds_to_string(int seconds)
{ {
if (seconds % 60 == 0) if (seconds % 60 == 0)

View file

@ -63,6 +63,7 @@ QString getPrintingTemplatePathUser();
QString getPrintingTemplatePathBundle(); QString getPrintingTemplatePathBundle();
int gettimezoneoffset(timestamp_t when = 0); int gettimezoneoffset(timestamp_t when = 0);
QDateTime timestampToDateTime(timestamp_t when); QDateTime timestampToDateTime(timestamp_t when);
timestamp_t dateTimeToTimestamp(const QDateTime &t);
int parseDurationToSeconds(const QString &text); int parseDurationToSeconds(const QString &text);
int parseLengthToMm(const QString &text); int parseLengthToMm(const QString &text);
int parseTemperatureToMkelvin(const QString &text); int parseTemperatureToMkelvin(const QString &text);

View file

@ -279,7 +279,7 @@ void ShiftImageTimesDialog::dcDateTimeChanged(const QDateTime &newDateTime)
if (!dcImageEpoch) if (!dcImageEpoch)
return; return;
newtime.setTimeSpec(Qt::UTC); newtime.setTimeSpec(Qt::UTC);
setOffset(newtime.toTime_t() - dcImageEpoch); setOffset(dateTimeToTimestamp(newtime) - dcImageEpoch);
} }
void ShiftImageTimesDialog::matchAllImagesToggled(bool state) void ShiftImageTimesDialog::matchAllImagesToggled(bool state)

View file

@ -598,7 +598,7 @@ void MainTab::on_depth_editingFinished()
// all dives are shifted by an offset. // all dives are shifted by an offset.
static void shiftTime(QDateTime &dateTime) static void shiftTime(QDateTime &dateTime)
{ {
timestamp_t when = dateTime.toTime_t(); timestamp_t when = dateTimeToTimestamp(dateTime);
if (current_dive && current_dive->when != when) { if (current_dive && current_dive->when != when) {
timestamp_t offset = when - current_dive->when; timestamp_t offset = when - current_dive->when;
Command::shiftTime(getDiveSelection(), (int)offset); Command::shiftTime(getDiveSelection(), (int)offset);

View file

@ -908,7 +908,7 @@ parsed:
// add a hundred years. // add a hundred years.
if (newDate.addYears(100) < QDateTime::currentDateTime().addYears(1)) if (newDate.addYears(100) < QDateTime::currentDateTime().addYears(1))
newDate = newDate.addYears(100); newDate = newDate.addYears(100);
d->dc.when = d->when = newDate.toMSecsSinceEpoch() / 1000; d->dc.when = d->when = dateTimeToTimestamp(newDate);
return true; return true;
} }
appendTextToLog("none of our parsing attempts worked for the date string"); appendTextToLog("none of our parsing attempts worked for the date string");

View file

@ -85,7 +85,7 @@ void DivePlannerPointsModel::setupStartTime()
if (dive_table.nr) { if (dive_table.nr) {
struct dive *d = get_dive(dive_table.nr - 1); struct dive *d = get_dive(dive_table.nr - 1);
time_t ends = dive_endtime(d); time_t ends = dive_endtime(d);
time_t diff = ends - startTime.toTime_t(); time_t diff = ends - dateTimeToTimestamp(startTime);
if (diff > 0) { if (diff > 0) {
startTime = startTime.addSecs(diff + 3600); startTime = startTime.addSecs(diff + 3600);
} }
@ -700,7 +700,7 @@ void DivePlannerPointsModel::setSurfaceSegment(int duration)
void DivePlannerPointsModel::setStartDate(const QDate &date) void DivePlannerPointsModel::setStartDate(const QDate &date)
{ {
startTime.setDate(date); startTime.setDate(date);
diveplan.when = startTime.toTime_t(); diveplan.when = dateTimeToTimestamp(startTime);
displayed_dive.when = diveplan.when; displayed_dive.when = diveplan.when;
emitDataChanged(); emitDataChanged();
} }
@ -708,7 +708,7 @@ void DivePlannerPointsModel::setStartDate(const QDate &date)
void DivePlannerPointsModel::setStartTime(const QTime &t) void DivePlannerPointsModel::setStartTime(const QTime &t)
{ {
startTime.setTime(t); startTime.setTime(t);
diveplan.when = startTime.toTime_t(); diveplan.when = dateTimeToTimestamp(startTime);
displayed_dive.when = diveplan.when; displayed_dive.when = diveplan.when;
emitDataChanged(); emitDataChanged();
} }

View file

@ -263,7 +263,7 @@ void DiveSummaryModel::calc(int column, int period)
if (startTime == currentTime) if (startTime == currentTime)
start = 0; start = 0;
else else
start = startTime.toMSecsSinceEpoch() / 1000L + gettimezoneoffset(); start = dateTimeToTimestamp(startTime) + gettimezoneoffset();
// Loop over all dives and sum up data // Loop over all dives and sum up data
Stats stats = loopDives(start); Stats stats = loopDives(start);