adate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "adate.h"
  2. QDate ADate::parseInput(QString &io_user_input, OPL::DateFormat format)
  3. {
  4. // Try input string
  5. const QString &format_string = ADATEFORMATSMAP.value(format);
  6. QDate return_date = QDate::fromString(io_user_input, format_string);
  7. if (return_date.isValid())
  8. return return_date;
  9. // try to fix the user input
  10. tryToFix(io_user_input, format);
  11. return QDate::fromString(io_user_input, format_string);
  12. }
  13. void ADate::tryToFix(QString &io_user_input, OPL::DateFormat format)
  14. {
  15. if (io_user_input.length() < 10) {
  16. if (containsSeperator(io_user_input)) {
  17. padZeroes(io_user_input);
  18. }
  19. else {
  20. addSeperators(io_user_input, format);
  21. padZeroes(io_user_input);
  22. }
  23. }
  24. if (io_user_input.length() == 8)
  25. padCentury(io_user_input, format);
  26. }
  27. // Input contains seperators and is of length 8
  28. void ADate::padCentury(QString &io_user_input, OPL::DateFormat format)
  29. {
  30. switch (format) {
  31. case OPL::DateFormat::ISODate: {
  32. int year = io_user_input.left(2).toInt();
  33. if (year > 50)
  34. io_user_input.prepend(QStringLiteral("19"));
  35. else
  36. io_user_input.prepend(QStringLiteral("20"));
  37. break;
  38. }
  39. case OPL::DateFormat::DE: {
  40. int year = io_user_input.right(2).toInt();
  41. if (year > 50)
  42. io_user_input.insert(6, QStringLiteral("19"));
  43. else
  44. io_user_input.insert(6, QStringLiteral("20"));
  45. break;
  46. }
  47. case OPL::DateFormat::EN: {
  48. int year = io_user_input.right(2).toInt();
  49. if (year > 50)
  50. io_user_input.insert(6, QStringLiteral("19"));
  51. else
  52. io_user_input.insert(6, QStringLiteral("20"));
  53. break;
  54. }
  55. }
  56. DEB << "Padded century: " << io_user_input;
  57. }
  58. void ADate::padZeroes(QString &io_user_input)
  59. {
  60. const auto unpadded_start = QRegularExpression(QStringLiteral("^\\d{1}\\W"));
  61. const auto unpadded_middle = QRegularExpression(QStringLiteral("\\W\\d\\W"));
  62. const auto unpadded_end = QRegularExpression(QStringLiteral("\\W\\d$"));
  63. auto match = unpadded_start.match(io_user_input);
  64. if (match.hasMatch())
  65. io_user_input.insert(match.capturedStart(), QLatin1Char('0'));
  66. match = unpadded_middle.match(io_user_input);
  67. if (match.hasMatch())
  68. io_user_input.insert(match.capturedStart() + 1, QLatin1Char('0'));
  69. match = unpadded_end.match(io_user_input);
  70. if (match.hasMatch())
  71. io_user_input.insert(match.capturedStart() + 1, QLatin1Char('0'));
  72. DEB << "Padded zeroes: " << io_user_input;
  73. }
  74. // 10.10.2020
  75. void ADate::addSeperators(QString &io_user_input, const OPL::DateFormat &format)
  76. {
  77. switch (format) {
  78. case OPL::DateFormat::ISODate:
  79. if (io_user_input.length() > 7) {
  80. io_user_input.insert(4, QLatin1Char('-'));
  81. io_user_input.insert(7, QLatin1Char('-'));
  82. } else {
  83. io_user_input.insert(2, QLatin1Char('-'));
  84. io_user_input.insert(5, QLatin1Char('-'));
  85. }
  86. break;
  87. case OPL::DateFormat::DE:
  88. io_user_input.insert(2, QLatin1Char('.'));
  89. io_user_input.insert(5, QLatin1Char('.'));
  90. break;
  91. case OPL::DateFormat::EN:
  92. io_user_input.insert(2, QLatin1Char('/'));
  93. io_user_input.insert(5, QLatin1Char('/'));
  94. break;
  95. }
  96. DEB << "Added Seperators: " << io_user_input;
  97. }
  98. bool ADate::containsSeperator(const QString &user_input)
  99. {
  100. if (user_input.contains(QLatin1Char('.')))
  101. return true;
  102. if (user_input.contains(QLatin1Char('-')))
  103. return true;
  104. if (user_input.contains(QLatin1Char('/')))
  105. return true;
  106. DEB << "No Seperators found.";
  107. return false;
  108. }
  109. const QStringList& ADate::getDisplayNames()
  110. {
  111. return DISPLAY_NAMES;
  112. }
  113. const QString ADate::getFormatString(OPL::DateFormat format)
  114. {
  115. return ADATEFORMATSMAP.value(format);
  116. }
  117. const QString ADate::currentDate()
  118. {
  119. return QDate::currentDate().toString(Qt::ISODate);
  120. }