datetime.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef DATETIME_H
  2. #define DATETIME_H
  3. #include "src/opl.h"
  4. namespace OPL {
  5. class DateTime {
  6. public:
  7. const inline static QString ISO_FORMAT_STRING = QStringLiteral("yyyy-MM-dd");
  8. const inline static QString DE_FORMAT_STRING = QStringLiteral("dd.MM.yyyy");
  9. const inline static QString EN_FORMAT_STRING = QStringLiteral("MM/dd/yyyy");
  10. const static inline QMap<OPL::DateFormat, QString> DATEFORMATSMAP = {
  11. {OPL::DateFormat::ISODate, ISO_FORMAT_STRING},
  12. {OPL::DateFormat::DE, DE_FORMAT_STRING },
  13. {OPL::DateFormat::EN, EN_FORMAT_STRING },
  14. };
  15. const static inline QStringList DISPLAY_NAMES = {
  16. QStringLiteral("ISO 8601: yyyy-MM-dd"),
  17. QStringLiteral("DE: dd.MM.yyyy"),
  18. QStringLiteral("EN: MM/dd/yyyy")
  19. };
  20. /*!
  21. * \brief Reimplements QDate::toString to accept OPL::Date::DateFormat enums
  22. */
  23. inline static QString dateToString(const QDate &date, OPL::DateFormat format = OPL::DateFormat::ISODate)
  24. {
  25. return date.toString(DATEFORMATSMAP.value(format));
  26. };
  27. /*!
  28. * \brief takes a user-provided input and tries to convert it to a (valid) QDate.
  29. * \return QDate (invalid if input not recognized)
  30. */
  31. static QDate parseInput(QString &user_input, OPL::DateFormat format);
  32. static void tryToFix(QString &user_input, OPL::DateFormat format);
  33. /*!
  34. * \brief padCentury adds the century to a date where it was omitted
  35. */
  36. static void padCentury(QString &io_user_input, OPL::DateFormat format);
  37. /*!
  38. * \brief pads a user-provided date string with 0s to facilitate conversion to QDate
  39. */
  40. static void padZeroes(QString &io_user_input);
  41. static void addSeperators(QString &io_user_input, const OPL::DateFormat &format);
  42. static bool containsSeperator(const QString &user_input);
  43. static const QStringList& getDisplayNames();
  44. static const QString getFormatString(OPL::DateFormat format);
  45. /*!
  46. * \brief today Returns a string containing the current date in ISO format
  47. * \return
  48. */
  49. static const QString currentDate();
  50. /*!
  51. * \brief dateTimeToString formats a QDateTime object into a string in a uniform way.
  52. * \return
  53. */
  54. static inline const QString dateTimeToString (const QDateTime& date_time, OPL::DateTimeFormat_deprecated format) {
  55. switch (format) {
  56. case OPL::DateTimeFormat_deprecated::Default:
  57. return date_time.toString(Qt::ISODate);
  58. case OPL::DateTimeFormat_deprecated::Backup:
  59. return date_time.toString(QStringLiteral("yyyy_MM_dd_T_hh_mm"));
  60. default:
  61. return QString();
  62. }
  63. }
  64. static inline QDateTime fromString(const QString& date_time_string)
  65. {
  66. auto date_time = QDateTime::fromString(date_time_string, QStringLiteral("yyyy-MM-ddhh:mm"));
  67. date_time.setTimeZone(QTimeZone::utc());
  68. return date_time;
  69. }
  70. };
  71. } // namespace OPL
  72. #endif // DATETIME_H