4 #include <QCoreApplication>
5 #include <QRegularExpression>
17 QDateTime rettime = QDateTime::currentDateTimeUtc();
19 rettime = rettime.addMSecs(-rettime.time().msec());
28 QDateTime
as_utc(
const QDateTime &old_dt)
31 #if QT_VERSION < QT_VERSION_CHECK(6,5,0)
32 dt.setTimeSpec(Qt::UTC);
34 dt.setTimeZone(QTimeZone(QTimeZone::UTC));
45 if (!dtstr.contains(
"-") && dtstr.length() == 14)
61 #if QT_VERSION < QT_VERSION_CHECK(6,5,0)
62 dt.setTimeSpec(Qt::UTC);
64 dt.setTimeZone(QTimeZone(QTimeZone::UTC));
97 if (!raw_dt.isValid())
108 (format &
kOverrideUTC) ? raw_dt.toUTC() : raw_dt.toLocalTime();
111 return datetime.toString(
"yyyy-MM-dd hh:mm:ss");
117 return datetime.toUTC().toString(
"ddd, dd MMM yyyy hh:mm:ss").append(
" GMT");
120 return datetime.toString(
"yyyyMMddhhmmss");
123 return datetime.toString(
"yyyy-MM-ddThh-mm-ss.zzz");
126 result +=
toString(datetime.date(), format);
130 if (!result.isEmpty())
134 result += datetime.time().toString(
timeformat);
154 QString stringformat;
162 if (!stringformat.contains(
"yy"))
163 stringformat.append(
" yyyy");
168 if (!stringformat.contains(
"yy")
169 && date.year() != QDateTime::currentDateTime().date().year())
170 stringformat.append(
" yyyy");
175 QDate now =
current().toLocalTime().date();
176 if ((format &
kSimplify) && (now == date))
177 result = QCoreApplication::translate(
"(Common)",
"Today");
178 else if (((format &
kSimplify) != 0U) && (now.addDays(-1) == date))
179 result = QCoreApplication::translate(
"(Common)",
"Yesterday");
180 else if (((format &
kSimplify) != 0U) && (now.addDays(1) == date))
181 result = QCoreApplication::translate(
"(Common)",
"Tomorrow");
184 if (result.isEmpty())
200 std::chrono::seconds nSecs = std::chrono::hours(time.hour());
201 nSecs += std::chrono::minutes(time.minute());
202 nSecs += std::chrono::seconds(time.second());
209 return std::chrono::milliseconds(QDateTime::currentMSecsSinceEpoch());
242 QString
formatTime(std::chrono::milliseconds msecs, QString fmt)
244 static const QRegularExpression hRe(
"H+");
245 static const QRegularExpression mRe(
"m+");
246 static const QRegularExpression sRe(
"s+");
247 static const QRegularExpression zRe(
"z+");
249 bool negativeTime = msecs < 0ms;
250 msecs = std::chrono::milliseconds(std::abs(msecs.count()));
252 QRegularExpressionMatch match = hRe.match(fmt);
253 if (match.hasMatch())
255 int width = match.capturedLength();
257 fmt.replace(match.capturedStart(), width, text);
261 match = mRe.match(fmt);
262 if (match.hasMatch())
264 int width = match.capturedLength();
266 fmt.replace(match.capturedStart(), width, text);
267 msecs = msecs % 1min;
270 match = sRe.match(fmt);
271 if (match.hasMatch())
273 int width = match.capturedLength();
275 fmt.replace(match.capturedStart(), width, text);
278 match = zRe.match(fmt);
279 if (match.hasMatch())
281 static constexpr std::array<int,4> divisor = {1000, 100, 10, 1};
282 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
283 int width = std::min(3, match.capturedLength());
285 int width = std::min(3LL, match.capturedLength());
287 int value = (msecs % 1s).count() / divisor[width];
289 fmt.replace(match.capturedStart(), match.capturedLength(), text);