7 #include "EngaugeAssert.h" 8 #include "FormatDateTime.h" 14 const Qt::TimeSpec REFERENCE_TIME_ZONE (Qt::UTC);
19 loadFormatsParseAcceptable();
20 loadFormatsParseIncomplete();
23 bool FormatDateTime::ambiguityBetweenDateAndTime (CoordUnitsDate coordUnitsDate,
24 CoordUnitsTime coordUnitsTime,
25 const QString &
string)
const 27 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::ambiguityBetweenDateAndTime";
29 bool ambiguous =
false;
32 if (coordUnitsDate != COORD_UNITS_DATE_SKIP &&
33 coordUnitsTime != COORD_UNITS_TIME_SKIP) {
36 QStringList fields =
string.trimmed().split(QRegExp (
"[/- :]"));
38 if (fields.count() == 1) {
49 void FormatDateTime::dateTimeLookup (
const FormatsDate &formatsDateAll,
50 const FormatsTime &formatsTimeAll,
51 CoordUnitsDate coordUnitsDate,
52 CoordUnitsTime coordUnitsTime,
53 const QString &
string,
54 bool useQDateTimeElseQRegExp,
58 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup";
62 ENGAUGE_ASSERT (formatsDateAll.contains (coordUnitsDate));
63 ENGAUGE_ASSERT (formatsTimeAll.contains (coordUnitsTime));
65 QStringList formatsDate = formatsDateAll [coordUnitsDate];
66 QStringList formatsTime = formatsTimeAll [coordUnitsTime];
69 QStringList::const_iterator itrDate, itrTime;
70 bool iterating =
true;
71 for (itrDate = formatsDate.begin(); itrDate != formatsDate.end() && iterating; itrDate++) {
73 QString formatDate = *itrDate;
75 for (itrTime = formatsTime.begin(); itrTime != formatsTime.end() && iterating; itrTime++) {
77 QString formatTime = *itrTime;
80 QString separator = (!formatDate.isEmpty() && !formatTime.isEmpty() ?
" " :
"");
82 QString formatDateTime = formatDate + separator + formatTime;
84 if (!formatDateTime.isEmpty()) {
87 if (useQDateTimeElseQRegExp) {
89 QDateTime dt = QDateTime::fromString (
string,
92 if (dt.isValid() && !ambiguityBetweenDateAndTime (coordUnitsDate,
97 value = dt.toTimeSpec (REFERENCE_TIME_ZONE).toTime_t ();
100 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup" 101 <<
" string=" <<
string.toLatin1().data()
102 <<
" qDateTimeFormatMatched=" << formatDateTime.toLatin1().data()
103 <<
" value=" << value
104 <<
" stringQDateTime=" << dt.toString().toLatin1().data();
109 QRegExp reg (formatDateTime);
110 if (reg.exactMatch(
string)) {
115 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::dateTimeLookup" 116 <<
" string=" <<
string.toLatin1().data()
117 <<
" regExpMatched=" << formatDateTime.toLatin1().data();
127 CoordUnitsTime coordUnitsTime,
130 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::formatOutput" 131 <<
" value=" << value;
133 ENGAUGE_ASSERT (m_formatsDateFormat.contains (coordUnitsDate));
134 ENGAUGE_ASSERT (m_formatsTimeFormat.contains (coordUnitsTime));
136 QString format = m_formatsDateFormat [coordUnitsDate] +
" " + m_formatsTimeFormat [coordUnitsTime];
137 format = format.trimmed();
139 QDateTime dt = QDateTime::fromTime_t (value);
141 return dt.toTimeSpec(REFERENCE_TIME_ZONE).toString (format);
144 void FormatDateTime::loadFormatsFormat()
146 m_formatsDateFormat [COORD_UNITS_DATE_SKIP] =
"";
147 m_formatsDateFormat [COORD_UNITS_DATE_MONTH_DAY_YEAR] =
"MM/dd/yyyy";
148 m_formatsDateFormat [COORD_UNITS_DATE_DAY_MONTH_YEAR] =
"dd/MM/yyyy";
149 m_formatsDateFormat [COORD_UNITS_DATE_YEAR_MONTH_DAY] =
"yyyy/MM/dd";
151 ENGAUGE_ASSERT (m_formatsDateFormat.count () == NUM_COORD_UNITS_DATE);
153 m_formatsTimeFormat [COORD_UNITS_TIME_SKIP] =
"";
154 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE] =
"hh/mm";
155 m_formatsTimeFormat [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] =
"hh:mm:ss";
157 ENGAUGE_ASSERT (m_formatsTimeFormat.count () == NUM_COORD_UNITS_TIME);
160 void FormatDateTime::loadFormatsParseAcceptable()
162 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseAcceptable";
164 QStringList skip, dayMonth, dayMonthYear, monthDay, monthDayYear, yearMonth, yearMonthDay;
188 dayMonthYear <<
"d/M/yyyy" 232 monthDayYear <<
"M/d/yyyy" 256 yearMonth <<
"yyyy/M" 268 yearMonthDay <<
"yyyy/M/d" 288 m_formatsDateParseAcceptable [COORD_UNITS_DATE_SKIP] = skip + monthDay + monthDayYear + yearMonthDay;
289 m_formatsDateParseAcceptable [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + monthDay + monthDayYear + yearMonthDay;
290 m_formatsDateParseAcceptable [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + dayMonth + dayMonthYear + yearMonthDay;
291 m_formatsDateParseAcceptable [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + yearMonth + yearMonthDay;
293 ENGAUGE_ASSERT (m_formatsDateParseAcceptable.count () == NUM_COORD_UNITS_DATE);
295 QStringList hour, hourMinute, hourMinuteSecond, hourMinutePm, hourMinuteSecondPm;
298 hourMinute <<
"hh:mm";
299 hourMinuteSecond <<
"hh:mm:ss";
300 hourMinutePm <<
"hh:mmA" 304 hourMinuteSecondPm <<
"hh:mm:ssA" 309 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_SKIP] = skip + hour + hourMinute + hourMinuteSecond + hourMinutePm + hourMinuteSecondPm;
310 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
311 m_formatsTimeParseAcceptable [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip + hour + hourMinute + hourMinutePm + hourMinuteSecond + hourMinuteSecondPm;
313 ENGAUGE_ASSERT (m_formatsTimeParseAcceptable.count () == NUM_COORD_UNITS_TIME);
316 void FormatDateTime::loadFormatsParseIncomplete()
318 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::loadFormatsParseIncomplete";
320 QStringList skip, day, dayMonth, month, monthDay, monthDayYear, year, yearMonth, yearMonthDay;
331 dayMonth <<
"\\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}/[a-zA-Z]{1,12}/" 336 <<
"\\d{1,2}-[a-zA-Z]{1,12}-" 337 <<
"\\d{1,2} [a-zA-Z]{1,12} ";
341 <<
"[a-zA-Z]{1,12} ";
342 monthDay <<
"\\d{1,2}/\\d{1,2}" 343 <<
"\\d{1,2}/\\d{1,2} " 344 <<
"\\d{1,2}/\\d{1,2}/" 345 <<
"\\d{1,2} \\d{1,2}" 346 <<
"\\d{1,2} \\d{1,2} " 347 <<
"\\d{1,2}-\\d{1,2}-" 350 <<
"[a-zA-Z]{1,12} \\d{1,2}" 351 <<
"[a-zA-Z]{1,12} \\d{1,2} ";
352 monthDayYear <<
"\\d{1,2}/\\d{1,2}/\\d{1,4}" 353 <<
"\\d{1,2}/\\d{1,2}/\\d{1,4} " 354 <<
"\\d{1,2}-\\d{1,2}-\\d{1,4}" 355 <<
"\\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} ";
362 yearMonth <<
"\\d{4}/\\d{1,2}" 363 <<
"\\d{4}/\\d{1,2} " 364 <<
"\\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}/[a-zA-Z]{1,12}" 371 <<
"\\d{4}/[a-zA-Z]{1,12} " 372 <<
"\\d{4}/[a-zA-Z]{1,12}/" 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 yearMonthDay <<
"\\d{4}/\\d{1,2}/\\d{1,2}" 379 <<
"\\d{4}/\\d{1,2}-\\d{1,2}" 380 <<
"\\d{4} \\d{1,2} \\d{1,2}" 381 <<
"\\d{4}/[a-zA-Z]{1,12}/\\d{1,2}" 382 <<
"\\d{4}-[a-zA-Z]{1,12}-\\d{1,2}";
386 m_formatsDateParseIncomplete [COORD_UNITS_DATE_SKIP] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
387 m_formatsDateParseIncomplete [COORD_UNITS_DATE_MONTH_DAY_YEAR] = skip + month + monthDay + monthDayYear + year + yearMonth + yearMonthDay;
388 m_formatsDateParseIncomplete [COORD_UNITS_DATE_DAY_MONTH_YEAR] = skip + day + dayMonth + year + yearMonth + yearMonthDay;
389 m_formatsDateParseIncomplete [COORD_UNITS_DATE_YEAR_MONTH_DAY] = skip + year + yearMonth + yearMonthDay;
391 ENGAUGE_ASSERT (m_formatsDateParseIncomplete.count () == NUM_COORD_UNITS_DATE);
393 QStringList hour, hourMinute, hourMinuteAmPm, hourMinuteSecond, hourMinuteSecondAmPm;
397 hourMinute <<
"\\d{1,2}:\\d{1,2}" 398 <<
"\\d{1,2}:\\d{1,2}:" 399 <<
"\\d{1,2}:\\d{1,2} ";
400 hourMinuteAmPm <<
"\\d{1,2}:\\d{1,2} [aApP]";
401 hourMinuteSecond <<
"\\d{1,2}:\\d{1,2}:\\d{1,2}" 402 <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} ";
403 hourMinuteSecondAmPm <<
"\\d{1,2}:\\d{1,2}:\\d{1,2} [aApP]";
406 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_SKIP] = skip +
408 hourMinute + hourMinuteAmPm +
409 hourMinuteSecond + hourMinuteSecondAmPm;
410 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE] = skip +
412 hourMinute + hourMinuteAmPm +
413 hourMinuteSecond + hourMinuteSecondAmPm;
414 m_formatsTimeParseIncomplete [COORD_UNITS_TIME_HOUR_MINUTE_SECOND] = skip +
416 hourMinute + hourMinuteAmPm +
417 hourMinuteSecond + hourMinuteSecondAmPm;
419 ENGAUGE_ASSERT (m_formatsTimeParseIncomplete.count () == NUM_COORD_UNITS_TIME);
423 CoordUnitsTime coordUnitsTime,
424 const QString &stringUntrimmed,
427 LOG4CPP_INFO_S ((*mainCat)) <<
"FormatDateTime::parseInput" 428 <<
" date=" << coordUnitsDateToString (coordUnitsDate).toLatin1().data()
429 <<
" time=" << coordUnitsTimeToString (coordUnitsTime).toLatin1().data()
430 <<
" string=" << stringUntrimmed.toLatin1().data();
432 const bool USE_QREGEXP =
true, DO_NOT_USE_QREGEXP =
false;
434 const QString
string = stringUntrimmed.trimmed();
436 QValidator::State state;
437 if (
string.isEmpty()) {
439 state = QValidator::Intermediate;
443 state = QValidator::Invalid;
446 bool success =
false;
447 dateTimeLookup (m_formatsDateParseAcceptable,
448 m_formatsTimeParseAcceptable,
457 state = QValidator::Acceptable;
462 dateTimeLookup (m_formatsDateParseIncomplete,
463 m_formatsTimeParseIncomplete,
472 state = QValidator::Intermediate;