* 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){
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;
* @param unitList description of the allowed units with its factor<br>
* 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();
}
/**
* @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);
}
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;
}
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.
*
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;
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.
*
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 <QByteArray>& expected,
const QList <QByteArray>& current, const char* file, int lineNo);
bool assertTrue(bool condition, const char* file, int lineNo);
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();