1 #include "EngaugeAssert.h"
2 #include "FormatDateTime.h"
8 loadFormatsParseAcceptable();
9 loadFormatsParseIncomplete();
12 bool FormatDateTime::ambiguityBetweenDateAndTime (CoordUnitsDate coordUnitsDate,
13 CoordUnitsTime coordUnitsTime,
14 const QString &
string)
const
16 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::ambiguityBetweenDateAndTime";
18 bool ambiguous =
false;
21 if (coordUnitsDate != COORD_UNITS_DATE_SKIP &&
22 coordUnitsTime != COORD_UNITS_TIME_SKIP) {
25 QStringList fields =
string.trimmed().split(QRegExp (
"[/- :]"));
27 if (fields.count() == 1) {
38 void FormatDateTime::dateTimeLookup (
const FormatsDate &formatsDateAll,
39 const FormatsTime &formatsTimeAll,
40 CoordUnitsDate coordUnitsDate,
41 CoordUnitsTime coordUnitsTime,
42 const QString &
string,
43 bool useQDateTimeElseQRegExp,
47 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup";
51 ENGAUGE_ASSERT (formatsDateAll.contains (coordUnitsDate));
52 ENGAUGE_ASSERT (formatsTimeAll.contains (coordUnitsTime));
54 QStringList formatsDate = formatsDateAll [coordUnitsDate];
55 QStringList formatsTime = formatsTimeAll [coordUnitsTime];
58 QStringList::const_iterator itrDate, itrTime;
59 bool iterating =
true;
60 for (itrDate = formatsDate.begin(); itrDate != formatsDate.end() && iterating; itrDate++) {
62 QString formatDate = *itrDate;
64 for (itrTime = formatsTime.begin(); itrTime != formatsTime.end() && iterating; itrTime++) {
66 QString formatTime = *itrTime;
69 QString separator = (!formatDate.isEmpty() && !formatTime.isEmpty() ?
" " :
"");
71 QString formatDateTime = formatDate + separator + formatTime;
73 if (!formatDateTime.isEmpty()) {
76 if (useQDateTimeElseQRegExp) {
78 QDateTime dt = QDateTime::fromString (
string,
81 if (dt.isValid() && !ambiguityBetweenDateAndTime (coordUnitsDate,
86 value = dt.toTime_t();
89 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
90 <<
" string=" <<
string.toLatin1().data()
91 <<
" qDateTimeFormatMatched=" << formatDateTime.toLatin1().data()
93 <<
" stringQDateTime=" << dt.toString().toLatin1().data();
98 QRegExp reg (formatDateTime);
99 if (reg.exactMatch(
string)) {
104 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
105 <<
" string=" <<
string.toLatin1().data()
106 <<
" regExpMatched=" << formatDateTime.toLatin1().data();
116 CoordUnitsTime coordUnitsTime,
119 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::formatOutput"
120 <<
" value=" << value;
122 ENGAUGE_ASSERT (m_formatsDateFormat.contains (coordUnitsDate));
123 ENGAUGE_ASSERT (m_formatsTimeFormat.contains (coordUnitsTime));
125 QString format = m_formatsDateFormat [coordUnitsDate] +
" " + m_formatsTimeFormat [coordUnitsTime];
126 format = format.trimmed();
128 QDateTime dt = QDateTime::fromTime_t (value);
130 return dt.toString (format);
133 void FormatDateTime::loadFormatsFormat()
135 m_formatsDateFormat [COORD_UNITS_DATE_SKIP] =
"";
136 m_formatsDateFormat [COORD_UNITS_DATE_MONTH_DAY_YEAR] =
"MM/dd/yyyy";
137 m_formatsDateFormat [COORD_UNITS_DATE_DAY_MONTH_YEAR] =
"dd/MM/yyyy";
138 m_formatsDateFormat [COORD_UNITS_DATE_YEAR_MONTH_DAY] =
"yyyy/MM/dd";
140 ENGAUGE_ASSERT (m_formatsDateFormat.count () == NUM_COORD_UNITS_DATE);
142 m_formatsTimeFormat [COORD_UNITS_TIME_SKIP] =
"";
143 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE] =
"hh/mm";
144 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] =
"hh:mm:ss";
146 ENGAUGE_ASSERT (m_formatsTimeFormat.count () == NUM_COORD_UNITS_TIME);
149 void FormatDateTime::loadFormatsParseAcceptable()
151 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseAcceptable";
153 QStringList skip, dayMonth, dayMonthYear, monthDay, monthDayYear, yearMonth, yearMonthDay;
177 dayMonthYear <<
"d/M/yyyy"
221 monthDayYear <<
"M/d/yyyy"
245 yearMonth <<
"yyyy/M"
257 yearMonthDay <<
"yyyy/M/d"
277 m_formatsDateParseAcceptable [COORD_UNITS_DATE_SKIP] = skip + monthDay + monthDayYear + yearMonthDay;
278 m_formatsDateParseAcceptable [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + monthDay + monthDayYear + yearMonthDay;
279 m_formatsDateParseAcceptable [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + dayMonth + dayMonthYear + yearMonthDay;
280 m_formatsDateParseAcceptable [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + yearMonth + yearMonthDay;
282 ENGAUGE_ASSERT (m_formatsDateParseAcceptable.count () == NUM_COORD_UNITS_DATE);
284 QStringList hour, hourMinute, hourMinuteSecond, hourMinutePm, hourMinuteSecondPm;
287 hourMinute <<
"hh:mm";
288 hourMinuteSecond <<
"hh:mm:ss";
289 hourMinutePm <<
"hh:mmA"
293 hourMinuteSecondPm <<
"hh:mm:ssA"
298 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_SKIP] = skip + hour + hourMinute + hourMinuteSecond + hourMinutePm + hourMinuteSecondPm;
299 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
300 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
302 ENGAUGE_ASSERT (m_formatsTimeParseAcceptable.count () == NUM_COORD_UNITS_TIME);
305 void FormatDateTime::loadFormatsParseIncomplete()
307 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseIncomplete";
309 QStringList skip, day, dayMonth, month, monthDay, monthDayYear, year, yearMonth, yearMonthDay;
320 dayMonth <<
"\\d{1,2}/\\d{1,2}"
321 <<
"\\d{1,2}/\\d{1,2} "
322 <<
"\\d{1,2}/\\d{1,2}/"
323 <<
"\\d{1,2}-\\d{1,2}-"
324 <<
"\\d{1,2}/[a-zA-Z]{1,12}/"
325 <<
"\\d{1,2}-[a-zA-Z]{1,12}-"
326 <<
"\\d{1,2} [a-zA-Z]{1,12} ";
330 <<
"[a-zA-Z]{1,12} ";
331 monthDay <<
"\\d{1,2}/\\d{1,2}"
332 <<
"\\d{1,2}/\\d{1,2} "
333 <<
"\\d{1,2}/\\d{1,2}/"
334 <<
"\\d{1,2} \\d{1,2}"
335 <<
"\\d{1,2} \\d{1,2} "
336 <<
"\\d{1,2}-\\d{1,2}-"
339 <<
"[a-zA-Z]{1,12} \\d{1,2}"
340 <<
"[a-zA-Z]{1,12} \\d{1,2} ";
341 monthDayYear <<
"\\d{1,2}/\\d{1,2}/\\d{1,4}"
342 <<
"\\d{1,2}/\\d{1,2}/\\d{1,4} "
343 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4}"
344 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4} "
345 <<
"\\d{1,2} \\d{1,2} \\d{1,4}"
346 <<
"\\d{1,2} \\d{1,2} \\d{1,4} ";
351 yearMonth <<
"\\d{4}/\\d{1,2}"
352 <<
"\\d{4}/\\d{1,2} "
353 <<
"\\d{4}/\\d{1,2}/"
355 <<
"\\d{4}-\\d{1,2} "
356 <<
"\\d{4}-\\d{1,2}-"
358 <<
"\\d{4} \\d{1,2} "
359 <<
"\\d{4}/[a-zA-Z]{1,12}"
360 <<
"\\d{4}/[a-zA-Z]{1,12} "
361 <<
"\\d{4}/[a-zA-Z]{1,12}/"
362 <<
"\\d{4}-[a-zA-Z]{1,12}"
363 <<
"\\d{4}-[a-zA-Z]{1,12} "
364 <<
"\\d{4}-[a-zA-Z]{1,12}-"
365 <<
"\\d{4} [a-zA-Z]{1,12}"
366 <<
"\\d{4} [a-zA-Z]{1,12} ";
367 yearMonthDay <<
"\\d{4}/\\d{1,2}/\\d{1,2}"
368 <<
"\\d{4}/\\d{1,2}-\\d{1,2}"
369 <<
"\\d{4} \\d{1,2} \\d{1,2}"
370 <<
"\\d{4}/[a-zA-Z]{1,12}/\\d{1,2}"
371 <<
"\\d{4}-[a-zA-Z]{1,12}-\\d{1,2}";
375 m_formatsDateParseIncomplete [COORD_UNITS_DATE_SKIP] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
376 m_formatsDateParseIncomplete [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
377 m_formatsDateParseIncomplete [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + day + dayMonth + year + yearMonth + yearMonthDay;
378 m_formatsDateParseIncomplete [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + year + yearMonth + yearMonthDay;
380 ENGAUGE_ASSERT (m_formatsDateParseIncomplete.count () == NUM_COORD_UNITS_DATE);
382 QStringList hour, hourMinute, hourMinuteAmPm, hourMinuteSecond, hourMinuteSecondAmPm;
386 hourMinute <<
"\\d{1,2}:\\d{1,2}"
387 <<
"\\d{1,2}:\\d{1,2}:"
388 <<
"\\d{1,2}:\\d{1,2} ";
389 hourMinuteAmPm <<
"\\d{1,2}:\\d{1,2} [aApP]";
390 hourMinuteSecond <<
"\\d{1,2}:\\d{1,2}:\\d{1,2}"
391 <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} ";
392 hourMinuteSecondAmPm <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} [aApP]";
395 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_SKIP] = skip +
397 hourMinute + hourMinuteAmPm +
398 hourMinuteSecond + hourMinuteSecondAmPm;
399 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE] = skip +
401 hourMinute + hourMinuteAmPm +
402 hourMinuteSecond + hourMinuteSecondAmPm;
403 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip +
405 hourMinute + hourMinuteAmPm +
406 hourMinuteSecond + hourMinuteSecondAmPm;
408 ENGAUGE_ASSERT (m_formatsTimeParseIncomplete.count () == NUM_COORD_UNITS_TIME);
412 CoordUnitsTime coordUnitsTime,
413 const QString &stringUntrimmed,
416 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::parseInput"
417 <<
" date=" << coordUnitsDateToString (coordUnitsDate).toLatin1().data()
418 <<
" time=" << coordUnitsTimeToString (coordUnitsTime).toLatin1().data()
419 <<
" string=" << stringUntrimmed.toLatin1().data();
421 const bool USE_QREGEXP =
true, DO_NOT_USE_QREGEXP =
false;
423 const QString
string = stringUntrimmed.trimmed();
425 QValidator::State state;
426 if (
string.isEmpty()) {
428 state = QValidator::Intermediate;
432 state = QValidator::Invalid;
435 bool success =
false;
436 dateTimeLookup (m_formatsDateParseAcceptable,
437 m_formatsTimeParseAcceptable,
446 state = QValidator::Acceptable;
451 dateTimeLookup (m_formatsDateParseIncomplete,
452 m_formatsTimeParseIncomplete,
461 state = QValidator::Intermediate;