summaryrefslogtreecommitdiffstats
path: root/src/corelib/time
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/time')
-rw-r--r--src/corelib/time/qcalendar.cpp11
-rw-r--r--src/corelib/time/qdatetime.cpp289
-rw-r--r--src/corelib/time/qdatetime.h16
-rw-r--r--src/corelib/time/qdatetimeparser.cpp39
-rw-r--r--src/corelib/time/qtimezoneprivate_win.cpp2
5 files changed, 195 insertions, 162 deletions
diff --git a/src/corelib/time/qcalendar.cpp b/src/corelib/time/qcalendar.cpp
index 9d485f181e..32f0a511a3 100644
--- a/src/corelib/time/qcalendar.cpp
+++ b/src/corelib/time/qcalendar.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -750,6 +750,8 @@ int QCalendar::daysInMonth(int month, int year) const
/*!
Returns the number of days in the given \a year.
+
+ Handling of \c Unspecified as \a year is undefined.
*/
int QCalendar::daysInYear(int year) const
{
@@ -758,10 +760,15 @@ int QCalendar::daysInYear(int year) const
/*!
Returns the number of months in the given \a year.
+
+ If \a year is \c Unspecified, returns the maximum number of months in a
+ year.
+
+ \sa maximumMonthsInYear()
*/
int QCalendar::monthsInYear(int year) const
{
- return d ? d->monthsInYear(year) : 0;
+ return d ? year == Unspecified ? d->maximumMonthsInYear() : d->monthsInYear(year) : 0;
}
/*!
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp
index 80751e60a0..773280ad68 100644
--- a/src/corelib/time/qdatetime.cpp
+++ b/src/corelib/time/qdatetime.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -682,7 +682,7 @@ static bool inDateTimeRange(qint64 jd, bool start)
return jd >= minDay && jd < maxDay;
}
-static QDateTime toEarliest(const QDate &day, const QDateTime &form)
+static QDateTime toEarliest(QDate day, const QDateTime &form)
{
const Qt::TimeSpec spec = form.timeSpec();
const int offset = (spec == Qt::OffsetFromUTC) ? form.offsetFromUtc() : 0;
@@ -806,7 +806,7 @@ QDateTime QDate::startOfDay(const QTimeZone &zone) const
}
#endif // timezone
-static QDateTime toLatest(const QDate &day, const QDateTime &form)
+static QDateTime toLatest(QDate day, const QDateTime &form)
{
const Qt::TimeSpec spec = form.timeSpec();
const int offset = (spec == Qt::OffsetFromUTC) ? form.offsetFromUtc() : 0;
@@ -1098,9 +1098,8 @@ QString QDate::longDayName(int weekday, MonthNameType type)
}
#endif // textdate && deprecated
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
-#if QT_CONFIG(textdate)
static QString toStringTextDate(QDate date, QCalendar cal)
{
if (date.isValid()) {
@@ -1115,13 +1114,7 @@ static QString toStringTextDate(QDate date, QCalendar cal)
return QString();
}
-static QString toStringTextDate(QDate date)
-{
- return toStringTextDate(date, QCalendar());
-}
-#endif // textdate
-
-static QString toStringIsoDate(const QDate &date)
+static QString toStringIsoDate(QDate date)
{
const auto parts = QCalendar().partsFromDate(date);
if (parts.isValid() && parts.year >= 0 && parts.year <= 9999)
@@ -1131,11 +1124,13 @@ static QString toStringIsoDate(const QDate &date)
/*!
\fn QString QDate::toString(Qt::DateFormat format) const
+ \fn QString QDate::toString(Qt::DateFormat format, QCalendar cal) const
\overload
- Returns the date as a string. The \a format parameter determines
- the format of the string.
+ Returns the date as a string. The \a format parameter determines the format
+ of the string. If \a cal is supplied, it determines the calendar used to
+ represent the date; it defaults to Gregorian.
If the \a format is Qt::TextDate, the string is formatted in the default
way. The day and month names will be localized names using the system
@@ -1148,18 +1143,16 @@ static QString toStringIsoDate(const QDate &date)
year, MM is the month of the year (between 01 and 12), and dd is
the day of the month between 01 and 31.
- If the \a format is Qt::SystemLocaleShortDate or
- Qt::SystemLocaleLongDate, the string format depends on the locale
- settings of the system. Identical to calling
- QLocale::system().toString(date, QLocale::ShortFormat) or
- QLocale::system().toString(date, QLocale::LongFormat).
-
- If the \a format is Qt::DefaultLocaleShortDate or
- Qt::DefaultLocaleLongDate, the string format depends on the
- default application locale. This is the locale set with
- QLocale::setDefault(), or the system locale if no default locale
- has been set. Identical to calling
- \l {QLocale::toString()}{QLocale().toString(date, QLocale::ShortFormat) } or
+ The \a format options Qt::SystemLocaleDate, Qt::SystemLocaleShortDate and
+ Qt::SystemLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with
+ \l {QLocale::toString()}{QLocale::system().toString(date, QLocale::ShortFormat)} or
+ \l {QLocale::toString()}{QLocale::system().toString(date, QLocale::LongFormat)}.
+
+ The \a format options Qt::LocaleDate, Qt::DefaultLocaleShortDate and
+ Qt::DefaultLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with
+ \l {QLocale::toString()}{QLocale().toString(date, QLocale::ShortFormat)} or
\l {QLocale::toString()}{QLocale().toString(date, QLocale::LongFormat)}.
If the \a format is Qt::RFC2822Date, the string is formatted in
@@ -1169,46 +1162,54 @@ static QString toStringIsoDate(const QDate &date)
If the date is invalid, an empty string will be returned.
\warning The Qt::ISODate format is only valid for years in the
- range 0 to 9999. This restriction may apply to locale-aware
- formats as well, depending on the locale settings.
+ range 0 to 9999.
\sa fromString(), QLocale::toString()
*/
QString QDate::toString(Qt::DateFormat format) const
{
+ return toString(format, QCalendar());
+}
+
+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);
+ return QLocale::system().toString(*this, QLocale::ShortFormat, cal);
case Qt::SystemLocaleLongDate:
- return QLocale::system().toString(*this, QLocale::LongFormat);
+ return QLocale::system().toString(*this, QLocale::LongFormat, cal);
case Qt::LocaleDate:
case Qt::DefaultLocaleShortDate:
- return QLocale().toString(*this, QLocale::ShortFormat);
+ return QLocale().toString(*this, QLocale::ShortFormat, cal);
case Qt::DefaultLocaleLongDate:
- return QLocale().toString(*this, QLocale::LongFormat);
+ return QLocale().toString(*this, QLocale::LongFormat, cal);
+#endif // 5.15
case Qt::RFC2822Date:
- return QLocale::c().toString(*this, u"dd MMM yyyy");
+ return QLocale::c().toString(*this, QStringView(u"dd MMM yyyy"), cal);
default:
-#if QT_CONFIG(textdate)
case Qt::TextDate:
- return toStringTextDate(*this);
-#endif
+ return toStringTextDate(*this, cal);
case Qt::ISODate:
case Qt::ISODateWithMs:
+ // No calendar dependence
return toStringIsoDate(*this);
}
}
/*!
\fn QString QDate::toString(const QString &format) const
+ \fn QString QDate::toString(const QString &format, QCalendar cal) const
\fn QString QDate::toString(QStringView format) const
+ \fn QString QDate::toString(QStringView format, QCalendar cal) const
- Returns the date as a string. The \a format parameter determines
- the format of the result string.
+ Returns the date as a string. The \a format parameter determines the format
+ of the result string. If \cal is supplied, it determines the calendar used
+ to represent the date; it defaults to Gregorian.
These expressions may be used:
@@ -1263,43 +1264,7 @@ QString QDate::toString(Qt::DateFormat 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(qToStringViewIgnoringNull(format));
-}
-#endif
-
-QString QDate::toString(Qt::DateFormat format, QCalendar cal) const
-{
- if (!isValid())
- return QString();
-
- switch (format) {
- 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);
- 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);
- }
+ return toString(format, QCalendar());
}
QString QDate::toString(QStringView format, QCalendar cal) const
@@ -1308,6 +1273,11 @@ QString QDate::toString(QStringView format, QCalendar cal) const
}
#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
{
return toString(qToStringViewIgnoringNull(format), cal);
@@ -1625,7 +1595,7 @@ qint64 QDate::daysTo(const QDate &d) const
\sa QTime::currentTime(), QDateTime::currentDateTime()
*/
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
namespace {
struct ParsedInt { int value = 0; bool ok = false; };
@@ -1654,9 +1624,14 @@ ParsedInt readInt(QStringView text)
\a format given, or an invalid date if the string cannot be
parsed.
- Note for Qt::TextDate: It is recommended that you use the
- English short month names (e.g. "Jan"). Although localized month
- names can also be used, they depend on the user's locale settings.
+ Note for Qt::TextDate: It is recommended that you use the English short
+ month names (e.g. "Jan"). Although localized month names can also be used in
+ Qt 5, they depend on the user's locale settings.
+
+ \note Support for localized dates, including the format options
+ Qt::SystemLocaleDate, Qt::SystemLocaleShortDate, Qt::SystemLocaleLongDate,
+ Qt::LocaleDate, Qt::DefaultLocaleShortDate, and Qt::DefaultLocaleLongDate,
+ shall be removed in Qt 6. Use QLocale::toDate() instead.
\sa toString(), QLocale::toDate()
*/
@@ -1667,6 +1642,7 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format)
return QDate();
switch (format) {
+#if QT_DEPRECATED_SINCE(5, 15)
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
return QLocale::system().toDate(string, QLocale::ShortFormat);
@@ -1677,10 +1653,10 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format)
return QLocale().toDate(string, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toDate(string, QLocale::LongFormat);
+#endif // 5.15
case Qt::RFC2822Date:
return rfcDateImpl(string).date;
default:
-#if QT_CONFIG(textdate)
case Qt::TextDate: {
QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
@@ -1699,7 +1675,6 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format)
return QDate(year, month, day);
}
-#endif // textdate
case Qt::ISODate:
// Semi-strict parsing, must be long enough and have punctuators as separators
if (string.size() >= 10 && string.at(4).isPunct() && string.at(7).isPunct()
@@ -2014,7 +1989,7 @@ int QTime::msec() const
return ds() % 1000;
}
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
/*!
\overload
@@ -2030,18 +2005,15 @@ int QTime::msec() const
date, use the \a format Qt::ISODateWithMs, which corresponds to
HH:mm:ss.zzz.
- If the \a format is Qt::SystemLocaleShortDate or
- Qt::SystemLocaleLongDate, the string format depends on the locale
- settings of the system. Identical to calling
- QLocale::system().toString(time, QLocale::ShortFormat) or
- QLocale::system().toString(time, QLocale::LongFormat).
-
- If the \a format is Qt::DefaultLocaleShortDate or
- Qt::DefaultLocaleLongDate, the string format depends on the
- default application locale. This is the locale set with
- QLocale::setDefault(), or the system locale if no default locale
- has been set. Identical to calling
+ The \a format options Qt::SystemLocaleDate:, Qt::SystemLocaleShortDate and
+ Qt::SystemLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with:
+ \l {QLocale::toString()}{QLocale::system().toString(time, QLocale::ShortFormat)} or
+ \l {QLocale::toString()}{QLocale::system().toString(time, QLocale::LongFormat)}.
+ The \a format options Qt::LocaleDate, Qt::DefaultLocaleShortDate and
+ Qt::DefaultLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with:
\l {QLocale::toString()}{QLocale().toString(time, QLocale::ShortFormat)} or
\l {QLocale::toString()}{QLocale().toString(time, QLocale::LongFormat)}.
@@ -2060,6 +2032,7 @@ QString QTime::toString(Qt::DateFormat format) const
return QString();
switch (format) {
+#if QT_DEPRECATED_SINCE(5, 15)
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
return QLocale::system().toString(*this, QLocale::ShortFormat);
@@ -2070,6 +2043,7 @@ QString QTime::toString(Qt::DateFormat format) const
return QLocale().toString(*this, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toString(*this, QLocale::LongFormat);
+#endif // 5.15
case Qt::ISODateWithMs:
return QString::asprintf("%02d:%02d:%02d.%03d", hour(), minute(), second(), msec());
case Qt::RFC2822Date:
@@ -2347,7 +2321,7 @@ int QTime::msecsTo(const QTime &t) const
\sa QDateTime::currentDateTime(), QDateTime::currentDateTimeUtc()
*/
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *isMidnight24)
{
@@ -2445,6 +2419,12 @@ static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *
fails for the default locale). This should be considered an
implementation detail.
+
+ \note Support for localized dates, including the format options
+ Qt::SystemLocaleDate, Qt::SystemLocaleShortDate, Qt::SystemLocaleLongDate,
+ Qt::LocaleDate, Qt::DefaultLocaleShortDate, and Qt::DefaultLocaleLongDate,
+ shall be removed in Qt 6. Use QLocale::toTime() instead.
+
\sa toString(), QLocale::toTime()
*/
QTime QTime::fromString(const QString &string, Qt::DateFormat format)
@@ -2453,6 +2433,7 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format)
return QTime();
switch (format) {
+#if QT_DEPRECATED_SINCE(5, 15)
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
return QLocale::system().toTime(string, QLocale::ShortFormat);
@@ -2463,6 +2444,7 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format)
return QLocale().toTime(string, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toTime(string, QLocale::LongFormat);
+#endif // 5.15
case Qt::RFC2822Date:
return rfcDateImpl(string).time;
case Qt::ISODate:
@@ -2877,7 +2859,7 @@ static void msecsToTime(qint64 msecs, QDate *date, QTime *time)
}
// Converts a date/time value into msecs
-static qint64 timeToMSecs(const QDate &date, const QTime &time)
+static qint64 timeToMSecs(QDate date, QTime time)
{
return ((date.toJulianDay() - JULIAN_DAY_FOR_EPOCH) * MSECS_PER_DAY)
+ time.msecsSinceStartOfDay();
@@ -3222,7 +3204,7 @@ static void setTimeSpec(QDateTimeData &d, Qt::TimeSpec spec, int offsetSeconds)
}
}
-static void setDateTime(QDateTimeData &d, const QDate &date, const QTime &time)
+static void setDateTime(QDateTimeData &d, QDate date, QTime time)
{
// If the date is valid and the time is not we set time to 00:00:00
QTime useTime = time;
@@ -3631,15 +3613,18 @@ QDateTime::QDateTime() noexcept(Data::CanBeSmall)
}
+#if QT_DEPRECATED_SINCE(5, 17) // ### Qt 6: remove
/*!
- Constructs a datetime with the given \a date, a valid
- time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime.
-*/
+ Constructs a datetime with the given \a date, using Qt::LocalTime as the
+ timeSpec() and the time at the start of that date.
+ \sa QDate::startOfDay()
+*/
QDateTime::QDateTime(const QDate &date)
- : d(QDateTimePrivate::create(date, QTime(0, 0), Qt::LocalTime, 0))
+ : QDateTime(date.startOfDay(Qt::LocalTime, 0))
{
}
+#endif
/*!
Constructs a datetime with the given \a date and \a time, using
@@ -4292,13 +4277,16 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
}
#endif
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
/*!
\fn QString QDateTime::toString(Qt::DateFormat format) const
+ \fn QString QDateTime::toString(Qt::DateFormat format, QCalendar cal) const
\overload
- Returns the datetime as a string in the \a format given.
+ Returns the datetime as a string in the \a format given. If \cal is
+ supplied, it determines the calendar used to represent the date; it defaults
+ to Gregorian.
If the \a format is Qt::TextDate, the string is formatted in the default
way. The day and month names will be localized names using the system
@@ -4315,19 +4303,17 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
date, use the \a format Qt::ISODateWithMs, which corresponds to
yyyy-MM-ddTHH:mm:ss.zzz[Z|[+|-]HH:mm].
- If the \a format is Qt::SystemLocaleShortDate or
- Qt::SystemLocaleLongDate, the string format depends on the locale
- settings of the system. Identical to calling
- QLocale::system().toString(datetime, QLocale::ShortFormat) or
- QLocale::system().toString(datetime, QLocale::LongFormat).
+ The \a format options Qt::SystemLocaleDate, Qt::SystemLocaleShortDate and
+ Qt::SystemLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with
+ \l {QLocale::toString()}{QLocale::system().toString(datetime, QLocale::ShortFormat)} or
+ \l {QLocale::toString()}{QLocale::system().toString(datetime, QLocale::LongFormat)}.
- If the \a format is Qt::DefaultLocaleShortDate or
- Qt::DefaultLocaleLongDate, the string format depends on the
- default application locale. This is the locale set with
- QLocale::setDefault(), or the system locale if no default locale
- has been set. Identical to calling QLocale().toString(datetime,
- QLocale::ShortFormat) or QLocale().toString(datetime,
- QLocale::LongFormat).
+ The \a format options Qt::LocaleDate, Qt::DefaultLocaleShortDate and
+ Qt::DefaultLocaleLongDate shall be removed in Qt 6. Their use should be
+ replaced with
+ \l {QLocale::toString()}{QLocale().toString(datetime, QLocale::ShortFormat)} or
+ \l {QLocale::toString()}{QLocale().toString(datetime, QLocale::LongFormat)}.
If the \a format is Qt::RFC2822Date, the string is formatted
following \l{RFC 2822}.
@@ -4335,8 +4321,7 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
If the datetime is invalid, an empty string will be returned.
\warning The Qt::ISODate format is only valid for years in the
- range 0 to 9999. This restriction may apply to locale-aware
- formats as well, depending on the locale settings.
+ range 0 to 9999.
\sa fromString(), QDate::toString(), QTime::toString(),
QLocale::toString()
@@ -4344,31 +4329,37 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
QString QDateTime::toString(Qt::DateFormat format) const
{
+ return toString(format, QCalendar());
+}
+
+QString QDateTime::toString(Qt::DateFormat format, QCalendar cal) const
+{
QString buf;
if (!isValid())
return buf;
switch (format) {
+#if QT_DEPRECATED_SINCE(5, 15)
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
- return QLocale::system().toString(*this, QLocale::ShortFormat);
+ return QLocale::system().toString(*this, QLocale::ShortFormat, cal);
case Qt::SystemLocaleLongDate:
- return QLocale::system().toString(*this, QLocale::LongFormat);
+ return QLocale::system().toString(*this, QLocale::LongFormat, cal);
case Qt::LocaleDate:
case Qt::DefaultLocaleShortDate:
- return QLocale().toString(*this, QLocale::ShortFormat);
+ return QLocale().toString(*this, QLocale::ShortFormat, cal);
case Qt::DefaultLocaleLongDate:
- return QLocale().toString(*this, QLocale::LongFormat);
+ return QLocale().toString(*this, QLocale::LongFormat, cal);
+#endif // 5.15
case Qt::RFC2822Date: {
- buf = QLocale::c().toString(*this, u"dd MMM yyyy hh:mm:ss ");
+ buf = QLocale::c().toString(*this, u"dd MMM yyyy hh:mm:ss ", cal);
buf += toOffsetString(Qt::TextDate, offsetFromUtc());
return buf;
}
default:
-#if QT_CONFIG(textdate)
case Qt::TextDate: {
const QPair<QDate, QTime> p = getDateTime(d);
- buf = p.first.toString(Qt::TextDate);
+ buf = toStringTextDate(p.first, cal);
// Insert time between date's day and year:
buf.insert(buf.lastIndexOf(QLatin1Char(' ')),
QLatin1Char(' ') + p.second.toString(Qt::TextDate));
@@ -4376,11 +4367,11 @@ QString QDateTime::toString(Qt::DateFormat format) const
switch (timeSpec()) {
case Qt::LocalTime:
break;
-# if QT_CONFIG(timezone)
+#if QT_CONFIG(timezone)
case Qt::TimeZone:
buf += QLatin1Char(' ') + d->m_timeZone.abbreviation(*this);
break;
-# endif
+#endif
default:
buf += QLatin1String(" GMT");
if (getSpec(d) == Qt::OffsetFromUTC)
@@ -4388,17 +4379,14 @@ QString QDateTime::toString(Qt::DateFormat format) const
}
return buf;
}
-#endif
case Qt::ISODate:
case Qt::ISODateWithMs: {
+ // No calendar dependence
const QPair<QDate, QTime> p = getDateTime(d);
- const QDate &dt = p.first;
- const QTime &tm = p.second;
- buf = dt.toString(Qt::ISODate);
+ buf = toStringIsoDate(p.first);
if (buf.isEmpty())
return QString(); // failed to convert
- buf += QLatin1Char('T');
- buf += tm.toString(format);
+ buf += QLatin1Char('T') + p.second.toString(format);
switch (getSpec(d)) {
case Qt::UTC:
buf += QLatin1Char('Z');
@@ -4419,11 +4407,15 @@ QString QDateTime::toString(Qt::DateFormat format) const
/*!
\fn QString QDateTime::toString(const QString &format) const
+ \fn QString QDateTime::toString(const QString &format, QCalendar cal) const
\fn QString QDateTime::toString(QStringView format) const
+ \fn QString QDateTime::toString(QStringView format, QCalendar cal) const
Returns the datetime as a string. The \a format parameter determines the
- format of the result string. See QTime::toString() and QDate::toString() for
- the supported specifiers for time and date, respectively.
+ format of the result string. If \cal is supplied, it determines the calendar
+ used to represent the date; it defaults to Gregorian. See QTime::toString()
+ and QDate::toString() for the supported specifiers for time and date,
+ respectively.
Any sequence of characters enclosed in single quotes will be included
verbatim in the output string (stripped of the quotes), even if it contains
@@ -4454,13 +4446,23 @@ QString QDateTime::toString(Qt::DateFormat 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
+{
+ return QLocale::system().toString(*this, format, cal); // QLocale::c() ### Qt6
}
#if QT_STRINGVIEW_LEVEL < 2
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
+{
+ return toString(qToStringViewIgnoringNull(format), cal);
}
#endif
@@ -5202,15 +5204,20 @@ int QDateTime::utcOffset() const
}
#endif // QT_DEPRECATED_SINCE
-#if QT_CONFIG(datestring)
+#if QT_CONFIG(datestring) // depends on, so implies, textdate
/*!
Returns the QDateTime represented by the \a string, using the
\a format given, or an invalid datetime if this is not possible.
- Note for Qt::TextDate: It is recommended that you use the
- English short month names (e.g. "Jan"). Although localized month
- names can also be used, they depend on the user's locale settings.
+ Note for Qt::TextDate: It is recommended that you use the English short
+ month names (e.g. "Jan"). Although localized month names can also be used in
+ Qt 5, they depend on the user's locale settings.
+
+ \note Support for localized dates, including the format options
+ Qt::SystemLocaleDate, Qt::SystemLocaleShortDate, Qt::SystemLocaleLongDate,
+ Qt::LocaleDate, Qt::DefaultLocaleShortDate, and Qt::DefaultLocaleLongDate,
+ shall be removed in Qt 6. Use QLocale::toDateTime() instead.
\sa toString(), QLocale::toDateTime()
*/
@@ -5220,6 +5227,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
return QDateTime();
switch (format) {
+#if QT_DEPRECATED_SINCE(5, 15)
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
return QLocale::system().toDateTime(string, QLocale::ShortFormat);
@@ -5230,6 +5238,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
return QLocale().toDateTime(string, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toDateTime(string, QLocale::LongFormat);
+#endif // 5.15
case Qt::RFC2822Date: {
const ParsedRfcDateTime rfc = rfcDateImpl(string);
@@ -5306,7 +5315,6 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
date = date.addDays(1);
return QDateTime(date, time, spec, offset);
}
-#if QT_CONFIG(textdate)
case Qt::TextDate: {
QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
@@ -5412,7 +5420,6 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
return QDateTime(date, time, Qt::UTC);
}
}
-#endif // textdate
}
return QDateTime();
diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h
index 3eae8ebf64..c1653b5585 100644
--- a/src/corelib/time/qdatetime.h
+++ b/src/corelib/time/qdatetime.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -110,14 +110,15 @@ public:
static QString longDayName(int weekday, MonthNameType type = DateFormat);
#endif // textdate && deprecated
#if QT_CONFIG(datestring)
- QString toString(Qt::DateFormat f = Qt::TextDate) const;
+ QString toString(Qt::DateFormat format = Qt::TextDate) const;
+ QString toString(Qt::DateFormat format, QCalendar cal) const;
+
#if QT_STRINGVIEW_LEVEL < 2
QString toString(const QString &format) const;
QString toString(const QString &format, QCalendar cal) const;
#endif
QString toString(QStringView format) const;
- QString toString(Qt::DateFormat f, QCalendar cal) const;
QString toString(QStringView format, QCalendar cal) const;
#endif
#if QT_DEPRECATED_SINCE(5,0)
@@ -287,7 +288,9 @@ class Q_CORE_EXPORT QDateTime
public:
QDateTime() noexcept(Data::CanBeSmall);
- explicit QDateTime(const QDate &); // ### Qt 6: plain QDate, QTime
+#if QT_DEPRECATED_SINCE(5, 15) // ### Qt 6: remove
+ QT_DEPRECATED_X("Use QDate::startOfDay()") explicit QDateTime(const QDate &);
+#endif
QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
// ### Qt 6: Merge with above with default offsetSeconds = 0
QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds);
@@ -330,11 +333,14 @@ public:
void setSecsSinceEpoch(qint64 secs);
#if QT_CONFIG(datestring)
- QString toString(Qt::DateFormat f = Qt::TextDate) const;
+ QString toString(Qt::DateFormat format = Qt::TextDate) const;
+ QString toString(Qt::DateFormat format, QCalendar cal) const;
#if QT_STRINGVIEW_LEVEL < 2
QString toString(const QString &format) const;
+ QString toString(const QString &format, QCalendar cal) const;
#endif
QString toString(QStringView format) const;
+ QString toString(QStringView format, QCalendar cal) const;
#endif
Q_REQUIRED_RESULT QDateTime addDays(qint64 days) const;
Q_REQUIRED_RESULT QDateTime addMonths(int months) const;
diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp
index 70460ae632..790c20004a 100644
--- a/src/corelib/time/qdatetimeparser.cpp
+++ b/src/corelib/time/qdatetimeparser.cpp
@@ -219,7 +219,8 @@ int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const
const SectionNode &sn = sectionNode(s);
switch (sn.type) {
#if QT_CONFIG(timezone)
- case TimeZoneSection: return QTimeZone::MaxUtcOffsetSecs;
+ case TimeZoneSection:
+ return QTimeZone::MaxUtcOffsetSecs;
#endif
case Hour24Section:
case Hour12Section:
@@ -227,20 +228,25 @@ int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const
// We want it to be 23 for the stepBy case.
return 23;
case MinuteSection:
- case SecondSection: return 59;
- case MSecSection: return 999;
+ case SecondSection:
+ return 59;
+ case MSecSection:
+ return 999;
case YearSection2Digits:
case YearSection:
// sectionMaxSize will prevent people from typing in a larger number in
// count == 2 sections; stepBy() will work on real years anyway.
return 9999;
- case MonthSection: return calendar.maximumMonthsInYear();
+ case MonthSection:
+ return calendar.maximumMonthsInYear();
case DaySection:
case DayOfWeekSectionShort:
case DayOfWeekSectionLong:
return cur.isValid() ? cur.date().daysInMonth(calendar) : calendar.maximumDaysInMonth();
- case AmPmSection: return 1;
- default: break;
+ case AmPmSection:
+ return 1;
+ default:
+ break;
}
qWarning("QDateTimeParser::absoluteMax() Internal error (%ls)",
qUtf16Printable(sn.name()));
@@ -620,7 +626,8 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const
switch (s) {
case FirstSection:
case NoSection:
- case LastSection: return 0;
+ case LastSection:
+ return 0;
case AmPmSection: {
const int lowerMax = qMax(getAmPmText(AmText, LowerCase).size(),
@@ -634,7 +641,9 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const
case Hour12Section:
case MinuteSection:
case SecondSection:
- case DaySection: return 2;
+ case DaySection:
+ return 2;
+
case DayOfWeekSectionShort:
case DayOfWeekSectionLong:
#if !QT_CONFIG(textdate)
@@ -663,11 +672,15 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const
return ret;
}
#endif
- case MSecSection: return 3;
- case YearSection: return 4;
- case YearSection2Digits: return 2;
+ case MSecSection:
+ return 3;
+ case YearSection:
+ return 4;
+ case YearSection2Digits:
+ return 2;
+ case TimeZoneSection:
// Arbitrarily many tokens (each up to 14 bytes) joined with / separators:
- case TimeZoneSection: return std::numeric_limits<int>::max();
+ return std::numeric_limits<int>::max();
case CalendarPopupSection:
case Internal:
@@ -903,7 +916,7 @@ QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionIndex,
by \a weekDay.
*/
-static int weekDayWithinMonth(const QCalendar &calendar, const QDate &rough, int weekDay)
+static int weekDayWithinMonth(QCalendar calendar, QDate rough, int weekDay)
{
// TODO: can we adapt this to cope gracefully with intercallary days (day of
// week > 7) without making it slower for more widely-used calendars ?
diff --git a/src/corelib/time/qtimezoneprivate_win.cpp b/src/corelib/time/qtimezoneprivate_win.cpp
index 0fec5355b2..0aaf469ed9 100644
--- a/src/corelib/time/qtimezoneprivate_win.cpp
+++ b/src/corelib/time/qtimezoneprivate_win.cpp
@@ -363,7 +363,7 @@ QDate calculateTransitionLocalDate(const SYSTEMTIME &rule, int year)
}
// Converts a date/time value into msecs
-inline qint64 timeToMSecs(const QDate &date, const QTime &time)
+inline qint64 timeToMSecs(QDate date, QTime time)
{
return ((date.toJulianDay() - JULIAN_DAY_FOR_EPOCH) * MSECS_PER_DAY)
+ time.msecsSinceStartOfDay();