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)
{
QTime t;
QString ts = time.trimmed();
// deal with sign
@@ -63,11 +62,11 @@ namespace BlackMisc
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");
(*this) = CTime(t);
const int hour = ts.mid(0, 2).toInt();
const int minute = ts.mid(3, 2).toInt();
int second = 0;
if (ts.length() == 8) second = ts.mid(6, 2).toInt();
(*this) = CTime(hour, minute, second);
// fix sign if required
if (factor < 0)
@@ -85,28 +84,22 @@ namespace BlackMisc
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
{
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;
parts << t.hour() << t.minute() << t.second();
parts << hr << mi << se;
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
void parseFromString(const QString &time, BlackMisc::PhysicalQuantities::CPqString::SeparatorMode mode);
//! To Qt time
//! \warning sign not considered
QTime toQTime() const;
//! Parts hh, mm, ss
//! \warning sign not considered
QList<int> getHrsMinSecParts() const;