summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qdatetime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qdatetime.cpp')
-rw-r--r--src/corelib/tools/qdatetime.cpp423
1 files changed, 200 insertions, 223 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 082b721e82..eeefea8137 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -1,7 +1,7 @@
/****************************************************************************
**
-** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
-** Contact: http://www.qt-project.org/legal
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
@@ -10,9 +10,9 @@
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and Digia. For licensing terms and
-** conditions see http://qt.digia.com/licensing. For further information
-** use the contact form at http://qt.digia.com/contact-us.
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
@@ -23,8 +23,8 @@
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
-** In addition, as a special exception, Digia gives you certain additional
-** rights. These rights are described in the Digia Qt LGPL Exception
+** As a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
@@ -117,7 +117,12 @@ static inline qint64 julianDayFromDate(int year, int month, int day)
return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045;
}
-static void getDateFromJulianDay(qint64 julianDay, int *yearp, int *monthp, int *dayp)
+struct ParsedDate
+{
+ int year, month, day;
+};
+
+static ParsedDate getDateFromJulianDay(qint64 julianDay)
{
/*
* Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php
@@ -140,12 +145,8 @@ static void getDateFromJulianDay(qint64 julianDay, int *yearp, int *monthp, int
if (year <= 0)
--year ;
- if (yearp)
- *yearp = year;
- if (monthp)
- *monthp = month;
- if (dayp)
- *dayp = day;
+ const ParsedDate result = { year, month, day };
+ return result;
}
/*****************************************************************************
@@ -155,101 +156,90 @@ static void getDateFromJulianDay(qint64 julianDay, int *yearp, int *monthp, int
static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
#ifndef QT_NO_TEXTDATE
-static const char * const qt_shortMonthNames[] = {
+static const char qt_shortMonthNames[][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
-int qt_monthNumberFromShortName(const QString &shortName)
+static int qt_monthNumberFromShortName(QStringRef shortName)
{
for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) {
- if (shortName == QLatin1String(qt_shortMonthNames[i]))
+ if (shortName == QLatin1String(qt_shortMonthNames[i], 3))
return i + 1;
}
return -1;
}
-#endif
+static int qt_monthNumberFromShortName(const QString &shortName)
+{ return qt_monthNumberFromShortName(QStringRef(&shortName)); }
+
+static int fromShortMonthName(const QStringRef &monthName)
+{
+ // Assume that English monthnames are the default
+ int month = qt_monthNumberFromShortName(monthName);
+ if (month != -1)
+ return month;
+ // If English names can't be found, search the localized ones
+ for (int i = 1; i <= 12; ++i) {
+ if (monthName == QDate::shortMonthName(i))
+ return i;
+ }
+ return -1;
+}
+#endif // QT_NO_TEXTDATE
#ifndef QT_NO_DATESTRING
-static void rfcDateImpl(const QString &s, QDate *dd = 0, QTime *dt = 0, int *utcOffset = 0)
-{
- int day = -1;
- int month = -1;
- int year = -1;
- int hour = -1;
- int min = -1;
- int sec = -1;
- int hourOffset = 0;
- int minOffset = 0;
- bool positiveOffset = false;
+struct ParsedRfcDateTime {
+ QDate date;
+ QTime time;
+ int utcOffset;
+};
+
+static ParsedRfcDateTime rfcDateImpl(const QString &s)
+{
+ ParsedRfcDateTime result;
// Matches "Wdy, DD Mon YYYY HH:mm:ss ±hhmm" (Wdy, being optional)
QRegExp rex(QStringLiteral("^(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
if (s.indexOf(rex) == 0) {
- if (dd) {
- day = rex.cap(1).toInt();
- month = qt_monthNumberFromShortName(rex.cap(2));
- year = rex.cap(3).toInt();
- }
- if (dt) {
- if (!rex.cap(4).isEmpty()) {
- hour = rex.cap(4).toInt();
- min = rex.cap(5).toInt();
- sec = rex.cap(6).toInt();
- }
- positiveOffset = (rex.cap(7) == QLatin1String("+"));
- hourOffset = rex.cap(8).toInt();
- minOffset = rex.cap(9).toInt();
- }
- if (utcOffset)
- *utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
+ const QStringList cap = rex.capturedTexts();
+ result.date = QDate(cap[3].toInt(), qt_monthNumberFromShortName(cap[2]), cap[1].toInt());
+ if (!cap[4].isEmpty())
+ result.time = QTime(cap[4].toInt(), cap[5].toInt(), cap[6].toInt());
+ const bool positiveOffset = (cap[7] == QLatin1String("+"));
+ const int hourOffset = cap[8].toInt();
+ const int minOffset = cap[9].toInt();
+ result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
} else {
// Matches "Wdy Mon DD HH:mm:ss YYYY"
QRegExp rex(QStringLiteral("^[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
if (s.indexOf(rex) == 0) {
- if (dd) {
- month = qt_monthNumberFromShortName(rex.cap(1));
- day = rex.cap(2).toInt();
- year = rex.cap(6).toInt();
- }
- if (dt) {
- if (!rex.cap(3).isEmpty()) {
- hour = rex.cap(3).toInt();
- min = rex.cap(4).toInt();
- sec = rex.cap(5).toInt();
- }
- positiveOffset = (rex.cap(7) == QLatin1String("+"));
- hourOffset = rex.cap(8).toInt();
- minOffset = rex.cap(9).toInt();
- }
- if (utcOffset)
- *utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
+ const QStringList cap = rex.capturedTexts();
+ result.date = QDate(cap[6].toInt(), qt_monthNumberFromShortName(cap[1]), cap[2].toInt());
+ if (!cap[3].isEmpty())
+ result.time = QTime(cap[3].toInt(), cap[4].toInt(), cap[5].toInt());
+ const bool positiveOffset = (cap[7] == QLatin1String("+"));
+ const int hourOffset = cap[8].toInt();
+ const int minOffset = cap[9].toInt();
+ result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
}
}
- if (dd)
- *dd = QDate(year, month, day);
- if (dt)
- *dt = QTime(hour, min, sec);
+ return result;
}
#endif // QT_NO_DATESTRING
// Return offset in [+-]HH:mm format
-// Qt::ISODate puts : between the hours and minutes, but Qt:TextDate does not
static QString toOffsetString(Qt::DateFormat format, int offset)
{
- QString result;
- if (format == Qt::TextDate)
- result = QStringLiteral("%1%2%3");
- else // Qt::ISODate
- result = QStringLiteral("%1%2:%3");
-
- return result.arg(offset >= 0 ? QLatin1Char('+') : QLatin1Char('-'))
- .arg(qAbs(offset) / SECS_PER_HOUR, 2, 10, QLatin1Char('0'))
- .arg((qAbs(offset) / 60) % 60, 2, 10, QLatin1Char('0'));
+ return QString::asprintf("%c%02d%s%02d",
+ offset >= 0 ? '+' : '-',
+ qAbs(offset) / SECS_PER_HOUR,
+ // Qt::ISODate puts : between the hours and minutes, but Qt:TextDate does not:
+ format == Qt::TextDate ? "" : ":",
+ (qAbs(offset) / 60) % 60);
}
// Parse offset in [+-]HH[[:]mm] format
-static int fromOffsetString(const QString &offsetString, bool *valid)
+static int fromOffsetString(const QStringRef &offsetString, bool *valid)
{
*valid = false;
@@ -270,15 +260,15 @@ static int fromOffsetString(const QString &offsetString, bool *valid)
return 0;
// Split the hour and minute parts
- QStringList parts = offsetString.mid(1).split(QLatin1Char(':'));
+ QVector<QStringRef> parts = offsetString.mid(1).split(QLatin1Char(':'));
if (parts.count() == 1) {
// [+-]HHmm or [+-]HH format
- parts.append(parts.at(0).mid(2));
- parts[0] = parts.at(0).left(2);
+ parts.append(parts.first().mid(2));
+ parts[0] = parts.first().left(2);
}
bool ok = false;
- const int hour = parts.at(0).toInt(&ok);
+ const int hour = parts.first().toInt(&ok);
if (!ok)
return 0;
@@ -433,9 +423,7 @@ int QDate::year() const
if (isNull())
return 0;
- int y;
- getDateFromJulianDay(jd, &y, 0, 0);
- return y;
+ return getDateFromJulianDay(jd).year;
}
/*!
@@ -467,9 +455,7 @@ int QDate::month() const
if (isNull())
return 0;
- int m;
- getDateFromJulianDay(jd, 0, &m, 0);
- return m;
+ return getDateFromJulianDay(jd).month;
}
/*!
@@ -485,9 +471,7 @@ int QDate::day() const
if (isNull())
return 0;
- int d;
- getDateFromJulianDay(jd, 0, 0, &d);
- return d;
+ return getDateFromJulianDay(jd).day;
}
/*!
@@ -539,12 +523,11 @@ int QDate::daysInMonth() const
if (isNull())
return 0;
- int y, m;
- getDateFromJulianDay(jd, &y, &m, 0);
- if (m == 2 && isLeapYear(y))
+ const ParsedDate pd = getDateFromJulianDay(jd);
+ if (pd.month == 2 && isLeapYear(pd.year))
return 29;
else
- return monthDays[m];
+ return monthDays[pd.month];
}
/*!
@@ -560,9 +543,7 @@ int QDate::daysInYear() const
if (isNull())
return 0;
- int y;
- getDateFromJulianDay(jd, &y, 0, 0);
- return isLeapYear(y) ? 366 : 365;
+ return isLeapYear(getDateFromJulianDay(jd).year) ? 366 : 365;
}
/*!
@@ -879,7 +860,7 @@ QString QDate::toString(Qt::DateFormat format) const
if (!isValid())
return QString();
- int y, m, d;
+ ParsedDate pd;
switch (format) {
case Qt::SystemLocaleDate:
@@ -897,19 +878,17 @@ QString QDate::toString(Qt::DateFormat format) const
default:
#ifndef QT_NO_TEXTDATE
case Qt::TextDate:
- getDateFromJulianDay(jd, &y, &m, &d);
- return QString::fromUtf8("%1 %2 %3 %4").arg(shortDayName(dayOfWeek()))
- .arg(shortMonthName(m))
- .arg(d)
- .arg(y);
+ pd = getDateFromJulianDay(jd);
+ return QString::fromLatin1("%1 %2 %3 %4").arg(shortDayName(dayOfWeek()))
+ .arg(shortMonthName(pd.month))
+ .arg(pd.day)
+ .arg(pd.year);
#endif
case Qt::ISODate:
- getDateFromJulianDay(jd, &y, &m, &d);
- if (y < 0 || y > 9999)
+ pd = getDateFromJulianDay(jd);
+ if (pd.year < 0 || pd.year > 9999)
return QString();
- return QString::fromUtf8("%1-%2-%3").arg(y, 4, 10, QLatin1Char('0'))
- .arg(m, 2, 10, QLatin1Char('0'))
- .arg(d, 2, 10, QLatin1Char('0'));
+ return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
}
}
@@ -1015,16 +994,16 @@ bool QDate::setDate(int year, int month, int day)
*/
void QDate::getDate(int *year, int *month, int *day)
{
- if (isValid()) {
- getDateFromJulianDay(jd, year, month, day);
- } else {
- if (year)
- *year = 0;
- if (month)
- *month = 0;
- if (day)
- *day = 0;
- }
+ ParsedDate pd = { 0, 0, 0 };
+ if (isValid())
+ pd = getDateFromJulianDay(jd);
+
+ if (year)
+ *year = pd.year;
+ if (month)
+ *month = pd.month;
+ if (day)
+ *day = pd.day;
}
/*!
@@ -1066,7 +1045,12 @@ QDate QDate::addMonths(int nmonths) const
return *this;
int old_y, y, m, d;
- getDateFromJulianDay(jd, &y, &m, &d);
+ {
+ const ParsedDate pd = getDateFromJulianDay(jd);
+ y = pd.year;
+ m = pd.month;
+ d = pd.day;
+ }
old_y = y;
bool increasing = nmonths > 0;
@@ -1124,19 +1108,18 @@ QDate QDate::addYears(int nyears) const
if (!isValid())
return QDate();
- int y, m, d;
- getDateFromJulianDay(jd, &y, &m, &d);
+ ParsedDate pd = getDateFromJulianDay(jd);
- int old_y = y;
- y += nyears;
+ int old_y = pd.year;
+ pd.year += nyears;
// was there a sign change?
- if ((old_y > 0 && y <= 0) ||
- (old_y < 0 && y >= 0))
+ if ((old_y > 0 && pd.year <= 0) ||
+ (old_y < 0 && pd.year >= 0))
// yes, adjust the date by +1 or -1 years
- y += nyears > 0 ? +1 : -1;
+ pd.year += nyears > 0 ? +1 : -1;
- return fixedDate(y, m, d);
+ return fixedDate(pd.year, pd.month, pd.day);
}
/*!
@@ -1239,39 +1222,21 @@ QDate QDate::fromString(const QString& string, Qt::DateFormat format)
return QLocale().toDate(string, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toDate(string, QLocale::LongFormat);
- case Qt::RFC2822Date: {
- QDate date;
- rfcDateImpl(string, &date);
- return date;
- }
+ case Qt::RFC2822Date:
+ return rfcDateImpl(string).date;
default:
#ifndef QT_NO_TEXTDATE
case Qt::TextDate: {
- QStringList parts = string.split(QLatin1Char(' '), QString::SkipEmptyParts);
+ QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
if (parts.count() != 4)
return QDate();
- QString monthName = parts.at(1);
- int month = -1;
- // Assume that English monthnames are the default
- for (int i = 0; i < 12; ++i) {
- if (monthName == QLatin1String(qt_shortMonthNames[i])) {
- month = i + 1;
- break;
- }
- }
- // If English names can't be found, search the localized ones
+ QStringRef monthName = parts.at(1);
+ const int month = fromShortMonthName(monthName);
if (month == -1) {
- for (int i = 1; i <= 12; ++i) {
- if (monthName == QDate::shortMonthName(i)) {
- month = i;
- break;
- }
- }
- if (month == -1)
- // Month name matches neither English nor other localised name.
- return QDate();
+ // Month name matches neither English nor other localised name.
+ return QDate();
}
bool ok = false;
@@ -1288,10 +1253,10 @@ QDate QDate::fromString(const QString& string, Qt::DateFormat format)
|| (string.size() > 10 && string.at(10).isDigit())) {
return QDate();
}
- const int year = string.mid(0, 4).toInt();
+ const int year = string.midRef(0, 4).toInt();
if (year <= 0 || year > 9999)
return QDate();
- return QDate(year, string.mid(5, 2).toInt(), string.mid(8, 2).toInt());
+ return QDate(year, string.midRef(5, 2).toInt(), string.midRef(8, 2).toInt());
}
}
return QDate();
@@ -1649,9 +1614,9 @@ QString QTime::toString(Qt::DateFormat format) const
case Qt::ISODate:
case Qt::TextDate:
default:
- return QString::fromUtf8("%1:%2:%3").arg(hour(), 2, 10, QLatin1Char('0'))
- .arg(minute(), 2, 10, QLatin1Char('0'))
- .arg(second(), 2, 10, QLatin1Char('0'));
+ return QString::fromLatin1("%1:%2:%3").arg(hour(), 2, 10, QLatin1Char('0'))
+ .arg(minute(), 2, 10, QLatin1Char('0'))
+ .arg(second(), 2, 10, QLatin1Char('0'));
}
}
@@ -2002,11 +1967,8 @@ QTime QTime::fromString(const QString& string, Qt::DateFormat format)
return QLocale().toTime(string, QLocale::ShortFormat);
case Qt::DefaultLocaleLongDate:
return QLocale().toTime(string, QLocale::LongFormat);
- case Qt::RFC2822Date: {
- QTime time;
- rfcDateImpl(string, 0, &time);
- return time;
- }
+ case Qt::RFC2822Date:
+ return rfcDateImpl(string).time;
case Qt::ISODate:
case Qt::TextDate:
default:
@@ -2682,29 +2644,29 @@ void QDateTimePrivate::setDateTime(const QDate &date, const QTime &time)
if (!useTime.isValid() && date.isValid())
useTime = QTime::fromMSecsSinceStartOfDay(0);
- // Reset the status
- m_status = 0;
+ StatusFlags newStatus;
// Set date value and status
qint64 days = 0;
if (date.isValid()) {
days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
- m_status = ValidDate;
+ newStatus = ValidDate;
} else if (date.isNull()) {
- m_status = NullDate;
+ newStatus = NullDate;
}
// Set time value and status
int ds = 0;
if (useTime.isValid()) {
ds = useTime.msecsSinceStartOfDay();
- m_status = m_status | ValidTime;
+ newStatus |= ValidTime;
} else if (time.isNull()) {
- m_status = m_status | NullTime;
+ newStatus |= NullTime;
}
// Set msecs serial value
m_msecs = (days * MSECS_PER_DAY) + ds;
+ m_status = newStatus;
// Set if date and time are valid
checkValidDateTime();
@@ -3186,13 +3148,11 @@ QTimeZone QDateTime::timeZone() const
d->m_timeZone = QTimeZone(d->m_offsetFromUtc);
return d->m_timeZone;
case Qt::UTC:
- if (!d->m_timeZone.isValid())
- d->m_timeZone = QTimeZone(QTimeZonePrivate::utcQByteArray());
- return d->m_timeZone;
- case Qt::TimeZone :
+ return QTimeZone::utc();
+ case Qt::TimeZone:
return d->m_timeZone;
case Qt::LocalTime:
- return QTimeZone(QTimeZone::systemTimeZoneId());
+ return QTimeZone::systemTimeZone();
}
return QTimeZone();
}
@@ -3603,11 +3563,11 @@ QString QDateTime::toString(Qt::DateFormat format) const
QTime tm;
d->getDateTime(&dt, &tm);
//We cant use date.toString(Qt::TextDate) as we need to insert the time before the year
- buf = QString::fromUtf8("%1 %2 %3 %4 %5").arg(dt.shortDayName(dt.dayOfWeek()))
- .arg(dt.shortMonthName(dt.month()))
- .arg(dt.day())
- .arg(tm.toString(Qt::TextDate))
- .arg(dt.year());
+ buf = QString::fromLatin1("%1 %2 %3 %4 %5").arg(dt.shortDayName(dt.dayOfWeek()))
+ .arg(dt.shortMonthName(dt.month()))
+ .arg(dt.day())
+ .arg(tm.toString(Qt::TextDate))
+ .arg(dt.year());
if (timeSpec() != Qt::LocalTime) {
buf += QStringLiteral(" GMT");
if (d->m_spec == Qt::OffsetFromUTC)
@@ -3747,7 +3707,7 @@ QDateTime QDateTime::addDays(qint64 ndays) const
localMSecsToEpochMSecs(timeToMSecs(date, time), &date, &time);
#ifndef QT_BOOTSTRAPPED
else if (d->m_spec == Qt::TimeZone)
- d->zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
+ QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
#endif // QT_BOOTSTRAPPED
dt.d->setDateTime(date, time);
return dt;
@@ -3781,7 +3741,7 @@ QDateTime QDateTime::addMonths(int nmonths) const
localMSecsToEpochMSecs(timeToMSecs(date, time), &date, &time);
#ifndef QT_BOOTSTRAPPED
else if (d->m_spec == Qt::TimeZone)
- d->zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
+ QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
#endif // QT_BOOTSTRAPPED
dt.d->setDateTime(date, time);
return dt;
@@ -3815,7 +3775,7 @@ QDateTime QDateTime::addYears(int nyears) const
localMSecsToEpochMSecs(timeToMSecs(date, time), &date, &time);
#ifndef QT_BOOTSTRAPPED
else if (d->m_spec == Qt::TimeZone)
- d->zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
+ QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(date, time), d->m_timeZone, &date, &time);
#endif // QT_BOOTSTRAPPED
dt.d->setDateTime(date, time);
return dt;
@@ -4204,6 +4164,39 @@ qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW
#error "What system is this?"
#endif
+/*! \fn QDateTime QDateTime::fromCFDate(CFDateRef date)
+ \since 5.5
+
+ Constructs a new QDateTime containing a copy of the CFDate \a date.
+
+ \sa toCFDate()
+*/
+
+/*! \fn CFDateRef QDateTime::toCFDate() const
+ \since 5.5
+
+ Creates a CFDate from a QDateTime. The caller owns the CFDate object
+ and is responsible for releasing it.
+
+ \sa fromCFDate()
+*/
+
+/*! \fn QDateTime QDateTime::fromNSDate(const NSDate *date)
+ \since 5.5
+
+ Constructs a new QDateTime containing a copy of the NSDate \a date.
+
+ \sa toNSDate()
+*/
+
+/*! \fn NSDate QDateTime::toNSDate() const
+ \since 5.5
+
+ Creates an NSDate from a QDateTime. The NSDate object is autoreleased.
+
+ \sa fromNSDate()
+*/
+
/*!
\since 4.2
@@ -4358,21 +4351,6 @@ int QDateTime::utcOffset() const
#ifndef QT_NO_DATESTRING
-static int fromShortMonthName(const QString &monthName)
-{
- // Assume that English monthnames are the default
- for (int i = 0; i < 12; ++i) {
- if (monthName == QLatin1String(qt_shortMonthNames[i]))
- return i + 1;
- }
- // If English names can't be found, search the localized ones
- for (int i = 1; i <= 12; ++i) {
- if (monthName == QDate::shortMonthName(i))
- return i;
- }
- return -1;
-}
-
/*!
\fn QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
@@ -4400,16 +4378,13 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
case Qt::DefaultLocaleLongDate:
return QLocale().toDateTime(string, QLocale::LongFormat);
case Qt::RFC2822Date: {
- QDate date;
- QTime time;
- int utcOffset = 0;
- rfcDateImpl(string, &date, &time, &utcOffset);
+ const ParsedRfcDateTime rfc = rfcDateImpl(string);
- if (!date.isValid() || !time.isValid())
+ if (!rfc.date.isValid() || !rfc.time.isValid())
return QDateTime();
- QDateTime dateTime(date, time, Qt::UTC);
- dateTime.setOffsetFromUtc(utcOffset);
+ QDateTime dateTime(rfc.date, rfc.time, Qt::UTC);
+ dateTime.setOffsetFromUtc(rfc.utcOffset);
return dateTime;
}
case Qt::ISODate: {
@@ -4449,7 +4424,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
if (found) {
bool ok;
- offset = fromOffsetString(isoString.mid(signIndex).toString(), &ok);
+ offset = fromOffsetString(isoString.mid(signIndex), &ok);
if (!ok)
return QDateTime();
isoString = isoString.left(signIndex);
@@ -4469,7 +4444,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
}
#if !defined(QT_NO_TEXTDATE)
case Qt::TextDate: {
- QStringList parts = string.split(QLatin1Char(' '), QString::SkipEmptyParts);
+ QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
if ((parts.count() < 5) || (parts.count() > 6))
return QDateTime();
@@ -4488,9 +4463,9 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
if (!month || !day) {
month = fromShortMonthName(parts.at(2));
if (month) {
- QString dayStr = parts.at(1);
+ QStringRef dayStr = parts.at(1);
if (dayStr.endsWith(QLatin1Char('.'))) {
- dayStr.chop(1);
+ dayStr = dayStr.left(dayStr.size() - 1);
day = dayStr.toInt();
}
}
@@ -4523,7 +4498,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
if (!date.isValid())
return QDateTime();
- QStringList timeParts = parts.at(timePart).split(QLatin1Char(':'));
+ QVector<QStringRef> timeParts = parts.at(timePart).split(QLatin1Char(':'));
if (timeParts.count() < 2 || timeParts.count() > 3)
return QDateTime();
@@ -4538,7 +4513,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
int second = 0;
int millisecond = 0;
if (timeParts.count() > 2) {
- QStringList secondParts = timeParts.at(2).split(QLatin1Char('.'));
+ QVector<QStringRef> secondParts = timeParts.at(2).split(QLatin1Char('.'));
if (secondParts.size() > 2) {
return QDateTime();
}
@@ -4563,10 +4538,10 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
if (parts.count() == 5)
return QDateTime(date, time, Qt::LocalTime);
- QString tz = parts.at(5);
+ QStringRef tz = parts.at(5);
if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
return QDateTime();
- tz.remove(0, 3);
+ tz = tz.mid(3);
if (!tz.isEmpty()) {
int offset = fromOffsetString(tz, &ok);
if (!ok)
@@ -4975,38 +4950,40 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
QDebug operator<<(QDebug dbg, const QDate &date)
{
+ QDebugStateSaver saver(dbg);
dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')';
- return dbg.space();
+ return dbg;
}
QDebug operator<<(QDebug dbg, const QTime &time)
{
+ QDebugStateSaver saver(dbg);
dbg.nospace() << "QTime(" << time.toString(QStringLiteral("HH:mm:ss.zzz")) << ')';
- return dbg.space();
+ return dbg;
}
QDebug operator<<(QDebug dbg, const QDateTime &date)
{
- QString spec;
- switch (date.d->m_spec) {
+ 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"))
+ << ' ' << ts;
+ switch (ts) {
case Qt::UTC:
- spec = QStringLiteral(" Qt::UTC");
break;
case Qt::OffsetFromUTC:
- spec = QString::fromUtf8(" Qt::OffsetFromUTC %1s").arg(date.offsetFromUtc());
+ dbg << ' ' << date.offsetFromUtc() << 's';
break;
case Qt::TimeZone:
#ifndef QT_BOOTSTRAPPED
- spec = QStringLiteral(" Qt::TimeZone ") + QString::fromLatin1(date.timeZone().id());
+ dbg << ' ' << date.timeZone().id();
break;
#endif // QT_BOOTSTRAPPED
case Qt::LocalTime:
- spec = QStringLiteral(" Qt::LocalTime");
break;
}
- QString output = date.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")) + spec;
- dbg.nospace() << "QDateTime(" << output << ')';
- return dbg.space();
+ return dbg << ')';
}
#endif