refs #212, fixed parsing of time strings

This commit is contained in:
Klaus Basan
2014-04-30 02:34:30 +02:00
parent 112bd55362
commit 036de1b0e6
2 changed files with 19 additions and 23 deletions

View File

@@ -6,35 +6,31 @@ namespace BlackMisc
{
CTime::CTime(const QTime &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
{
int seconds = QTime().secsTo(time);
int seconds = QTime(0, 0, 0).secsTo(time);
CTime converted(seconds, CTimeUnit::s());
converted.setUnit(CTimeUnit::hms());
this->set(converted);
}
CTime::CTime(const QString &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
{
CTime parsed;
parsed.parseFromString(time);
this->set(parsed);
converted.switchUnit(CTimeUnit::hms());
*this = converted;
}
void CTime::parseFromString(const QString &time)
{
Q_ASSERT(time.length() == 5 || time.length() == 8);
QTime t;
if (time.length() == 5)
t = QTime::fromString("hh:mm");
else if (time.length() == 8)
t = QTime::fromString("hh:mm:ss");
CTime parsed(t);
if (time.length() == 5)
parsed.setUnit(CTimeUnit::hrmin());
else if (time.length() == 8)
parsed.setUnit(CTimeUnit::hms());
const QString ts = time.trimmed();
if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5))
{
if (ts.length() == 5)
t = QTime::fromString(ts, "hh:mm");
else if (ts.length() == 8)
t = QTime::fromString(ts, "hh:mm:ss");
CTime parsed(t);
if (ts.length() == 5)
parsed.switchUnit(CTimeUnit::hrmin());
else if (ts.length() == 8)
parsed.switchUnit(CTimeUnit::hms());
*this = parsed;
}
else
parsed.setUnit(CTimeUnit::nullUnit()); // indicates invalid
this->set(parsed);
CPhysicalQuantity::parseFromString(ts);
}
QTime CTime::toQTime() const