From 705d3818744059039d0e8d4ba9d6d5068481cda9 Mon Sep 17 00:00:00 2001 From: hama Date: Thu, 9 Apr 2015 00:26:33 +0200 Subject: [PATCH] QDateTimeParser works --- base/ReQStringUtil.cpp | 70 +++++++++++++++++---------------------- base/ReQStringUtil.hpp | 4 +-- base/ReTest.cpp | 51 ++++++++++++++++++++++++++++ base/ReTest.hpp | 6 ++++ cunit/cuReQStringUtil.cpp | 41 ++++++++++++++++++++--- 5 files changed, 126 insertions(+), 46 deletions(-) diff --git a/base/ReQStringUtil.cpp b/base/ReQStringUtil.cpp index 9d52556..2f85a64 100644 --- a/base/ReQStringUtil.cpp +++ b/base/ReQStringUtil.cpp @@ -103,7 +103,7 @@ int ReQStringUtil::lengthOfUInt(const ReString& text, int start, int radix, * otherwise: the length is incremented */ void ReQStringUtil::skipExpected(const ReString& text, QChar expected, - int& index, int length){ + int& index, int& length){ if (length == 0){ // error state, do nothing } else if (index >= text.length() || text[index] != expected){ @@ -144,42 +144,35 @@ int ReQStringUtil::lengthOfDate(const ReString& text, int start, QDate* value) length = 0; break; } - if (length > 0){ - start += length; - if (start >= text.length() || text[start] != '.') - length = 0; - else - start++, length++; - } int length2; - if (length > 0){ - length2 = lengthOfUInt(text, start, 10, &month); - if (length2 < 1 || length2 > 2) - length = 0; - else - start += length2, length += length; - } + start += length; skipExpected(text, '.', start, length); if (length > 0){ length2 = lengthOfUInt(text, start, 10, &month); if (length2 < 1 || length2 > 2) length = 0; - else - start += length2, length += length2; + else { + start += length2; + length += length2; + } } skipExpected(text, '.', start, length); if (year > 0){ length2 = lengthOfUInt(text, start, 10, &day); if (length2 < 1 || length2 > 2) length = 0; - else - start += length2, length += length2; + else { + start += length2; + length += length2; + } } else{ - length2 = lengthOfUInt(text, start, 10, &day); + length2 = lengthOfUInt(text, start, 10, &year); if (length2 != 4) length = 0; - else - start += length2, length += length2; + else { + start += length2; + length += length2; + } } if (day < 1 || day > 31 || month < 1 || month > 12 || year < 1970 || year > 2100) length = 0; @@ -385,10 +378,11 @@ public: * @param unitList description of the allowed units with its factor
* example: "kibyte:1024;kbyte:1000;mibyte:1048576;mbyte:1000000" */ -ReUnitParser::ReUnitParser(const QString& expr, const char* unitList) : +ReUnitParser::ReUnitParser(const QString& expr, const char* unitList, bool parseAtOnce) : m_result(0), m_expr(expr), m_message(), m_unitList(unitList){ normalize(); - parse(); + if (parseAtOnce) + parse(); } /** @@ -561,7 +555,7 @@ ReSizeParser::ReSizeParser(const QString& expr) : * @param expr an expression, e.g. "3*3days-5min+3weeks" */ ReDateTimeParser::ReDateTimeParser(const QString& expr) : - ReUnitParser("", "minutes:60;hours:3600;days:86400;weeks:604800"){ + ReUnitParser("", "minutes:60;hours:3600;days:86400;weeks:604800", false){ parseDateTime(expr); } @@ -588,40 +582,36 @@ QDateTime ReDateTimeParser::parseDateTime(const QString& expr){ m_expr = expr; normalize(); QDateTime rc = QDateTime::currentDateTime(); - bool olderThan = true; int64_t relativeSeconds = 0; if (m_expr.isEmpty()) m_message = QObject::tr("empty string is not a date/time"); else { QDateTime dateTime; int length2 = 0; - bool checkSum = false; + bool checkSum = true; if (m_expr.startsWith("now", Qt::CaseInsensitive)){ m_expr.remove(0, 3); - checkSum = true; - } else if ( (length2 = ReQStringUtil::lengthOfDateTime(m_expr, 0, true, true, &dateTime)) > 0){ + } else if ( (length2 = ReQStringUtil::lengthOfDateTime(m_expr, 0, true, + true, &dateTime)) > 0){ rc = dateTime; m_expr.remove(0, length2); } else { + checkSum = false; parse(); - relativeSeconds = m_result; + // meaning is "older than x seconds" + relativeSeconds = m_result = - m_result; } if (checkSum){ if (m_expr.startsWith("+")){ - olderThan = false; m_expr.remove(0, 1); } - parse(); - relativeSeconds = m_result; + if (! m_expr.isEmpty()){ + parse(); + relativeSeconds = m_result; + } } } - if (! isValid()) - rc.setMSecsSinceEpoch(0); - else { - if (! olderThan) - relativeSeconds = -relativeSeconds; - rc.addSecs(relativeSeconds); - } + rc.setMSecsSinceEpoch(isValid() ? rc.toMSecsSinceEpoch() + 1000 * relativeSeconds : 0); m_dateTime = rc; return rc; } diff --git a/base/ReQStringUtil.hpp b/base/ReQStringUtil.hpp index 1b7ccc2..b5573e7 100644 --- a/base/ReQStringUtil.hpp +++ b/base/ReQStringUtil.hpp @@ -27,7 +27,7 @@ public: int radix = 10, uint64_t* value = NULL); static int lengthOfUInt(const ReString& text, int start, int radix, uint* pValue); - static void skipExpected(const ReString& text, QChar expected, int& index, int length); + static void skipExpected(const ReString& text, QChar expected, int& index, int& length); /** * @brief Returns the value of a hexadecimal digit. * @@ -45,7 +45,7 @@ public: class ReUnitParser { public: - ReUnitParser(const QString& expr, const char* unitList); + ReUnitParser(const QString& expr, const char* unitList, bool parseAtOnce = true); public: bool isValid() const; const QString& errorMessage() const; diff --git a/base/ReTest.cpp b/base/ReTest.cpp index a8f9537..50ebc7b 100644 --- a/base/ReTest.cpp +++ b/base/ReTest.cpp @@ -269,6 +269,57 @@ bool ReTest::assertEquals(const char* expected, const QByteArray& current, return assertEquals(expected, current.constData(), file, lineNo); } +/** + * @brief Tests the equality of two values. + * + * Differences will be logged. + * + * @param expected the expected value + * @param current the current value + * @param file the file containing the test + * @param lineNo the line number containing the test + * @return true: equal + */ +bool ReTest::assertEquals(const QDate& expected, + const QDate& current, const char* file, int lineNo){ + return assertEquals(expected.toString("yyyy.MM.dd"), + current.toString("yyyy.MM.dd"), file, lineNo); +} + +/** + * @brief Tests the equality of two values. + * + * Differences will be logged. + * + * @param expected the expected value + * @param current the current value + * @param file the file containing the test + * @param lineNo the line number containing the test + * @return true: equal + */ +bool ReTest::assertEquals(const QDateTime& expected, + const QDateTime& current, const char* file, int lineNo){ + return assertEquals(expected.toString("yyyy.MM.dd hh:mm:ss"), + current.toString("yyyy.MM.dd hh:mm:ss"), file, lineNo); +} + +/** + * @brief Tests the equality of two values. + * + * Differences will be logged. + * + * @param expected the expected value + * @param current the current value + * @param file the file containing the test + * @param lineNo the line number containing the test + * @return true: equal + */ +bool ReTest::assertEquals(const QTime& expected, + const QTime& current, const char* file, int lineNo){ + return assertEquals(expected.toString("hh:mm:ss"), + current.toString("hh:mm:ss"), file, lineNo); +} + /** * @brief Tests whether a value is true. * diff --git a/base/ReTest.hpp b/base/ReTest.hpp index 0e85581..952dbc4 100644 --- a/base/ReTest.hpp +++ b/base/ReTest.hpp @@ -39,6 +39,12 @@ public: const char* file, int lineNo); bool assertEquals(const char* expected, const QByteArray& current, const char* file, int lineNo); + bool assertEquals(const QDate& expected, + const QDate& current, const char* file, int lineNo); + bool assertEquals(const QDateTime& expected, + const QDateTime& current, const char* file, int lineNo); + bool assertEquals(const QTime& expected, + const QTime& current, const char* file, int lineNo); bool assertEquals(const QList & expected, const QList & current, const char* file, int lineNo); bool assertTrue(bool condition, const char* file, int lineNo); diff --git a/cunit/cuReQStringUtil.cpp b/cunit/cuReQStringUtil.cpp index c04145d..bcf8132 100644 --- a/cunit/cuReQStringUtil.cpp +++ b/cunit/cuReQStringUtil.cpp @@ -129,15 +129,48 @@ public: checkEqu(1100586419200ll, parser2.asInt64()); } void testDateTimeParser(){ - ReDateTimeParser parser("1+1min+1h+1day+1week"); - checkT(parser.isValid()); - checkEqu(694861, parser.asInt()); + ReDateTimeParser parser("3.4.2014"); + checkEqu(QDateTime(QDate(2014, 4, 3)), parser.asDateTime()); + ReDateTimeParser parser2("21.4.2014-2w"); + checkEqu(QDateTime(QDate(2014, 4, 7)), parser2.asDateTime()); + ReDateTimeParser parserB2("1+1min+1h+1day+1week"); + checkT(parserB2.isValid()); + checkEqu(-694861, parserB2.asInt()); + } + void testLengtOfDate(){ + QDate date; + checkEqu(8, ReQStringUtil::lengthOfDate("1.2.2001", 0, &date)); + checkEqu(QDate(2001, 2, 1), date); + checkEqu(9, ReQStringUtil::lengthOfDate("5.12.2001xxx", 0, &date)); + checkEqu(QDate(2001, 12, 5), date); + checkEqu(10, ReQStringUtil::lengthOfDate("011.10.2001xxx", 1, &date)); + checkEqu(QDate(2001, 10, 11), date); + + checkEqu(8, ReQStringUtil::lengthOfDate("2001.2.1", 0, &date)); + checkEqu(QDate(2001, 2, 1), date); + checkEqu(9, ReQStringUtil::lengthOfDate("2001.12.5xxx", 0, &date)); + checkEqu(QDate(2001, 12, 5), date); + checkEqu(10, ReQStringUtil::lengthOfDate("02001.03.01xxx", 1, &date)); + checkEqu(QDate(2001, 3, 1), date); + } + void testLengtOfTime(){ + QTime time; + checkEqu(3, ReQStringUtil::lengthOfTime("1:2", 0, &time)); + checkEqu(QTime(1, 2, 0), time); + checkEqu(5, ReQStringUtil::lengthOfTime("301:02", 1, &time)); + checkEqu(QTime(1, 2, 0), time); + checkEqu(7, ReQStringUtil::lengthOfTime("301:02:9", 1, &time)); + checkEqu(QTime(1, 2, 9), time); + checkEqu(8, ReQStringUtil::lengthOfTime("301:02:09x", 1, &time)); + checkEqu(QTime(1, 2, 9), time); } virtual void run(void) { + testLengtOfTime(); + testLengtOfDate(); + testDateTimeParser(); testUnitParser(); testSizeParser(); - testDateTimeParser(); testUtf8(); testLengthOfUInt64(); testLengthOfUInt(); -- 2.39.5