Skip to content

Commit

Permalink
Respect Android's 24h time format setting
Browse files Browse the repository at this point in the history
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 <assam.boudjelthia@qt.io>
  • Loading branch information
vkrause committed Nov 9, 2024
1 parent 60c665d commit e1f83e0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/plugins/platforms/android/qandroidsystemlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/platforms/android/qandroidsystemlocale.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ class QAndroidSystemLocale : public QSystemLocale
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
Expand Down

0 comments on commit e1f83e0

Please sign in to comment.