Consolidate some Q(Date|Time)+ methods to call calendared versions
The string-returning methods were in any case delegating to either a local static or QLocale methods that delegate to their calendar variants, so do the default-calendar step early and reduce the number of distinct code-paths, along with the gross number of lines of code. In the process, short-cut past QDate::toString() when we can save its switch and go direct to the toString{Text|Iso}Date() it's calling. Tidy up somewhat in the process. Change-Id: I8ba70b29ef9e8b6553c41310ebb2b63ec5570bb9 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
d6d98f782a
commit
d6ba2ae441
@ -1114,11 +1114,6 @@ static QString toStringTextDate(QDate date, QCalendar cal)
|
|||||||
}
|
}
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static QString toStringTextDate(QDate date)
|
|
||||||
{
|
|
||||||
return toStringTextDate(date, QCalendar());
|
|
||||||
}
|
|
||||||
#endif // textdate
|
#endif // textdate
|
||||||
|
|
||||||
static QString toStringIsoDate(const QDate &date)
|
static QString toStringIsoDate(const QDate &date)
|
||||||
@ -1174,6 +1169,11 @@ static QString toStringIsoDate(const QDate &date)
|
|||||||
\sa fromString(), QLocale::toString()
|
\sa fromString(), QLocale::toString()
|
||||||
*/
|
*/
|
||||||
QString QDate::toString(Qt::DateFormat format) const
|
QString QDate::toString(Qt::DateFormat format) const
|
||||||
|
{
|
||||||
|
return toString(format, QCalendar());
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QDate::toString(Qt::DateFormat format, QCalendar cal) const
|
||||||
{
|
{
|
||||||
if (!isValid())
|
if (!isValid())
|
||||||
return QString();
|
return QString();
|
||||||
@ -1182,24 +1182,25 @@ QString QDate::toString(Qt::DateFormat format) const
|
|||||||
#if QT_DEPRECATED_SINCE(5, 15)
|
#if QT_DEPRECATED_SINCE(5, 15)
|
||||||
case Qt::SystemLocaleDate:
|
case Qt::SystemLocaleDate:
|
||||||
case Qt::SystemLocaleShortDate:
|
case Qt::SystemLocaleShortDate:
|
||||||
return QLocale::system().toString(*this, QLocale::ShortFormat);
|
return QLocale::system().toString(*this, QLocale::ShortFormat, cal);
|
||||||
case Qt::SystemLocaleLongDate:
|
case Qt::SystemLocaleLongDate:
|
||||||
return QLocale::system().toString(*this, QLocale::LongFormat);
|
return QLocale::system().toString(*this, QLocale::LongFormat, cal);
|
||||||
case Qt::LocaleDate:
|
case Qt::LocaleDate:
|
||||||
case Qt::DefaultLocaleShortDate:
|
case Qt::DefaultLocaleShortDate:
|
||||||
return QLocale().toString(*this, QLocale::ShortFormat);
|
return QLocale().toString(*this, QLocale::ShortFormat, cal);
|
||||||
case Qt::DefaultLocaleLongDate:
|
case Qt::DefaultLocaleLongDate:
|
||||||
return QLocale().toString(*this, QLocale::LongFormat);
|
return QLocale().toString(*this, QLocale::LongFormat, cal);
|
||||||
#endif // 5.15
|
#endif // 5.15
|
||||||
case Qt::RFC2822Date:
|
case Qt::RFC2822Date:
|
||||||
return QLocale::c().toString(*this, u"dd MMM yyyy");
|
return QLocale::c().toString(*this, QStringView(u"dd MMM yyyy"), cal);
|
||||||
default:
|
default:
|
||||||
#if QT_CONFIG(textdate)
|
#ifndef QT_NO_TEXTDATE
|
||||||
case Qt::TextDate:
|
case Qt::TextDate:
|
||||||
return toStringTextDate(*this);
|
return toStringTextDate(*this, cal);
|
||||||
#endif
|
#endif
|
||||||
case Qt::ISODate:
|
case Qt::ISODate:
|
||||||
case Qt::ISODateWithMs:
|
case Qt::ISODateWithMs:
|
||||||
|
// No calendar dependence
|
||||||
return toStringIsoDate(*this);
|
return toStringIsoDate(*this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1267,45 +1268,7 @@ QString QDate::toString(Qt::DateFormat format) const
|
|||||||
*/
|
*/
|
||||||
QString QDate::toString(QStringView format) const
|
QString QDate::toString(QStringView format) const
|
||||||
{
|
{
|
||||||
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
return toString(format, QCalendar());
|
||||||
}
|
|
||||||
|
|
||||||
#if QT_STRINGVIEW_LEVEL < 2
|
|
||||||
QString QDate::toString(const QString &format) const
|
|
||||||
{
|
|
||||||
return toString(qToStringViewIgnoringNull(format));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QString QDate::toString(Qt::DateFormat format, QCalendar cal) const
|
|
||||||
{
|
|
||||||
if (!isValid())
|
|
||||||
return QString();
|
|
||||||
|
|
||||||
switch (format) {
|
|
||||||
#if QT_DEPRECATED_SINCE(5, 15)
|
|
||||||
case Qt::SystemLocaleDate:
|
|
||||||
case Qt::SystemLocaleShortDate:
|
|
||||||
return QLocale::system().toString(*this, QLocale::ShortFormat, cal);
|
|
||||||
case Qt::SystemLocaleLongDate:
|
|
||||||
return QLocale::system().toString(*this, QLocale::LongFormat, cal);
|
|
||||||
case Qt::LocaleDate:
|
|
||||||
case Qt::DefaultLocaleShortDate:
|
|
||||||
return QLocale().toString(*this, QLocale::ShortFormat, cal);
|
|
||||||
case Qt::DefaultLocaleLongDate:
|
|
||||||
return QLocale().toString(*this, QLocale::LongFormat, cal);
|
|
||||||
#endif // 5.15
|
|
||||||
case Qt::RFC2822Date:
|
|
||||||
return QLocale::c().toString(*this, QStringView(u"dd MMM yyyy"), cal);
|
|
||||||
default:
|
|
||||||
#ifndef QT_NO_TEXTDATE
|
|
||||||
case Qt::TextDate:
|
|
||||||
return toStringTextDate(*this, cal);
|
|
||||||
#endif
|
|
||||||
case Qt::ISODate:
|
|
||||||
case Qt::ISODateWithMs:
|
|
||||||
return toStringIsoDate(*this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QDate::toString(QStringView format, QCalendar cal) const
|
QString QDate::toString(QStringView format, QCalendar cal) const
|
||||||
@ -1314,6 +1277,11 @@ QString QDate::toString(QStringView format, QCalendar cal) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if QT_STRINGVIEW_LEVEL < 2
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
|
QString QDate::toString(const QString &format) const
|
||||||
|
{
|
||||||
|
return toString(qToStringViewIgnoringNull(format), QCalendar());
|
||||||
|
}
|
||||||
|
|
||||||
QString QDate::toString(const QString &format, QCalendar cal) const
|
QString QDate::toString(const QString &format, QCalendar cal) const
|
||||||
{
|
{
|
||||||
return toString(qToStringViewIgnoringNull(format), cal);
|
return toString(qToStringViewIgnoringNull(format), cal);
|
||||||
@ -4398,7 +4366,7 @@ QString QDateTime::toString(Qt::DateFormat format, QCalendar cal) const
|
|||||||
#if QT_CONFIG(textdate)
|
#if QT_CONFIG(textdate)
|
||||||
case Qt::TextDate: {
|
case Qt::TextDate: {
|
||||||
const QPair<QDate, QTime> p = getDateTime(d);
|
const QPair<QDate, QTime> p = getDateTime(d);
|
||||||
buf = p.first.toString(Qt::TextDate, cal);
|
buf = toStringTextDate(p.first, cal);
|
||||||
// Insert time between date's day and year:
|
// Insert time between date's day and year:
|
||||||
buf.insert(buf.lastIndexOf(QLatin1Char(' ')),
|
buf.insert(buf.lastIndexOf(QLatin1Char(' ')),
|
||||||
QLatin1Char(' ') + p.second.toString(Qt::TextDate));
|
QLatin1Char(' ') + p.second.toString(Qt::TextDate));
|
||||||
@ -4423,13 +4391,10 @@ QString QDateTime::toString(Qt::DateFormat format, QCalendar cal) const
|
|||||||
case Qt::ISODateWithMs: {
|
case Qt::ISODateWithMs: {
|
||||||
// No calendar dependence
|
// No calendar dependence
|
||||||
const QPair<QDate, QTime> p = getDateTime(d);
|
const QPair<QDate, QTime> p = getDateTime(d);
|
||||||
const QDate &dt = p.first;
|
buf = toStringIsoDate(p.first);
|
||||||
const QTime &tm = p.second;
|
|
||||||
buf = dt.toString(Qt::ISODate);
|
|
||||||
if (buf.isEmpty())
|
if (buf.isEmpty())
|
||||||
return QString(); // failed to convert
|
return QString(); // failed to convert
|
||||||
buf += QLatin1Char('T');
|
buf += QLatin1Char('T') + p.second.toString(format);
|
||||||
buf += tm.toString(format);
|
|
||||||
switch (getSpec(d)) {
|
switch (getSpec(d)) {
|
||||||
case Qt::UTC:
|
case Qt::UTC:
|
||||||
buf += QLatin1Char('Z');
|
buf += QLatin1Char('Z');
|
||||||
@ -4489,7 +4454,7 @@ QString QDateTime::toString(Qt::DateFormat format, QCalendar cal) const
|
|||||||
*/
|
*/
|
||||||
QString QDateTime::toString(QStringView format) const
|
QString QDateTime::toString(QStringView format) const
|
||||||
{
|
{
|
||||||
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
return toString(format, QCalendar());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QDateTime::toString(QStringView format, QCalendar cal) const
|
QString QDateTime::toString(QStringView format, QCalendar cal) const
|
||||||
@ -4500,7 +4465,7 @@ QString QDateTime::toString(QStringView format, QCalendar cal) const
|
|||||||
#if QT_STRINGVIEW_LEVEL < 2
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
QString QDateTime::toString(const QString &format) const
|
QString QDateTime::toString(const QString &format) const
|
||||||
{
|
{
|
||||||
return toString(qToStringViewIgnoringNull(format));
|
return toString(qToStringViewIgnoringNull(format), QCalendar());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QDateTime::toString(const QString &format, QCalendar cal) const
|
QString QDateTime::toString(const QString &format, QCalendar cal) const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user