Commit a7f80990 authored by Volker Krause's avatar Volker Krause
Browse files

Let timezoneForLocation return a QTimeZone

This reduces leaking the internal timezone enum out of the knowledge db
code, which helps with the port to KI18nLocaleData.
parent df3fc8c1
......@@ -132,7 +132,7 @@ private Q_SLOTS:
QCOMPARE(KnowledgeDb::timezoneForCoordinate(coord.latitude, coord.longitude), KnowledgeDb::Tz::Asia_Hong_Kong);
const auto country = KnowledgeDb::countryForAirport(KnowledgeDb::IataCode{"HKG"});
QCOMPARE(country, KnowledgeDb::CountryId{"CN"});
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, country), KnowledgeDb::Tz::Asia_Shanghai);
QCOMPARE(KnowledgeDb::timezoneForLocation(coord.latitude, coord.longitude, country), QTimeZone("Asia/Shanghai"));
tz = KnowledgeDb::timezoneForAirport(KnowledgeDb::IataCode{"HKG"});
QCOMPARE(tz.id(), QByteArray("Asia/Shanghai"));
}
......
......@@ -269,40 +269,40 @@ private Q_SLOTS:
QCOMPARE(timezoneForCoordinate(21.0, 106.0), Tz::Asia_Bangkok);
// Maastricht (NL), very close to the BE border
QCOMPARE(timezoneForLocation(50.8505, 5.6881, CountryId{}), Tz::Europe_Amsterdam);
QCOMPARE(timezoneForLocation(50.8505, 5.6881, CountryId{"NL"}), Tz::Europe_Amsterdam);
QCOMPARE(timezoneForLocation(50.8505, 5.6881, CountryId{}), QTimeZone("Europe/Amsterdam"));
QCOMPARE(timezoneForLocation(50.8505, 5.6881, CountryId{"NL"}), QTimeZone("Europe/Amsterdam"));
// Aachen, at the BE/DE/NL corner
QCOMPARE(timezoneForLocation(50.7717, 6.04235, CountryId{}), Tz::Europe_Berlin);
QCOMPARE(timezoneForLocation(50.7717, 6.04235, CountryId{"DE"}), Tz::Europe_Berlin);
//QCOMPARE(timezoneForLocation(50.7727, 6.01565, CountryId{}), Tz::Europe_Brussels);
QCOMPARE(timezoneForLocation(50.7727, 6.01565, CountryId{"BE"}), Tz::Europe_Brussels);
QCOMPARE(timezoneForLocation(50.7717, 6.04235, CountryId{}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(50.7717, 6.04235, CountryId{"DE"}), QTimeZone("Europe/Berlin"));
//QCOMPARE(timezoneForLocation(50.7727, 6.01565, CountryId{}), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(50.7727, 6.01565, CountryId{"BE"}), QTimeZone("Europe/Brussels"));
// Geneva (CH), very close to the FR border
QCOMPARE(timezoneForLocation(46.23213, 6.10636, CountryId{"CH"}), Tz::Europe_Zurich);
QCOMPARE(timezoneForLocation(46.23213, 6.10636, CountryId{"CH"}), QTimeZone("Europe/Zurich"));
// Busingen (DE), enclosed by CH, and in theory its own timezone (which we ignore)
QCOMPARE(timezoneForLocation(47.69947, 8.68833, CountryId{"DE"}), Tz::Europe_Berlin);
QCOMPARE(timezoneForLocation(47.67904, 8.68813, {}), Tz::Europe_Zurich);
QCOMPARE(timezoneForLocation(47.69947, 8.68833, CountryId{"DE"}), QTimeZone("Europe/Berlin"));
QCOMPARE(timezoneForLocation(47.67904, 8.68813, {}), QTimeZone("Europe/Zurich"));
// Baarle, the ultimate special case, NL/BE differs house by house
QCOMPARE(timezoneForLocation(51.44344, 4.93373, CountryId{"BE"}), Tz::Europe_Brussels);
QCOMPARE(timezoneForLocation(51.44344, 4.93373, CountryId{"NL"}), Tz::Europe_Amsterdam);
QCOMPARE(timezoneForLocation(51.44344, 4.93373, CountryId{"BE"}), QTimeZone("Europe/Brussels"));
QCOMPARE(timezoneForLocation(51.44344, 4.93373, CountryId{"NL"}), QTimeZone("Europe/Amsterdam"));
bool ambiguous = false;
auto tz = timezoneForCoordinate(51.44344, 4.93373, &ambiguous);
QVERIFY(ambiguous);
QVERIFY(tz == Tz::Europe_Amsterdam || tz == Tz::Europe_Brussels);
// Eliat Airport (IL), close to JO, and with a minor timezone variation due to different weekends
QCOMPARE(timezoneForLocation(29.72530, 35.00598, CountryId{"IL"}), Tz::Asia_Jerusalem);
QCOMPARE(timezoneForLocation(29.60908, 35.02038, CountryId{"JO"}), Tz::Asia_Amman);
QCOMPARE(timezoneForLocation(29.72530, 35.00598, CountryId{"IL"}), QTimeZone("Asia/Jerusalem"));
QCOMPARE(timezoneForLocation(29.60908, 35.02038, CountryId{"JO"}), QTimeZone("Asia/Amman"));
// Tijuana (MX), close to US, tests equivalent tz search in the neighbouring country
QCOMPARE(timezoneForLocation(32.54274, -116.97505, CountryId{"MX"}), Tz::America_Tijuana);
QCOMPARE(timezoneForLocation(32.55783, -117.04773, CountryId{"US"}), Tz::America_Los_Angeles);
QCOMPARE(timezoneForLocation(32.54274, -116.97505, CountryId{"MX"}), QTimeZone("America/Tijuana"));
QCOMPARE(timezoneForLocation(32.55783, -117.04773, CountryId{"US"}), QTimeZone("America/Los_Angeles"));
// Cordoba (AR), AR has several sub-zones that are all equivalent
QCOMPARE(timezoneForLocation(-31.4, -64.2, CountryId{"AR"}), Tz::America_Argentina_Cordoba);
QCOMPARE(timezoneForLocation(-31.4, -64.2, CountryId{"AR"}), QTimeZone("America/Argentina/Cordoba"));
// polar regions
QCOMPARE(timezoneForCoordinate(-90.0, 0.0), Tz::Undefined);
......
......@@ -279,7 +279,7 @@ QDateTime ExtractorPostprocessorPrivate::processTrainTripTime(QDateTime dt, QDat
const auto geo = station.geo();
const KnowledgeDb::CountryId country{station.address().addressCountry()};
if (geo.isValid()) {
tz = KnowledgeDb::toQTimeZone(KnowledgeDb::timezoneForLocation(geo.latitude(), geo.longitude(), country));
tz = KnowledgeDb::timezoneForLocation(geo.latitude(), geo.longitude(), country);
} else {
tz = KnowledgeDb::toQTimeZone(KnowledgeDb::timezoneForCountry(country));
}
......@@ -581,8 +581,8 @@ QDateTime ExtractorPostprocessorPrivate::processTimeForLocation(QDateTime dt, co
QTimeZone tz;
if (!place.address().addressCountry().isEmpty()) {
tz = KnowledgeDb::toQTimeZone(KnowledgeDb::timezoneForLocation(place.geo().latitude(), place.geo().longitude(),
KnowledgeDb::CountryId{place.address().addressCountry()}));
tz = KnowledgeDb::timezoneForLocation(place.geo().latitude(), place.geo().longitude(),
KnowledgeDb::CountryId{place.address().addressCountry()});
}
if (!tz.isValid()) {
return dt;
......
......@@ -43,7 +43,7 @@ QTimeZone timezoneForAirport(IataCode iataCode)
return {};
}
return KnowledgeDb::toQTimeZone(KnowledgeDb::timezoneForLocation((*it).coordinate.latitude, (*it).coordinate.longitude, (*it).country));
return KnowledgeDb::timezoneForLocation((*it).coordinate.latitude, (*it).coordinate.longitude, (*it).country);
}
KnowledgeDb::CountryId countryForAirport(IataCode iataCode)
......
......@@ -102,7 +102,7 @@ static bool isEquivalentTimezone(const QTimeZone &lhs, const QTimeZone &rhs)
return true;
}
KnowledgeDb::Tz KnowledgeDb::timezoneForLocation(float lat, float lon, CountryId country)
QTimeZone KnowledgeDb::timezoneForLocation(float lat, float lon, CountryId country)
{
bool ambiguous = false;
const auto coordTz = timezoneForCoordinate(lat, lon, &ambiguous);
......@@ -134,26 +134,28 @@ KnowledgeDb::Tz KnowledgeDb::timezoneForLocation(float lat, float lon, CountryId
}
if (!nonUnique && foundTz != Tz::Undefined) {
return foundTz;
return toQTimeZone(foundTz);
}
}
// only one method found a result, let's use that one
if (coordTz == Tz::Undefined || coordTz == countryTz) {
return countryTz;
return toQTimeZone(countryTz);
}
if (countryTz == Tz::Undefined) {
return coordTz;
return toQTimeZone(coordTz);
}
// if the coordinate-based timezone is also in @p country, that takes precedence
// example: the various AR sub-zones, or the MY sub-zone
if (country == countryFromCoord || !ambiguous) {
return coordTz;
return toQTimeZone(coordTz);
}
// if both timezones are equivalent, the country-based one wins, otherwise we use the coordinate one
return isEquivalentTimezone(toQTimeZone(coordTz), toQTimeZone(countryTz)) ? countryTz : coordTz;
const auto coordQtz = toQTimeZone(coordTz);
const auto countryQtz = toQTimeZone(countryTz);
return isEquivalentTimezone(coordQtz, countryQtz) ? countryQtz : coordQtz;
}
KnowledgeDb::CountryId KnowledgeDb::countryForCoordinate(float lat, float lon)
......
......@@ -43,7 +43,7 @@ namespace KnowledgeDb {
* This combines the results of the two above individual queries
* to obtain better results close to borders.
*/
KITINERARY_EXPORT Tz timezoneForLocation(float lat, float lon, CountryId country);
KITINERARY_EXPORT QTimeZone timezoneForLocation(float lat, float lon, CountryId country);
/** Returns the country for the given coordinate.
* The result will be the country identifier only if the underlying
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment