最终调用函数是这个QString QLocalePrivate::dateTimeToString(const QString &format, const QDateTime &datetime, const QDate &dateOnly, const QTime &timeOnly, const QLocale *q) const,在qlocale.cpp里面。关键部分:
switch (c.unicode()) { case 'h': { used = true; repeat = qMin(repeat, 2); int hour = time.hour(); if (timeFormatContainsAP(format)) { if (hour > 12) hour -= 12; else if (hour == 0) hour = 12; } switch (repeat) { case 1: result.append(m_data->longLongToString(hour)); break; case 2: result.append(m_data->longLongToString(hour, -1, 10, 2, QLocaleData::ZeroPadded)); break; } break; } case 'H': used = true; repeat = qMin(repeat, 2); switch (repeat) { case 1: result.append(m_data->longLongToString(time.hour())); break; case 2: result.append(m_data->longLongToString(time.hour(), -1, 10, 2, QLocaleData::ZeroPadded)); break; } break; case 'm': used = true; repeat = qMin(repeat, 2); switch (repeat) { case 1: result.append(m_data->longLongToString(time.minute())); break; case 2: result.append(m_data->longLongToString(time.minute(), -1, 10, 2, QLocaleData::ZeroPadded)); break; } break; case 's': used = true; repeat = qMin(repeat, 2); switch (repeat) { case 1: result.append(m_data->longLongToString(time.second())); break; case 2: result.append(m_data->longLongToString(time.second(), -1, 10, 2, QLocaleData::ZeroPadded)); break; } break; //.... }