Skip to content

Commit

Permalink
Do fewer calendrical calculations in QDateTimeParser::setDigit()
Browse files Browse the repository at this point in the history
It was calling a QDate's year(), month() and day() methods, each of
which repeats most of the same calendrical calculations; and the same
results can be obtained from the calendar's partsFromDate() all in one
go. This also reduces the number of local variables needed.

Change-Id: I8f84e66a5f677f55cb2113c56ebbdf7c2517e828
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
ediosyncratic committed Jan 14, 2020
1 parent 01d24ee commit 7b3d0ab
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/corelib/time/qdatetimeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
#endif
return false;
}
const SectionNode &node = sectionNodes.at(index);

const QDate date = v.date();
QCalendar::YearMonthDay date = calendar.partsFromDate(v.date());
if (!date.isValid())
return false;

const QTime time = v.time();
int year = date.year(calendar);
int month = date.month(calendar);
int day = date.day(calendar);
int hour = time.hour();
int minute = time.minute();
int second = time.second();
Expand All @@ -152,14 +151,15 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
// Only offset from UTC is amenable to setting an int value:
int offset = tspec == Qt::OffsetFromUTC ? v.offsetFromUtc() : 0;

const SectionNode &node = sectionNodes.at(index);
switch (node.type) {
case Hour24Section: case Hour12Section: hour = newVal; break;
case MinuteSection: minute = newVal; break;
case SecondSection: second = newVal; break;
case MSecSection: msec = newVal; break;
case YearSection2Digits:
case YearSection: year = newVal; break;
case MonthSection: month = newVal; break;
case YearSection: date.year = newVal; break;
case MonthSection: date.month = newVal; break;
case DaySection:
case DayOfWeekSectionShort:
case DayOfWeekSectionLong:
Expand All @@ -169,7 +169,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
// to 31 for february should return true
return false;
}
day = newVal;
date.day = newVal;
break;
case TimeZoneSection:
if (newVal < absoluteMin(index) || newVal > absoluteMax(index))
Expand All @@ -185,15 +185,14 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
}

if (!(node.type & DaySectionMask)) {
if (day < cachedDay)
day = cachedDay;
const int max = calendar.daysInMonth(month, year);
if (day > max) {
day = max;
}
if (date.day < cachedDay)
date.day = cachedDay;
const int max = calendar.daysInMonth(date.month, date.year);
if (date.day > max)
date.day = max;
}

const QDate newDate(year, month, day, calendar);
const QDate newDate = calendar.dateFromParts(date);
const QTime newTime(hour, minute, second, msec);
if (!newDate.isValid() || !newTime.isValid())
return false;
Expand Down

0 comments on commit 7b3d0ab

Please sign in to comment.