mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
refs #212, fixed parsing of time strings
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user