Add helper to parse duration text

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2017-02-25 20:23:03 -08:00
parent 3afc4528b3
commit e9debdf281
2 changed files with 25 additions and 0 deletions

View file

@ -28,6 +28,7 @@ QString getPrintingTemplatePathBundle();
void copyPath(QString src, QString dst);
extern const QString get_dc_nickname(const char *model, uint32_t deviceid);
int gettimezoneoffset(timestamp_t when = 0);
int parseDurationToSeconds(const QString &text);
int parseLengthToMm(const QString &text);
int parseTemperatureToMkelvin(const QString &text);
int parseWeightToGrams(const QString &text);

View file

@ -755,6 +755,30 @@ int gettimezoneoffset(timestamp_t when)
return dt2.secsTo(dt1);
}
int parseDurationToSeconds(const QString &text)
{
int secs;
QString numOnly = text;
QString hours, minutes, seconds;
numOnly.replace(",", ".").remove(QRegExp("[^-0-9.:]"));
if (numOnly.isEmpty())
return 0;
if (numOnly.contains(':')) {
hours = numOnly.left(numOnly.indexOf(':'));
minutes = numOnly.right(numOnly.length() - hours.length() - 1);
if (minutes.contains(':')) {
numOnly = minutes;
minutes = numOnly.left(numOnly.indexOf(':'));
seconds = numOnly.right(numOnly.length() - minutes.length() - 1);
}
} else {
hours = "0";
minutes = numOnly;
}
secs = hours.toDouble() * 3600 + minutes.toDouble() * 60 + seconds.toDouble();
return secs;
}
int parseLengthToMm(const QString &text)
{
int mm;