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.cpp191
1 files changed, 92 insertions, 99 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 505a6f863b..67f16e1f49 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -122,6 +122,8 @@ struct ParsedDate
int year, month, day;
};
+// prevent this function from being inlined into all 10 users
+Q_NEVER_INLINE
static ParsedDate getDateFromJulianDay(qint64 julianDay)
{
/*
@@ -628,16 +630,13 @@ int QDate::weekNumber(int *yearNumber) const
QString QDate::shortMonthName(int month, QDate::MonthNameType type)
{
- if (month < 1 || month > 12)
- return QString();
-
- switch (type) {
- case QDate::DateFormat:
- return QLocale::system().monthName(month, QLocale::ShortFormat);
- case QDate::StandaloneFormat:
- return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
- default:
- break;
+ if (month >= 1 || month <= 12) {
+ switch (type) {
+ case QDate::DateFormat:
+ return QLocale::system().monthName(month, QLocale::ShortFormat);
+ case QDate::StandaloneFormat:
+ return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
+ }
}
return QString();
}
@@ -675,16 +674,13 @@ QString QDate::shortMonthName(int month, QDate::MonthNameType type)
QString QDate::longMonthName(int month, MonthNameType type)
{
- if (month < 1 || month > 12)
- return QString();
-
- switch (type) {
- case QDate::DateFormat:
- return QLocale::system().monthName(month, QLocale::LongFormat);
- case QDate::StandaloneFormat:
- return QLocale::system().standaloneMonthName(month, QLocale::LongFormat);
- default:
- break;
+ if (month >= 1 && month <= 12) {
+ switch (type) {
+ case QDate::DateFormat:
+ return QLocale::system().monthName(month, QLocale::LongFormat);
+ case QDate::StandaloneFormat:
+ return QLocale::system().standaloneMonthName(month, QLocale::LongFormat);
+ }
}
return QString();
}
@@ -717,16 +713,13 @@ QString QDate::longMonthName(int month, MonthNameType type)
QString QDate::shortDayName(int weekday, MonthNameType type)
{
- if (weekday < 1 || weekday > 7)
- return QString();
-
- switch (type) {
- case QDate::DateFormat:
- return QLocale::system().dayName(weekday, QLocale::ShortFormat);
- case QDate::StandaloneFormat:
- return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat);
- default:
- break;
+ if (weekday >= 1 && weekday <= 7) {
+ switch (type) {
+ case QDate::DateFormat:
+ return QLocale::system().dayName(weekday, QLocale::ShortFormat);
+ case QDate::StandaloneFormat:
+ return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat);
+ }
}
return QString();
}
@@ -759,23 +752,41 @@ QString QDate::shortDayName(int weekday, MonthNameType type)
QString QDate::longDayName(int weekday, MonthNameType type)
{
- if (weekday < 1 || weekday > 7)
- return QString();
-
- switch (type) {
- case QDate::DateFormat:
- return QLocale::system().dayName(weekday, QLocale::LongFormat);
- case QDate::StandaloneFormat:
- return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat);
- default:
- break;
+ if (weekday >= 1 && weekday <= 7) {
+ switch (type) {
+ case QDate::DateFormat:
+ return QLocale::system().dayName(weekday, QLocale::LongFormat);
+ case QDate::StandaloneFormat:
+ return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat);
+ }
}
- return QLocale::system().dayName(weekday, QLocale::LongFormat);
+ return QString();
}
#endif //QT_NO_TEXTDATE
#ifndef QT_NO_DATESTRING
+#ifndef QT_NO_TEXTDATE
+static QString toStringTextDate(QDate date)
+{
+ const ParsedDate pd = getDateFromJulianDay(date.toJulianDay());
+ static const QLatin1Char sp(' ');
+ return date.shortDayName(date.dayOfWeek()) + sp
+ + date.shortMonthName(pd.month) + sp
+ + QString::number(pd.day) + sp
+ + QString::number(pd.year);
+}
+#endif // QT_NO_TEXTDATE
+
+static QString toStringIsoDate(qint64 jd)
+{
+ const ParsedDate pd = getDateFromJulianDay(jd);
+ if (pd.year >= 0 && pd.year <= 9999)
+ return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
+ else
+ return QString();
+}
+
/*!
\fn QString QDate::toString(Qt::DateFormat format) const
@@ -827,8 +838,6 @@ QString QDate::toString(Qt::DateFormat format) const
if (!isValid())
return QString();
- ParsedDate pd;
-
switch (format) {
case Qt::SystemLocaleDate:
case Qt::SystemLocaleShortDate:
@@ -845,17 +854,10 @@ QString QDate::toString(Qt::DateFormat format) const
default:
#ifndef QT_NO_TEXTDATE
case Qt::TextDate:
- pd = getDateFromJulianDay(jd);
- return QString::fromLatin1("%1 %2 %3 %4").arg(shortDayName(dayOfWeek()))
- .arg(shortMonthName(pd.month))
- .arg(pd.day)
- .arg(pd.year);
+ return toStringTextDate(*this);
#endif
case Qt::ISODate:
- pd = getDateFromJulianDay(jd);
- if (pd.year < 0 || pd.year > 9999)
- return QString();
- return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
+ return toStringIsoDate(jd);
}
}
@@ -1581,9 +1583,7 @@ QString QTime::toString(Qt::DateFormat format) const
case Qt::ISODate:
case Qt::TextDate:
default:
- return QString::fromLatin1("%1:%2:%3").arg(hour(), 2, 10, QLatin1Char('0'))
- .arg(minute(), 2, 10, QLatin1Char('0'))
- .arg(second(), 2, 10, QLatin1Char('0'));
+ return QString::asprintf("%02d:%02d:%02d", hour(), minute(), second());
}
}
@@ -2185,7 +2185,7 @@ static QString qt_tzname(QDateTimePrivate::DaylightStatus daylightStatus)
// then null date/time will be returned, you should adjust the date first if
// you need a guaranteed result.
static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStatus *daylightStatus,
- QString *abbreviation, bool *ok)
+ QString *abbreviation, bool *ok = 0)
{
const qint64 msec = time->msec();
int yy, mm, dd;
@@ -2446,7 +2446,7 @@ static bool epochMSecsToLocalTime(qint64 msecs, QDate *localDate, QTime *localTi
static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
QDateTimePrivate::DaylightStatus *daylightStatus,
QDate *localDate = 0, QTime *localTime = 0,
- QString *abbreviation = 0, bool *ok = 0)
+ QString *abbreviation = 0)
{
QDate dt;
QTime tm;
@@ -2469,8 +2469,6 @@ static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
*localDate = dt;
if (localTime)
*localTime = tm;
- if (ok)
- *ok = true;
return utcMsecs;
}
} else {
@@ -2485,8 +2483,6 @@ static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
*daylightStatus = QDateTimePrivate::StandardTime;
if (abbreviation)
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
- if (ok)
- *ok = true;
return utcMsecs;
} else if (localMsecs >= msecsMax - MSECS_PER_DAY) {
@@ -2505,8 +2501,6 @@ static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
*localDate = dt;
if (localTime)
*localTime = tm;
- if (ok)
- *ok = true;
return utcMsecs;
}
}
@@ -2520,7 +2514,7 @@ static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
--day;
QDate fakeDate(2037, month, day);
qint64 fakeDiff = fakeDate.daysTo(dt);
- qint64 utcMsecs = qt_mktime(&fakeDate, &tm, daylightStatus, abbreviation, ok);
+ qint64 utcMsecs = qt_mktime(&fakeDate, &tm, daylightStatus, abbreviation);
if (localDate)
*localDate = fakeDate.addDays(fakeDiff);
if (localTime)
@@ -2535,7 +2529,7 @@ static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
} else {
// Clearly falls inside 1970-2037 suported range so can use mktime
- qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation, ok);
+ qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation);
if (localDate)
*localDate = dt;
if (localTime)
@@ -2639,15 +2633,18 @@ void QDateTimePrivate::setDateTime(const QDate &date, const QTime &time)
checkValidDateTime();
}
-void QDateTimePrivate::getDateTime(QDate *date, QTime *time) const
+QPair<QDate, QTime> QDateTimePrivate::getDateTime() const
{
- msecsToTime(m_msecs, date, time);
+ QPair<QDate, QTime> result;
+ msecsToTime(m_msecs, &result.first, &result.second);
- if (date && isNullDate())
- *date = QDate();
+ if (isNullDate())
+ result.first = QDate();
- if (time && isNullTime())
- *time = QTime();
+ if (isNullTime())
+ result.second = QTime();
+
+ return result;
}
// Set the Daylight Status if LocalTime set via msecs
@@ -3535,9 +3532,9 @@ QString QDateTime::toString(Qt::DateFormat format) const
default:
#ifndef QT_NO_TEXTDATE
case Qt::TextDate: {
- QDate dt;
- QTime tm;
- d->getDateTime(&dt, &tm);
+ const QPair<QDate, QTime> p = d->getDateTime();
+ const QDate &dt = p.first;
+ const QTime &tm = p.second;
//We cant use date.toString(Qt::TextDate) as we need to insert the time before the year
buf = QString::fromLatin1("%1 %2 %3 %4 %5").arg(dt.shortDayName(dt.dayOfWeek()))
.arg(dt.shortMonthName(dt.month()))
@@ -3553,9 +3550,9 @@ QString QDateTime::toString(Qt::DateFormat format) const
}
#endif
case Qt::ISODate: {
- QDate dt;
- QTime tm;
- d->getDateTime(&dt, &tm);
+ const QPair<QDate, QTime> p = d->getDateTime();
+ const QDate &dt = p.first;
+ const QTime &tm = p.second;
buf = dt.toString(Qt::ISODate);
if (buf.isEmpty())
return QString(); // failed to convert
@@ -3673,9 +3670,9 @@ QDateTime QDateTime::addDays(qint64 ndays) const
{
QDateTime dt(*this);
dt.detach();
- QDate date;
- QTime time;
- d->getDateTime(&date, &time);
+ QPair<QDate, QTime> p = d->getDateTime();
+ QDate &date = p.first;
+ QTime &time = p.second;
date = date.addDays(ndays);
// Result might fall into "missing" DaylightTime transition hour,
// so call conversion and use the adjusted returned time
@@ -3709,9 +3706,9 @@ QDateTime QDateTime::addMonths(int nmonths) const
{
QDateTime dt(*this);
dt.detach();
- QDate date;
- QTime time;
- d->getDateTime(&date, &time);
+ QPair<QDate, QTime> p = d->getDateTime();
+ QDate &date = p.first;
+ QTime &time = p.second;
date = date.addMonths(nmonths);
// Result might fall into "missing" DaylightTime transition hour,
// so call conversion and use the adjusted returned time
@@ -3745,9 +3742,9 @@ QDateTime QDateTime::addYears(int nyears) const
{
QDateTime dt(*this);
dt.detach();
- QDate date;
- QTime time;
- d->getDateTime(&date, &time);
+ QPair<QDate, QTime> p = d->getDateTime();
+ QDate &date = p.first;
+ QTime &time = p.second;
date = date.addYears(nyears);
// Result might fall into "missing" DaylightTime transition hour,
// so call conversion and use the adjusted returned time
@@ -4781,14 +4778,13 @@ QDataStream &operator>>(QDataStream &in, QTime &time)
*/
QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
{
- QDate dt;
- QTime tm;
+ QPair<QDate, QTime> dateAndTime;
if (out.version() >= QDataStream::Qt_5_2) {
// In 5.2 we switched to using Qt::TimeSpec and added offset support
- dateTime.d->getDateTime(&dt, &tm);
- out << dt << tm << qint8(dateTime.timeSpec());
+ dateAndTime = dateTime.d->getDateTime();
+ out << dateAndTime << qint8(dateTime.timeSpec());
if (dateTime.timeSpec() == Qt::OffsetFromUTC)
out << qint32(dateTime.offsetFromUtc());
#ifndef QT_BOOTSTRAPPED
@@ -4802,17 +4798,14 @@ QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
// This approach is wrong and should not be used again; it breaks
// the guarantee that a deserialised local datetime is the same time
// of day, regardless of which timezone it was serialised in.
- if (dateTime.isValid())
- dateTime.toUTC().d->getDateTime(&dt, &tm);
- else
- dateTime.d->getDateTime(&dt, &tm);
- out << dt << tm << qint8(dateTime.timeSpec());
+ dateAndTime = (dateTime.isValid() ? dateTime.toUTC() : dateTime).d->getDateTime();
+ out << dateAndTime << qint8(dateTime.timeSpec());
} else if (out.version() >= QDataStream::Qt_4_0) {
// From 4.0 to 5.1 (except 5.0) we used QDateTimePrivate::Spec
- dateTime.d->getDateTime(&dt, &tm);
- out << dt << tm;
+ dateAndTime = dateTime.d->getDateTime();
+ out << dateAndTime;
if (out.version() >= QDataStream::Qt_4_0) {
switch (dateTime.timeSpec()) {
case Qt::UTC:
@@ -4835,8 +4828,8 @@ QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
} else { // version < QDataStream::Qt_4_0
// Before 4.0 there was no TimeSpec, only Qt::LocalTime was supported
- dateTime.d->getDateTime(&dt, &tm);
- out << dt << tm;
+ dateAndTime = dateTime.d->getDateTime();
+ out << dateAndTime;
}