diff --git a/src/plugins/platforms/android/qandroidsystemlocale.cpp b/src/plugins/platforms/android/qandroidsystemlocale.cpp index c5f2e59097f..fedd226aa89 100644 --- a/src/plugins/platforms/android/qandroidsystemlocale.cpp +++ b/src/plugins/platforms/android/qandroidsystemlocale.cpp @@ -16,6 +16,7 @@ Q_DECLARE_JNI_CLASS(Locale, "java/util/Locale") Q_DECLARE_JNI_CLASS(Resources, "android/content/res/Resources") Q_DECLARE_JNI_CLASS(Configuration, "android/content/res/Configuration") Q_DECLARE_JNI_CLASS(LocaleList, "android/os/LocaleList") +Q_DECLARE_JNI_CLASS(DateFormat, "android/text/format/DateFormat") using namespace QtJniTypes; @@ -39,8 +40,50 @@ void QAndroidSystemLocale::getLocaleFromJava() const const QString languageCode = javaLocaleObject.callMethod("getLanguage"); const QString countryCode = javaLocaleObject.callMethod("getCountry"); + const bool is24HourFormat = DateFormat::callStaticMethod("is24HourFormat", + QNativeInterface::QAndroidApplication::context()); + QWriteLocker locker(&m_lock); m_locale = QLocale(languageCode + u'_' + countryCode); + m_24hFormat = is24HourFormat; +} + +QString QAndroidSystemLocale::convertTo24hFormat(const QString &format) const +{ + if (!m_24hFormat) + return format; + + QString format24(format); + bool inQuoted = false; + for (qsizetype i = 0; i < format24.size(); ++i) { + if (format24[i] == QLatin1Char('\'')) { + inQuoted = !inQuoted; + continue; + } + if (inQuoted) + continue; + + // remove AM/PM markerg from format string + const auto c = format24[i].toUpper(); + if (c == QLatin1Char('A') || c == QLatin1Char('P')) + format24.remove(i--, 1); + } + + return format24.trimmed(); +} + +QString QAndroidSystemLocale::timeToString(const QTime &time, QLocale::FormatType type) const +{ + if (m_24hFormat) + return m_locale.toString(time, convertTo24hFormat(m_locale.timeFormat(type))); + return m_locale.toString(time, type); +} + +QString QAndroidSystemLocale::dateTimeToString(const QDateTime &dt, QLocale::FormatType type) const +{ + if (m_24hFormat) + return m_locale.toString(dt, convertTo24hFormat(m_locale.dateTimeFormat(type))); + return m_locale.toString(dt, type); } QVariant QAndroidSystemLocale::query(QueryType type, QVariant &&in) const @@ -66,9 +109,9 @@ QVariant QAndroidSystemLocale::query(QueryType type, QVariant &&in) const case DateFormatShort: return m_locale.dateFormat(QLocale::ShortFormat); case TimeFormatLong: - return m_locale.timeFormat(QLocale::LongFormat); + return convertTo24hFormat(m_locale.timeFormat(QLocale::LongFormat)); case TimeFormatShort: - return m_locale.timeFormat(QLocale::ShortFormat); + return convertTo24hFormat(m_locale.timeFormat(QLocale::ShortFormat)); case DayNameLong: return m_locale.dayName(in.toInt(), QLocale::LongFormat); case DayNameShort: @@ -98,17 +141,17 @@ QVariant QAndroidSystemLocale::query(QueryType type, QVariant &&in) const case DateToStringShort: return m_locale.toString(in.toDate(), QLocale::ShortFormat); case TimeToStringLong: - return m_locale.toString(in.toTime(), QLocale::LongFormat); + return timeToString(in.toTime(), QLocale::LongFormat); case TimeToStringShort: - return m_locale.toString(in.toTime(), QLocale::ShortFormat); + return timeToString(in.toTime(), QLocale::ShortFormat); case DateTimeFormatLong: - return m_locale.dateTimeFormat(QLocale::LongFormat); + return convertTo24hFormat(m_locale.dateTimeFormat(QLocale::LongFormat)); case DateTimeFormatShort: - return m_locale.dateTimeFormat(QLocale::ShortFormat); + return convertTo24hFormat(m_locale.dateTimeFormat(QLocale::ShortFormat)); case DateTimeToStringLong: - return m_locale.toString(in.toDateTime(), QLocale::LongFormat); + return dateTimeToString(in.toDateTime(), QLocale::LongFormat); case DateTimeToStringShort: - return m_locale.toString(in.toDateTime(), QLocale::ShortFormat); + return dateTimeToString(in.toDateTime(), QLocale::ShortFormat); case PositiveSign: return m_locale.positiveSign(); case AMText: diff --git a/src/plugins/platforms/android/qandroidsystemlocale.h b/src/plugins/platforms/android/qandroidsystemlocale.h index cd37b482708..1c85945e499 100644 --- a/src/plugins/platforms/android/qandroidsystemlocale.h +++ b/src/plugins/platforms/android/qandroidsystemlocale.h @@ -21,8 +21,13 @@ public: private: void getLocaleFromJava() const; + QString convertTo24hFormat(const QString &format) const; + QString timeToString(const QTime &time, QLocale::FormatType type) const; + QString dateTimeToString(const QDateTime &dt, QLocale::FormatType type) const; + mutable QLocale m_locale; mutable QReadWriteLock m_lock; + mutable bool m_24hFormat = false; }; QT_END_NAMESPACE