4#include <QCoreApplication>
5#include <QRegularExpression>
17 QDateTime rettime = QDateTime::currentDateTimeUtc();
19 rettime = rettime.addMSecs(-rettime.time().msec());
28QDateTime
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());
242QString
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(
static_cast<qsizetype
>(3), match.capturedLength());
287 int value = (msecs % 1s).count() / divisor[width];
289 fmt.replace(match.capturedStart(), match.capturedLength(), text);
QString GetSetting(const QString &key, const QString &defaultval="")
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
QString formatTime(std::chrono::milliseconds msecs, QString fmt)
Format a milliseconds time value.
std::chrono::milliseconds currentMSecsSinceEpochAsDuration(void)
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
std::chrono::seconds secsInPast(const QDateTime &past)
MBASE_PUBLIC QDateTime fromSecsSinceEpoch(int64_t seconds)
This function takes the number of seconds since the start of the epoch and returns a QDateTime with t...
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
@ kOverrideUTC
Present date/time in UTC.
@ kScreenShotFilename
"yyyy-MM-ddThh-mm-ss.zzz"
@ kFilename
Default UTC, "yyyyMMddhhmmss".
@ kRFC822
HTTP Date format.
@ kSimplify
Do Today/Yesterday/Tomorrow transform.
@ kTime
Default local time.
@ kAddYear
Add year to string if not included.
@ kOverrideLocal
Present date/time in localtime.
@ kDateEither
Default local time.
@ kDateShort
Default local time.
@ kDatabase
Default UTC, database format.
@ kAutoYear
Add year only if different from current year.
std::chrono::seconds secsInFuture(const QDateTime &future)
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
std::chrono::seconds toSeconds(QTime time)
Returns the total number of seconds since midnight of the supplied QTime.
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
MBASE_PUBLIC QDateTime fromString(const QString &str, const QString &format)
Converts dy in format to QDateTime.
QString intToPaddedString(int n, int width=2)
Creates a zero padded string representation of an integer.