mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-21 04:45:31 +08:00
refs #289, negative time values supported, required for time offset
This commit is contained in:
@@ -15,8 +15,9 @@ namespace BlackMiscTest
|
|||||||
*/
|
*/
|
||||||
int CSamplesPhysicalQuantities::samples()
|
int CSamplesPhysicalQuantities::samples()
|
||||||
{
|
{
|
||||||
CSpeed parsedPq1 = CPqString::parseToVariant("100 km/h").value<CSpeed>();
|
// parsing
|
||||||
CLength parsedPq2 = CPqString::parseToVariant("-33ft").value<CLength>();
|
CSpeed parsedPq1 = CPqString::parseToVariant("100.123 km/h").value<CSpeed>();
|
||||||
|
CLength parsedPq2 = CPqString::parseToVariant("-33.123ft").value<CLength>();
|
||||||
QVariant parsedPq3 = CPqString::parseToVariant("666");
|
QVariant parsedPq3 = CPqString::parseToVariant("666");
|
||||||
qDebug() << "parsed" << parsedPq1 << parsedPq2 << parsedPq3;
|
qDebug() << "parsed" << parsedPq1 << parsedPq2 << parsedPq3;
|
||||||
|
|
||||||
@@ -24,9 +25,19 @@ namespace BlackMiscTest
|
|||||||
CFrequency frequencyParsed = CPqString::parse<CFrequency>("122.8MHz");
|
CFrequency frequencyParsed = CPqString::parse<CFrequency>("122.8MHz");
|
||||||
qDebug() << "parsed" << speedParsed << speedParsed.valueRoundedWithUnit(2, true) << frequencyParsed << frequencyParsed.valueRoundedWithUnit(2, true);
|
qDebug() << "parsed" << speedParsed << speedParsed.valueRoundedWithUnit(2, true) << frequencyParsed << frequencyParsed.valueRoundedWithUnit(2, true);
|
||||||
|
|
||||||
// cases which must not work
|
// the time clasas
|
||||||
// CLengthUnit du1(CAngleUnit::rad());
|
CTime time1;
|
||||||
|
time1.parseFromString("11:30"); // hhmm
|
||||||
|
qDebug() << time1 << time1.toQTime() << time1.formattedHrsMin();
|
||||||
|
|
||||||
|
CTime time2;
|
||||||
|
time2.parseFromString("-11:30"); // hhmm
|
||||||
|
qDebug() << time2 << time2.toQTime() << time2.formattedHrsMin();
|
||||||
|
|
||||||
|
time1 += time2;
|
||||||
|
qDebug() << time1 << time1.toQTime() << time1.formattedHrsMin();
|
||||||
|
|
||||||
|
// standard tests
|
||||||
CLengthUnit lu1(CLengthUnit::cm());
|
CLengthUnit lu1(CLengthUnit::cm());
|
||||||
CLengthUnit lu2(CLengthUnit::ft());
|
CLengthUnit lu2(CLengthUnit::ft());
|
||||||
QString lu1s = lu1.toQString(true);
|
QString lu1s = lu1.toQString(true);
|
||||||
|
|||||||
@@ -219,6 +219,18 @@ namespace BlackMisc
|
|||||||
return !this->isZeroEpsilonConsidered() && this->m_value < 0;
|
return !this->isZeroEpsilonConsidered() && this->m_value < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Make value always positive
|
||||||
|
void makePositive()
|
||||||
|
{
|
||||||
|
if (this->m_value < 0) { this->m_value *= -1.0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Make value always negative
|
||||||
|
void makeNegative()
|
||||||
|
{
|
||||||
|
if (this->m_value > 0) { this->m_value *= -1.0; }
|
||||||
|
}
|
||||||
|
|
||||||
//! \copydoc CValueObject::marshallToDbus
|
//! \copydoc CValueObject::marshallToDbus
|
||||||
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
||||||
|
|
||||||
|
|||||||
@@ -95,9 +95,14 @@ namespace BlackMisc
|
|||||||
*/
|
*/
|
||||||
QVariant CPqString::parseToVariant(const QString &value, SeparatorMode mode)
|
QVariant CPqString::parseToVariant(const QString &value, SeparatorMode mode)
|
||||||
{
|
{
|
||||||
static QRegExp rx("([0-9]+)\\s*(\\D*)$");
|
static QRegExp rx("([-+]?[0-9]*[\\.,]?[0-9]+)\\s*(\\D*)$");
|
||||||
QVariant v;
|
QVariant v;
|
||||||
if (value.isEmpty()) return v;
|
|
||||||
|
// fine tuning of the string
|
||||||
|
QString vs = value.trimmed().simplified();
|
||||||
|
|
||||||
|
// check
|
||||||
|
if (vs.isEmpty()) return v;
|
||||||
|
|
||||||
if (rx.indexIn(value) < 0) return v; // not a valid number
|
if (rx.indexIn(value) < 0) return v; // not a valid number
|
||||||
QString unit = rx.cap(2).trimmed();
|
QString unit = rx.cap(2).trimmed();
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
CTime::CTime(int hours, int minutes, int seconds) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
CTime::CTime(int hours, int minutes, int seconds) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
||||||
{
|
{
|
||||||
double value = hours + minutes / 100.0 + seconds / 10000.0;
|
bool negative = (hours < 0);
|
||||||
|
double value = qAbs(hours) + minutes / 100.0 + seconds / 10000.0;
|
||||||
if (minutes == 0 && seconds == 0)
|
if (minutes == 0 && seconds == 0)
|
||||||
{
|
{
|
||||||
(*this) = CTime(hours, CTimeUnit::h());
|
(*this) = CTime(hours, CTimeUnit::h());
|
||||||
@@ -18,18 +19,39 @@ namespace BlackMisc
|
|||||||
else
|
else
|
||||||
(*this) = CTime(value, CTimeUnit::hms());
|
(*this) = CTime(value, CTimeUnit::hms());
|
||||||
}
|
}
|
||||||
|
if (negative)
|
||||||
|
{
|
||||||
|
this->makeNegative();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CTime::CTime(const QTime &time) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
CTime::CTime(const QTime &time, bool negative) : CPhysicalQuantity(0, CTimeUnit::nullUnit())
|
||||||
{
|
{
|
||||||
CTime converted(time.hour(), time.minute(), time.second());
|
CTime converted(time.hour(), time.minute(), time.second());
|
||||||
(*this) = converted;
|
(*this) = converted;
|
||||||
|
if (negative)
|
||||||
|
{
|
||||||
|
this->makeNegative();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CTime::parseFromString(const QString &time)
|
void CTime::parseFromString(const QString &time)
|
||||||
{
|
{
|
||||||
QTime t;
|
QTime t;
|
||||||
const QString ts = time.trimmed();
|
QString ts = time.trimmed();
|
||||||
|
|
||||||
|
// deal with sign
|
||||||
|
double factor = 1.0;
|
||||||
|
if (ts.startsWith('+'))
|
||||||
|
{
|
||||||
|
ts.remove(0, 1);
|
||||||
|
}
|
||||||
|
else if (ts.startsWith('-'))
|
||||||
|
{
|
||||||
|
factor = -1.0;
|
||||||
|
ts.remove(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5))
|
if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5))
|
||||||
{
|
{
|
||||||
if (ts.length() == 5)
|
if (ts.length() == 5)
|
||||||
@@ -37,15 +59,27 @@ namespace BlackMisc
|
|||||||
else if (ts.length() == 8)
|
else if (ts.length() == 8)
|
||||||
t = QTime::fromString(ts, "hh:mm:ss");
|
t = QTime::fromString(ts, "hh:mm:ss");
|
||||||
(*this) = CTime(t);
|
(*this) = CTime(t);
|
||||||
|
|
||||||
|
// fix sign if required
|
||||||
|
if (factor < 0)
|
||||||
|
{
|
||||||
|
this->setValueSameUnit(this->value() * factor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
CPhysicalQuantity::parseFromString(ts);
|
CPhysicalQuantity::parseFromString(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
QTime CTime::toQTime() const
|
QTime CTime::toQTime() const
|
||||||
{
|
{
|
||||||
CTime copy(*this);
|
CTime copy(*this);
|
||||||
copy.setUnit(CTimeUnit::hms());
|
copy.setUnit(CTimeUnit::hms());
|
||||||
|
|
||||||
|
// QTime is not defined for negative numbers
|
||||||
|
// so we use the absolute value here
|
||||||
|
copy.makePositive();
|
||||||
|
|
||||||
|
// convert
|
||||||
QString s = copy.toQString(false).replace('h', ':').replace('m', ':').replace('s', "");
|
QString s = copy.toQString(false).replace('h', ':').replace('m', ':').replace('s', "");
|
||||||
QTime t = s.length() == 8 ?
|
QTime t = s.length() == 8 ?
|
||||||
QTime::fromString(s, "hh:mm:ss") :
|
QTime::fromString(s, "hh:mm:ss") :
|
||||||
@@ -60,5 +94,39 @@ namespace BlackMisc
|
|||||||
parts << t.hour() << t.minute() << t.second();
|
parts << t.hour() << t.minute() << t.second();
|
||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString CTime::formattedHrsMinSec() const
|
||||||
|
{
|
||||||
|
QList<int> parts = getHrsMinSecParts();
|
||||||
|
QString h = QString("00%1").arg(QString::number(parts.at(0))).right(2);
|
||||||
|
QString m = QString("00%1").arg(QString::number(parts.at(1))).right(2);
|
||||||
|
QString s = QString("00%1").arg(QString::number(parts.at(2))).right(2);
|
||||||
|
|
||||||
|
QString fs = QString("%1:%2:%3").arg(h).arg(m).arg(s);
|
||||||
|
if (this->isNegativeWithEpsilonConsidered())
|
||||||
|
{
|
||||||
|
return QString("-").append(fs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString CTime::formattedHrsMin() const
|
||||||
|
{
|
||||||
|
QList<int> parts = getHrsMinSecParts();
|
||||||
|
QString h = QString("00%1").arg(QString::number(parts.at(0))).right(2);
|
||||||
|
QString m = QString("00%1").arg(QString::number(parts.at(1))).right(2);
|
||||||
|
QString fs = QString("%1:%2").arg(h).arg(m);
|
||||||
|
if (this->isNegativeWithEpsilonConsidered())
|
||||||
|
{
|
||||||
|
return QString("-").append(fs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ namespace BlackMisc
|
|||||||
CTime(int hours, int minutes, int seconds = 0);
|
CTime(int hours, int minutes, int seconds = 0);
|
||||||
|
|
||||||
//! By Qt time
|
//! By Qt time
|
||||||
CTime(const QTime &time);
|
CTime(const QTime &time, bool negative = false);
|
||||||
|
|
||||||
//! \copydoc CPhysicalQuantity(const QString &unitString)
|
//! \copydoc CPhysicalQuantity(const QString &unitString)
|
||||||
CTime(const QString &unitString) : CPhysicalQuantity(0, CTimeUnit::nullUnit()) { this->parseFromString(unitString); }
|
CTime(const QString &unitString) : CPhysicalQuantity(0, CTimeUnit::nullUnit()) { this->parseFromString(unitString); }
|
||||||
@@ -53,11 +53,19 @@ namespace BlackMisc
|
|||||||
virtual void parseFromString(const QString &time) override;
|
virtual void parseFromString(const QString &time) override;
|
||||||
|
|
||||||
//! To Qt time
|
//! To Qt time
|
||||||
|
//! \warning sign not considered
|
||||||
QTime toQTime() const;
|
QTime toQTime() const;
|
||||||
|
|
||||||
//! Parts hh, mm, ss
|
//! Parts hh, mm, ss
|
||||||
|
//! \warning sign not considered
|
||||||
QList<int> getHrsMinSecParts() const;
|
QList<int> getHrsMinSecParts() const;
|
||||||
|
|
||||||
|
//! Formatted as hh:mm:ss
|
||||||
|
QString formattedHrsMinSec() const;
|
||||||
|
|
||||||
|
//! Formatted as hh:mm
|
||||||
|
QString formattedHrsMin() const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user