KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
datetimeutil.h
1/**
2 * DateTimeUtil
3 *
4 * Some static helper methods for converting date/times to and from strings.
5 *
6 * In general, on of two formats are preferred:
7 * 1. ISO-8601 ZULU e.g. 2021-09-17T05:30:00.123Z
8 * 2. MySQL e.g. 2021-09-17 05:30:00.123
9 *
10 * Stephen Punak, September 17 2020
11 */
12#ifndef DATETIMEUTIL_H
13#define DATETIMEUTIL_H
14#include <QDateTime>
15#include <QString>
16#include <QTimeZone>
17#include <QVariant>
18#include "kanoopcommon.h"
19
20/**
21 * @brief Static helper methods for parsing and formatting QDateTime values.
22 *
23 * Supports ISO-8601 Zulu (e.g. 2021-09-17T05:30:00.123Z) and MySQL-style
24 * (e.g. 2021-09-17 05:30:00.123) formats as well as a compact "squashed" form.
25 */
26class KANOOP_EXPORT DateTimeUtil
27{
28public:
29 /**
30 * @brief Parse an ISO-8601 string into a UTC QDateTime.
31 * @param date ISO-8601 date/time string with milliseconds
32 * @return Parsed UTC QDateTime, or invalid QDateTime on error
33 */
34 static QDateTime fromISOString(const QString& date)
35 {
36 QDateTime result = QDateTime::fromString(date, Qt::DateFormat::ISODateWithMs);
37 result.setTimeZone(QTimeZone::utc());
38 return result;
39 }
40
41 /**
42 * @brief Parse a MySQL-style "yyyy-MM-dd hh:mm:ss.zzz" string into a QDateTime.
43 * @param date MySQL format date/time string
44 * @return Parsed QDateTime, or invalid QDateTime on error
45 */
46 static QDateTime fromStandardString(const QString& date);
47
48 /**
49 * @brief Parse a compact "yyyyMMddhhmmsszzz" string into a QDateTime.
50 * @param date Squashed format date/time string
51 * @return Parsed QDateTime, or invalid QDateTime on error
52 */
53 static QDateTime fromSquashedString(const QString& date);
54
55 /**
56 * @brief Parse a date/time string, trying multiple known formats.
57 * @param date Date/time string in any supported format
58 * @return Parsed QDateTime, or invalid QDateTime if no format matches
59 */
60 static QDateTime fromString(const QString& date);
61
62 /**
63 * @brief Parse a QVariant containing a date/time string or QDateTime.
64 * @param date Variant holding a date/time value
65 * @return Parsed QDateTime, or invalid QDateTime on error
66 */
67 static QDateTime fromVariant(const QVariant& date);
68
69 /**
70 * @brief Format a QDateTime as a MySQL-style string.
71 * @param date Date/time to format
72 * @param milliseconds Whether to include milliseconds (default true)
73 * @return Formatted string, e.g. "2021-09-17 05:30:00.123"
74 */
75 static QString toStandardString(const QDateTime& date, bool milliseconds = true)
76 {
77 return milliseconds ? date.toString("yyyy-MM-dd hh:mm:ss.zzz") : date.toString("yyyy-MM-dd hh:mm:ss");
78 }
79
80 /**
81 * @brief Format a QDateTime as an ISO-8601 Zulu string.
82 * @param date Date/time to format
83 * @return Formatted string, e.g. "2021-09-17T05:30:00.123Z"
84 */
85 static QString toISOString(const QDateTime& date) { return date.toString("yyyy-MM-ddThh:mm:ss.zzzZ"); }
86
87 /**
88 * @brief Format a QDateTime as a compact squashed string.
89 * @param date Date/time to format
90 * @return Formatted string, e.g. "20210917053000123"
91 */
92 static QString toSquashedString(const QDateTime& date) { return date.toString("yyyyMMddhhmmsszzz"); }
93
94 /**
95 * @brief Format the time portion of a QDateTime as "HH:mm:ss.zzz".
96 * @param date Date/time whose time portion to format
97 * @return Formatted time string
98 */
99 static QString toStandardTimeString(const QDateTime& date) { return date.time().toString("HH:mm:ss.zzz"); }
100
101 /**
102 * @brief Format the date portion of a QDateTime as "yyyy-MM-dd".
103 * @param date Date/time whose date portion to format
104 * @return Formatted date string
105 */
106 static QString toStandardDateString(const QDateTime& date) { return date.toString("yyyy-MM-dd"); }
107
108 /**
109 * @brief Return the current UTC time as an ISO-8601 Zulu string.
110 * @return Current UTC timestamp string
111 */
112 static QString currentToISOString() { return QDateTime::currentDateTimeUtc().toString("yyyy-MM-ddThh:mm:ss.zzzZ"); }
113
114 /**
115 * @brief Return the current UTC time as a MySQL-style string.
116 * @param milliseconds Whether to include milliseconds (default true)
117 * @return Current UTC timestamp string
118 */
119 static QString currentToStandardString(bool milliseconds = true)
120 {
121 return milliseconds ? QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd hh:mm:ss.zzz") : QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd hh:mm:ss");
122 }
123
124 /**
125 * @brief Return the current UTC time as a compact squashed string.
126 * @return Current UTC timestamp in squashed format
127 */
128 static QString currentToSquashedString() { return QDateTime::currentDateTimeUtc().toString("yyyyMMddhhmmsszzz"); }
129
130private:
131 /**
132 * @brief fromAlternate1String
133 * Parse this format: 2021-12-17 Friday 22:22:51.569
134 * @return
135 */
136 static QDateTime fromAlternate1String(const QString& date)
137 {
138 QDateTime result = QDateTime::fromString(date, "yyyy-MM-dd dddd hh:mm:ss.zzz");
139 result.setTimeZone(QTimeZone::utc());
140 return result;
141 }
142 static QDateTime fromGitRepresentation(const QString& date)
143 {
144 QDateTime dateTime = QDateTime::fromString(date, "ddd MMM d hh:mm:ss yyyy tt").toUTC();
145 return dateTime;
146 }
147};
148
149#endif // DATETIMEUTIL_H
DateTimeUtil.
static QString currentToSquashedString()
Return the current UTC time as a compact squashed string.
static QString toStandardString(const QDateTime &date, bool milliseconds=true)
Format a QDateTime as a MySQL-style string.
static QString toStandardTimeString(const QDateTime &date)
Format the time portion of a QDateTime as "HH:mm:ss.zzz".
static QDateTime fromStandardString(const QString &date)
Parse a MySQL-style "yyyy-MM-dd hh:mm:ss.zzz" string into a QDateTime.
static QString currentToStandardString(bool milliseconds=true)
Return the current UTC time as a MySQL-style string.
static QDateTime fromISOString(const QString &date)
Parse an ISO-8601 string into a UTC QDateTime.
static QString toSquashedString(const QDateTime &date)
Format a QDateTime as a compact squashed string.
static QString toStandardDateString(const QDateTime &date)
Format the date portion of a QDateTime as "yyyy-MM-dd".
static QDateTime fromString(const QString &date)
Parse a date/time string, trying multiple known formats.
static QDateTime fromVariant(const QVariant &date)
Parse a QVariant containing a date/time string or QDateTime.
static QDateTime fromSquashedString(const QString &date)
Parse a compact "yyyyMMddhhmmsszzz" string into a QDateTime.
static QString toISOString(const QDateTime &date)
Format a QDateTime as an ISO-8601 Zulu string.
static QString currentToISOString()
Return the current UTC time as an ISO-8601 Zulu string.