Skip to content

Commit

Permalink
cleanup: move minute formating to format-string.cpp
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bstoeger authored and dirkhh committed Sep 3, 2022
1 parent 1047937 commit 4a7ee87
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
7 changes: 4 additions & 3 deletions core/divelogexportlogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) << "\",";
Expand Down
8 changes: 0 additions & 8 deletions core/statistics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion core/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions core/string-format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions core/string-format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions desktop-widgets/templatelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
7 changes: 4 additions & 3 deletions qt-models/yearlystatisticsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4a7ee87

Please sign in to comment.