refs #449 Remove internal usage of QTime in CTime

QTime does not support values greater than 24 hours. Therefore
parsing from such values or conversions failed. This commit
reimplements all necessary methods without using QTime.
This commit is contained in:
Roland Winklmeier
2015-07-08 00:34:09 +02:00
committed by Mathew Sutcliffe
parent 2d9e9863c2
commit 86b238a092
3 changed files with 28 additions and 29 deletions

View File

@@ -46,7 +46,6 @@ namespace BlackMisc
void CTime::parseFromString(const QString &time) void CTime::parseFromString(const QString &time)
{ {
QTime t;
QString ts = time.trimmed(); QString ts = time.trimmed();
// deal with sign // deal with sign
@@ -63,11 +62,11 @@ namespace BlackMisc
if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5)) if (ts.contains(":") && (ts.length() == 8 || ts.length() == 5))
{ {
if (ts.length() == 5) const int hour = ts.mid(0, 2).toInt();
t = QTime::fromString(ts, "hh:mm"); const int minute = ts.mid(3, 2).toInt();
else if (ts.length() == 8) int second = 0;
t = QTime::fromString(ts, "hh:mm:ss"); if (ts.length() == 8) second = ts.mid(6, 2).toInt();
(*this) = CTime(t); (*this) = CTime(hour, minute, second);
// fix sign if required // fix sign if required
if (factor < 0) if (factor < 0)
@@ -85,28 +84,22 @@ namespace BlackMisc
Q_UNUSED(mode); Q_UNUSED(mode);
} }
QTime CTime::toQTime() const
{
CTime copy(*this);
copy.switchUnit(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', "");
QTime t = s.length() == 8 ?
QTime::fromString(s, "hh:mm:ss") :
QTime::fromString(s, "hh:mm");
return t;
}
QList<int> CTime::getHrsMinSecParts() const QList<int> CTime::getHrsMinSecParts() const
{ {
QTime t = this->toQTime(); using BlackMisc::Math::CMathUtils;
CTime copy(*this);
copy.switchUnit(CTimeUnit::s());
using BlackMisc::Math::CMathUtils;
double currentValue = copy.value();
double hr = CMathUtils::trunc(currentValue / 3600);
double remaining = std::fmod(currentValue, 3600);
double mi = CMathUtils::trunc(remaining / 60);
double se = std::fmod(remaining, 60);
QList<int> parts; QList<int> parts;
parts << t.hour() << t.minute() << t.second(); parts << hr << mi << se;
return parts; return parts;
} }

View File

@@ -55,10 +55,6 @@ namespace BlackMisc
//! From string hh:mm, or hh:mm:ss, or time units such as s, min //! From string hh:mm, or hh:mm:ss, or time units such as s, min
void parseFromString(const QString &time, BlackMisc::PhysicalQuantities::CPqString::SeparatorMode mode); void parseFromString(const QString &time, BlackMisc::PhysicalQuantities::CPqString::SeparatorMode mode);
//! To Qt time
//! \warning sign not considered
QTime toQTime() const;
//! Parts hh, mm, ss //! Parts hh, mm, ss
//! \warning sign not considered //! \warning sign not considered
QList<int> getHrsMinSecParts() const; QList<int> getHrsMinSecParts() const;

View File

@@ -160,10 +160,20 @@ namespace BlackMiscTest
CTime t2(1.5, CTimeUnit::h()); CTime t2(1.5, CTimeUnit::h());
CTime t3(1.25, CTimeUnit::min()); CTime t3(1.25, CTimeUnit::min());
CTime t4(1.0101, CTimeUnit::hms()); CTime t4(1.0101, CTimeUnit::hms());
CTime t5(26, 35, 40);
CTime t6(3661, CTimeUnit::s());
CTime t7;
QVERIFY2(CMathUtils::epsilonEqual(t1.value(CTimeUnit::defaultUnit()), 3600), "1hour shall be 3600s"); QVERIFY2(CMathUtils::epsilonEqual(t1.value(CTimeUnit::defaultUnit()), 3600), "1hour shall be 3600s");
QVERIFY2(CMathUtils::epsilonEqual(t2.value(CTimeUnit::hrmin()), 1.3), "1.5hour shall be 1h30m"); QVERIFY2(CMathUtils::epsilonEqual(t2.value(CTimeUnit::hrmin()), 1.3), "1.5hour shall be 1h30m");
QVERIFY2(CMathUtils::epsilonEqual(t3.value(CTimeUnit::minsec()), 1.15), "1.25min shall be 1m15s"); QVERIFY2(CMathUtils::epsilonEqual(t3.value(CTimeUnit::minsec()), 1.15), "1.25min shall be 1m15s");
QVERIFY2(CMathUtils::epsilonEqual(t4.value(CTimeUnit::s()), 3661), "1h01m01s shall be 3661s"); QVERIFY2(CMathUtils::epsilonEqual(t4.value(CTimeUnit::s()), 3661), "1h01m01s shall be 3661s");
QVERIFY2(CMathUtils::epsilonEqual(t5.value(CTimeUnit::s()), 95740), "Time greater than 24h failed");
QVERIFY2(t6.formattedHrsMinSec() == "01:01:01", "Formatted output hh:mm:ss failed");
QVERIFY2(t6.formattedHrsMin() == "01:01", "Formatted output hh:mm failed");
t6.switchUnit(CTimeUnit::hms());
QVERIFY2(CMathUtils::epsilonEqual(t6.value(), 1.0101), "Switching the unit produced a wrong a value");
t7.parseFromString("27:30:55");
QVERIFY2(t7.formattedHrsMinSec() == "27:30:55", "Parsed time greater than 24h failed");
} }
/* /*