summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/android/qandroidsystemlocale.cpp
diff options
context:
space:
mode:
authorVolker Krause <[email protected]>2024-10-28 17:15:54 +0100
committerVolker Krause <[email protected]>2024-11-09 13:59:26 +0100
commite1f83e0ffbbd079b41e5a903e35d6cd6a62e6f99 (patch)
tree9d72c2fb45defdc7a40939c0ca6a9f62f7742d65 /src/plugins/platforms/android/qandroidsystemlocale.cpp
parent60c665d0b320324affa76702b5e5de3d37117809 (diff)
Respect Android's 24h time format setting
Android allows to explicitly chose the 24h format even when the selected locale would not usually use that. Pick-to: 6.8 Change-Id: I6181d343a12d8d264c369341ef753d872f573121 Reviewed-by: Assam Boudjelthia <[email protected]>
Diffstat (limited to 'src/plugins/platforms/android/qandroidsystemlocale.cpp')
-rw-r--r--src/plugins/platforms/android/qandroidsystemlocale.cpp59
1 files changed, 51 insertions, 8 deletions
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<QString>("getLanguage");
const QString countryCode = javaLocaleObject.callMethod<QString>("getCountry");
+ const bool is24HourFormat = DateFormat::callStaticMethod<bool>("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: