QDate/Time: add toString(QStringView) overloads
[ChangeLog][QtCore][QDate/QTime/QDateTime] Added toString() overloads taking the format as a QStringView. Change-Id: I322fa22e6b13fe8ba4badf0a3133425bd067ef32 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>bb10
parent
d68c162c1b
commit
58a4f41af2
|
|
@ -70,6 +70,13 @@
|
|||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
static inline QStringView quick_stringview_cast(const QString &format)
|
||||
{
|
||||
return QStringView(format.data(), format.size()); // avoids isNull() check as we don't care
|
||||
}
|
||||
#endif
|
||||
|
||||
/*****************************************************************************
|
||||
Date/Time Constants
|
||||
*****************************************************************************/
|
||||
|
|
@ -879,6 +886,9 @@ QString QDate::toString(Qt::DateFormat format) const
|
|||
}
|
||||
|
||||
/*!
|
||||
\fn QString QDate::toString(const QString &format) const
|
||||
\fn QString QDate::toString(QStringView format) const
|
||||
|
||||
Returns the date as a string. The \a format parameter determines
|
||||
the format of the result string.
|
||||
|
||||
|
|
@ -927,10 +937,18 @@ QString QDate::toString(Qt::DateFormat format) const
|
|||
\sa fromString(), QDateTime::toString(), QTime::toString(), QLocale::toString()
|
||||
|
||||
*/
|
||||
QString QDate::toString(const QString& format) const
|
||||
QString QDate::toString(QStringView format) const
|
||||
{
|
||||
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
||||
}
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
QString QDate::toString(const QString &format) const
|
||||
{
|
||||
return toString(quick_stringview_cast(format));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //QT_NO_DATESTRING
|
||||
|
||||
/*!
|
||||
|
|
@ -1625,6 +1643,9 @@ QString QTime::toString(Qt::DateFormat format) const
|
|||
}
|
||||
|
||||
/*!
|
||||
\fn QString QTime::toString(const QString &format) const
|
||||
\fn QString QTime::toString(QStringView format) const
|
||||
|
||||
Returns the time as a string. The \a format parameter determines
|
||||
the format of the result string.
|
||||
|
||||
|
|
@ -1675,11 +1696,20 @@ QString QTime::toString(Qt::DateFormat format) const
|
|||
|
||||
\sa fromString(), QDate::toString(), QDateTime::toString(), QLocale::toString()
|
||||
*/
|
||||
QString QTime::toString(const QString& format) const
|
||||
QString QTime::toString(QStringView format) const
|
||||
{
|
||||
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
||||
}
|
||||
|
||||
#if QT_STRINGVIEW_VERSION < 2
|
||||
QString QTime::toString(const QString &format) const
|
||||
{
|
||||
return toString(quick_stringview_cast(format));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //QT_NO_DATESTRING
|
||||
|
||||
/*!
|
||||
Sets the time to hour \a h, minute \a m, seconds \a s and
|
||||
milliseconds \a ms.
|
||||
|
|
@ -3853,6 +3883,9 @@ QString QDateTime::toString(Qt::DateFormat format) const
|
|||
}
|
||||
|
||||
/*!
|
||||
\fn QString QDateTime::toString(const QString &format) const
|
||||
\fn QString QDateTime::toString(QStringView format) const
|
||||
|
||||
Returns the datetime as a string. The \a format parameter
|
||||
determines the format of the result string.
|
||||
|
||||
|
|
@ -3925,10 +3958,18 @@ QString QDateTime::toString(Qt::DateFormat format) const
|
|||
|
||||
\sa fromString(), QDate::toString(), QTime::toString(), QLocale::toString()
|
||||
*/
|
||||
QString QDateTime::toString(const QString& format) const
|
||||
QString QDateTime::toString(QStringView format) const
|
||||
{
|
||||
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
||||
}
|
||||
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
QString QDateTime::toString(const QString &format) const
|
||||
{
|
||||
return toString(quick_stringview_cast(format));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //QT_NO_DATESTRING
|
||||
|
||||
static inline void massageAdjustedDateTime(const QDateTimeData &d, QDate *date, QTime *time)
|
||||
|
|
@ -5264,7 +5305,7 @@ QDebug operator<<(QDebug dbg, const QDate &date)
|
|||
QDebug operator<<(QDebug dbg, const QTime &time)
|
||||
{
|
||||
QDebugStateSaver saver(dbg);
|
||||
dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')';
|
||||
dbg.nospace() << "QTime(" << time.toString(QStringViewLiteral("HH:mm:ss.zzz")) << ')';
|
||||
return dbg;
|
||||
}
|
||||
|
||||
|
|
@ -5273,7 +5314,7 @@ QDebug operator<<(QDebug dbg, const QDateTime &date)
|
|||
QDebugStateSaver saver(dbg);
|
||||
const Qt::TimeSpec ts = date.timeSpec();
|
||||
dbg.nospace() << "QDateTime(";
|
||||
dbg.noquote() << date.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t"))
|
||||
dbg.noquote() << date.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t"))
|
||||
<< ' ' << ts;
|
||||
switch (ts) {
|
||||
case Qt::UTC:
|
||||
|
|
|
|||
|
|
@ -89,7 +89,10 @@ public:
|
|||
#endif // QT_NO_TEXTDATE
|
||||
#ifndef QT_NO_DATESTRING
|
||||
QString toString(Qt::DateFormat f = Qt::TextDate) const;
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
QString toString(const QString &format) const;
|
||||
#endif
|
||||
QString toString(QStringView format) const;
|
||||
#endif
|
||||
#if QT_DEPRECATED_SINCE(5,0)
|
||||
QT_DEPRECATED inline bool setYMD(int y, int m, int d)
|
||||
|
|
@ -162,7 +165,10 @@ public:
|
|||
int msec() const;
|
||||
#ifndef QT_NO_DATESTRING
|
||||
QString toString(Qt::DateFormat f = Qt::TextDate) const;
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
QString toString(const QString &format) const;
|
||||
#endif
|
||||
QString toString(QStringView format) const;
|
||||
#endif
|
||||
bool setHMS(int h, int m, int s, int ms = 0);
|
||||
|
||||
|
|
@ -295,7 +301,10 @@ public:
|
|||
|
||||
#ifndef QT_NO_DATESTRING
|
||||
QString toString(Qt::DateFormat f = Qt::TextDate) const;
|
||||
#if QT_STRINGVIEW_LEVEL < 2
|
||||
QString toString(const QString &format) const;
|
||||
#endif
|
||||
QString toString(QStringView format) const;
|
||||
#endif
|
||||
QDateTime addDays(qint64 days) const Q_REQUIRED_RESULT;
|
||||
QDateTime addMonths(int months) const Q_REQUIRED_RESULT;
|
||||
|
|
|
|||
|
|
@ -479,14 +479,14 @@ bool QSQLiteResult::exec()
|
|||
break;
|
||||
case QVariant::DateTime: {
|
||||
const QDateTime dateTime = value.toDateTime();
|
||||
const QString str = dateTime.toString(QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz"));
|
||||
const QString str = dateTime.toString(QStringViewLiteral("yyyy-MM-ddThh:mm:ss.zzz"));
|
||||
res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(),
|
||||
str.size() * sizeof(ushort), SQLITE_TRANSIENT);
|
||||
break;
|
||||
}
|
||||
case QVariant::Time: {
|
||||
const QTime time = value.toTime();
|
||||
const QString str = time.toString(QStringLiteral("hh:mm:ss.zzz"));
|
||||
const QString str = time.toString(QStringViewLiteral("hh:mm:ss.zzz"));
|
||||
res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(),
|
||||
str.size() * sizeof(ushort), SQLITE_TRANSIENT);
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ static inline QString jobHoldToString(const QCUPSSupport::JobHoldUntil jobHold,
|
|||
if (holdUntilTime < localDateTime.time())
|
||||
localDateTime = localDateTime.addDays(1);
|
||||
localDateTime.setTime(holdUntilTime);
|
||||
return localDateTime.toUTC().time().toString(QStringLiteral("HH:mm"));
|
||||
return localDateTime.toUTC().time().toString(QStringViewLiteral("HH:mm"));
|
||||
}
|
||||
// else fall through:
|
||||
Q_FALLTHROUGH();
|
||||
|
|
|
|||
|
|
@ -83,21 +83,21 @@ template<> inline char *toString(const QByteArray &ba)
|
|||
template<> inline char *toString(const QTime &time)
|
||||
{
|
||||
return time.isValid()
|
||||
? qstrdup(qPrintable(time.toString(QLatin1String("hh:mm:ss.zzz"))))
|
||||
? qstrdup(qPrintable(time.toString(QStringViewLiteral("hh:mm:ss.zzz"))))
|
||||
: qstrdup("Invalid QTime");
|
||||
}
|
||||
|
||||
template<> inline char *toString(const QDate &date)
|
||||
{
|
||||
return date.isValid()
|
||||
? qstrdup(qPrintable(date.toString(QLatin1String("yyyy/MM/dd"))))
|
||||
? qstrdup(qPrintable(date.toString(QStringViewLiteral("yyyy/MM/dd"))))
|
||||
: qstrdup("Invalid QDate");
|
||||
}
|
||||
|
||||
template<> inline char *toString(const QDateTime &dateTime)
|
||||
{
|
||||
return dateTime.isValid()
|
||||
? qstrdup(qPrintable(dateTime.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz[t]"))))
|
||||
? qstrdup(qPrintable(dateTime.toString(QStringViewLiteral("yyyy/MM/dd hh:mm:ss.zzz[t]"))))
|
||||
: qstrdup("Invalid QDateTime");
|
||||
}
|
||||
#endif // QT_NO_DATESTRING
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ void tst_QNoDebug::noDebugOutput() const
|
|||
void tst_QNoDebug::streaming() const
|
||||
{
|
||||
QDateTime dt(QDate(1,2,3),QTime(4,5,6));
|
||||
const QByteArray debugString = dt.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")).toLatin1();
|
||||
const QByteArray debugString = dt.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t")).toLatin1();
|
||||
const QByteArray message = "QDateTime(" + debugString + " Qt::TimeSpec(LocalTime))";
|
||||
QTest::ignoreMessage(QtWarningMsg, message.constData());
|
||||
qWarning() << dt;
|
||||
|
|
|
|||
Loading…
Reference in New Issue