7 #include "EngaugeAssert.h"
8 #include "FormatDateTime.h"
14 loadFormatsParseAcceptable();
15 loadFormatsParseIncomplete();
18 bool FormatDateTime::ambiguityBetweenDateAndTime (CoordUnitsDate coordUnitsDate,
19 CoordUnitsTime coordUnitsTime,
20 const QString &
string)
const
22 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::ambiguityBetweenDateAndTime";
24 bool ambiguous =
false;
27 if (coordUnitsDate != COORD_UNITS_DATE_SKIP &&
28 coordUnitsTime != COORD_UNITS_TIME_SKIP) {
31 QStringList fields =
string.trimmed().split(QRegExp (
"[/- :]"));
33 if (fields.count() == 1) {
44 void FormatDateTime::dateTimeLookup (
const FormatsDate &formatsDateAll,
45 const FormatsTime &formatsTimeAll,
46 CoordUnitsDate coordUnitsDate,
47 CoordUnitsTime coordUnitsTime,
48 const QString &
string,
49 bool useQDateTimeElseQRegExp,
53 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup";
57 ENGAUGE_ASSERT (formatsDateAll.contains (coordUnitsDate));
58 ENGAUGE_ASSERT (formatsTimeAll.contains (coordUnitsTime));
60 QStringList formatsDate = formatsDateAll [coordUnitsDate];
61 QStringList formatsTime = formatsTimeAll [coordUnitsTime];
64 QStringList::const_iterator itrDate, itrTime;
65 bool iterating =
true;
66 for (itrDate = formatsDate.begin(); itrDate != formatsDate.end() && iterating; itrDate++) {
68 QString formatDate = *itrDate;
70 for (itrTime = formatsTime.begin(); itrTime != formatsTime.end() && iterating; itrTime++) {
72 QString formatTime = *itrTime;
75 QString separator = (!formatDate.isEmpty() && !formatTime.isEmpty() ?
" " :
"");
77 QString formatDateTime = formatDate + separator + formatTime;
79 if (!formatDateTime.isEmpty()) {
82 if (useQDateTimeElseQRegExp) {
84 QDateTime dt = QDateTime::fromString (
string,
87 if (dt.isValid() && !ambiguityBetweenDateAndTime (coordUnitsDate,
92 value = dt.toTime_t();
95 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
96 <<
" string=" <<
string.toLatin1().data()
97 <<
" qDateTimeFormatMatched=" << formatDateTime.toLatin1().data()
99 <<
" stringQDateTime=" << dt.toString().toLatin1().data();
104 QRegExp reg (formatDateTime);
105 if (reg.exactMatch(
string)) {
110 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
111 <<
" string=" <<
string.toLatin1().data()
112 <<
" regExpMatched=" << formatDateTime.toLatin1().data();
122 CoordUnitsTime coordUnitsTime,
125 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::formatOutput"
126 <<
" value=" << value;
128 ENGAUGE_ASSERT (m_formatsDateFormat.contains (coordUnitsDate));
129 ENGAUGE_ASSERT (m_formatsTimeFormat.contains (coordUnitsTime));
131 QString format = m_formatsDateFormat [coordUnitsDate] +
" " + m_formatsTimeFormat [coordUnitsTime];
132 format = format.trimmed();
134 QDateTime dt = QDateTime::fromTime_t (value);
136 return dt.toString (format);
139 void FormatDateTime::loadFormatsFormat()
141 m_formatsDateFormat [COORD_UNITS_DATE_SKIP] =
"";
142 m_formatsDateFormat [COORD_UNITS_DATE_MONTH_DAY_YEAR] =
"MM/dd/yyyy";
143 m_formatsDateFormat [COORD_UNITS_DATE_DAY_MONTH_YEAR] =
"dd/MM/yyyy";
144 m_formatsDateFormat [COORD_UNITS_DATE_YEAR_MONTH_DAY] =
"yyyy/MM/dd";
146 ENGAUGE_ASSERT (m_formatsDateFormat.count () == NUM_COORD_UNITS_DATE);
148 m_formatsTimeFormat [COORD_UNITS_TIME_SKIP] =
"";
149 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE] =
"hh/mm";
150 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] =
"hh:mm:ss";
152 ENGAUGE_ASSERT (m_formatsTimeFormat.count () == NUM_COORD_UNITS_TIME);
155 void FormatDateTime::loadFormatsParseAcceptable()
157 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseAcceptable";
159 QStringList skip, dayMonth, dayMonthYear, monthDay, monthDayYear, yearMonth, yearMonthDay;
183 dayMonthYear <<
"d/M/yyyy"
227 monthDayYear <<
"M/d/yyyy"
251 yearMonth <<
"yyyy/M"
263 yearMonthDay <<
"yyyy/M/d"
283 m_formatsDateParseAcceptable [COORD_UNITS_DATE_SKIP] = skip + monthDay + monthDayYear + yearMonthDay;
284 m_formatsDateParseAcceptable [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + monthDay + monthDayYear + yearMonthDay;
285 m_formatsDateParseAcceptable [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + dayMonth + dayMonthYear + yearMonthDay;
286 m_formatsDateParseAcceptable [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + yearMonth + yearMonthDay;
288 ENGAUGE_ASSERT (m_formatsDateParseAcceptable.count () == NUM_COORD_UNITS_DATE);
290 QStringList hour, hourMinute, hourMinuteSecond, hourMinutePm, hourMinuteSecondPm;
293 hourMinute <<
"hh:mm";
294 hourMinuteSecond <<
"hh:mm:ss";
295 hourMinutePm <<
"hh:mmA"
299 hourMinuteSecondPm <<
"hh:mm:ssA"
304 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_SKIP] = skip + hour + hourMinute + hourMinuteSecond + hourMinutePm + hourMinuteSecondPm;
305 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
306 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
308 ENGAUGE_ASSERT (m_formatsTimeParseAcceptable.count () == NUM_COORD_UNITS_TIME);
311 void FormatDateTime::loadFormatsParseIncomplete()
313 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseIncomplete";
315 QStringList skip, day, dayMonth, month, monthDay, monthDayYear, year, yearMonth, yearMonthDay;
326 dayMonth <<
"\\d{1,2}/\\d{1,2}"
327 <<
"\\d{1,2}/\\d{1,2} "
328 <<
"\\d{1,2}/\\d{1,2}/"
329 <<
"\\d{1,2}-\\d{1,2}-"
330 <<
"\\d{1,2}/[a-zA-Z]{1,12}/"
331 <<
"\\d{1,2}-[a-zA-Z]{1,12}-"
332 <<
"\\d{1,2} [a-zA-Z]{1,12} ";
336 <<
"[a-zA-Z]{1,12} ";
337 monthDay <<
"\\d{1,2}/\\d{1,2}"
338 <<
"\\d{1,2}/\\d{1,2} "
339 <<
"\\d{1,2}/\\d{1,2}/"
340 <<
"\\d{1,2} \\d{1,2}"
341 <<
"\\d{1,2} \\d{1,2} "
342 <<
"\\d{1,2}-\\d{1,2}-"
345 <<
"[a-zA-Z]{1,12} \\d{1,2}"
346 <<
"[a-zA-Z]{1,12} \\d{1,2} ";
347 monthDayYear <<
"\\d{1,2}/\\d{1,2}/\\d{1,4}"
348 <<
"\\d{1,2}/\\d{1,2}/\\d{1,4} "
349 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4}"
350 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4} "
351 <<
"\\d{1,2} \\d{1,2} \\d{1,4}"
352 <<
"\\d{1,2} \\d{1,2} \\d{1,4} ";
357 yearMonth <<
"\\d{4}/\\d{1,2}"
358 <<
"\\d{4}/\\d{1,2} "
359 <<
"\\d{4}/\\d{1,2}/"
361 <<
"\\d{4}-\\d{1,2} "
362 <<
"\\d{4}-\\d{1,2}-"
364 <<
"\\d{4} \\d{1,2} "
365 <<
"\\d{4}/[a-zA-Z]{1,12}"
366 <<
"\\d{4}/[a-zA-Z]{1,12} "
367 <<
"\\d{4}/[a-zA-Z]{1,12}/"
368 <<
"\\d{4}-[a-zA-Z]{1,12}"
369 <<
"\\d{4}-[a-zA-Z]{1,12} "
370 <<
"\\d{4}-[a-zA-Z]{1,12}-"
371 <<
"\\d{4} [a-zA-Z]{1,12}"
372 <<
"\\d{4} [a-zA-Z]{1,12} ";
373 yearMonthDay <<
"\\d{4}/\\d{1,2}/\\d{1,2}"
374 <<
"\\d{4}/\\d{1,2}-\\d{1,2}"
375 <<
"\\d{4} \\d{1,2} \\d{1,2}"
376 <<
"\\d{4}/[a-zA-Z]{1,12}/\\d{1,2}"
377 <<
"\\d{4}-[a-zA-Z]{1,12}-\\d{1,2}";
381 m_formatsDateParseIncomplete [COORD_UNITS_DATE_SKIP] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
382 m_formatsDateParseIncomplete [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
383 m_formatsDateParseIncomplete [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + day + dayMonth + year + yearMonth + yearMonthDay;
384 m_formatsDateParseIncomplete [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + year + yearMonth + yearMonthDay;
386 ENGAUGE_ASSERT (m_formatsDateParseIncomplete.count () == NUM_COORD_UNITS_DATE);
388 QStringList hour, hourMinute, hourMinuteAmPm, hourMinuteSecond, hourMinuteSecondAmPm;
392 hourMinute <<
"\\d{1,2}:\\d{1,2}"
393 <<
"\\d{1,2}:\\d{1,2}:"
394 <<
"\\d{1,2}:\\d{1,2} ";
395 hourMinuteAmPm <<
"\\d{1,2}:\\d{1,2} [aApP]";
396 hourMinuteSecond <<
"\\d{1,2}:\\d{1,2}:\\d{1,2}"
397 <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} ";
398 hourMinuteSecondAmPm <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} [aApP]";
401 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_SKIP] = skip +
403 hourMinute + hourMinuteAmPm +
404 hourMinuteSecond + hourMinuteSecondAmPm;
405 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE] = skip +
407 hourMinute + hourMinuteAmPm +
408 hourMinuteSecond + hourMinuteSecondAmPm;
409 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip +
411 hourMinute + hourMinuteAmPm +
412 hourMinuteSecond + hourMinuteSecondAmPm;
414 ENGAUGE_ASSERT (m_formatsTimeParseIncomplete.count () == NUM_COORD_UNITS_TIME);
418 CoordUnitsTime coordUnitsTime,
419 const QString &stringUntrimmed,
422 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::parseInput"
423 <<
" date=" << coordUnitsDateToString (coordUnitsDate).toLatin1().data()
424 <<
" time=" << coordUnitsTimeToString (coordUnitsTime).toLatin1().data()
425 <<
" string=" << stringUntrimmed.toLatin1().data();
427 const bool USE_QREGEXP =
true, DO_NOT_USE_QREGEXP =
false;
429 const QString
string = stringUntrimmed.trimmed();
431 QValidator::State state;
432 if (
string.isEmpty()) {
434 state = QValidator::Intermediate;
438 state = QValidator::Invalid;
441 bool success =
false;
442 dateTimeLookup (m_formatsDateParseAcceptable,
443 m_formatsTimeParseAcceptable,
452 state = QValidator::Acceptable;
457 dateTimeLookup (m_formatsDateParseIncomplete,
458 m_formatsTimeParseIncomplete,
467 state = QValidator::Intermediate;