openPilotLog
datetime.h
1 #ifndef DATETIME_H
2 #define DATETIME_H
3 #include "src/opl.h"
4 
5 namespace OPL {
6 
7 class DateTime {
8 public:
9  const inline static QString ISO_FORMAT_STRING = QStringLiteral("yyyy-MM-dd");
10  const inline static QString DE_FORMAT_STRING = QStringLiteral("dd.MM.yyyy");
11  const inline static QString EN_FORMAT_STRING = QStringLiteral("MM/dd/yyyy");
12 
13  const static inline QMap<OPL::DateFormat, QString> DATEFORMATSMAP = {
14  {OPL::DateFormat::ISODate, ISO_FORMAT_STRING},
15  {OPL::DateFormat::DE, DE_FORMAT_STRING },
16  {OPL::DateFormat::EN, EN_FORMAT_STRING },
17 
18  };
19 
20  const static inline QStringList DISPLAY_NAMES = {
21  QStringLiteral("ISO 8601: yyyy-MM-dd"),
22  QStringLiteral("DE: dd.MM.yyyy"),
23  QStringLiteral("EN: MM/dd/yyyy")
24  };
25 
29  inline static QString dateToString(const QDate &date, OPL::DateFormat format = OPL::DateFormat::ISODate)
30  {
31  return date.toString(DATEFORMATSMAP.value(format));
32  };
33 
38  static QDate parseInput(QString &user_input, OPL::DateFormat format);
39 
40  static void tryToFix(QString &user_input, OPL::DateFormat format);
41 
45  static void padCentury(QString &io_user_input, OPL::DateFormat format);
46 
50  static void padZeroes(QString &io_user_input);
51 
52  static void addSeperators(QString &io_user_input, const OPL::DateFormat &format);
53 
54  static bool containsSeperator(const QString &user_input);
55 
56 
57 
58  static const QStringList& getDisplayNames();
59 
60  static const QString getFormatString(OPL::DateFormat format);
61 
66  static const QString currentDate();
67 
72  static inline const QString dateTimeToString (const QDateTime& date_time, OPL::DateTimeFormat format) {
73  switch (format) {
74  case OPL::DateTimeFormat::Default:
75  return date_time.toString(Qt::ISODate);
76  case OPL::DateTimeFormat::Backup:
77  return date_time.toString(QStringLiteral("yyyy_MM_dd_T_hh_mm"));
78  default:
79  return QString();
80  }
81  }
82 
83  static inline QDateTime fromString(const QString& date_time_string)
84  {
85  auto date_time = QDateTime::fromString(date_time_string, QStringLiteral("yyyy-MM-ddhh:mm"));
86  date_time.setTimeZone(QTimeZone::utc());
87  return date_time;
88  }
89 
90 };
91 
92 } // namespace OPL
93 
94 #endif // DATETIME_H
Definition: datetime.h:7
static void padZeroes(QString &io_user_input)
pads a user-provided date string with 0s to facilitate conversion to QDate
Definition: datetime.cpp:69
static QDate parseInput(QString &user_input, OPL::DateFormat format)
takes a user-provided input and tries to convert it to a (valid) QDate.
Definition: datetime.cpp:5
static void padCentury(QString &io_user_input, OPL::DateFormat format)
padCentury adds the century to a date where it was omitted
Definition: datetime.cpp:38
static const QString currentDate()
today Returns a string containing the current date in ISO format
Definition: datetime.cpp:137
static const QString dateTimeToString(const QDateTime &date_time, OPL::DateTimeFormat format)
dateTimeToString formats a QDateTime object into a string in a uniform way.
Definition: datetime.h:72
static QString dateToString(const QDate &date, OPL::DateFormat format=OPL::DateFormat::ISODate)
Reimplements QDate::toString to accept OPL::Date::DateFormat enums.
Definition: datetime.h:29
A namespace to collect constants and enums used throughout the application.
Definition: paths.cpp:3
DateFormat
ADateFormats enumerates the accepted date formats for QDateEdits.
Definition: opl.h:92