7 #include "EngaugeAssert.h"
8 #include "FormatDateTime.h"
15 const Qt::TimeSpec REFERENCE_TIME_ZONE (Qt::UTC);
20 loadFormatsParseAcceptable();
21 loadFormatsParseIncomplete();
24 bool FormatDateTime::ambiguityBetweenDateAndTime (CoordUnitsDate coordUnitsDate,
25 CoordUnitsTime coordUnitsTime,
26 const QString &
string)
const
28 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::ambiguityBetweenDateAndTime";
30 bool ambiguous =
false;
33 if (coordUnitsDate != COORD_UNITS_DATE_SKIP &&
34 coordUnitsTime != COORD_UNITS_TIME_SKIP) {
37 QStringList fields =
string.trimmed().split(QRegExp (
"[/- :]"));
39 if (fields.count() == 1) {
50 void FormatDateTime::dateTimeLookup (
const FormatsDate &formatsDateAll,
51 const FormatsTime &formatsTimeAll,
52 CoordUnitsDate coordUnitsDate,
53 CoordUnitsTime coordUnitsTime,
54 const QString &
string,
55 bool useQDateTimeElseQRegExp,
59 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup";
63 ENGAUGE_ASSERT (formatsDateAll.contains (coordUnitsDate));
64 ENGAUGE_ASSERT (formatsTimeAll.contains (coordUnitsTime));
66 QStringList formatsDate = formatsDateAll [coordUnitsDate];
67 QStringList formatsTime = formatsTimeAll [coordUnitsTime];
70 QStringList::const_iterator itrDate, itrTime;
71 bool iterating =
true;
72 for (itrDate = formatsDate.begin(); itrDate != formatsDate.end() && iterating; itrDate++) {
74 QString formatDate = *itrDate;
76 for (itrTime = formatsTime.begin(); itrTime != formatsTime.end() && iterating; itrTime++) {
78 QString formatTime = *itrTime;
81 QString separator = (!formatDate.isEmpty() && !formatTime.isEmpty() ?
" " :
"");
83 QString formatDateTime = formatDate + separator + formatTime;
85 if (!formatDateTime.isEmpty()) {
88 if (useQDateTimeElseQRegExp) {
90 QDateTime dt = QDateTime::fromString (
string,
93 if (dt.isValid() && !ambiguityBetweenDateAndTime (coordUnitsDate,
98 value = dt.toTimeSpec (REFERENCE_TIME_ZONE).toTime_t ();
101 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
102 <<
" string=" <<
string.toLatin1().data()
103 <<
" qDateTimeFormatMatched=" << formatDateTime.toLatin1().data()
104 <<
" value=" << value
105 <<
" stringQDateTime=" << dt.toString().toLatin1().data();
110 QRegExp reg (formatDateTime);
111 if (reg.exactMatch(
string)) {
116 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup"
117 <<
" string=" <<
string.toLatin1().data()
118 <<
" regExpMatched=" << formatDateTime.toLatin1().data();
128 CoordUnitsTime coordUnitsTime,
131 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::formatOutput"
132 <<
" value=" << value;
134 ENGAUGE_ASSERT (m_formatsDateFormat.contains (coordUnitsDate));
135 ENGAUGE_ASSERT (m_formatsTimeFormat.contains (coordUnitsTime));
137 QString format = m_formatsDateFormat [coordUnitsDate] +
" " + m_formatsTimeFormat [coordUnitsTime];
138 format = format.trimmed();
142 QDateTime dt = QDateTime::fromTime_t (
unsigned (qFloor (value)));
144 return dt.toTimeSpec(REFERENCE_TIME_ZONE).toString (format);
147 void FormatDateTime::loadFormatsFormat()
149 m_formatsDateFormat [COORD_UNITS_DATE_SKIP] =
"";
150 m_formatsDateFormat [COORD_UNITS_DATE_MONTH_DAY_YEAR] =
"MM/dd/yyyy";
151 m_formatsDateFormat [COORD_UNITS_DATE_DAY_MONTH_YEAR] =
"dd/MM/yyyy";
152 m_formatsDateFormat [COORD_UNITS_DATE_YEAR_MONTH_DAY] =
"yyyy/MM/dd";
154 ENGAUGE_ASSERT (m_formatsDateFormat.count () == NUM_COORD_UNITS_DATE);
156 m_formatsTimeFormat [COORD_UNITS_TIME_SKIP] =
"";
157 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE] =
"hh/mm";
158 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] =
"hh:mm:ss";
160 ENGAUGE_ASSERT (m_formatsTimeFormat.count () == NUM_COORD_UNITS_TIME);
163 void FormatDateTime::loadFormatsParseAcceptable()
165 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseAcceptable";
167 QStringList skip, dayMonth, dayMonthYear, monthDay, monthDayYear, yearMonth, yearMonthDay;
191 dayMonthYear <<
"d/M/yyyy"
235 monthDayYear <<
"M/d/yyyy"
259 yearMonth <<
"yyyy/M"
271 yearMonthDay <<
"yyyy/M/d"
291 m_formatsDateParseAcceptable [COORD_UNITS_DATE_SKIP] = skip + monthDay + monthDayYear + yearMonthDay;
292 m_formatsDateParseAcceptable [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + monthDay + monthDayYear + yearMonthDay;
293 m_formatsDateParseAcceptable [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + dayMonth + dayMonthYear + yearMonthDay;
294 m_formatsDateParseAcceptable [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + yearMonth + yearMonthDay;
296 ENGAUGE_ASSERT (m_formatsDateParseAcceptable.count () == NUM_COORD_UNITS_DATE);
298 QStringList hour, hourMinute, hourMinuteSecond, hourMinutePm, hourMinuteSecondPm;
301 hourMinute <<
"hh:mm";
302 hourMinuteSecond <<
"hh:mm:ss";
303 hourMinutePm <<
"hh:mmA"
307 hourMinuteSecondPm <<
"hh:mm:ssA"
312 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_SKIP] = skip + hour + hourMinute + hourMinuteSecond + hourMinutePm + hourMinuteSecondPm;
313 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
314 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
316 ENGAUGE_ASSERT (m_formatsTimeParseAcceptable.count () == NUM_COORD_UNITS_TIME);
319 void FormatDateTime::loadFormatsParseIncomplete()
321 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseIncomplete";
323 QStringList skip, day, dayMonth, month, monthDay, monthDayYear, year, yearMonth, yearMonthDay;
334 dayMonth <<
"\\d{1,2}/\\d{1,2}"
335 <<
"\\d{1,2}/\\d{1,2} "
336 <<
"\\d{1,2}/\\d{1,2}/"
337 <<
"\\d{1,2}-\\d{1,2}-"
338 <<
"\\d{1,2}/[a-zA-Z]{1,12}/"
339 <<
"\\d{1,2}-[a-zA-Z]{1,12}-"
340 <<
"\\d{1,2} [a-zA-Z]{1,12} ";
344 <<
"[a-zA-Z]{1,12} ";
345 monthDay <<
"\\d{1,2}/\\d{1,2}"
346 <<
"\\d{1,2}/\\d{1,2} "
347 <<
"\\d{1,2}/\\d{1,2}/"
348 <<
"\\d{1,2} \\d{1,2}"
349 <<
"\\d{1,2} \\d{1,2} "
350 <<
"\\d{1,2}-\\d{1,2}-"
353 <<
"[a-zA-Z]{1,12} \\d{1,2}"
354 <<
"[a-zA-Z]{1,12} \\d{1,2} ";
355 monthDayYear <<
"\\d{1,2}/\\d{1,2}/\\d{1,4}"
356 <<
"\\d{1,2}/\\d{1,2}/\\d{1,4} "
357 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4}"
358 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4} "
359 <<
"\\d{1,2} \\d{1,2} \\d{1,4}"
360 <<
"\\d{1,2} \\d{1,2} \\d{1,4} ";
365 yearMonth <<
"\\d{4}/\\d{1,2}"
366 <<
"\\d{4}/\\d{1,2} "
367 <<
"\\d{4}/\\d{1,2}/"
369 <<
"\\d{4}-\\d{1,2} "
370 <<
"\\d{4}-\\d{1,2}-"
372 <<
"\\d{4} \\d{1,2} "
373 <<
"\\d{4}/[a-zA-Z]{1,12}"
374 <<
"\\d{4}/[a-zA-Z]{1,12} "
375 <<
"\\d{4}/[a-zA-Z]{1,12}/"
376 <<
"\\d{4}-[a-zA-Z]{1,12}"
377 <<
"\\d{4}-[a-zA-Z]{1,12} "
378 <<
"\\d{4}-[a-zA-Z]{1,12}-"
379 <<
"\\d{4} [a-zA-Z]{1,12}"
380 <<
"\\d{4} [a-zA-Z]{1,12} ";
381 yearMonthDay <<
"\\d{4}/\\d{1,2}/\\d{1,2}"
382 <<
"\\d{4}/\\d{1,2}-\\d{1,2}"
383 <<
"\\d{4} \\d{1,2} \\d{1,2}"
384 <<
"\\d{4}/[a-zA-Z]{1,12}/\\d{1,2}"
385 <<
"\\d{4}-[a-zA-Z]{1,12}-\\d{1,2}";
389 m_formatsDateParseIncomplete [COORD_UNITS_DATE_SKIP] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
390 m_formatsDateParseIncomplete [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
391 m_formatsDateParseIncomplete [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + day + dayMonth + year + yearMonth + yearMonthDay;
392 m_formatsDateParseIncomplete [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + year + yearMonth + yearMonthDay;
394 ENGAUGE_ASSERT (m_formatsDateParseIncomplete.count () == NUM_COORD_UNITS_DATE);
396 QStringList hour, hourMinute, hourMinuteAmPm, hourMinuteSecond, hourMinuteSecondAmPm;
400 hourMinute <<
"\\d{1,2}:\\d{1,2}"
401 <<
"\\d{1,2}:\\d{1,2}:"
402 <<
"\\d{1,2}:\\d{1,2} ";
403 hourMinuteAmPm <<
"\\d{1,2}:\\d{1,2} [aApP]";
404 hourMinuteSecond <<
"\\d{1,2}:\\d{1,2}:\\d{1,2}"
405 <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} ";
406 hourMinuteSecondAmPm <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} [aApP]";
409 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_SKIP] = skip +
411 hourMinute + hourMinuteAmPm +
412 hourMinuteSecond + hourMinuteSecondAmPm;
413 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE] = skip +
415 hourMinute + hourMinuteAmPm +
416 hourMinuteSecond + hourMinuteSecondAmPm;
417 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip +
419 hourMinute + hourMinuteAmPm +
420 hourMinuteSecond + hourMinuteSecondAmPm;
422 ENGAUGE_ASSERT (m_formatsTimeParseIncomplete.count () == NUM_COORD_UNITS_TIME);
426 CoordUnitsTime coordUnitsTime,
427 const QString &stringUntrimmed,
430 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::parseInput"
431 <<
" date=" << coordUnitsDateToString (coordUnitsDate).toLatin1().data()
432 <<
" time=" << coordUnitsTimeToString (coordUnitsTime).toLatin1().data()
433 <<
" string=" << stringUntrimmed.toLatin1().data();
435 const bool USE_QREGEXP =
true, DO_NOT_USE_QREGEXP =
false;
437 const QString
string = stringUntrimmed.trimmed();
439 QValidator::State state;
440 if (
string.isEmpty()) {
442 state = QValidator::Intermediate;
446 state = QValidator::Invalid;
449 bool success =
false;
450 dateTimeLookup (m_formatsDateParseAcceptable,
451 m_formatsTimeParseAcceptable,
460 state = QValidator::Acceptable;
465 dateTimeLookup (m_formatsDateParseIncomplete,
466 m_formatsTimeParseIncomplete,
475 state = QValidator::Intermediate;