mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-23 05:45:35 +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())
|
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());
|
CTime converted(seconds, CTimeUnit::s());
|
||||||
converted.setUnit(CTimeUnit::hms());
|
converted.switchUnit(CTimeUnit::hms());
|
||||||
this->set(converted);
|
*this = converted;
|
||||||
}
|
|
||||||
|
|
||||||
CTime::CTime(const QString &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
|
||||||
{
|
|
||||||
CTime parsed;
|
|
||||||
parsed.parseFromString(time);
|
|
||||||
this->set(parsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CTime::parseFromString(const QString &time)
|
void CTime::parseFromString(const QString &time)
|
||||||
{
|
{
|
||||||
Q_ASSERT(time.length() == 5 || time.length() == 8);
|
|
||||||
QTime t;
|
QTime t;
|
||||||
if (time.length() == 5)
|
const QString ts = time.trimmed();
|
||||||
t = QTime::fromString("hh:mm");
|
if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5))
|
||||||
else if (time.length() == 8)
|
{
|
||||||
t = QTime::fromString("hh:mm:ss");
|
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);
|
CTime parsed(t);
|
||||||
if (time.length() == 5)
|
if (ts.length() == 5)
|
||||||
parsed.setUnit(CTimeUnit::hrmin());
|
parsed.switchUnit(CTimeUnit::hrmin());
|
||||||
else if (time.length() == 8)
|
else if (ts.length() == 8)
|
||||||
parsed.setUnit(CTimeUnit::hms());
|
parsed.switchUnit(CTimeUnit::hms());
|
||||||
|
*this = parsed;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
parsed.setUnit(CTimeUnit::nullUnit()); // indicates invalid
|
CPhysicalQuantity::parseFromString(ts);
|
||||||
this->set(parsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QTime CTime::toQTime() const
|
QTime CTime::toQTime() const
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ namespace BlackMisc
|
|||||||
CTime(const QTime &time);
|
CTime(const QTime &time);
|
||||||
|
|
||||||
//! \copydoc CPhysicalQuantity(const QString &unitString)
|
//! \copydoc CPhysicalQuantity(const QString &unitString)
|
||||||
CTime(const QString &unitString) : CPhysicalQuantity(unitString) {}
|
CTime(const QString &unitString) : CPhysicalQuantity(0, CTimeUnit::nullUnit()) { this->parseFromString(unitString); }
|
||||||
|
|
||||||
//! \copydoc CValueObject::toQVariant
|
//! \copydoc CValueObject::toQVariant
|
||||||
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
||||||
|
|||||||
Reference in New Issue
Block a user