4 #include <QCoreApplication>
5 #include <QRegularExpression>
16 QDateTime rettime = QDateTime::currentDateTimeUtc();
18 rettime = rettime.addMSecs(-rettime.time().msec());
27 QDateTime
as_utc(
const QDateTime &old_dt)
30 dt.setTimeSpec(Qt::UTC);
40 if (!dtstr.contains(
"-") && dtstr.length() == 14)
56 dt.setTimeSpec(Qt::UTC);
88 if (!raw_dt.isValid())
99 (format &
kOverrideUTC) ? raw_dt.toUTC() : raw_dt.toLocalTime();
102 return datetime.toString(
"yyyy-MM-dd hh:mm:ss");
108 return datetime.toUTC().toString(
"ddd, dd MMM yyyy hh:mm:ss").append(
" GMT");
111 return datetime.toString(
"yyyyMMddhhmmss");
114 return datetime.toString(
"yyyy-MM-ddThh-mm-ss.zzz");
117 result +=
toString(datetime.date(), format);
121 if (!result.isEmpty())
125 result += datetime.time().toString(
timeformat);
145 QString stringformat;
153 if (!stringformat.contains(
"yy"))
154 stringformat.append(
" yyyy");
159 if (!stringformat.contains(
"yy")
160 && date.year() != QDateTime::currentDateTime().date().year())
161 stringformat.append(
" yyyy");
166 QDate now =
current().toLocalTime().date();
167 if ((format &
kSimplify) && (now == date))
168 result = QCoreApplication::translate(
"(Common)",
"Today");
169 else if (((format &
kSimplify) != 0U) && (now.addDays(-1) == date))
170 result = QCoreApplication::translate(
"(Common)",
"Yesterday");
171 else if (((format &
kSimplify) != 0U) && (now.addDays(1) == date))
172 result = QCoreApplication::translate(
"(Common)",
"Tomorrow");
175 if (result.isEmpty())
191 std::chrono::seconds nSecs = std::chrono::hours(time.hour());
192 nSecs += std::chrono::minutes(time.minute());
193 nSecs += std::chrono::seconds(time.second());
200 return std::chrono::milliseconds(QDateTime::currentMSecsSinceEpoch());
233 QString
formatTime(std::chrono::milliseconds msecs, QString fmt)
235 static const QRegularExpression hRe(
"H+");
236 static const QRegularExpression mRe(
"m+");
237 static const QRegularExpression sRe(
"s+");
238 static const QRegularExpression zRe(
"z+");
240 bool negativeTime = msecs < 0ms;
241 msecs = std::chrono::milliseconds(std::abs(msecs.count()));
243 QRegularExpressionMatch match = hRe.match(fmt);
244 if (match.hasMatch())
246 int width = match.capturedLength();
248 fmt.replace(match.capturedStart(), width, text);
252 match = mRe.match(fmt);
253 if (match.hasMatch())
255 int width = match.capturedLength();
257 fmt.replace(match.capturedStart(), width, text);
258 msecs = msecs % 1min;
261 match = sRe.match(fmt);
262 if (match.hasMatch())
264 int width = match.capturedLength();
266 fmt.replace(match.capturedStart(), width, text);
269 match = zRe.match(fmt);
270 if (match.hasMatch())
272 static constexpr std::array<int,4> divisor = {1000, 100, 10, 1};
273 int width = std::min(3,
static_cast<int>(match.capturedLength()));
274 int value = (msecs % 1s).count() / divisor[width];
276 fmt.replace(match.capturedStart(), match.capturedLength(), text);