adate.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef ADATE_H
  2. #define ADATE_H
  3. #include "src/opl.h"
  4. const static auto ISO = QStringLiteral("yyyy-MM-dd");
  5. const static auto DE = QStringLiteral("dd.MM.yyyy");
  6. const static auto EN = QStringLiteral("MM/dd/yyyy");
  7. const static QMap<OPL::DateFormat, QString> ADATEFORMATSMAP = {
  8. {OPL::DateFormat::ISODate, ISO},
  9. {OPL::DateFormat::DE, DE },
  10. {OPL::DateFormat::EN, EN },
  11. };
  12. const static QStringList DISPLAY_NAMES = {
  13. QStringLiteral("ISO 8601: yyyy-MM-dd"),
  14. QStringLiteral("DE: dd.MM.yyyy"),
  15. QStringLiteral("EN: MM/dd/yyyy")
  16. };
  17. /*!
  18. * \brief The ADate class is responsible for input/output of Dates and handling the different
  19. * Date Formats.
  20. */
  21. class ADate
  22. {
  23. public:
  24. /*!
  25. * \brief takes a user-provided input and tries to convert it to a (valid) QDate.
  26. * \return QDate (invalid if input not recognized)
  27. */
  28. static QDate parseInput(QString &io_user_input, OPL::DateFormat format);
  29. static void tryToFix(QString &io_user_input, OPL::DateFormat format);
  30. /*!
  31. * \brief padCentury adds the century to a date where it was omitted
  32. */
  33. static void padCentury(QString &io_user_input, OPL::DateFormat format);
  34. /*!
  35. * \brief pads a user-provided date string with 0s to facilitate conversion to QDate
  36. */
  37. static void padZeroes(QString &io_user_input);
  38. static void addSeperators(QString &io_user_input, const OPL::DateFormat &format);
  39. static bool containsSeperator(const QString &user_input);
  40. /*!
  41. * \brief Reimplements QDate::toString to accept OPL::Date::ADateFormat enums
  42. */
  43. inline static QString toString(const QDate &date, OPL::DateFormat format = OPL::DateFormat::ISODate)
  44. {
  45. return date.toString(ADATEFORMATSMAP.value(format));
  46. };
  47. static const QStringList& getDisplayNames();
  48. static const QString getFormatString(OPL::DateFormat format);
  49. /*!
  50. * \brief today Returns a string containing the current date in ISO format
  51. * \return
  52. */
  53. static const QString currentDate();
  54. };
  55. #endif // ADATE_H