📄 qlocale.cpp
字号:
success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toUShort(), toString()*/short QLocale::toShort(const QString &s, bool *ok, int base) const{ qlonglong i = toLongLong(s, ok, base); if (i < SHRT_MIN || i > SHRT_MAX) { if (ok != 0) *ok = false; return 0; } return short(i);}/*! Returns the unsigned short int represented by the localized string \a s, using base \a base. If \a base is 0 the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal. If the conversion fails the function returns 0. If \a ok is not 0, failure is reported by setting *ok to false, and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toShort(), toString()*/ushort QLocale::toUShort(const QString &s, bool *ok, int base) const{ qulonglong i = toULongLong(s, ok, base); if (i > USHRT_MAX) { if (ok != 0) *ok = false; return 0; } return ushort(i);}/*! Returns the int represented by the localized string \a s, using base \a base. If \a base is 0 the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal. If the conversion fails the function returns 0. If \a ok is not 0, failure is reported by setting *ok to false, and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toUInt(), toString()*/int QLocale::toInt(const QString &s, bool *ok, int base) const{ qlonglong i = toLongLong(s, ok, base); if (i < INT_MIN || i > INT_MAX) { if (ok != 0) *ok = false; return 0; } return int(i);}/*! Returns the unsigned int represented by the localized string \a s, using base \a base. If \a base is 0 the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal. If the conversion fails the function returns 0. If \a ok is not 0, failure is reported by setting *ok to false, and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toInt(), toString()*/uint QLocale::toUInt(const QString &s, bool *ok, int base) const{ qulonglong i = toULongLong(s, ok, base); if (i > UINT_MAX) { if (ok != 0) *ok = false; return 0; } return uint(i);}/*! Returns the long long int represented by the localized string \a s, using base \a base. If \a base is 0 the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal. If the conversion fails the function returns 0. If \a ok is not 0, failure is reported by setting *ok to false, and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toInt(), toULongLong(), toDouble(), toString()*/qlonglong QLocale::toLongLong(const QString &s, bool *ok, int base) const{ return d->stringToLongLong(s, base, ok, QLocalePrivate::ParseGroupSeparators);}/*! Returns the unsigned long long int represented by the localized string \a s, using base \a base. If \a base is 0 the base is determined automatically using the following rules: If the string begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal. If the conversion fails the function returns 0. If \a ok is not 0, failure is reported by setting *ok to false, and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toLongLong(), toInt(), toDouble(), toString()*/qlonglong QLocale::toULongLong(const QString &s, bool *ok, int base) const{ return d->stringToUnsLongLong(s, base, ok, QLocalePrivate::ParseGroupSeparators);}/*! Returns the float represented by the localized string \a s, or 0.0 if the conversion failed. If \a ok is not 0, reports failure by setting *ok to false and success by setting *ok to true. This function ignores leading and trailing whitespace. \sa toDouble(), toInt(), toString()*/#define QT_MAX_FLOAT 3.4028234663852886e+38float QLocale::toFloat(const QString &s, bool *ok) const{ bool myOk; double d = toDouble(s, &myOk); if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { if (ok != 0) *ok = false; return 0.0; } if (ok != 0) *ok = true; return float(d);}/*! Returns the double represented by the localized string \a s, or 0.0 if the conversion failed. If \a ok is not 0, reports failure by setting *ok to false and success by setting *ok to true. Unlike QString::toDouble(), this function does not fall back to the "C" locale if the string cannot be interpreted in this locale. \code bool ok; double d; QLocale c(QLocale::C); d = c.toDouble( "1234.56", &ok ); // ok == true, d == 1234.56 d = c.toDouble( "1,234.56", &ok ); // ok == true, d == 1234.56 d = c.toDouble( "1234,56", &ok ); // ok == false QLocale german(QLocale::German); d = german.toDouble( "1234,56", &ok ); // ok == true, d == 1234.56 d = german.toDouble( "1.234,56", &ok ); // ok == true, d == 1234.56 d = german.toDouble( "1234.56", &ok ); // ok == false d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0 \endcode Notice that the last conversion returns 1234.0, because '.' is the thousands group separator in the German locale. This function ignores leading and trailing whitespace. \sa toFloat(), toInt(), toString()*/double QLocale::toDouble(const QString &s, bool *ok) const{ return d->stringToDouble(s, ok, QLocalePrivate::ParseGroupSeparators);}/*! Returns a localized string representation of \a i. \sa toLongLong()*/QString QLocale::toString(qlonglong i) const{ return d->longLongToString(i, -1, 10, -1, QLocalePrivate::ThousandsGroup);}/*! \overload \sa toULongLong()*/QString QLocale::toString(qulonglong i) const{ return d->unsLongLongToString(i, -1, 10, -1, QLocalePrivate::ThousandsGroup);}/*! Returns a localized string representation of the given \a date in the specified \a format. If \a format is an empty string, an empty string is returned.*/QString QLocale::toString(const QDate &date, const QString &format) const{ QString result; int i = 0; while (i < format.size()) { if (format.at(i).unicode() == '\'') { result.append(readEscapedFormatString(format, &i)); continue; } QChar c = format.at(i); int repeat = repeatCount(format, i); switch (c.unicode()) { case 'y': if (repeat >= 4) repeat = 4; else if (repeat >= 2) repeat = 2; switch (repeat) { case 4: result.append(d->longLongToString(date.year())); break; case 2: result.append(d->longLongToString(date.year()%100, -1, 10, 2, QLocalePrivate::ZeroPadded)); break; default: repeat = 1; result.append(c); break; } break; case 'M': if (repeat > 4) repeat = 4; switch (repeat) { case 1: result.append(d->longLongToString(date.month())); break; case 2: result.append(d->longLongToString(date.month(), -1, 10, 2, QLocalePrivate::ZeroPadded)); break; case 3: result.append(d->month(date.month() - 1, true)); break; case 4: result.append(d->month(date.month() - 1)); break; } break; case 'd': if (repeat > 4) repeat = 4; switch (repeat) { case 1: result.append(d->longLongToString(date.day())); break; case 2: result.append(d->longLongToString(date.day(), -1, 10, 2, QLocalePrivate::ZeroPadded)); break; case 3: result.append(d->day(date.dayOfWeek(), true)); break; case 4: result.append(d->day(date.dayOfWeek())); break; } break; default: result.append(QString(repeat, c)); break; } i += repeat; } return result;}/*! Returns a localized string representation of the given \a date according to the specified \a format.*/QString QLocale::toString(const QDate &date, FormatType format) const{#ifndef QT_USE_DATABASE if (d == systemLocale()) return systemDateToString(date, format);#endif QString format_str = dateFormat(format); return toString(date, format_str);}static bool timeFormatContainsAP(const QString &format){ int i = 0; while (i + 1 < format.size()) { if (format.at(i).unicode() == '\'') { readEscapedFormatString(format, &i); continue; } QChar c1 = format.at(i); QChar c2 = format.at(i + 1); if (c1.unicode() == 'a' && c2.unicode() == 'p' || c1.unicode() == 'A' && c2.unicode() == 'P') return true; ++i; } return false;}static QString timeZone(){#ifdef Q_OS_WIN _tzset();# if defined(_MSC_VER) && _MSC_VER >= 1400 size_t returnSize = 0; char timeZoneName[512]; if (_get_tzname(&returnSize, timeZoneName, 512, 1)) return QString(); return QString::fromLocal8Bit(timeZoneName);# else return QString::fromLocal8Bit(_tzname[1]);# endif#else tzset(); return QString::fromLocal8Bit(tzname[1]);#endif}/*! Returns a localized string representation of the given \a time according to the specified \a format. If \a format is an empty string, an empty string is returned.*/QString QLocale::toString(const QTime &time, const QString &format) const{ QString result; int hour12 = time.hour(); enum { AM, PM } am_pm = AM; if (hour12 == 0) { am_pm = AM; hour12 = 12; } else if (hour12 < 12) { am_pm = AM; } else if (hour12 == 12) { am_pm = PM; } else { am_pm = PM; hour12 -= 12; } bool format_am_pm = timeFormatContainsAP(format); int i = 0; while (i < format.size()) { if (format.at(i).unicode() == '\'') { result.append(readEscapedFormatString(format, &i)); continue; } QChar c = format.at(i); int repeat = repeatCount(format, i); switch (c.unicode()) { case 'h': { if (repeat > 2) repeat = 2; int hour = format_am_pm ? hour12 : time.hour(); switch (repeat) { case 1: result.append(d->longLongToString(hour)); break; case 2: result.append(d->longLongToString(hour, -1, 10, 2, QLocalePrivate::ZeroPadded)); break; } break; } case 'H': if (repeat > 2) repeat = 2; switch (repeat) { case 1: result.append(d->longLongToString(time.hour())); break; case 2: result.append(d->longLongToString(time.hour(), -1, 10, 2, QLocalePrivate::ZeroPadded)); break; } break; case 'm': if (repeat > 2) repeat = 2; switch (repeat) { case 1: result.append(d->longLongToString(time.minute())); break; case 2: result.append(d->longLongToString(time.minute(), -1, 10, 2, QLocalePrivate::ZeroPadded)); break; } break; case 's': if (repeat > 2) repeat = 2; switch (repeat) { case 1: result.append(d->longLongToString(time.secon
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -