Use helper function dive_endtime() where apropriate

Calculating dive.when + dive.duration doesn't always give the correct
endtime of a dive especially when a dive has surface interval(s) in
the middle.
Using the helper function dive_endtime() fixes this issue.

Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
This commit is contained in:
Stefan Fuchs 2017-09-30 18:00:34 +02:00 committed by Lubomir I. Ivanov
parent 325c4459ad
commit 7713c7e607
6 changed files with 20 additions and 18 deletions

View file

@ -229,7 +229,7 @@ static int calculate_cns(struct dive *dive)
if (divenr) { if (divenr) {
prev_dive = get_dive(divenr - 1); prev_dive = get_dive(divenr - 1);
if (prev_dive) { if (prev_dive) {
endtime = prev_dive->when + prev_dive->duration.seconds; endtime = dive_endtime(prev_dive);
if (dive->when < (endtime + 3600 * 12)) { if (dive->when < (endtime + 3600 * 12)) {
cns = calculate_cns(prev_dive); cns = calculate_cns(prev_dive);
cns = cns * 1 / pow(2, (dive->when - endtime) / (90.0 * 60.0)); cns = cns * 1 / pow(2, (dive->when - endtime) / (90.0 * 60.0));
@ -395,13 +395,12 @@ unsigned int init_decompression(struct dive *dive)
* for how far back we need to go */ * for how far back we need to go */
if (dive->divetrip && pdive->divetrip != dive->divetrip) if (dive->divetrip && pdive->divetrip != dive->divetrip)
continue; continue;
if (!pdive || pdive->when >= when || pdive->when + pdive->duration.seconds + 48 * 60 * 60 < when) if (!pdive || pdive->when >= when || dive_endtime(pdive) + 48 * 60 * 60 < when)
break; break;
/* For simultaneous dives, only consider the first */ /* For simultaneous dives, only consider the first */
if (pdive->when == laststart) if (pdive->when == laststart)
continue; continue;
when = pdive->when; lasttime = dive_endtime(pdive);
lasttime = when + pdive->duration.seconds;
} }
while (++i < (divenr >= 0 ? divenr : dive_table.nr)) { while (++i < (divenr >= 0 ? divenr : dive_table.nr)) {
struct dive *pdive = get_dive(i); struct dive *pdive = get_dive(i);
@ -421,7 +420,7 @@ unsigned int init_decompression(struct dive *dive)
} }
if (pdive->when > lasttime) { if (pdive->when > lasttime) {
surface_time = pdive->when - lasttime; surface_time = pdive->when - lasttime;
lasttime = pdive->when + pdive->duration.seconds; lasttime = dive_endtime(pdive);
add_segment(surface_pressure, &air, surface_time, 0, dive, prefs.decosac); add_segment(surface_pressure, &air, surface_time, 0, dive, prefs.decosac);
#if DECO_CALC_DEBUG & 2 #if DECO_CALC_DEBUG & 2
printf("after surface intervall of %d:%02u\n", FRACTION(surface_time, 60)); printf("after surface intervall of %d:%02u\n", FRACTION(surface_time, 60));
@ -1136,7 +1135,7 @@ void process_dives(bool is_imported, bool prefer_imported)
/* only try to merge overlapping dives - or if one of the dives has /* only try to merge overlapping dives - or if one of the dives has
* zero duration (that might be a gps marker from the webservice) */ * zero duration (that might be a gps marker from the webservice) */
if (prev->duration.seconds && dive->duration.seconds && if (prev->duration.seconds && dive->duration.seconds &&
prev->when + prev->duration.seconds < dive->when) dive_endtime(prev) < dive->when)
continue; continue;
merged = try_to_merge(prev, dive, prefer_imported); merged = try_to_merge(prev, dive, prefer_imported);

View file

@ -275,7 +275,7 @@ bool GpsLocation::applyLocations()
qDebug() << "processing gpsFix @" << get_dive_date_string(gpsTable[j].when) << qDebug() << "processing gpsFix @" << get_dive_date_string(gpsTable[j].when) <<
"which is withing six hours of dive from" << "which is withing six hours of dive from" <<
get_dive_date_string(d->when) << "until" << get_dive_date_string(d->when) << "until" <<
get_dive_date_string(d->when + d->duration.seconds); get_dive_date_string(dive_endtime(d));
/* /*
* If position is fixed during dive. This is the good one. * If position is fixed during dive. This is the good one.
* Asign and mark position, and end gps_location loop * Asign and mark position, and end gps_location loop
@ -310,7 +310,7 @@ bool GpsLocation::applyLocations()
if (verbose) if (verbose)
qDebug() << "which is closer to the start of the dive, do continue with that"; qDebug() << "which is closer to the start of the dive, do continue with that";
continue; continue;
} else if (gpsTable[j].when > d->when + d->duration.seconds) { } else if (gpsTable[j].when > dive_endtime(d)) {
if (verbose) if (verbose)
qDebug() << "which is even later after the end of the dive, so pick the previous one"; qDebug() << "which is even later after the end of the dive, so pick the previous one";
copy_gps_location(gpsTable[j], d); copy_gps_location(gpsTable[j], d);
@ -319,7 +319,7 @@ bool GpsLocation::applyLocations()
break; break;
} else { } else {
/* ok, gpsFix is before, nextgpsFix is after */ /* ok, gpsFix is before, nextgpsFix is after */
if (d->when - gpsTable[j].when <= gpsTable[j+1].when - (d->when + d->duration.seconds)) { if (d->when - gpsTable[j].when <= gpsTable[j+1].when - dive_endtime(d)) {
if (verbose) if (verbose)
qDebug() << "pick the one before as it's closer to the start"; qDebug() << "pick the one before as it's closer to the start";
copy_gps_location(gpsTable[j], d); copy_gps_location(gpsTable[j], d);
@ -351,7 +351,7 @@ bool GpsLocation::applyLocations()
/* If position is out of SAME_GROUP range and in the future, mark position for /* If position is out of SAME_GROUP range and in the future, mark position for
* next dive iteration and end the gps_location loop * next dive iteration and end the gps_location loop
*/ */
if (gpsTable[j].when >= d->when + d->duration.seconds + SAME_GROUP) { if (gpsTable[j].when >= dive_endtime(d) + SAME_GROUP) {
last = j; last = j;
break; break;
} }

View file

@ -12,6 +12,7 @@
#include "core/subsurface-qt/SettingsObjectWrapper.h" #include "core/subsurface-qt/SettingsObjectWrapper.h"
#include <errno.h> #include <errno.h>
#include "core/cloudstorage.h" #include "core/cloudstorage.h"
#include "core/dive.h"
#include <QDir> #include <QDir>
#include <QHttpMultiPart> #include <QHttpMultiPart>
@ -70,7 +71,7 @@ static bool merge_locations_into_dives(void)
qDebug() << "processing gpsfix @" << get_dive_date_string(gpsfix->when) << qDebug() << "processing gpsfix @" << get_dive_date_string(gpsfix->when) <<
"which is withing six hours of dive from" << "which is withing six hours of dive from" <<
get_dive_date_string(dive->when) << "until" << get_dive_date_string(dive->when) << "until" <<
get_dive_date_string(dive->when + dive->duration.seconds); get_dive_date_string(dive_endtime(dive));
/* /*
* If position is fixed during dive. This is the good one. * If position is fixed during dive. This is the good one.
* Asign and mark position, and end gps_location loop * Asign and mark position, and end gps_location loop
@ -106,7 +107,7 @@ static bool merge_locations_into_dives(void)
if (verbose) if (verbose)
qDebug() << "which is closer to the start of the dive, do continue with that"; qDebug() << "which is closer to the start of the dive, do continue with that";
continue; continue;
} else if (gpsfix->when > dive->when + dive->duration.seconds) { } else if (gpsfix->when > dive_endtime(dive)) {
if (verbose) if (verbose)
qDebug() << "which is even later after the end of the dive, so pick the previous one"; qDebug() << "which is even later after the end of the dive, so pick the previous one";
copy_gps_location(gpsfix, dive); copy_gps_location(gpsfix, dive);
@ -115,7 +116,7 @@ static bool merge_locations_into_dives(void)
break; break;
} else { } else {
/* ok, gpsfix is before, nextgpsfix is after */ /* ok, gpsfix is before, nextgpsfix is after */
if (dive->when - gpsfix->when <= nextgpsfix->when - (dive->when + dive->duration.seconds)) { if (dive->when - gpsfix->when <= nextgpsfix->when - dive_endtime(dive)) {
if (verbose) if (verbose)
qDebug() << "pick the one before as it's closer to the start"; qDebug() << "pick the one before as it's closer to the start";
copy_gps_location(gpsfix, dive); copy_gps_location(gpsfix, dive);
@ -147,7 +148,7 @@ static bool merge_locations_into_dives(void)
/* If position is out of SAME_GROUP range and in the future, mark position for /* If position is out of SAME_GROUP range and in the future, mark position for
* next dive iteration and end the gps_location loop * next dive iteration and end the gps_location loop
*/ */
if (gpsfix->when >= dive->when + dive->duration.seconds + SAME_GROUP) { if (gpsfix->when >= dive_endtime(dive) + SAME_GROUP) {
tracer = j; tracer = j;
break; break;
} }

View file

@ -6,6 +6,7 @@
#include <core/helpers.h> #include <core/helpers.h>
#include <core/statistics.h> #include <core/statistics.h>
#include <core/display.h> #include <core/display.h>
#include <core/dive.h>
TabDiveInformation::TabDiveInformation(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveInformation()) TabDiveInformation::TabDiveInformation(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveInformation())
{ {
@ -81,7 +82,7 @@ void TabDiveInformation::updateData()
process_all_dives(&displayed_dive, &prevd); process_all_dives(&displayed_dive, &prevd);
if (prevd) if (prevd)
ui->surfaceIntervalText->setText(get_dive_surfint_string(displayed_dive.when - (prevd->when + prevd->duration.seconds), tr("d"), tr("h"), tr("min"))); ui->surfaceIntervalText->setText(get_dive_surfint_string(displayed_dive.when - (dive_endtime(prevd)), tr("d"), tr("h"), tr("min")));
else else
ui->surfaceIntervalText->clear(); ui->surfaceIntervalText->clear();

View file

@ -58,7 +58,7 @@ void DivePlannerPointsModel::setupStartTime()
startTime = QDateTime::currentDateTimeUtc().addSecs(3600 + gettimezoneoffset()); startTime = QDateTime::currentDateTimeUtc().addSecs(3600 + gettimezoneoffset());
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 = d->when + d->duration.seconds; time_t ends = dive_endtime(d);
time_t diff = ends - startTime.toTime_t(); time_t diff = ends - startTime.toTime_t();
if (diff > 0) { if (diff > 0) {
startTime = startTime.addSecs(diff + 3600); startTime = startTime.addSecs(diff + 3600);

View file

@ -4,6 +4,7 @@
#include "core/metrics.h" #include "core/metrics.h"
#include "core/divelist.h" #include "core/divelist.h"
#include "core/helpers.h" #include "core/helpers.h"
#include "core/dive.h"
#include <QIcon> #include <QIcon>
static int nitrox_sort_value(struct dive *dive) static int nitrox_sort_value(struct dive *dive)
@ -325,12 +326,12 @@ QString DiveItem::displayDepthWithUnit() const
int DiveItem::countPhotos(dive *dive) const int DiveItem::countPhotos(dive *dive) const
{ // Determine whether dive has pictures, and whether they were taken during or before/after dive. { // Determine whether dive has pictures, and whether they were taken during or before/after dive.
const int bufperiod = 120; // A 2-min buffer period. Photos within 2 min of dive are assumed as const int bufperiod = 120; // A 2-min buffer period. Photos within 2 min of dive are assumed as
int diveDuration = dive->duration.seconds; // taken during the dive, not before/after. int diveTotaltime = dive_endtime(dive) - dive->when; // taken during the dive, not before/after.
int pic_offset, icon_index = 0; int pic_offset, icon_index = 0;
FOR_EACH_PICTURE (dive) { // Step through each of the pictures for this dive: FOR_EACH_PICTURE (dive) { // Step through each of the pictures for this dive:
if (!picture) break; // if there are no pictures for this dive, return 0 if (!picture) break; // if there are no pictures for this dive, return 0
pic_offset = picture->offset.seconds; pic_offset = picture->offset.seconds;
if ((pic_offset < -bufperiod) | (pic_offset > diveDuration+bufperiod)) { if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) {
icon_index |= 0x02; // If picture is before/after the dive icon_index |= 0x02; // If picture is before/after the dive
} // then set the appropriate bit ... } // then set the appropriate bit ...
else { else {