cleanup: move minute formating to format-string.cpp

The get_minutes() function formats a time as m:ss
and returns a static C-string. Since all callers are
C++ anyway and transform directly into QString, let us
move this to the other string formatting function.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-09-03 10:37:58 +02:00 committed by Dirk Hohndel
parent 1047937197
commit 4a7ee872f3
7 changed files with 17 additions and 18 deletions

View file

@ -9,6 +9,7 @@
#include "qthelper.h"
#include "units.h"
#include "statistics.h"
#include "string-format.h"
#include "save-html.h"
static void file_copy_and_overwrite(const QString &fileName, const QString &newName)
@ -93,9 +94,9 @@ static void exportHTMLstatistics(const QString filename, struct htmlExportSettin
out << "\"DIVES\":\"" << stats.stats_yearly[i].selection_size << "\",";
out << "\"TOTAL_TIME\":\"" << get_dive_duration_string(stats.stats_yearly[i].total_time.seconds,
gettextFromC::tr("h"), gettextFromC::tr("min"), gettextFromC::tr("sec"), " ") << "\",";
out << "\"AVERAGE_TIME\":\"" << get_minutes(stats.stats_yearly[i].total_time.seconds / stats.stats_yearly[i].selection_size) << "\",";
out << "\"SHORTEST_TIME\":\"" << get_minutes(stats.stats_yearly[i].shortest_time.seconds) << "\",";
out << "\"LONGEST_TIME\":\"" << get_minutes(stats.stats_yearly[i].longest_time.seconds) << "\",";
out << "\"AVERAGE_TIME\":\"" << formatMinutes(stats.stats_yearly[i].total_time.seconds / stats.stats_yearly[i].selection_size) << "\",";
out << "\"SHORTEST_TIME\":\"" << formatMinutes(stats.stats_yearly[i].shortest_time.seconds) << "\",";
out << "\"LONGEST_TIME\":\"" << formatMinutes(stats.stats_yearly[i].longest_time.seconds) << "\",";
out << "\"AVG_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].avg_depth) << "\",";
out << "\"MIN_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].min_depth) << "\",";
out << "\"MAX_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].max_depth) << "\",";

View file

@ -2,7 +2,6 @@
/* statistics.c
*
* core logic for the Info & Stats page -
* char *get_minutes(int seconds);
* void calculate_stats_summary(struct stats_summary *out, bool selected_only);
* void calculate_stats_selected(stats_t *stats_selection);
*/
@ -84,13 +83,6 @@ static void process_dive(struct dive *dive, stats_t *stats)
}
}
char *get_minutes(int seconds)
{
static char buf[80];
snprintf(buf, sizeof(buf), "%d:%.2d", FRACTION(seconds, 60));
return buf;
}
/*
* Calculate a summary of the statistics and put in the stats_summary
* structure provided in the first parameter.

View file

@ -58,7 +58,6 @@ struct stats_summary {
extern "C" {
#endif
extern char *get_minutes(int seconds);
extern void init_stats_summary(struct stats_summary *stats);
extern void free_stats_summary(struct stats_summary *stats);
extern void calculate_stats_summary(struct stats_summary *stats, bool selected_only);

View file

@ -277,6 +277,11 @@ QString formatDayOfWeek(int day)
}
}
QString formatMinutes(int seconds)
{
return QString::asprintf("%d:%.2d", FRACTION(seconds, 60));
}
QString formatTripTitle(const dive_trip *trip)
{
if (!trip)

View file

@ -27,6 +27,7 @@ QString formatDiveDate(const dive *d);
QString formatDiveTime(const dive *d);
QString formatDiveDateTime(const dive *d);
QString formatDayOfWeek(int day);
QString formatMinutes(int seconds);
QString formatTripTitle(const dive_trip *trip);
QString formatTripTitleWithDives(const dive_trip *trip);

View file

@ -464,11 +464,11 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s
return get_dive_duration_string(object->total_time.seconds, gettextFromC::tr("h"),
gettextFromC::tr("min"), gettextFromC::tr("sec"), " ");
} else if (property == "avg_time") {
return get_minutes(object->total_time.seconds / object->selection_size);
return formatMinutes(object->total_time.seconds / object->selection_size);
} else if (property == "shortest_time") {
return get_minutes(object->shortest_time.seconds);
return formatMinutes(object->shortest_time.seconds);
} else if (property == "longest_time") {
return get_minutes(object->longest_time.seconds);
return formatMinutes(object->longest_time.seconds);
} else if (property == "avg_depth") {
return get_depth_string(object->avg_depth);
} else if (property == "min_depth") {

View file

@ -3,6 +3,7 @@
#include "core/qthelper.h"
#include "core/metrics.h"
#include "core/statistics.h"
#include "core/string-format.h"
#include "core/dive.h" // For NUM_DIVEMODE
class YearStatisticsItem : public TreeItem {
@ -65,13 +66,13 @@ QVariant YearStatisticsItem::data(int column, int role) const
ret = get_dive_duration_string(stats_interval.total_time.seconds, tr("h"), tr("min"), tr("sec"), " ");
break;
case AVERAGE_TIME:
ret = get_minutes(stats_interval.total_time.seconds / stats_interval.selection_size);
ret = formatMinutes(stats_interval.total_time.seconds / stats_interval.selection_size);
break;
case SHORTEST_TIME:
ret = get_minutes(stats_interval.shortest_time.seconds);
ret = formatMinutes(stats_interval.shortest_time.seconds);
break;
case LONGEST_TIME:
ret = get_minutes(stats_interval.longest_time.seconds);
ret = formatMinutes(stats_interval.longest_time.seconds);
break;
case AVG_DEPTH:
ret = get_depth_string(stats_interval.avg_depth);