diff --git a/src/libcalamares/locale/Tests.cpp b/src/libcalamares/locale/Tests.cpp index 8a6a6ad84..db2ed2fe2 100644 --- a/src/libcalamares/locale/Tests.cpp +++ b/src/libcalamares/locale/Tests.cpp @@ -50,6 +50,7 @@ private Q_SLOTS: void testRegions(); void testSimpleZones(); void testComplexZones(); + void testTZLookup(); }; LocaleTests::LocaleTests() {} @@ -345,6 +346,21 @@ LocaleTests::testComplexZones() QVERIFY( names.contains( "Abidjan" ) ); } +void +LocaleTests::testTZLookup() +{ + using namespace CalamaresUtils::Locale; + ZonesModel zones; + + QVERIFY( zones.find( "America", "New_York" ) ); + QCOMPARE( zones.find( "America", "New_York" )->zone(), QStringLiteral( "New_York" ) ); + QCOMPARE( zones.find( "America", "New_York" )->tr(), QStringLiteral( "New York" ) ); + + QVERIFY( !zones.find( "Europe", "New_York" ) ); + QVERIFY( !zones.find( "America", "New York" ) ); +} + + QTEST_GUILESS_MAIN( LocaleTests ) #include "utils/moc-warnings.h" diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 73ac328d6..4e56b5af1 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -279,6 +279,19 @@ ZonesModel::roleNames() const return { { NameRole, "name" }, { KeyRole, "key" } }; } +const TimeZoneData* +ZonesModel::find( const QString& region, const QString& zone ) +{ + for ( const auto* p : m_private->m_zones ) + { + if ( p->region() == region && p->zone() == zone ) + { + return p; + } + } + return nullptr; +} + RegionalZonesModel::RegionalZonesModel( CalamaresUtils::Locale::ZonesModel* source, QObject* parent ) : QSortFilterProxyModel( parent ) , m_private( privateInstance() ) diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index c1bcc96b9..de23f07d2 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -57,6 +57,7 @@ public: QString tr() const override; QString region() const { return m_region; } + QString zone() const { return key(); } private: QString m_region; @@ -113,6 +114,12 @@ public: QHash< int, QByteArray > roleNames() const override; + /** @brief Look up TZ data based on its name. + * + * Returns @c nullptr if not found. + */ + const TimeZoneData* find( const QString& region, const QString& zone ); + private: Private* m_private; };